Skip to content

Implement PoC of MeasurementProcessor proposal #4642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Blinkuu
Copy link

@Blinkuu Blinkuu commented Jun 17, 2025

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:

  • MeasurementProcessor abstract interface
  • MeasurementProcessorChain for managing processor execution
  • Integration with existing MeterProvider, SdkConfiguration, and MeasurementConsumer
  • 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.

  • New feature (non-breaking change which adds functionality)
  • 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

processor = BaggageMeasurementProcessor()

# Add only specific baggage keys
processor = BaggageMeasurementProcessor(baggage_keys=["user.id", "trace.id"])
Copy link
Member

Choose a reason for hiding this comment

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

using trace.id here is misleading as there is no good use cases for it. To achieve correlation with Distributed Tracing, Exemplars can (should) be leveraged!

Copy link
Member

Choose a reason for hiding this comment

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

"user.id" is a good use-case.

I may also suggest "synthetic_request" as another - its used in OTel Demo

(Its not a standard or anything, but something already in demo)


```python
processor = StaticAttributeMeasurementProcessor({
"environment": "production",
Copy link
Member

Choose a reason for hiding this comment

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

I am not sure if this is a good idea. For StaticAttributes for all measurements, couldn't we model it as Resource Attributes. If applicable to a subset, then maybe use Meter attributes?


#### 4. ValueRangeMeasurementProcessor

Drops measurements outside a specified value range.
Copy link
Member

Choose a reason for hiding this comment

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

while this looks valid, I am not sure if its common to drop measurements based on value....

May I suggest another one which seems to me like a very valid use case - collapsing certain attributes to lower the cardinality. Described here: open-telemetry/opentelemetry-dotnet#6332

) -> None:
# Example: Add timestamp attribute
new_attributes = dict(measurement.attributes or {})
new_attributes["processed_at"] = str(int(time.time()))
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should show example of adding timestamp as a Metric attribute - is there any valid use-case for it?

next_processor(modified_measurement)

# Unit conversion processor
class MetersToFeetProcessor(MeasurementProcessor):
Copy link
Member

Choose a reason for hiding this comment

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

I like this! A simpler one would be to do sec->msec.
Qn : Should a view also be added to change the unit to account for this processor?

else:
next_processor(measurement)

# Sampling processor
Copy link
Member

Choose a reason for hiding this comment

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

not sure of the utility of this for Metrics!

try:
request_counter.add(1, {"endpoint": "/api/users", "method": "GET"})
response_time_histogram.record(
0.150, {"endpoint": "/api/users", "status": "200"}
Copy link
Member

Choose a reason for hiding this comment

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

nit: if status_code, should it be 200 and not "200"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants