File: nrb3_test.go

package info (click to toggle)
golang-github-newrelic-go-agent 3.15.2-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 8,356 kB
  • sloc: sh: 65; makefile: 6
file content (157 lines) | stat: -rw-r--r-- 5,266 bytes parent folder | download
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package nrb3

import (
	"net/http"
	"testing"

	newrelic "github.com/newrelic/go-agent"
	"github.com/newrelic/go-agent/internal"
	"github.com/newrelic/go-agent/internal/integrationsupport"
)

func TestNewRoundTripperNil(t *testing.T) {
	rt := NewRoundTripper(nil)
	if orig := rt.(*b3Transport).original; orig != http.DefaultTransport {
		t.Error("original is not as expected:", orig)
	}
}

type roundTripperFn func(*http.Request) (*http.Response, error)

func (fn roundTripperFn) RoundTrip(r *http.Request) (*http.Response, error) { return fn(r) }

func TestRoundTripperNoTxn(t *testing.T) {
	app := integrationsupport.NewTestApp(nil, integrationsupport.DTEnabledCfgFn)
	txn := app.StartTransaction("test", nil, nil)

	var count int
	rt := NewRoundTripper(roundTripperFn(func(req *http.Request) (*http.Response, error) {
		count++
		return &http.Response{
			StatusCode: 200,
		}, nil
	}))
	client := &http.Client{Transport: rt}

	req, err := http.NewRequest("GET", "http://example.com", nil)
	if nil != err {
		t.Fatal(err)
	}
	_, err = client.Do(req)
	if nil != err {
		t.Fatal(err)
	}
	txn.End()

	if count != 1 {
		t.Error("incorrect call count to RoundTripper:", count)
	}
	app.ExpectMetrics(t, []internal.WantMetric{
		{Name: "DurationByCaller/Unknown/Unknown/Unknown/Unknown/all", Scope: "", Forced: false, Data: nil},
		{Name: "DurationByCaller/Unknown/Unknown/Unknown/Unknown/allOther", Scope: "", Forced: false, Data: nil},
		{Name: "OtherTransaction/Go/test", Scope: "", Forced: true, Data: nil},
		{Name: "OtherTransaction/all", Scope: "", Forced: true, Data: nil},
		{Name: "OtherTransactionTotalTime", Scope: "", Forced: true, Data: nil},
		{Name: "OtherTransactionTotalTime/Go/test", Scope: "", Forced: false, Data: nil},
	})
}

func TestRoundTripperWithTxnSampled(t *testing.T) {
	replyfn := func(reply *internal.ConnectReply) {
		reply.AdaptiveSampler = internal.SampleEverything{}
		reply.TraceIDGenerator = internal.NewTraceIDGenerator(123)
	}
	app := integrationsupport.NewTestApp(replyfn, integrationsupport.DTEnabledCfgFn)
	txn := app.StartTransaction("test", nil, nil)

	var count int
	var sent *http.Request
	rt := NewRoundTripper(roundTripperFn(func(req *http.Request) (*http.Response, error) {
		count++
		sent = req
		return &http.Response{
			StatusCode: 200,
		}, nil
	}))
	rt.(*b3Transport).idGen = internal.NewTraceIDGenerator(456)
	client := &http.Client{Transport: rt}

	req, err := http.NewRequest("GET", "http://example.com", nil)
	if nil != err {
		t.Fatal(err)
	}
	req = newrelic.RequestWithTransactionContext(req, txn)
	_, err = client.Do(req)
	if nil != err {
		t.Fatal(err)
	}
	txn.End()

	if count != 1 {
		t.Error("incorrect call count to RoundTripper:", count)
	}
	// original request is not modified
	if hdr := req.Header.Get("X-B3-TraceId"); hdr != "" {
		t.Error("original request was modified, X-B3-TraceId header set:", hdr)
	}
	// b3 headers added
	if hdr := sent.Header.Get("X-B3-TraceId"); hdr != "94d1331706b6a2b3" {
		t.Error("unexpected value for X-B3-TraceId header:", hdr)
	}
	if hdr := sent.Header.Get("X-B3-SpanId"); hdr != "5a4f2d1b7f0cf06d" {
		t.Error("unexpected value for X-B3-SpanId header:", hdr)
	}
	if hdr := sent.Header.Get("X-B3-ParentSpanId"); hdr != "3ffe00369da8a3b6" {
		t.Error("unexpected value for X-B3-ParentSpanId header:", hdr)
	}
	if hdr := sent.Header.Get("X-B3-Sampled"); hdr != "1" {
		t.Error("unexpected value for X-B3-Sampled header:", hdr)
	}
	app.ExpectMetrics(t, []internal.WantMetric{
		{Name: "DurationByCaller/Unknown/Unknown/Unknown/Unknown/all", Scope: "", Forced: false, Data: nil},
		{Name: "DurationByCaller/Unknown/Unknown/Unknown/Unknown/allOther", Scope: "", Forced: false, Data: nil},
		{Name: "External/all", Scope: "", Forced: true, Data: nil},
		{Name: "External/allOther", Scope: "", Forced: true, Data: nil},
		{Name: "External/example.com/all", Scope: "", Forced: false, Data: nil},
		{Name: "External/example.com/http/GET", Scope: "OtherTransaction/Go/test", Forced: false, Data: nil},
		{Name: "OtherTransaction/Go/test", Scope: "", Forced: true, Data: nil},
		{Name: "OtherTransaction/all", Scope: "", Forced: true, Data: nil},
		{Name: "OtherTransactionTotalTime", Scope: "", Forced: true, Data: nil},
		{Name: "OtherTransactionTotalTime/Go/test", Scope: "", Forced: false, Data: nil},
	})
}

func TestRoundTripperWithTxnNotSampled(t *testing.T) {
	replyfn := func(reply *internal.ConnectReply) {
		reply.AdaptiveSampler = internal.SampleNothing{}
	}
	app := integrationsupport.NewTestApp(replyfn, integrationsupport.DTEnabledCfgFn)
	txn := app.StartTransaction("test", nil, nil)

	var sent *http.Request
	rt := NewRoundTripper(roundTripperFn(func(req *http.Request) (*http.Response, error) {
		sent = req
		return &http.Response{
			StatusCode: 200,
		}, nil
	}))
	client := &http.Client{Transport: rt}

	req, err := http.NewRequest("GET", "http://example.com", nil)
	if nil != err {
		t.Fatal(err)
	}
	req = newrelic.RequestWithTransactionContext(req, txn)
	_, err = client.Do(req)
	if nil != err {
		t.Fatal(err)
	}
	txn.End()

	if hdr := sent.Header.Get("X-B3-Sampled"); hdr != "0" {
		t.Error("unexpected value for X-B3-Sampled header:", hdr)
	}
}