Skip to content

Support for adding Custom Attributes to the Metrics from context #3728

@punitmahes

Description

@punitmahes

What problem do you want to solve?

Currently, the OpenTelemetry Metrics SDK does not provide a mechanism to automatically enrich all metrics with custom attributes from the context, similar to how SpanProcessor works for traces. In tracing, a SpanProcessor can inspect a Span on start or end and add attributes from the current context.Context. This is incredibly useful for adding request-scoped or process-scoped metadata like tenant_id, request_id, or deployment_version to all spans generated within that context.

This functionality is missing for metrics. To add a contextual attribute to a metric, it must be manually added at every single instrumentation point. This leads to repetitive, boilerplate code and makes it easy to miss adding the attribute, leading to inconsistent metric data. Also, This does not get applied to the metrics collected using auto instrumentation.

Describe the solution you'd like

I would like to enhance both the Span and Metrics collections to allowing adding custom attributes from the span. For exmple, If I have attribute user-id which I set in the context / some object when i receive a request, All the spans and metrics collected after wards during the lifespan of that request should add this as attribute.

Describe alternatives you've considered

No response

Additional Context

No response

Would you like to implement a fix?

None

Tip

React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it. Learn more here.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions