Implement PoC of MeasurementProcessor proposal
Description
This PR is a Proof of Concept of the MeasurementProcessor concept for the OpenTelemetry Python SDK, following the design specified in OpenTelemetry Specification PR #4318.
MeasurementProcessor enables powerful measurement processing capabilities including:
- Dynamic attribute injection from OpenTelemetry Baggage for end-to-end telemetry correlation
- Attribute filtering to remove sensitive data
- Measurement dropping based on value ranges or other criteria
- Measurement modification for unit conversion, value transformation, etc.
- And more...
The implementation uses a chain-of-responsibility pattern where each processor decides whether to pass, modify, or drop measurements by calling (or not calling) the next processor in the chain.
Key components added:
-
MeasurementProcessorabstract interface -
MeasurementProcessorChainfor managing processor execution - Integration with existing
MeterProvider,SdkConfiguration, andMeasurementConsumer - Example processor implementations (BaggageMeasurementProcessor, StaticAttributeMeasurementProcessor, etc.)
Usage:
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics._internal.measurement_processor import (
BaggageMeasurementProcessor,
ValueRangeMeasurementProcessor,
)
processors = [
BaggageMeasurementProcessor(), # Add baggage as attributes
ValueRangeMeasurementProcessor(min_value=0), # Drop negative values
]
meter_provider = MeterProvider(measurement_processors=processors)
Type of change
Please delete options that are not relevant.
- [x] New feature (non-breaking change which adds functionality)
- [x] This change requires a documentation update
How Has This Been Tested?
I'm not a Python expert, and this is just a PoC. Therefore, no extensive testing has been conducted.
There are some tests that can be run with the following bash command:
python -m unittest opentelemetry-sdk/tests/metrics/test_measurement_processor.py -v
You can also see and run the example:
uv sync
source .venv/bin/activat
cd docs/examples/metrics/measurement-processors/
python measurement_processors.py
Does This PR Require a Contrib Repo Change?
- [ ] Yes. - Link to PR:
- [ ] No.
Checklist:
- [ ] Followed the style guidelines of this project
- [ ] Changelogs have been updated
- [ ] Unit tests have been added
- [ ] Documentation has been updated