Skip to content

RequestsInstrumentor metrics attributes exclude http.response.status_code if not sampled #3261

@tammy-baylis-swi

Description

@tammy-baylis-swi

Describe your environment

OS: Ubuntu
Python version: Python 3.10.16
Package version: 1.30.0/0.51b0

What happened?

I have manually instrumented a Flask app with an always-off sampler. My test app uses requests library to make outgoing request that always returns 200. requests is instrumented and generates the metric http.client.request.duration with these attributes:

{
    "http.request.method": "GET",
    "server.address": "postman-echo.com",
    "network.protocol.version": "1.1",
}

but it is missing "http.response.status_code": 200

Steps to Reproduce

Run script and make a request to localhost/test_trace

from opentelemetry import trace as trace_api
from opentelemetry.instrumentation.flask import FlaskInstrumentor
from opentelemetry.instrumentation.requests import RequestsInstrumentor
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.sdk.trace.export.in_memory_span_exporter import (
    InMemorySpanExporter,
)
from opentelemetry.sdk.trace.sampling import (
    Decision,
    StaticSampler,
)

sampler = StaticSampler(Decision.DROP)
tracer_provider = TracerProvider(sampler=sampler)
tracer_provider.add_span_processor(
    SimpleSpanProcessor(InMemorySpanExporter())
)
trace_api.set_tracer_provider(tracer_provider)

requests_inst = RequestsInstrumentor()
flask_inst = FlaskInstrumentor()
flask_inst.instrument(
    tracer_provider=trace_api.get_tracer_provider()
)
requests_inst.instrument(
    tracer_provider=trace_api.get_tracer_provider()
)

app = flask.Flask(__name__)
app.route("/test_trace/")(test_trace)

def test_trace():
    resp = requests.get(f"http://postman-echo.com/headers")
    return "Done!"

Expected Result

The metric http.client.request.duration should have these attributes:

{
    "http.request.method": "GET",
    "http.response.status_code": 200,
    "server.address": "postman-echo.com",
    "network.protocol.version": "1.1",
}

Actual Result

{
    "http.request.method": "GET",
    "server.address": "postman-echo.com",
    "network.protocol.version": "1.1",
}

Additional context

https://github.com/open-telemetry/opentelemetry-python-contrib/blob/main/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py#L276

Would you like to implement a fix?

None

Metadata

Metadata

Assignees

Labels

HTTPbugSomething isn't working

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions