github.com/newrelic/go-agent@v3.26.0+incompatible/_integrations/nrb3/nrb3_test.go (about) 1 // Copyright 2020 New Relic Corporation. All rights reserved. 2 // SPDX-License-Identifier: Apache-2.0 3 4 package nrb3 5 6 import ( 7 "net/http" 8 "testing" 9 10 newrelic "github.com/newrelic/go-agent" 11 "github.com/newrelic/go-agent/internal" 12 "github.com/newrelic/go-agent/internal/integrationsupport" 13 ) 14 15 func TestNewRoundTripperNil(t *testing.T) { 16 rt := NewRoundTripper(nil) 17 if orig := rt.(*b3Transport).original; orig != http.DefaultTransport { 18 t.Error("original is not as expected:", orig) 19 } 20 } 21 22 type roundTripperFn func(*http.Request) (*http.Response, error) 23 24 func (fn roundTripperFn) RoundTrip(r *http.Request) (*http.Response, error) { return fn(r) } 25 26 func TestRoundTripperNoTxn(t *testing.T) { 27 app := integrationsupport.NewTestApp(nil, integrationsupport.DTEnabledCfgFn) 28 txn := app.StartTransaction("test", nil, nil) 29 30 var count int 31 rt := NewRoundTripper(roundTripperFn(func(req *http.Request) (*http.Response, error) { 32 count++ 33 return &http.Response{ 34 StatusCode: 200, 35 }, nil 36 })) 37 client := &http.Client{Transport: rt} 38 39 req, err := http.NewRequest("GET", "http://example.com", nil) 40 if nil != err { 41 t.Fatal(err) 42 } 43 _, err = client.Do(req) 44 if nil != err { 45 t.Fatal(err) 46 } 47 txn.End() 48 49 if count != 1 { 50 t.Error("incorrect call count to RoundTripper:", count) 51 } 52 app.ExpectMetrics(t, []internal.WantMetric{ 53 {Name: "DurationByCaller/Unknown/Unknown/Unknown/Unknown/all", Scope: "", Forced: false, Data: nil}, 54 {Name: "DurationByCaller/Unknown/Unknown/Unknown/Unknown/allOther", Scope: "", Forced: false, Data: nil}, 55 {Name: "OtherTransaction/Go/test", Scope: "", Forced: true, Data: nil}, 56 {Name: "OtherTransaction/all", Scope: "", Forced: true, Data: nil}, 57 {Name: "OtherTransactionTotalTime", Scope: "", Forced: true, Data: nil}, 58 {Name: "OtherTransactionTotalTime/Go/test", Scope: "", Forced: false, Data: nil}, 59 }) 60 } 61 62 func TestRoundTripperWithTxnSampled(t *testing.T) { 63 replyfn := func(reply *internal.ConnectReply) { 64 reply.AdaptiveSampler = internal.SampleEverything{} 65 reply.TraceIDGenerator = internal.NewTraceIDGenerator(123) 66 } 67 app := integrationsupport.NewTestApp(replyfn, integrationsupport.DTEnabledCfgFn) 68 txn := app.StartTransaction("test", nil, nil) 69 70 var count int 71 var sent *http.Request 72 rt := NewRoundTripper(roundTripperFn(func(req *http.Request) (*http.Response, error) { 73 count++ 74 sent = req 75 return &http.Response{ 76 StatusCode: 200, 77 }, nil 78 })) 79 rt.(*b3Transport).idGen = internal.NewTraceIDGenerator(456) 80 client := &http.Client{Transport: rt} 81 82 req, err := http.NewRequest("GET", "http://example.com", nil) 83 if nil != err { 84 t.Fatal(err) 85 } 86 req = newrelic.RequestWithTransactionContext(req, txn) 87 _, err = client.Do(req) 88 if nil != err { 89 t.Fatal(err) 90 } 91 txn.End() 92 93 if count != 1 { 94 t.Error("incorrect call count to RoundTripper:", count) 95 } 96 // original request is not modified 97 if hdr := req.Header.Get("X-B3-TraceId"); hdr != "" { 98 t.Error("original request was modified, X-B3-TraceId header set:", hdr) 99 } 100 // b3 headers added 101 if hdr := sent.Header.Get("X-B3-TraceId"); hdr != "94d1331706b6a2b3" { 102 t.Error("unexpected value for X-B3-TraceId header:", hdr) 103 } 104 if hdr := sent.Header.Get("X-B3-SpanId"); hdr != "5a4f2d1b7f0cf06d" { 105 t.Error("unexpected value for X-B3-SpanId header:", hdr) 106 } 107 if hdr := sent.Header.Get("X-B3-ParentSpanId"); hdr != "3ffe00369da8a3b6" { 108 t.Error("unexpected value for X-B3-ParentSpanId header:", hdr) 109 } 110 if hdr := sent.Header.Get("X-B3-Sampled"); hdr != "1" { 111 t.Error("unexpected value for X-B3-Sampled header:", hdr) 112 } 113 app.ExpectMetrics(t, []internal.WantMetric{ 114 {Name: "DurationByCaller/Unknown/Unknown/Unknown/Unknown/all", Scope: "", Forced: false, Data: nil}, 115 {Name: "DurationByCaller/Unknown/Unknown/Unknown/Unknown/allOther", Scope: "", Forced: false, Data: nil}, 116 {Name: "External/all", Scope: "", Forced: true, Data: nil}, 117 {Name: "External/allOther", Scope: "", Forced: true, Data: nil}, 118 {Name: "External/example.com/all", Scope: "", Forced: false, Data: nil}, 119 {Name: "External/example.com/http/GET", Scope: "OtherTransaction/Go/test", Forced: false, Data: nil}, 120 {Name: "OtherTransaction/Go/test", Scope: "", Forced: true, Data: nil}, 121 {Name: "OtherTransaction/all", Scope: "", Forced: true, Data: nil}, 122 {Name: "OtherTransactionTotalTime", Scope: "", Forced: true, Data: nil}, 123 {Name: "OtherTransactionTotalTime/Go/test", Scope: "", Forced: false, Data: nil}, 124 }) 125 } 126 127 func TestRoundTripperWithTxnNotSampled(t *testing.T) { 128 replyfn := func(reply *internal.ConnectReply) { 129 reply.AdaptiveSampler = internal.SampleNothing{} 130 } 131 app := integrationsupport.NewTestApp(replyfn, integrationsupport.DTEnabledCfgFn) 132 txn := app.StartTransaction("test", nil, nil) 133 134 var sent *http.Request 135 rt := NewRoundTripper(roundTripperFn(func(req *http.Request) (*http.Response, error) { 136 sent = req 137 return &http.Response{ 138 StatusCode: 200, 139 }, nil 140 })) 141 client := &http.Client{Transport: rt} 142 143 req, err := http.NewRequest("GET", "http://example.com", nil) 144 if nil != err { 145 t.Fatal(err) 146 } 147 req = newrelic.RequestWithTransactionContext(req, txn) 148 _, err = client.Do(req) 149 if nil != err { 150 t.Fatal(err) 151 } 152 txn.End() 153 154 if hdr := sent.Header.Get("X-B3-Sampled"); hdr != "0" { 155 t.Error("unexpected value for X-B3-Sampled header:", hdr) 156 } 157 }