open-telemetry / open-telemetry/opentelemetry-python-contrib

Support for adding Custom Attributes to the Metrics from context

Open
#3,728 1 comment 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature-request
Dominant language
Python
Stars
1.1k
Forks
1.1k
Avg merge
4d 15h
Merged PRs (30d)
16

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the metrics and span collection APIs in the repository and reading how context.Context is currently used for span attributes. Determine the design needed to propagate request- or process-scoped attributes into automatically collected metrics without requiring each instrumentation point to add them. Done means the behavior is defined for both spans and metrics, including auto-instrumentation.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
observability
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.