Skip to content

feat: add OpenTelemetry metrics support#553

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

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

Conversation

@ajbozarth

@ajbozarth ajbozarth commented Feb 19, 2026

Copy link
Copy Markdown
Contributor

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
@ajbozarth ajbozarth added the area/telemetry OTel spans, metrics, tracing, semconv label Feb 19, 2026
@github-actions

Copy link
Copy Markdown
Contributor

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

@mergify

mergify Bot commented Feb 19, 2026

Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown
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.

@planetf1 planetf1 left a comment

Copy link
Copy Markdown
Contributor

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

Comment thread test/telemetry/test_metrics.py
@ajbozarth

Copy link
Copy Markdown
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
Copy Markdown
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

Comment thread mellea/telemetry/metrics.py Outdated

@psschwei psschwei left a comment

Copy link
Copy Markdown
Member

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

@psschwei psschwei left a comment

Copy link
Copy Markdown
Member

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
akihikokuroda pushed a commit to akihikokuroda/mellea that referenced this pull request May 27, 2026
* refactor: move tracing implementation from __init__.py to tracing.py

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>

* feat: implement OpenTelemetry metrics module

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>

* style: fix import sorting in telemetry __init__.py

Signed-off-by: Alex Bozarth <ajbozart@us.ibm.com>

* fix: update test fixtures to reload tracing submodule

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>

* test: add unit tests for OpenTelemetry metrics module

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>

* fix: update test to use tracing submodule after refactor

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>

* refactor(telemetry): remove unused boundaries param and add exporter validation

Signed-off-by: Alex Bozarth <ajbozart@us.ibm.com>

* refactor: use dynamic version for telemetry tracers/meters

Replace hardcoded version strings with importlib.metadata.version()

Signed-off-by: Alex Bozarth <ajbozart@us.ibm.com>

* fix: use eager initialization for metrics to avoid race condition

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>

---------

Signed-off-by: Alex Bozarth <ajbozart@us.ibm.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/telemetry OTel spans, metrics, tracing, semconv

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