Skip to content

Commit 930685e

Browse files
Add Flask merging of Labeler custom attrs to Flask metrics
1 parent 2d288fd commit 930685e

File tree

1 file changed

+41
-0
lines changed
  • instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask

1 file changed

+41
-0
lines changed

instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,38 @@ def hello():
9595
if __name__ == "__main__":
9696
app.run(debug=True)
9797
98+
Custom Metrics Attributes using Labeler
99+
***************************************
100+
The Flask instrumentation reads from a Labeler utility that supports adding custom attributes
101+
to the HTTP metrics recorded by the instrumentation.
102+
103+
.. code-block:: python
104+
105+
from flask import Flask
106+
from opentelemetry.instrumentation.flask import FlaskInstrumentor
107+
from opentelemetry.instrumentation._labeler import get_labeler
108+
109+
app = Flask(__name__)
110+
FlaskInstrumentor().instrument_app(app)
111+
112+
@app.route("/user/<user_id>")
113+
def user_profile(user_id):
114+
# Get the labeler for the current request
115+
labeler = get_labeler()
116+
117+
# Add custom attributes to Flask instrumentation metrics
118+
labeler.add("user_id", user_id)
119+
labeler.add("user_type", "registered")
120+
121+
# Or, add multiple attributes at once
122+
labeler.add_attributes({
123+
"feature_flag": "new_ui",
124+
"experiment_group": "control"
125+
})
126+
127+
return f"User profile for {user_id}"
128+
129+
98130
Configuration
99131
-------------
100132
@@ -268,6 +300,7 @@ def response_hook(span: Span, status: str, response_headers: List):
268300
from opentelemetry.instrumentation.flask.package import _instruments
269301
from opentelemetry.instrumentation.flask.version import __version__
270302
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
303+
from opentelemetry.instrumentation._labeler import enhance_metric_attributes
271304
from opentelemetry.instrumentation.propagators import (
272305
get_global_response_propagator,
273306
)
@@ -341,6 +374,10 @@ def _wrapped_app(wrapped_app_environ, start_response):
341374
attributes = otel_wsgi.collect_request_attributes(
342375
wrapped_app_environ, sem_conv_opt_in_mode
343376
)
377+
# Enhance attributes with custom labeler attributes
378+
attributes = enhance_metric_attributes(
379+
attributes, wrapped_app_environ, "request"
380+
)
344381
active_requests_count_attrs = (
345382
otel_wsgi._parse_active_request_count_attrs(
346383
attributes,
@@ -446,6 +483,10 @@ def _before_request():
446483
flask_request_environ,
447484
sem_conv_opt_in_mode=sem_conv_opt_in_mode,
448485
)
486+
# Enhance attributes with custom labeler attributes
487+
attributes = enhance_metric_attributes(
488+
attributes, flask_request_environ, "request"
489+
)
449490
if flask.request.url_rule:
450491
# For 404 that result from no route found, etc, we
451492
# don't have a url_rule.

0 commit comments

Comments
 (0)