Skip to content

Commit 4ef0c1a

Browse files
Docstring
1 parent ee45e0f commit 4ef0c1a

File tree

1 file changed

+23
-0
lines changed
  • instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django

1 file changed

+23
-0
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,29 @@ def response_hook(span, request, response):
237237
***************************************
238238
The Django instrumentation reads from a Labeler utility that supports adding custom attributes to the HTTP duration metrics recorded by the instrumentation.
239239
240+
.. code:: python
241+
242+
from django.http import HttpResponse
243+
from opentelemetry.instrumentation._labeler import get_labeler
244+
from opentelemetry.instrumentation.django import DjangoInstrumentor
245+
246+
DjangoInstrumentor().instrument()
247+
248+
# For urlpattern `/user/<user_id>/` mapped elsewhere
249+
def my_user_view(request, user_id):
250+
# Get the labeler for the current request
251+
labeler = get_labeler()
252+
253+
# Add custom attributes to Flask instrumentation metrics
254+
labeler.add("user_id", user_id)
255+
labeler.add("user_type", "registered")
256+
257+
# Or, add multiple attributes at once
258+
labeler.add_attributes({
259+
"feature_flag": "new_ui",
260+
"experiment_group": "control"
261+
})
262+
return HttpResponse("Done!")
240263
241264
API
242265
---

0 commit comments

Comments
 (0)