Skip to content

Django instrumentation doesn't include http.route attribute in http.server.duration metrics #3553

Open
@johns-agentio

Description

@johns-agentio

Describe your environment

OS: macOS
Python version: 3.12.7
Package version: 0.53b1

What happened?

Django instrumentation doesn't set http.route attribute on metrics.

Steps to Reproduce

Sample instrumentation setup:

from opentelemetry import metrics
from opentelemetry.instrumentation.django import DjangoInstrumentor
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import ConsoleMetricExporter
from opentelemetry.sdk.metrics.view import View

# Set up console exporter to see metrics
exporter = ConsoleMetricExporter()
reader = PeriodicExportingMetricReader(exporter=exporter, export_interval_millis=5000)

# View expecting http.route
view = View(
    instrument_name="http.server.duration",
    attribute_keys={"http.method", "http.status_code", "http.route"}
)

meter_provider = MeterProvider(
    metric_readers=[reader],
    views=[view]
)
metrics.set_meter_provider(meter_provider)

DjangoInstrumentor().instrument(is_middleware=True)

Expected Result

Console output should show a metric that includes http.route, compared to the actual console output below.

Actual Result

Console output:

...
                            "name": "http.server.duration",
                            "description": "Measures the duration of inbound HTTP requests.",
                            "unit": "ms",
                            "data": {
                                "data_points": [
                                    {
                                        "attributes": {
                                            "http.method": "GET",
                                            "http.status_code": 200
                                        },
...

Additional context

Note that I AM able to see the http.route in the response hook and set it on a span, like so:

    def set_http_route_attribute_response_hook(span, request, response):
        if hasattr(request, "resolver_match") and request.resolver_match:
            route = request.resolver_match.route
            span.set_attribute("http.route", str(route))

    DjangoInstrumentor().instrument(
        is_middleware=True,
        response_hook=set_http_route_attribute_response_hook,
    )

I can also see http.target correctly, just not http.route.

Would you like to implement a fix?

Probably not

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions