-
Notifications
You must be signed in to change notification settings - Fork 243
feat: http client integration #876
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
Open
aldy505
wants to merge
36
commits into
getsentry:master
Choose a base branch
from
aldy505:feat/httpclient
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+690
−0
Open
Changes from 28 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
6b48923
feat: http client integration
aldy505 fd66575
chore(httpclient): variable naming according to linter
aldy505 439e98b
Merge branch 'master' into feat/httpclient
aldy505 5f4c5ae
chore: add changelog entry
aldy505 5214ea5
fix(httpclient): dont iterate over map
aldy505 ed105c1
feat(examples): add httpclient
aldy505 4e55daa
Merge branch 'master' into feat/httpclient
ribice 15aa59a
feat(spans): only start 'http.client' op as a child span
aldy505 099dad4
chore: lint
aldy505 7d9555d
Merge branch 'master' into feat/httpclient
aldy505 f018dde
chore: remove GetSpanFromContext and replace it with SpanFromContext
aldy505 d6f2663
Merge branch 'master' into feat/httpclient
aldy505 6af10bf
feat(httpclient): use baggage and traceparent from hub
aldy505 c5ad7e4
Merge remote-tracking branch 'origin/master' into feat/httpclient
aldy505 081fc0b
feat(httpclient): trace propagation targets
aldy505 a3a6367
chore: remove trace options request
aldy505 2cb97af
Merge branch 'master' into feat/httpclient
aldy505 ce3d4ea
Merge branch 'master' into feat/httpclient
aldy505 1075de4
Update sentryhttpclient.go
aldy505 ae861f3
feat(httpclient): use WithDescription instead of WithTransactionName
aldy505 a5eb96f
Update _examples/httpclient/main.go
aldy505 791feb5
chore: lint errors
aldy505 c912fe9
chore: omit variable name
aldy505 3e9a23b
Merge branch 'master' into feat/httpclient
giortzisg 912797f
Update CHANGELOG.md
cleptric 0e21a00
Merge branch 'master' into feat/httpclient
cleptric b31001c
Merge branch 'master' into feat/httpclient
giortzisg 389ca62
Merge branch 'master' into feat/httpclient
aldy505 f8d55ad
Merge branch 'master' into feat/httpclient
giortzisg d57b114
chore: add documentation for global Client
aldy505 a7a65ae
Merge branch 'master' into feat/httpclient
aldy505 5c0a97d
chore: remove global http client
aldy505 7c43a51
Merge remote-tracking branch 'origin/feat/httpclient' into feat/httpc…
aldy505 9de99ef
test: remove stale default http client
aldy505 93edad2
test(httpclient): should get span on OPTIONS method
aldy505 4cc9a92
Merge branch 'master' into feat/httpclient
aldy505 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
|
|
||
| "github.com/getsentry/sentry-go" | ||
| sentryhttpclient "github.com/getsentry/sentry-go/httpclient" | ||
| ) | ||
|
|
||
| func main() { | ||
| _ = sentry.Init(sentry.ClientOptions{ | ||
| Dsn: "", | ||
| EnableTracing: true, | ||
| TracesSampleRate: 1.0, | ||
| BeforeSend: func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event { | ||
| fmt.Println(event) | ||
| return event | ||
| }, | ||
| BeforeSendTransaction: func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event { | ||
| fmt.Println(event) | ||
| return event | ||
| }, | ||
| Debug: true, | ||
| }) | ||
|
|
||
| // With custom HTTP client | ||
| ctx := sentry.SetHubOnContext(context.Background(), sentry.CurrentHub().Clone()) | ||
| httpClient := &http.Client{ | ||
| Transport: sentryhttpclient.NewSentryRoundTripper(nil), | ||
| } | ||
|
|
||
| err := getExamplePage(ctx, httpClient) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| // With Sentry's HTTP client | ||
| err = getExamplePage(ctx, sentryhttpclient.Client) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| } | ||
|
|
||
| func getExamplePage(ctx context.Context, httpClient *http.Client) error { | ||
| span := sentry.StartSpan(ctx, "getExamplePage") | ||
| ctx = span.Context() | ||
| defer span.Finish() | ||
|
|
||
| request, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://example.com", nil) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| response, err := httpClient.Do(request) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer func() { | ||
| if response.Body != nil { | ||
| _ = response.Body.Close() | ||
| } | ||
| }() | ||
|
|
||
| body, err := io.ReadAll(response.Body) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| fmt.Println(string(body)) | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| // Package sentryhttpclient provides Sentry integration for Requests modules to enable distributed tracing between services. | ||
| // It is compatible with `net/http.RoundTripper`. | ||
| // | ||
| // import sentryhttpclient "github.com/getsentry/sentry-go/httpclient" | ||
| // | ||
| // roundTrippper := sentryhttpclient.NewSentryRoundTripper(nil, nil) | ||
| // client := &http.Client{ | ||
| // Transport: roundTripper, | ||
| // } | ||
| // | ||
| // request, err := client.Do(request) | ||
| package sentryhttpclient | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/http" | ||
| "strings" | ||
|
|
||
| "github.com/getsentry/sentry-go" | ||
| ) | ||
|
|
||
| // SentryRoundTripTracerOption provides a specific type in which defines the option for SentryRoundTripper. | ||
| type SentryRoundTripTracerOption func(*SentryRoundTripper) | ||
|
|
||
| // WithTracePropagationTargets configures additional trace propagation targets URL for the RoundTripper. | ||
| // Does not support regex patterns. | ||
| func WithTracePropagationTargets(targets []string) SentryRoundTripTracerOption { | ||
| return func(t *SentryRoundTripper) { | ||
| if t.tracePropagationTargets == nil { | ||
| t.tracePropagationTargets = targets | ||
| } else { | ||
| t.tracePropagationTargets = append(t.tracePropagationTargets, targets...) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // NewSentryRoundTripper provides a wrapper to existing http.RoundTripper to have required span data and trace headers for outgoing HTTP requests. | ||
| // | ||
| // - If `nil` is passed to `originalRoundTripper`, it will use http.DefaultTransport instead. | ||
| func NewSentryRoundTripper(originalRoundTripper http.RoundTripper, opts ...SentryRoundTripTracerOption) http.RoundTripper { | ||
| if originalRoundTripper == nil { | ||
| originalRoundTripper = http.DefaultTransport | ||
| } | ||
|
|
||
| // Configure trace propagation targets | ||
| var tracePropagationTargets []string | ||
| if hub := sentry.CurrentHub(); hub != nil { | ||
| client := hub.Client() | ||
| if client != nil { | ||
| clientOptions := client.Options() | ||
| if clientOptions.TracePropagationTargets != nil { | ||
| tracePropagationTargets = clientOptions.TracePropagationTargets | ||
| } | ||
| } | ||
| } | ||
|
|
||
| t := &SentryRoundTripper{ | ||
| originalRoundTripper: originalRoundTripper, | ||
| tracePropagationTargets: tracePropagationTargets, | ||
| } | ||
|
|
||
| for _, opt := range opts { | ||
| if opt != nil { | ||
| opt(t) | ||
| } | ||
| } | ||
|
|
||
| return t | ||
| } | ||
|
|
||
| // SentryRoundTripper provides a http.RoundTripper implementation for Sentry Requests module. | ||
| type SentryRoundTripper struct { | ||
| originalRoundTripper http.RoundTripper | ||
|
|
||
| tracePropagationTargets []string | ||
| } | ||
|
|
||
| func (s *SentryRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) { | ||
giortzisg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Respect trace propagation targets | ||
| if len(s.tracePropagationTargets) > 0 { | ||
| requestURL := request.URL.String() | ||
| foundMatch := false | ||
| for _, target := range s.tracePropagationTargets { | ||
| if strings.Contains(requestURL, target) { | ||
giortzisg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| foundMatch = true | ||
| break | ||
| } | ||
| } | ||
|
|
||
| if !foundMatch { | ||
| return s.originalRoundTripper.RoundTrip(request) | ||
| } | ||
| } | ||
|
|
||
| // Only create the `http.client` span only if there is a parent span. | ||
| parentSpan := sentry.SpanFromContext(request.Context()) | ||
| if parentSpan == nil { | ||
| if hub := sentry.GetHubFromContext(request.Context()); hub != nil { | ||
| request.Header.Add("Baggage", hub.GetBaggage()) | ||
| request.Header.Add("Sentry-Trace", hub.GetTraceparent()) | ||
| } | ||
|
|
||
| return s.originalRoundTripper.RoundTrip(request) | ||
| } | ||
|
|
||
| cleanRequestURL := request.URL.Redacted() | ||
|
|
||
| span := parentSpan.StartChild("http.client", sentry.WithDescription(fmt.Sprintf("%s %s", request.Method, cleanRequestURL))) | ||
| defer span.Finish() | ||
giortzisg marked this conversation as resolved.
Show resolved
Hide resolved
giortzisg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| span.SetData("http.query", request.URL.Query().Encode()) | ||
| span.SetData("http.fragment", request.URL.Fragment) | ||
| span.SetData("http.request.method", request.Method) | ||
| span.SetData("server.address", request.URL.Hostname()) | ||
| span.SetData("server.port", request.URL.Port()) | ||
|
|
||
| // Always add `Baggage` and `Sentry-Trace` headers. | ||
| request.Header.Add("Baggage", span.ToBaggage()) | ||
| request.Header.Add("Sentry-Trace", span.ToSentryTrace()) | ||
|
Comment on lines
+117
to
+119
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To support the recently added "Tracing without Performance" feature, we should use |
||
|
|
||
| response, err := s.originalRoundTripper.RoundTrip(request) | ||
giortzisg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| span.Status = sentry.SpanStatusInternalError | ||
| } | ||
|
|
||
| if response != nil { | ||
| span.Status = sentry.HTTPtoSpanStatus(response.StatusCode) | ||
| span.SetData("http.response.status_code", response.StatusCode) | ||
| span.SetData("http.response_content_length", response.ContentLength) | ||
| } | ||
giortzisg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return response, err | ||
giortzisg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // SentryHTTPClient provides a default HTTP client with SentryRoundTripper included. | ||
| // This can be used directly to perform HTTP request. | ||
| var Client = &http.Client{ | ||
| Transport: NewSentryRoundTripper(http.DefaultTransport), | ||
| } | ||
giortzisg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: The example
_examples/httpclient/main.gouses the non-existentsentryhttpclient.Clientvariable, leading to a compilation error.Severity: CRITICAL | Confidence: High
🔍 Detailed Analysis
The example file
_examples/httpclient/main.goon line 41 attempts to usesentryhttpclient.Client. However, thesentryhttpclient.Clientvariable does not exist, as it was intentionally removed from thehttpclientpackage in recent commits. This oversight means the example was not updated, causing a compilation failure and rendering the example non-functional.💡 Suggested Fix
Update
_examples/httpclient/main.goto either remove the section usingsentryhttpclient.Clientor create a localhttp.Clientwith theRoundTripperas shown in lines 31-33.🤖 Prompt for AI Agent
Did we get this right? 👍 / 👎 to inform future reviews.
Reference ID:
4362237