Skip to content

feat: add OpenTelemetry metrics support#553

Merged
ajbozarth merged 11 commits intogenerative-computing:mainfrom
ajbozarth:feat/opentelemetry-metrics
Feb 26, 2026
Merged

feat: add OpenTelemetry metrics support#553
ajbozarth merged 11 commits intogenerative-computing:mainfrom
ajbozarth:feat/opentelemetry-metrics

Conversation

@ajbozarth
Copy link
Contributor

@ajbozarth ajbozarth commented Feb 19, 2026

Misc PR

Type of PR

  • Bug Fix
  • New Feature
  • Documentation
  • Other

Description

Implemented OpenTelemetry metrics support for Mellea:

Changes:

  • Refactored telemetry module: Moved tracing implementation from __init__.py to dedicated tracing.py module for better organization
  • Added metrics module: Implemented mellea/telemetry/metrics.py with full OpenTelemetry metrics support including:
    • Counter, UpDownCounter, Histogram, and Gauge metric types
    • Automatic metric provider initialization with OTLP export
    • Context-aware metric recording with span correlation
    • Comprehensive unit tests (418 lines) covering all metric types and edge cases

Testing:

  • Added test/telemetry/test_metrics.py with extensive coverage of metric creation, recording, and export
  • Updated existing telemetry tests to work with refactored module structure
  • All tests pass with proper isolation and cleanup

The implementation follows OpenTelemetry standards and integrates seamlessly with Mellea's existing tracing infrastructure.

Testing

  • Tests added to the respective file if code was changed
  • New code has 100% coverage if code as added
  • Ensure existing tests and github automation passes (a maintainer will kick off the github automation when the rest of the PR is populated)

Refactored telemetry module structure to follow Python best practices:
- Moved tracing implementation from __init__.py to new tracing.py module
- Updated __init__.py to only contain imports and exports
- Updated backend_instrumentation.py to import from tracing module

This improves code organization and maintainability by separating
implementation from the package interface.

Signed-off-by: Alex Bozarth <ajbozart@us.ibm.com>
Implemented comprehensive metrics support using OpenTelemetry Metrics API:

- Created mellea/telemetry/metrics.py with:
  - Lazy MeterProvider initialization (similar to tracing pattern)
  - Environment-based configuration (MELLEA_METRICS_ENABLED, MELLEA_METRICS_CONSOLE)
  - Zero overhead when disabled (no-op instrument classes)
  - Named meter: mellea.metrics

- Instrument creation helpers:
  - create_counter() - for monotonically increasing values
  - create_histogram() - for value distributions
  - create_up_down_counter() - for values that can increase/decrease

- Updated mellea/telemetry/__init__.py to export metrics functions

Environment Variables:
- MELLEA_METRICS_ENABLED (default: false) - Enable metrics collection
- MELLEA_METRICS_CONSOLE (default: false) - Print metrics to console

Signed-off-by: Alex Bozarth <ajbozart@us.ibm.com>
Signed-off-by: Alex Bozarth <ajbozart@us.ibm.com>
Fixes test failures after f449d9a moved tracing code to submodule.
Test fixtures now reload mellea.telemetry.tracing to pick up env var changes.
Also moved test file to test/telemetry/test_tracing.py to mirror source structure.

Signed-off-by: Alex Bozarth <ajbozart@us.ibm.com>
Comprehensive test coverage for metrics configuration, lazy initialization,
instrument creation, no-op behavior, and functional operations.

Signed-off-by: Alex Bozarth <ajbozart@us.ibm.com>
After moving tracing code from __init__.py to tracing.py, the test fixture
needs to import from mellea.telemetry.tracing instead of mellea.telemetry.

Signed-off-by: Alex Bozarth <ajbozart@us.ibm.com>
@ajbozarth ajbozarth self-assigned this Feb 19, 2026
@ajbozarth ajbozarth requested a review from a team as a code owner February 19, 2026 00:59
@github-actions
Copy link
Contributor

The PR description has been updated. Please fill out the template for your PR to be reviewed.

@mergify
Copy link

mergify bot commented Feb 19, 2026

Merge Protections

Your pull request matches the following merge protections and will not be merged until they are valid.

🟢 Enforce conventional commit

Wonderful, this rule succeeded.

Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/

  • title ~= ^(fix|feat|docs|style|refactor|perf|test|build|ci|chore|revert|release)(?:\(.+\))?:

@ajbozarth
Copy link
Contributor Author

Opening this with what I have at EOD for peer review, I want to spend more time re-reviewing this myself before final merge as it was primarily ai generated and I've only done one manual review pass so far

…validation

Signed-off-by: Alex Bozarth <ajbozart@us.ibm.com>
Replace hardcoded version strings with importlib.metadata.version()

Signed-off-by: Alex Bozarth <ajbozart@us.ibm.com>
@ajbozarth
Copy link
Contributor Author

I've pushed a couple commits handling review feedback from a couple more ai assistants.

This is now ready for review, approval, and merge.

Copy link
Contributor

@planetf1 planetf1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one comment on verification of metrics. Otherwise LGTM

@ajbozarth
Copy link
Contributor Author

I have a follow up PR for #462 almost ready once this is merged (I just need to finish updating the docs before opening it)

@ajbozarth
Copy link
Contributor Author

I tagged a few people directly to try and get this reviewed and merged to unblock the followup PR before its ready tomorrow morning

Copy link
Member

@psschwei psschwei left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One possible issue (flagged by one AI but not others), but otherwise LGTM

Switched from lazy to eager initialization to match tracing module pattern and prevent potential threading issues.

Signed-off-by: Alex Bozarth <ajbozart@us.ibm.com>
@ajbozarth ajbozarth requested a review from psschwei February 26, 2026 17:04
Copy link
Member

@psschwei psschwei left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ajbozarth ajbozarth added this pull request to the merge queue Feb 26, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Feb 26, 2026
@ajbozarth ajbozarth added this pull request to the merge queue Feb 26, 2026
Merged via the queue into generative-computing:main with commit 78c5aab Feb 26, 2026
4 checks passed
@ajbozarth ajbozarth deleted the feat/opentelemetry-metrics branch February 26, 2026 21:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create the foundational metrics module using OpenTelemetry Metrics API with support for counters, histograms, and gauges

3 participants