Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
tammy-baylis-swi opened this issue Feb 11, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@tammy-baylis-swi
Copy link
Contributor

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

@tammy-baylis-swi tammy-baylis-swi added the bug Something isn't working label Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant