Closed
Description
Is there a trick to this ?
I created an instrumented http client that just implements a custom RoundTripper, in which I create a span if needed and enrich its data and set it as an http.client
operation.
Is this supposed to be working with the current version of sentry-go ?
This is the RoundTripper :
// RoundTrip implements the http.RoundTripper interface
func (t *SentryTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if t.BaseTransport == nil {
t.BaseTransport = httpx.NewClient().Transport
}
if len(t.TracePropagationTargets) > 0 {
requestURL := req.URL.String()
foundMatch := false
for _, target := range t.TracePropagationTargets {
if strings.Contains(requestURL, target) {
foundMatch = true
break
}
}
if !foundMatch {
return t.BaseTransport.RoundTrip(req)
}
}
ctx := req.Context()
hub := sentry.GetHubFromContext(ctx)
if hub == nil {
if currentHub := sentry.CurrentHub(); currentHub != nil {
hub = currentHub.Clone()
ctx = sentry.SetHubOnContext(ctx, hub)
req = req.WithContext(ctx)
req.Header.Set("baggage", hub.GetBaggage())
req.Header.Set("sentry-trace", hub.GetTraceparent())
}
return t.BaseTransport.RoundTrip(req)
}
parentSpan := sentry.SpanFromContext(ctx)
if parentSpan == nil {
req.Header.Set("baggage", hub.GetBaggage())
req.Header.Set("sentry-trace", hub.GetTraceparent())
return t.BaseTransport.RoundTrip(req)
}
cleanRequestURL := req.URL.Redacted()
spanName := fmt.Sprintf("%s %s", req.Method, cleanRequestURL)
span := parentSpan.StartChild("http.client", sentry.WithTransactionName(spanName))
span.Name = spanName
defer span.Finish()
span.SetData("http.query", req.URL.Query().Encode())
span.SetData("http.fragment", req.URL.Fragment)
span.SetData("http.request.method", req.Method)
span.SetData("server.address", req.URL.Hostname())
span.SetData("server.port", req.URL.Port())
span.SetData("description", fmt.Sprintf("%s %s", req.Method, req.URL.Host))
span.SetData("url", req.URL.String())
span.SetData("url.full", req.URL.String())
span.SetData("url.scheme", req.URL.Scheme)
span.SetData("http.protocol.name", req.URL.Scheme)
span.SetData("http.request.body.size", req.ContentLength)
span.SetData("network.protocol.version", req.Proto)
AddDetailsToSpan(ctx, span)
req.Header.Set("baggage", span.ToBaggage())
req.Header.Set("sentry-trace", span.ToSentryTrace())
newReq := req.WithContext(span.Context())
resp, err := t.BaseTransport.RoundTrip(newReq)
if err != nil {
span.Status = sentry.SpanStatusInternalError
span.SetData("error", err.Error())
}
if resp != nil {
span.Status = HTTPtoSpanStatus(resp.StatusCode)
span.SetData("http.response.status_code", resp.StatusCode)
span.SetData("http.response_content_length", resp.ContentLength)
}
return resp, err
}
Metadata
Metadata
Assignees
Labels
No labels
Type
Projects
Status