Skip to content
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
6b48923
feat: http client integration
aldy505 Aug 27, 2024
fd66575
chore(httpclient): variable naming according to linter
aldy505 Aug 27, 2024
439e98b
Merge branch 'master' into feat/httpclient
aldy505 Sep 3, 2024
5f4c5ae
chore: add changelog entry
aldy505 Sep 3, 2024
5214ea5
fix(httpclient): dont iterate over map
aldy505 Sep 3, 2024
ed105c1
feat(examples): add httpclient
aldy505 Sep 3, 2024
4e55daa
Merge branch 'master' into feat/httpclient
ribice Sep 3, 2024
15aa59a
feat(spans): only start 'http.client' op as a child span
aldy505 Sep 4, 2024
099dad4
chore: lint
aldy505 Sep 4, 2024
7d9555d
Merge branch 'master' into feat/httpclient
aldy505 Sep 15, 2024
f018dde
chore: remove GetSpanFromContext and replace it with SpanFromContext
aldy505 Oct 18, 2024
d6f2663
Merge branch 'master' into feat/httpclient
aldy505 Oct 18, 2024
6af10bf
feat(httpclient): use baggage and traceparent from hub
aldy505 Nov 2, 2024
c5ad7e4
Merge remote-tracking branch 'origin/master' into feat/httpclient
aldy505 Nov 2, 2024
081fc0b
feat(httpclient): trace propagation targets
aldy505 Nov 6, 2024
a3a6367
chore: remove trace options request
aldy505 Nov 16, 2024
2cb97af
Merge branch 'master' into feat/httpclient
aldy505 Nov 25, 2024
ce3d4ea
Merge branch 'master' into feat/httpclient
aldy505 Apr 23, 2025
1075de4
Update sentryhttpclient.go
aldy505 Apr 24, 2025
ae861f3
feat(httpclient): use WithDescription instead of WithTransactionName
aldy505 Apr 25, 2025
a5eb96f
Update _examples/httpclient/main.go
aldy505 Apr 28, 2025
791feb5
chore: lint errors
aldy505 May 2, 2025
c912fe9
chore: omit variable name
aldy505 May 2, 2025
3e9a23b
Merge branch 'master' into feat/httpclient
giortzisg May 2, 2025
912797f
Update CHANGELOG.md
cleptric May 6, 2025
0e21a00
Merge branch 'master' into feat/httpclient
cleptric May 6, 2025
b31001c
Merge branch 'master' into feat/httpclient
giortzisg Jun 3, 2025
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Features

- Add net/http client integration ([#876](https://github.com/getsentry/sentry-go/pull/876))

## 0.29.1

The Sentry SDK team is happy to announce the immediate availability of Sentry Go SDK v0.29.1.
Expand Down
75 changes: 75 additions & 0 deletions _examples/httpclient/main.go
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.SentryHTTPClient)
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
}
107 changes: 107 additions & 0 deletions httpclient/sentryhttpclient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// 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"

"github.com/getsentry/sentry-go"
)

// SentryRoundTripTracerOption provides a specific type in which defines the option for SentryRoundTripper.
type SentryRoundTripTracerOption func(*SentryRoundTripper)

// WithTags allows the RoundTripper to includes additional tags.
func WithTags(tags map[string]string) SentryRoundTripTracerOption {
return func(t *SentryRoundTripper) {
for k, v := range tags {
t.tags[k] = v
}
}
}

// WithTag allows the RoundTripper to includes additional tag.
func WithTag(key, value string) SentryRoundTripTracerOption {
return func(t *SentryRoundTripper) {
t.tags[key] = value
}
}

// 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
}

t := &SentryRoundTripper{
originalRoundTripper: originalRoundTripper,
tags: make(map[string]string),
}

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

tags map[string]string
}

func (s *SentryRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) {
// Only create the `http.client` span only if there is a parent span.
parentSpan := sentry.SpanFromContext(request.Context())
if parentSpan == nil {
return s.originalRoundTripper.RoundTrip(request)
}

cleanRequestURL := request.URL.Redacted()

span := parentSpan.StartChild("http.client", sentry.WithTransactionName(fmt.Sprintf("%s %s", request.Method, cleanRequestURL)))
span.Tags = s.tags
Copy link
Member

Choose a reason for hiding this comment

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

Why are we setting span tags here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is it not ideal? Should I remove it?

Copy link
Member

Choose a reason for hiding this comment

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

What info is in there?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Decided to just remove it.

defer span.Finish()

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
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()


response, err := s.originalRoundTripper.RoundTrip(request)

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)
}

return response, err
}

// SentryHTTPClient provides a default HTTP client with SentryRoundTripper included.
// This can be used directly to perform HTTP request.
var SentryHTTPClient = &http.Client{
Transport: NewSentryRoundTripper(http.DefaultTransport),
}
Loading
Loading