forked from newrelic/go-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
internal_synthetics_test.go
195 lines (161 loc) · 6.4 KB
/
internal_synthetics_test.go
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package newrelic
import (
"net/http"
"testing"
"github.com/newrelic/go-agent/internal"
"github.com/newrelic/go-agent/internal/cat"
)
// This collection of top-level tests affirms, for all possible combinations of
// Old CAT, BetterCAT, and Synthetics, that when an inbound request contains a
// synthetics header, the subsequent outbound request propagates that synthetics
// header. Synthetics uses an obfuscated JSON header, so this test requires a
// really particular set of values, e.g. rrrrrrr-rrrr-1234-rrrr-rrrrrrrrrrrr.
var (
trustedAccounts = func() map[int]struct{} {
ta := make(map[int]struct{})
ta[1] = struct{}{} // Trust account 1, from syntheticsConnectReplyFn.
ta[444] = struct{}{} // Trust account 444, from syntheticsHeader.
return ta
}()
syntheticsConnectReplyFn = func(reply *internal.ConnectReply) {
reply.EncodingKey = "1234567890123456789012345678901234567890"
reply.CrossProcessID = "1#1"
reply.TrustedAccounts = trustedAccounts
}
)
func inboundSyntheticsRequestBuilder(oldCatEnabled bool, betterCatEnabled bool) *http.Request {
cfgFn := func(cfg *Config) {
cfg.CrossApplicationTracer.Enabled = oldCatEnabled
cfg.DistributedTracer.Enabled = betterCatEnabled
}
app := testApp(syntheticsConnectReplyFn, cfgFn, nil)
txn := app.StartTransaction("requester", nil, nil)
req, err := http.NewRequest("GET", "newrelic.com", nil)
if nil != err {
panic(err)
}
req.Header.Add(
"X-NewRelic-Synthetics",
"agMfAAECGxpLQkNAQUZHG0VKS0IcAwEHARtFSktCHEBBRkdERUpLQkNAQRYZFF1SU1pbWFkZX1xdUhQBAwEHGV9cXVIUWltYWV5fXF1SU1pbEB8WWFtaVVRdXB9eWVhbGgkLAwUfXllYWxpVVF1cX15ZWFtaVVQSbA==")
StartExternalSegment(txn, req)
if betterCatEnabled || !oldCatEnabled {
if cat.NewRelicIDName == req.Header.Get(cat.NewRelicIDName) {
panic("Header contains old cat header NewRelicIDName: " + req.Header.Get(cat.NewRelicIDName))
}
if cat.NewRelicTxnName == req.Header.Get(cat.NewRelicTxnName) {
panic("Header contains old cat header NewRelicTxnName: " + req.Header.Get(cat.NewRelicTxnName))
}
}
if oldCatEnabled {
if "" == req.Header.Get(cat.NewRelicIDName) {
panic("Missing old cat header NewRelicIDName: " + req.Header.Get(cat.NewRelicIDName))
}
if "" == req.Header.Get(cat.NewRelicTxnName) {
panic("Missing old cat header NewRelicTxnName: " + req.Header.Get(cat.NewRelicTxnName))
}
}
if "" == req.Header.Get(cat.NewRelicSyntheticsName) {
panic("missing synthetics header NewRelicSyntheticsName: " + req.Header.Get(cat.NewRelicSyntheticsName))
}
return req
}
func TestSyntheticsOldCAT(t *testing.T) {
cfgFn := func(cfg *Config) { cfg.CrossApplicationTracer.Enabled = true }
app := testApp(syntheticsConnectReplyFn, cfgFn, t)
clientTxn := app.StartTransaction(
"helloOldCAT",
nil,
inboundSyntheticsRequestBuilder(true, false))
req, err := http.NewRequest("GET", "newrelic.com", nil)
if nil != err {
panic(err)
}
StartExternalSegment(clientTxn, req)
clientTxn.End()
if "" == req.Header.Get(cat.NewRelicSyntheticsName) {
panic("Outbound request missing synthetics header NewRelicSyntheticsName: " + req.Header.Get(cat.NewRelicSyntheticsName))
}
expectedIntrinsics := map[string]interface{}{
"name": "WebTransaction/Go/helloOldCAT",
"client_cross_process_id": "1#1",
"nr.syntheticsResourceId": "rrrrrrr-rrrr-1234-rrrr-rrrrrrrrrrrr",
"nr.syntheticsJobId": "jjjjjjj-jjjj-1234-jjjj-jjjjjjjjjjjj",
"nr.syntheticsMonitorId": "mmmmmmm-mmmm-1234-mmmm-mmmmmmmmmmmm",
"nr.apdexPerfZone": internal.MatchAnything,
"nr.tripId": internal.MatchAnything,
"nr.pathHash": internal.MatchAnything,
"nr.referringPathHash": internal.MatchAnything,
"nr.referringTransactionGuid": internal.MatchAnything,
"nr.guid": internal.MatchAnything,
}
app.ExpectTxnEvents(t, []internal.WantEvent{{
Intrinsics: expectedIntrinsics,
}})
}
func TestSyntheticsBetterCAT(t *testing.T) {
cfgFn := func(cfg *Config) {
cfg.CrossApplicationTracer.Enabled = false
cfg.DistributedTracer.Enabled = true
}
app := testApp(syntheticsConnectReplyFn, cfgFn, t)
clientTxn := app.StartTransaction(
"helloBetterCAT",
nil,
inboundSyntheticsRequestBuilder(false, true))
req, err := http.NewRequest("GET", "newrelic.com", nil)
if nil != err {
panic(err)
}
StartExternalSegment(clientTxn, req)
clientTxn.End()
if "" == req.Header.Get(cat.NewRelicSyntheticsName) {
panic("Outbound request missing synthetics header NewRelicSyntheticsName: " + req.Header.Get(cat.NewRelicSyntheticsName))
}
expectedIntrinsics := map[string]interface{}{
"name": "WebTransaction/Go/helloBetterCAT",
"nr.syntheticsResourceId": "rrrrrrr-rrrr-1234-rrrr-rrrrrrrrrrrr",
"nr.syntheticsJobId": "jjjjjjj-jjjj-1234-jjjj-jjjjjjjjjjjj",
"nr.syntheticsMonitorId": "mmmmmmm-mmmm-1234-mmmm-mmmmmmmmmmmm",
"nr.apdexPerfZone": internal.MatchAnything,
"priority": internal.MatchAnything,
"sampled": internal.MatchAnything,
"traceId": internal.MatchAnything,
"guid": internal.MatchAnything,
}
app.ExpectTxnEvents(t, []internal.WantEvent{{
Intrinsics: expectedIntrinsics,
}})
}
func TestSyntheticsStandalone(t *testing.T) {
cfgFn := func(cfg *Config) {
cfg.AppName = "syntheticsReceiver"
cfg.CrossApplicationTracer.Enabled = false
}
app := testApp(syntheticsConnectReplyFn, cfgFn, t)
clientTxn := app.StartTransaction(
"helloSynthetics",
nil,
inboundSyntheticsRequestBuilder(false, false))
req, err := http.NewRequest("GET", "newrelic.com", nil)
if nil != err {
panic(err)
}
StartExternalSegment(clientTxn, req)
clientTxn.End()
if "" == req.Header.Get(cat.NewRelicSyntheticsName) {
panic("Outbound request missing synthetics header NewRelicSyntheticsName: " + req.Header.Get(cat.NewRelicSyntheticsName))
}
expectedIntrinsics := map[string]interface{}{
"name": "WebTransaction/Go/helloSynthetics",
"nr.syntheticsResourceId": "rrrrrrr-rrrr-1234-rrrr-rrrrrrrrrrrr",
"nr.syntheticsJobId": "jjjjjjj-jjjj-1234-jjjj-jjjjjjjjjjjj",
"nr.syntheticsMonitorId": "mmmmmmm-mmmm-1234-mmmm-mmmmmmmmmmmm",
"nr.apdexPerfZone": internal.MatchAnything,
"nr.guid": internal.MatchAnything,
}
app.ExpectTxnEvents(t, []internal.WantEvent{{
Intrinsics: expectedIntrinsics,
}})
}