Skip to content

feat: http client integration#876

Merged
giortzisg merged 39 commits intogetsentry:masterfrom
aldy505:feat/httpclient
Dec 18, 2025
Merged

feat: http client integration#876
giortzisg merged 39 commits intogetsentry:masterfrom
aldy505:feat/httpclient

Conversation

@aldy505
Copy link
Contributor

@aldy505 aldy505 commented Aug 27, 2024

An effort to implement this: https://develop.sentry.dev/sdk/telemetry/traces/modules/requests/

Since we already have tracing without performance, this should be good to go.

@codecov
Copy link

codecov bot commented Aug 27, 2024

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.23%. Comparing base (1ce3436) to head (6a4ac69).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #876      +/-   ##
==========================================
+ Coverage   86.04%   86.23%   +0.19%     
==========================================
  Files          62       63       +1     
  Lines        6090     6148      +58     
==========================================
+ Hits         5240     5302      +62     
+ Misses        635      631       -4     
  Partials      215      215              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ribice
Copy link
Collaborator

ribice commented Sep 3, 2024

@aldy505 We should document this (in _examples, and perhaps sentry-docs?), and also update changelog accordingly.

@aldy505 aldy505 requested a review from ribice September 3, 2024 14:04
@aldy505 aldy505 requested a review from cleptric September 4, 2024 01:06
Comment on lines 70 to 74
// Only create the `http.client` span only if there is a parent span.
parentSpan := sentry.GetSpanFromContext(request.Context())
if parentSpan == nil {
return s.originalRoundTripper.RoundTrip(request)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

A quick note why I prefer this instead of creating a sentry.WithOnlyIfParentExists() (a SpanOption type):

I used the JS SDK a lot at work lately, and I've been wondering the entire week, on their startSpan function, they have this as an option: https://github.com/getsentry/sentry-javascript/blob/216aaeba1ee27cce8a4876e1f9212ba374eb30b3/packages/types/src/startSpanOptions.ts#L14-L15

Should the Go SDK move forward with that, as a SpanOption or not?

We could to the SpanOption thing, but it feels really weird since the way Go SDK handles scopes/hubs with span is to add the span pointer into the scope. See

sentry-go/tracing.go

Lines 196 to 199 in a6acd05

if clientOptions.EnableTracing {
hub := hubFromContext(ctx)
hub.Scope().SetSpan(&span)
}

Although we could just not call the scope.SetSpan() function, I don't know about the behavior if this span that we're creating will ever have a child span being created manually by the user.

Comment on lines +88 to +90
// Always add `Baggage` and `Sentry-Trace` headers.
request.Header.Add("Baggage", span.ToBaggage())
request.Header.Add("Sentry-Trace", span.ToSentryTrace())
Copy link
Member

Choose a reason for hiding this comment

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

To support the recently added "Tracing without Performance" feature, we should use hub.GetTraceparent() and hub.GetBaggage()

@aldy505 aldy505 requested a review from cleptric November 6, 2024 13:59
@aldy505 aldy505 requested a review from cleptric November 16, 2024 11:02
@giortzisg
Copy link
Contributor

Will have a look after transport refactoring.

@aldy505
Copy link
Contributor Author

aldy505 commented Nov 30, 2025

@giortzisg updated

}

// With Sentry's HTTP client
err = getExamplePage(ctx, sentryhttpclient.Client)

This comment was marked as outdated.

if tt.WantSpan != nil && !foundMatch {
t.Errorf("Span mismatch (-want +got):\n%s", strings.Join(diffs, "\n"))
} else if tt.WantSpan == nil && foundMatch {
t.Errorf("Expected no span, got %+v", gotSpans)
Copy link

Choose a reason for hiding this comment

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

Bug: Test cannot detect unwanted spans when expecting none

The test logic for validating the "no span expected" case is broken. When WantSpan is nil, the condition tt.WantSpan == nil && foundMatch at line 308 can never trigger because foundMatch only becomes true when cmp.Diff returns an empty string, but cmp.Diff(nil, gotSpan) always returns a diff when comparing nil to an actual span. This means if the implementation incorrectly creates an http.client span when none is expected (like when a URL doesn't match trace propagation targets), the test will silently pass instead of catching the bug.

Fix in Cursor Fix in Web

Comment on lines 40 to 45
// With Sentry's HTTP client
err = getExamplePage(ctx, sentryhttpclient.Client)
if err != nil {
panic(err)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

we can remove this

Co-authored-by: Giannis Gkiortzis <58184179+giortzisg@users.noreply.github.com>

if res.Body != nil {
res.Body.Close()
}
Copy link

Choose a reason for hiding this comment

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

Bug: Nil pointer dereference when HTTP request fails

In TestDefaults, when client.Head() returns an error, res may be nil. Using t.Error(err) logs the error but does not stop execution, so the code continues to res.Body != nil. If res is nil, accessing res.Body causes a nil pointer dereference panic. The check needs to verify res != nil before accessing res.Body, or use t.Fatal(err) to stop execution on error.

Fix in Cursor Fix in Web

Comment on lines +99 to +100
request.Header.Add("Baggage", hub.GetBaggage())
request.Header.Add("Sentry-Trace", hub.GetTraceparent())
Copy link

Choose a reason for hiding this comment

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

Bug: Calls to hub.GetBaggage() and hub.GetTraceparent() can cause a panic due to a potential nil pointer dereference if the hub's scope is nil.
Severity: CRITICAL | Confidence: High

🔍 Detailed Analysis

The new code in sentryhttpclient.go retrieves a hub from the request context and checks if it's non-nil. However, it then calls hub.GetBaggage() and hub.GetTraceparent() without verifying that the hub's internal scope is also non-nil. The Sentry Go SDK's API contract allows for a hub to be created with a nil scope, via NewHub(client, nil). If such a hub is present in the context, the calls to GetBaggage() and GetTraceparent() will attempt to dereference a nil pointer, causing the application to panic.

💡 Suggested Fix

Before calling hub.GetBaggage() and hub.GetTraceparent(), add a check to ensure the hub's scope is not nil. For example: if hub := sentry.GetHubFromContext(request.Context()); hub != nil && hub.Scope() != nil { ... }.

🤖 Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: httpclient/sentryhttpclient.go#L99-L100

Potential issue: The new code in `sentryhttpclient.go` retrieves a `hub` from the
request context and checks if it's non-nil. However, it then calls `hub.GetBaggage()`
and `hub.GetTraceparent()` without verifying that the hub's internal scope is also
non-nil. The Sentry Go SDK's API contract allows for a hub to be created with a `nil`
scope, via `NewHub(client, nil)`. If such a hub is present in the context, the calls to
`GetBaggage()` and `GetTraceparent()` will attempt to dereference a nil pointer, causing
the application to panic.

Did we get this right? 👍 / 👎 to inform future reviews.
Reference ID: 7702273

@giortzisg giortzisg dismissed cleptric’s stale review December 18, 2025 12:57

The requested changes were resolved.

@giortzisg giortzisg merged commit d7582e8 into getsentry:master Dec 18, 2025
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Feature Issue type

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants