forked from iwanbk/go-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
internal_set_web_request_test.go
325 lines (310 loc) · 13.3 KB
/
internal_set_web_request_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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package newrelic
import (
"net/http"
"net/url"
"testing"
"github.com/iwanbk/go-agent/internal"
)
type customRequest struct {
header http.Header
u *url.URL
method string
transport TransportType
}
func (r customRequest) Header() http.Header { return r.header }
func (r customRequest) URL() *url.URL { return r.u }
func (r customRequest) Method() string { return r.method }
func (r customRequest) Transport() TransportType { return r.transport }
var (
sampleHTTPRequest = func() *http.Request {
req, err := http.NewRequest("GET", "http://www.newrelic.com", nil)
if nil != err {
panic(err)
}
req.Header.Set("Accept", "myaccept")
req.Header.Set("Content-Type", "mycontent")
req.Header.Set("Host", "myhost")
req.Header.Set("Content-Length", "123")
return req
}()
sampleCustomRequest = func() customRequest {
u, err := url.Parse("http://www.newrelic.com")
if nil != err {
panic(err)
}
hdr := make(http.Header)
hdr.Set("Accept", "myaccept")
hdr.Set("Content-Type", "mycontent")
hdr.Set("Host", "myhost")
hdr.Set("Content-Length", "123")
return customRequest{
header: hdr,
u: u,
method: "GET",
transport: TransportHTTP,
}
}()
sampleRequestAgentAttributes = map[string]interface{}{
AttributeRequestMethod: "GET",
AttributeRequestAccept: "myaccept",
AttributeRequestContentType: "mycontent",
AttributeRequestContentLength: 123,
AttributeRequestHost: "myhost",
AttributeRequestURI: "http://www.newrelic.com",
}
)
func TestSetWebRequestNil(t *testing.T) {
// Test that using SetWebRequest with nil marks the transaction as a web
// transaction.
app := testApp(distributedTracingReplyFields, enableBetterCAT, t)
txn := app.StartTransaction("hello", nil, nil)
err := txn.SetWebRequest(nil)
if err != nil {
t.Error("unexpected error", err)
}
txn.End()
app.ExpectMetrics(t, []internal.WantMetric{
{Name: "WebTransaction/Go/hello", Scope: "", Forced: true, Data: nil},
{Name: "WebTransaction", Scope: "", Forced: true, Data: nil},
{Name: "WebTransactionTotalTime/Go/hello", Scope: "", Forced: false, Data: nil},
{Name: "WebTransactionTotalTime", Scope: "", Forced: true, Data: nil},
{Name: "HttpDispatcher", Scope: "", Forced: true, Data: nil},
{Name: "Apdex", Scope: "", Forced: true, Data: nil},
{Name: "Apdex/Go/hello", Scope: "", Forced: false, Data: nil},
{Name: "DurationByCaller/Unknown/Unknown/Unknown/Unknown/all", Scope: "", Forced: false, Data: nil},
{Name: "DurationByCaller/Unknown/Unknown/Unknown/Unknown/allWeb", Scope: "", Forced: false, Data: nil},
})
app.ExpectTxnEvents(t, []internal.WantEvent{{
AgentAttributes: map[string]interface{}{},
Intrinsics: map[string]interface{}{
"name": "WebTransaction/Go/hello",
"guid": internal.MatchAnything,
"sampled": internal.MatchAnything,
"priority": internal.MatchAnything,
"traceId": internal.MatchAnything,
"nr.apdexPerfZone": internal.MatchAnything,
},
}})
}
func TestSetWebRequestNilPointer(t *testing.T) {
// Test that calling NewWebRequest with a nil pointer is safe and
// returns a nil interface that SetWebRequest handles safely.
app := testApp(distributedTracingReplyFields, enableBetterCAT, t)
txn := app.StartTransaction("hello", nil, nil)
err := txn.SetWebRequest(NewWebRequest(nil))
if err != nil {
t.Error("unexpected error", err)
}
txn.End()
app.ExpectMetrics(t, []internal.WantMetric{
{Name: "WebTransaction/Go/hello", Scope: "", Forced: true, Data: nil},
{Name: "WebTransaction", Scope: "", Forced: true, Data: nil},
{Name: "WebTransactionTotalTime/Go/hello", Scope: "", Forced: false, Data: nil},
{Name: "WebTransactionTotalTime", Scope: "", Forced: true, Data: nil},
{Name: "HttpDispatcher", Scope: "", Forced: true, Data: nil},
{Name: "Apdex", Scope: "", Forced: true, Data: nil},
{Name: "Apdex/Go/hello", Scope: "", Forced: false, Data: nil},
{Name: "DurationByCaller/Unknown/Unknown/Unknown/Unknown/all", Scope: "", Forced: false, Data: nil},
{Name: "DurationByCaller/Unknown/Unknown/Unknown/Unknown/allWeb", Scope: "", Forced: false, Data: nil},
})
app.ExpectTxnEvents(t, []internal.WantEvent{{
AgentAttributes: map[string]interface{}{},
Intrinsics: map[string]interface{}{
"name": "WebTransaction/Go/hello",
"guid": internal.MatchAnything,
"sampled": internal.MatchAnything,
"priority": internal.MatchAnything,
"traceId": internal.MatchAnything,
"nr.apdexPerfZone": internal.MatchAnything,
},
}})
}
func TestSetWebRequestHTTPRequest(t *testing.T) {
// Test that NewWebRequest correctly turns an *http.Request into a
// WebRequest that SetWebRequest uses as expected.
app := testApp(distributedTracingReplyFields, enableBetterCAT, t)
txn := app.StartTransaction("hello", nil, nil)
err := txn.SetWebRequest(NewWebRequest(sampleHTTPRequest))
if err != nil {
t.Error("unexpected error", err)
}
txn.End()
app.ExpectMetrics(t, []internal.WantMetric{
{Name: "WebTransaction/Go/hello", Scope: "", Forced: true, Data: nil},
{Name: "WebTransaction", Scope: "", Forced: true, Data: nil},
{Name: "WebTransactionTotalTime/Go/hello", Scope: "", Forced: false, Data: nil},
{Name: "WebTransactionTotalTime", Scope: "", Forced: true, Data: nil},
{Name: "HttpDispatcher", Scope: "", Forced: true, Data: nil},
{Name: "Apdex", Scope: "", Forced: true, Data: nil},
{Name: "Apdex/Go/hello", Scope: "", Forced: false, Data: nil},
{Name: "DurationByCaller/Unknown/Unknown/Unknown/Unknown/all", Scope: "", Forced: false, Data: nil},
{Name: "DurationByCaller/Unknown/Unknown/Unknown/Unknown/allWeb", Scope: "", Forced: false, Data: nil},
})
app.ExpectTxnEvents(t, []internal.WantEvent{{
AgentAttributes: sampleRequestAgentAttributes,
Intrinsics: map[string]interface{}{
"name": "WebTransaction/Go/hello",
"guid": internal.MatchAnything,
"sampled": internal.MatchAnything,
"priority": internal.MatchAnything,
"traceId": internal.MatchAnything,
"nr.apdexPerfZone": internal.MatchAnything,
},
}})
}
func TestSetWebRequestCustomRequest(t *testing.T) {
// Test that a custom type which implements WebRequest is used by
// SetWebRequest as expected.
app := testApp(distributedTracingReplyFields, enableBetterCAT, t)
txn := app.StartTransaction("hello", nil, nil)
err := txn.SetWebRequest(sampleCustomRequest)
if err != nil {
t.Error("unexpected error", err)
}
txn.End()
app.ExpectMetrics(t, []internal.WantMetric{
{Name: "WebTransaction/Go/hello", Scope: "", Forced: true, Data: nil},
{Name: "WebTransaction", Scope: "", Forced: true, Data: nil},
{Name: "WebTransactionTotalTime/Go/hello", Scope: "", Forced: false, Data: nil},
{Name: "WebTransactionTotalTime", Scope: "", Forced: true, Data: nil},
{Name: "HttpDispatcher", Scope: "", Forced: true, Data: nil},
{Name: "Apdex", Scope: "", Forced: true, Data: nil},
{Name: "Apdex/Go/hello", Scope: "", Forced: false, Data: nil},
{Name: "DurationByCaller/Unknown/Unknown/Unknown/Unknown/all", Scope: "", Forced: false, Data: nil},
{Name: "DurationByCaller/Unknown/Unknown/Unknown/Unknown/allWeb", Scope: "", Forced: false, Data: nil},
})
app.ExpectTxnEvents(t, []internal.WantEvent{{
AgentAttributes: sampleRequestAgentAttributes,
Intrinsics: map[string]interface{}{
"name": "WebTransaction/Go/hello",
"guid": internal.MatchAnything,
"sampled": internal.MatchAnything,
"priority": internal.MatchAnything,
"traceId": internal.MatchAnything,
"nr.apdexPerfZone": internal.MatchAnything,
},
}})
}
func TestSetWebRequestAlreadyEnded(t *testing.T) {
// Test that SetWebRequest returns an error if called after
// Transaction.End.
app := testApp(distributedTracingReplyFields, enableBetterCAT, t)
txn := app.StartTransaction("hello", nil, nil)
txn.End()
err := txn.SetWebRequest(sampleCustomRequest)
if err != errAlreadyEnded {
t.Error("incorrect error", err)
}
app.ExpectMetrics(t, []internal.WantMetric{
{Name: "OtherTransaction/Go/hello", Scope: "", Forced: true, Data: nil},
{Name: "OtherTransaction/all", Scope: "", Forced: true, Data: nil},
{Name: "OtherTransactionTotalTime/Go/hello", Scope: "", Forced: false, Data: nil},
{Name: "OtherTransactionTotalTime", Scope: "", Forced: true, Data: nil},
{Name: "DurationByCaller/Unknown/Unknown/Unknown/Unknown/all", Scope: "", Forced: false, Data: nil},
{Name: "DurationByCaller/Unknown/Unknown/Unknown/Unknown/allOther", Scope: "", Forced: false, Data: nil},
})
app.ExpectTxnEvents(t, []internal.WantEvent{{
AgentAttributes: map[string]interface{}{},
Intrinsics: map[string]interface{}{
"name": "OtherTransaction/Go/hello",
"guid": internal.MatchAnything,
"sampled": internal.MatchAnything,
"priority": internal.MatchAnything,
"traceId": internal.MatchAnything,
},
}})
}
func TestSetWebRequestWithDistributedTracing(t *testing.T) {
// Test that the WebRequest.Transport() return value is used as the
// distributed tracing transport if a distributed tracing header is
// found in the WebRequest.Header().
app := testApp(distributedTracingReplyFields, enableBetterCAT, t)
payload := makePayload(app, nil)
// Copy sampleCustomRequest to avoid modifying it since it is used in
// other tests.
req := sampleCustomRequest
req.header = map[string][]string{
DistributedTracePayloadHeader: {payload.Text()},
}
txn := app.StartTransaction("hello", nil, nil)
err := txn.SetWebRequest(req)
if nil != err {
t.Error("unexpected error", err)
}
txn.End()
app.ExpectMetrics(t, []internal.WantMetric{
{Name: "WebTransaction/Go/hello", Scope: "", Forced: true, Data: nil},
{Name: "WebTransaction", Scope: "", Forced: true, Data: nil},
{Name: "WebTransactionTotalTime/Go/hello", Scope: "", Forced: false, Data: nil},
{Name: "WebTransactionTotalTime", Scope: "", Forced: true, Data: nil},
{Name: "HttpDispatcher", Scope: "", Forced: true, Data: nil},
{Name: "Apdex", Scope: "", Forced: true, Data: nil},
{Name: "Apdex/Go/hello", Scope: "", Forced: false, Data: nil},
{Name: "DurationByCaller/App/123/456/HTTP/all", Scope: "", Forced: false, Data: nil},
{Name: "DurationByCaller/App/123/456/HTTP/allWeb", Scope: "", Forced: false, Data: nil},
{Name: "TransportDuration/App/123/456/HTTP/all", Scope: "", Forced: false, Data: nil},
{Name: "TransportDuration/App/123/456/HTTP/allWeb", Scope: "", Forced: false, Data: nil},
{Name: "Supportability/DistributedTrace/AcceptPayload/Success", Scope: "", Forced: true, Data: singleCount},
})
app.ExpectTxnEvents(t, []internal.WantEvent{{
AgentAttributes: map[string]interface{}{
"request.method": "GET",
"request.uri": "http://www.newrelic.com",
},
Intrinsics: map[string]interface{}{
"name": "WebTransaction/Go/hello",
"parent.type": "App",
"parent.account": "123",
"parent.app": "456",
"parent.transportType": "HTTP",
"parent.transportDuration": internal.MatchAnything,
"parentId": internal.MatchAnything,
"traceId": internal.MatchAnything,
"parentSpanId": internal.MatchAnything,
"guid": internal.MatchAnything,
"sampled": internal.MatchAnything,
"priority": internal.MatchAnything,
"nr.apdexPerfZone": internal.MatchAnything,
},
}})
}
type incompleteRequest struct{}
func (r incompleteRequest) Header() http.Header { return nil }
func (r incompleteRequest) URL() *url.URL { return nil }
func (r incompleteRequest) Method() string { return "" }
func (r incompleteRequest) Transport() TransportType { return TransportUnknown }
func TestSetWebRequestIncompleteRequest(t *testing.T) {
// Test SetWebRequest will safely handle situations where the request's
// URL() and Header() methods return nil.
app := testApp(distributedTracingReplyFields, enableBetterCAT, t)
txn := app.StartTransaction("hello", nil, nil)
err := txn.SetWebRequest(incompleteRequest{})
if err != nil {
t.Error("unexpected error", err)
}
txn.End()
app.ExpectMetrics(t, []internal.WantMetric{
{Name: "WebTransaction/Go/hello", Scope: "", Forced: true, Data: nil},
{Name: "WebTransaction", Scope: "", Forced: true, Data: nil},
{Name: "WebTransactionTotalTime/Go/hello", Scope: "", Forced: false, Data: nil},
{Name: "WebTransactionTotalTime", Scope: "", Forced: true, Data: nil},
{Name: "HttpDispatcher", Scope: "", Forced: true, Data: nil},
{Name: "Apdex", Scope: "", Forced: true, Data: nil},
{Name: "Apdex/Go/hello", Scope: "", Forced: false, Data: nil},
{Name: "DurationByCaller/Unknown/Unknown/Unknown/Unknown/all", Scope: "", Forced: false, Data: nil},
{Name: "DurationByCaller/Unknown/Unknown/Unknown/Unknown/allWeb", Scope: "", Forced: false, Data: nil},
})
app.ExpectTxnEvents(t, []internal.WantEvent{{
AgentAttributes: map[string]interface{}{},
Intrinsics: map[string]interface{}{
"name": "WebTransaction/Go/hello",
"guid": internal.MatchAnything,
"sampled": internal.MatchAnything,
"priority": internal.MatchAnything,
"traceId": internal.MatchAnything,
"nr.apdexPerfZone": internal.MatchAnything,
},
}})
}