Skip to content

Sentry does not fully instrument requests library requests #2277

Closed

Description

@filips123

How do you use Sentry?

Sentry Saas (sentry.io)

Version

1.28.1

Steps to Reproduce

I made an example program to show this error:

from hashlib import sha256

import requests
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration
from sentry_sdk.integrations.pure_eval import PureEvalIntegration
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration


def download() -> tuple[str, str]:
    """Download the timetable JS file."""

    response = requests.get("https://projekti.gimvic.org/urnik/data.js")
    response.raise_for_status()
    content = response.content

    return content.decode("utf8"), sha256(content).hexdigest()


def main():
    sentry_sdk.init(
        dsn="DSN",
        max_breadcrumbs=100,
        traces_sample_rate=1,
        profiles_sample_rate=1,
        integrations=[
            FlaskIntegration(transaction_style="url"),
            SqlalchemyIntegration(),
            PureEvalIntegration(),
        ],
        environment="production",
        release="0.0.0",
    )

    with sentry_sdk.start_transaction(name="test") as transaction:
        # Download the document
        content, sha = download()

        # Do some other processing
        with transaction.start_child(op="work") as child:
            child.set_tag("hash", sha)
            print(sha)


main()

The code in production is different and more complicated as it's integrated into a larger project. However, the same problem still happens.

Interestingly, it does not happen with all URLs, but it happens consistently with this URL (and a few others).

Expected Result

Sentry should auto-instrument all requests code and assign http.client span.

Actual Result

Sentry instruments only a part of requests.get as http.client. The rest of the HTTP request handling does not have a span assigned, which can make Sentry show "Missing span instrumentation", even though the profiler shows that the code is related to the HTTP request and comes from the requests library.

Screenshots of traces and running my example program:

traces
profile

Links to a few traces/profiles running my example program:

Link to a few traces/profiles showing the problem in production:

Activity

  1. moved this to Waiting for: Product Owner in GitHub Issues with 👀on Jul 28, 2023
  2. sentrivana commented on Jul 28, 2023

    @sentrivana
    Collaborator

    Hey @filips123, thanks a ton for the super detailed bug report, this will make it much easier for us to debug! We're a bit swamped at the moment but will take a look when time permits.

  3. antonpirker commented on Oct 2, 2023

    @antonpirker
    Contributor

    Looks like reading a big chunked response using response.content is not instrumented and this is the thing that causes the "missing instrumentation" (maybe, I just looked at it very briefly)

  4. added
    TriagedHas been looked at recently during old issue triage
    on Jun 3, 2024
  5. szokeasaurusrex commented on Jun 3, 2024

    @szokeasaurusrex
    Member

    We should not add a requests integration; rather, we should improve our support for instrumenting streaming responses

  6. added and removed
    TriagedHas been looked at recently during old issue triage
    on Mar 20, 2026
  7. sentrivana commented on Apr 22, 2026

    @sentrivana
    Collaborator

    Hey all. We're currently working on streaming spans, rather than sending them all at once -- I wonder if that would also solve this issue. I'll also need to recheck whether it's still an issue after all this time. I'll investigate when I get a moment and update.

  8. sentrivana commented on Apr 23, 2026

    @sentrivana
    Collaborator

    Since the link from the original repro doesn't work anymore, here's a workaround to repro:

    1. Create a server.py with
    from http.server import BaseHTTPRequestHandler, HTTPServer
    import time
    
    class H(BaseHTTPRequestHandler):
        def do_GET(self):
            self.send_response(200)
            self.send_header("Transfer-Encoding", "chunked")
            self.end_headers()
            for _ in range(20):
                chunk = b"x" * 50000
                self.wfile.write(f"{len(chunk):x}\r\n".encode() + chunk + b"\r\n")
                self.wfile.flush()
                time.sleep(0.2)
            self.wfile.write(b"0\r\n\r\n")
    
    
    HTTPServer(("127.0.0.1", 8000), H).serve_forever()
    1. Run the file ^

    2. Change the URL in the repro example above to "http://127.0.0.1:8000" and run it.

  9. sentrivana commented on Apr 23, 2026

    @sentrivana
    Collaborator

    So the problem is that we're closing the span too early. The Missing instrumentation part should be part of the client span. This is most visible when dealing with longer chunked responses -- reading the body is not included.

    Working on a way to improve this.

  10. self-assigned this
    on Apr 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions