Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions sentry-python/5349/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Reproduction for sentry-python#5349

**Issue:** https://github.com/getsentry/sentry-python/issues/5349

## Description

This reproduces the issue where `requests` library calls show incorrect/very short timing (< 1ms) in `http.client` spans with sentry-sdk 2.x. In version 1.45, the spans correctly measured the actual HTTP request duration.

The user reports that after upgrading from sentry-sdk 1.45 to 2.49, the `http.client` spans show impossibly short durations that don't reflect the actual I/O time spent on HTTP requests.

## Steps to Reproduce

1. Install dependencies:
```bash
uv sync
```

2. Set your Sentry DSN:
```bash
export SENTRY_DSN=your_dsn_here
```

3. Run the reproduction:
```bash
uv run python main.py
```

4. Make requests to the endpoints:
```bash
# Single request (should take ~1 second)
curl http://localhost:8000/

# Multiple requests (should take ~3 seconds total)
curl http://localhost:8000/multiple-requests
```

5. Check the traces in Sentry

## Expected Behavior

The `http.client` spans for requests to `httpbin.org/delay/1` should show approximately 1000ms duration, reflecting the actual time spent on the HTTP request.

## Actual Behavior

The `http.client` spans show impossibly short times (< 1ms) that don't reflect the real I/O time spent on the request.

## Environment

- Python: 3.9+
- sentry-sdk: 2.49.0+
- fastapi: 0.115.0+
- requests: 2.32.0+
100 changes: 100 additions & 0 deletions sentry-python/5349/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
"""
Reproduction for sentry-python#5349
https://github.com/getsentry/sentry-python/issues/5349

This reproduces the issue where requests library calls show incorrect/very short
timing (< 1ms) in http.client spans with sentry-sdk 2.x, compared to version 1.45
where it correctly measured the actual HTTP request duration.

Expected: http.client spans should show the actual time spent on HTTP requests
Actual: http.client spans show impossibly short times (< 1ms) that don't reflect
the real I/O time spent on the request
"""

import os
import requests
import sentry_sdk
from sentry_sdk.integrations.fastapi import FastApiIntegration
from sentry_sdk.integrations.starlette import StarletteIntegration
from fastapi import FastAPI

# Initialize Sentry - set SENTRY_DSN environment variable before running
sentry_dsn = os.environ.get("SENTRY_DSN", "")

if sentry_dsn:
sentry_sdk.init(
dsn=sentry_dsn,
integrations=[
FastApiIntegration(middleware_spans=True),
StarletteIntegration(middleware_spans=True),
],
traces_sample_rate=1.0,
debug=True, # Enable debug to see span info in console
)
print(f"Sentry initialized with DSN: {sentry_dsn[:20]}...")
else:
print("WARNING: SENTRY_DSN not set. Set it to see traces in Sentry.")
print("Run: export SENTRY_DSN=your_dsn_here")

app = FastAPI()


@app.get("/")
def root():
"""
Root endpoint that makes an external HTTP request using the requests library.

The http.client span for this request should show the actual time taken
(typically 100-500ms for a real HTTP request), but with the bug it shows
impossibly short times like < 1ms.
"""
# Make a request to a real external API that takes measurable time
# httpbin.org/delay/1 will delay the response by 1 second
response = requests.get("https://httpbin.org/delay/1")

return {
"message": "Check Sentry traces - the http.client span should show ~1000ms",
"status_code": response.status_code,
"external_url": "https://httpbin.org/delay/1",
}


@app.get("/multiple-requests")
def multiple_requests():
"""
Endpoint that makes multiple external HTTP requests.

This makes it easier to see the timing discrepancy - if each request
to /delay/1 actually takes ~1 second, but spans show < 1ms,
the bug is confirmed.
"""
results = []

# Make 3 requests, each should take ~1 second
for i in range(3):
response = requests.get(f"https://httpbin.org/delay/1")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary f-string with no variable interpolation

Low Severity

The f-string f"https://httpbin.org/delay/1" on line 75 doesn't interpolate any variables, making the f prefix unnecessary. The loop variable i is available but not used in the URL. This is inconsistent with line 53, which correctly uses a plain string "https://httpbin.org/delay/1" for the same URL.

Fix in Cursor Fix in Web

results.append({
"request": i + 1,
"status": response.status_code,
})

return {
"message": "Made 3 requests to httpbin.org/delay/1 - each should show ~1000ms in traces",
"results": results,
}


if __name__ == "__main__":
import uvicorn
print("\n" + "=" * 60)
print("Starting FastAPI server for sentry-python#5349 reproduction")
print("=" * 60)
print("\nEndpoints:")
print(" GET / - Single request to httpbin.org/delay/1")
print(" GET /multiple-requests - Three requests to httpbin.org/delay/1")
print("\nExpected behavior:")
print(" http.client spans should show ~1000ms (actual request time)")
print("\nActual behavior (bug):")
print(" http.client spans show < 1ms (incorrect/impossible timing)")
print("\n" + "=" * 60 + "\n")
uvicorn.run(app, host="0.0.0.0", port=8000)
14 changes: 14 additions & 0 deletions sentry-python/5349/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[project]
name = "repro-sentry-python-5349"
version = "0.1.0"
description = "Reproduction for sentry-python#5349 - Missing instrumentation for requests lib calls"
requires-python = ">=3.9"
dependencies = [
"fastapi>=0.115.0",
"uvicorn>=0.34.0",
"requests>=2.32.0",
"sentry-sdk[fastapi]>=2.49.0",
]

[dependency-groups]
dev = []
Loading