git.colasdn.top/newrelic/go-agent@v3.26.0+incompatible/internal_context_test.go (about) 1 // Copyright 2020 New Relic Corporation. All rights reserved. 2 // SPDX-License-Identifier: Apache-2.0 3 4 // +build go1.7 5 6 package newrelic 7 8 import ( 9 "net/http" 10 "testing" 11 12 "github.com/newrelic/go-agent/internal" 13 ) 14 15 func TestWrapHandlerContext(t *testing.T) { 16 // Test that WrapHandleFunc adds the transaction to the request's 17 // context, and that it is accessible through FromContext. 18 19 app := testApp(nil, nil, t) 20 _, h := WrapHandleFunc(app, "myTxn", func(rw http.ResponseWriter, r *http.Request) { 21 txn := FromContext(r.Context()) 22 segment := StartSegment(txn, "mySegment") 23 segment.End() 24 }) 25 req, _ := http.NewRequest("GET", "", nil) 26 h(nil, req) 27 28 scope := "WebTransaction/Go/myTxn" 29 app.ExpectMetrics(t, []internal.WantMetric{ 30 {Name: "WebTransaction/Go/myTxn", Scope: "", Forced: true, Data: nil}, 31 {Name: "WebTransaction", Scope: "", Forced: true, Data: nil}, 32 {Name: "WebTransactionTotalTime/Go/myTxn", Scope: "", Forced: false, Data: nil}, 33 {Name: "WebTransactionTotalTime", Scope: "", Forced: true, Data: nil}, 34 {Name: "HttpDispatcher", Scope: "", Forced: true, Data: nil}, 35 {Name: "Apdex", Scope: "", Forced: true, Data: nil}, 36 {Name: "Apdex/Go/myTxn", Scope: "", Forced: false, Data: nil}, 37 {Name: "Custom/mySegment", Scope: "", Forced: false, Data: nil}, 38 {Name: "Custom/mySegment", Scope: scope, Forced: false, Data: nil}, 39 }) 40 } 41 42 func TestStartExternalSegmentNilTransaction(t *testing.T) { 43 // Test that StartExternalSegment pulls the transaction from the 44 // request's context if it is not explicitly provided. 45 46 app := testApp(nil, nil, t) 47 txn := app.StartTransaction("myTxn", nil, nil) 48 49 req, _ := http.NewRequest("GET", "http://example.com", nil) 50 req = RequestWithTransactionContext(req, txn) 51 segment := StartExternalSegment(nil, req) 52 segment.End() 53 txn.End() 54 55 scope := "OtherTransaction/Go/myTxn" 56 app.ExpectMetrics(t, []internal.WantMetric{ 57 {Name: "OtherTransaction/Go/myTxn", Scope: "", Forced: true, Data: nil}, 58 {Name: "OtherTransaction/all", Scope: "", Forced: true, Data: nil}, 59 {Name: "OtherTransactionTotalTime/Go/myTxn", Scope: "", Forced: false, Data: nil}, 60 {Name: "OtherTransactionTotalTime", Scope: "", Forced: true, Data: nil}, 61 {Name: "External/all", Scope: "", Forced: true, Data: nil}, 62 {Name: "External/allOther", Scope: "", Forced: true, Data: nil}, 63 {Name: "External/example.com/all", Scope: "", Forced: false, Data: nil}, 64 {Name: "External/example.com/http/GET", Scope: scope, Forced: false, Data: nil}, 65 }) 66 } 67 func TestNewRoundTripperNilTransaction(t *testing.T) { 68 // Test that NewRoundTripper pulls the transaction from the 69 // request's context if it is not explicitly provided. 70 71 app := testApp(nil, nil, t) 72 txn := app.StartTransaction("myTxn", nil, nil) 73 74 client := &http.Client{} 75 client.Transport = roundTripperFunc(func(*http.Request) (*http.Response, error) { 76 return &http.Response{}, nil 77 }) 78 client.Transport = NewRoundTripper(nil, client.Transport) 79 req, _ := http.NewRequest("GET", "http://example.com", nil) 80 req = RequestWithTransactionContext(req, txn) 81 client.Do(req) 82 txn.End() 83 84 scope := "OtherTransaction/Go/myTxn" 85 app.ExpectMetrics(t, []internal.WantMetric{ 86 {Name: "OtherTransaction/Go/myTxn", Scope: "", Forced: true, Data: nil}, 87 {Name: "OtherTransaction/all", Scope: "", Forced: true, Data: nil}, 88 {Name: "OtherTransactionTotalTime/Go/myTxn", Scope: "", Forced: false, Data: nil}, 89 {Name: "OtherTransactionTotalTime", Scope: "", Forced: true, Data: nil}, 90 {Name: "External/all", Scope: "", Forced: true, Data: nil}, 91 {Name: "External/allOther", Scope: "", Forced: true, Data: nil}, 92 {Name: "External/example.com/all", Scope: "", Forced: false, Data: nil}, 93 {Name: "External/example.com/http/GET", Scope: scope, Forced: false, Data: nil}, 94 }) 95 }