Skip to content

[WIP] Add Flask enrich_metric_attributes if Labeler contains custom attributes#4307

Draft
tammy-baylis-swi wants to merge 16 commits intoopen-telemetry:mainfrom
tammy-baylis-swi:flask-enrich-metric-attributes
Draft

[WIP] Add Flask enrich_metric_attributes if Labeler contains custom attributes#4307
tammy-baylis-swi wants to merge 16 commits intoopen-telemetry:mainfrom
tammy-baylis-swi:flask-enrich-metric-attributes

Conversation

@tammy-baylis-swi
Copy link
Contributor

@tammy-baylis-swi tammy-baylis-swi commented Mar 5, 2026

Description

Updates Flask instrumentor to use Labeler enrich_metric_attributes before adding/recording each metric, old and new semconv: HTTP server duration, active requests counter. Has no effect if Labeler is not called by instrumented code or custom distro.

Depends on

Fixes # (issue)

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

  • tox -e py312-test-instrumentation-flask-0
  • Install FlaskInstrumentor and manually instrument Flask app with http/dup opt-in; use Labeler to add attrs such as user_id

Example app-side call to add attributes with Labeler:

@app.route("/users/<user_id>/")
def get_users(user_id):
    # Get the labeler for the current OTel context
    labeler = get_labeler()
    # Add custom attributes to Flask instrumentation metrics
    labeler.add("user_id", user_id)
    labeler.add("user_type", "registered")
    # Or, add multiple attributes at once
    labeler.add_attributes(
        {"feature_flag": "new_ui", "experiment_group": "control"}
    )
    return f"User profile for {escape(user_id)}"

Example otelcol debug HTTP server duration metrics from instrumentation

InstrumentationScope opentelemetry.instrumentation.flask 0.62b0.dev
...
Metric #1
Descriptor:
     -> Name: http.server.duration
     -> Description: Measures the duration of inbound HTTP requests.
     -> Unit: ms
     -> DataType: Histogram
     -> AggregationTemporality: Cumulative
HistogramDataPoints #0
Data point attributes:
     -> http.method: Str(GET)
     -> http.server_name: Str(127.0.0.1)
     -> http.scheme: Str(http)
     -> net.host.name: Str(0.0.0.0:8000)
     -> http.host: Str(0.0.0.0:8000)
     -> net.host.port: Int(8000)
     -> http.flavor: Str(1.1)
     -> http.status_code: Int(200)
     -> http.target: Str(/users/<user_id>/)
     -> user_id: Str(123)
     -> user_type: Str(registered)
     -> feature_flag: Str(new_ui)
     -> experiment_group: Str(control)

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant