1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package nrb3
import (
"net/http"
"time"
newrelic "github.com/newrelic/go-agent"
"github.com/newrelic/go-agent/internal"
)
func init() { internal.TrackUsage("integration", "b3") }
// NewRoundTripper creates an `http.RoundTripper` to instrument external
// requests. The RoundTripper returned creates an external segment and adds B3
// tracing headers to each request if and only if a `newrelic.Transaction`
// (https://godoc.org/github.com/newrelic/go-agent#Transaction) is found in the
// `http.Request`'s context. It then delegates to the original RoundTripper
// provided (or http.DefaultTransport if none is provided).
func NewRoundTripper(original http.RoundTripper) http.RoundTripper {
if nil == original {
original = http.DefaultTransport
}
return &b3Transport{
idGen: internal.NewTraceIDGenerator(int64(time.Now().UnixNano())),
original: original,
}
}
// cloneRequest mimics implementation of
// https://godoc.org/github.com/google/go-github/github#BasicAuthTransport.RoundTrip
func cloneRequest(r *http.Request) *http.Request {
// shallow copy of the struct
r2 := new(http.Request)
*r2 = *r
// deep copy of the Header
r2.Header = make(http.Header, len(r.Header))
for k, s := range r.Header {
r2.Header[k] = append([]string(nil), s...)
}
return r2
}
type b3Transport struct {
idGen *internal.TraceIDGenerator
original http.RoundTripper
}
func txnSampled(txn newrelic.Transaction) string {
if txn.IsSampled() {
return "1"
}
return "0"
}
func addHeader(request *http.Request, key, val string) {
if val != "" {
request.Header.Add(key, val)
}
}
func (t *b3Transport) RoundTrip(request *http.Request) (*http.Response, error) {
if txn := newrelic.FromContext(request.Context()); nil != txn {
// The specification of http.RoundTripper requires that the request is never modified.
request = cloneRequest(request)
segment := &newrelic.ExternalSegment{
StartTime: newrelic.StartSegmentNow(txn),
Request: request,
}
defer segment.End()
md := txn.GetTraceMetadata()
addHeader(request, "X-B3-TraceId", md.TraceID)
addHeader(request, "X-B3-SpanId", t.idGen.GenerateTraceID())
addHeader(request, "X-B3-ParentSpanId", md.SpanID)
addHeader(request, "X-B3-Sampled", txnSampled(txn))
}
return t.original.RoundTrip(request)
}
|