-
Notifications
You must be signed in to change notification settings - Fork 213
/
transport_test.go
715 lines (620 loc) · 20.4 KB
/
transport_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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
package sentry
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/http/httptrace"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/getsentry/sentry-go/internal/testutils"
"github.com/google/go-cmp/cmp"
)
type unserializableType struct {
UnsupportedField func()
}
const (
basicEvent = `{"message":"mkey","sdk":{},"user":{}}`
enhancedEventInvalidBreadcrumb = `{"extra":{"info":"Could not encode original event as JSON. Succeeded by removing Breadcrumbs, Contexts and Extra. Please verify the data you attach to the scope. Error: json: error calling MarshalJSON for type *sentry.Event: json: error calling MarshalJSON for type *sentry.Breadcrumb: json: unsupported type: func()"},"message":"mkey","sdk":{},"user":{}}`
enhancedEventInvalidContextOrExtra = `{"extra":{"info":"Could not encode original event as JSON. Succeeded by removing Breadcrumbs, Contexts and Extra. Please verify the data you attach to the scope. Error: json: error calling MarshalJSON for type *sentry.Event: json: unsupported type: func()"},"message":"mkey","sdk":{},"user":{}}`
)
func TestGetRequestBodyFromEventValid(t *testing.T) {
body := getRequestBodyFromEvent(&Event{
Message: "mkey",
})
got := string(body)
want := basicEvent
if got != want {
t.Errorf("expected different shape of body. \ngot: %s\nwant: %s", got, want)
}
}
func TestGetRequestBodyFromEventInvalidBreadcrumbsField(t *testing.T) {
body := getRequestBodyFromEvent(&Event{
Message: "mkey",
Breadcrumbs: []*Breadcrumb{{
Data: map[string]interface{}{
"wat": unserializableType{},
},
}},
})
got := string(body)
want := enhancedEventInvalidBreadcrumb
if got != want {
t.Errorf("expected different shape of body. \ngot: %s\nwant: %s", got, want)
}
}
func TestGetRequestBodyFromEventInvalidExtraField(t *testing.T) {
body := getRequestBodyFromEvent(&Event{
Message: "mkey",
Extra: map[string]interface{}{
"wat": unserializableType{},
},
})
got := string(body)
want := enhancedEventInvalidContextOrExtra
if got != want {
t.Errorf("expected different shape of body. \ngot: %s\nwant: %s", got, want)
}
}
func TestGetRequestBodyFromEventInvalidContextField(t *testing.T) {
body := getRequestBodyFromEvent(&Event{
Message: "mkey",
Contexts: map[string]Context{
"wat": {"key": unserializableType{}},
},
})
got := string(body)
want := enhancedEventInvalidContextOrExtra
if got != want {
t.Errorf("expected different shape of body. \ngot: %s\nwant: %s", got, want)
}
}
func TestGetRequestBodyFromEventMultipleInvalidFields(t *testing.T) {
body := getRequestBodyFromEvent(&Event{
Message: "mkey",
Breadcrumbs: []*Breadcrumb{{
Data: map[string]interface{}{
"wat": unserializableType{},
},
}},
Extra: map[string]interface{}{
"wat": unserializableType{},
},
Contexts: map[string]Context{
"wat": {"key": unserializableType{}},
},
})
got := string(body)
want := enhancedEventInvalidBreadcrumb
if got != want {
t.Errorf("expected different shape of body. \ngot: %s\nwant: %s", got, want)
}
}
func TestGetRequestBodyFromEventCompletelyInvalid(t *testing.T) {
body := getRequestBodyFromEvent(&Event{
Exception: []Exception{{
Stacktrace: &Stacktrace{
Frames: []Frame{{
Vars: map[string]interface{}{
"wat": unserializableType{},
},
}},
},
}},
})
if body != nil {
t.Error("expected body to be nil")
}
}
func newTestEvent(eventType string) *Event {
event := NewEvent()
event.Type = eventType
event.EventID = "b81c5be4d31e48959103a1f878a1efcb"
event.Sdk = SdkInfo{
Name: "sentry.go",
Version: "0.0.1",
}
return event
}
func newTestDSN(t *testing.T) *Dsn {
dsn, err := NewDsn("http://[email protected]/sentry/1")
if err != nil {
t.Fatal(err)
}
return dsn
}
func TestEnvelopeFromErrorBody(t *testing.T) {
event := newTestEvent(eventType)
sentAt := time.Unix(0, 0).UTC()
body := json.RawMessage(`{"type":"event","fields":"omitted"}`)
b, err := envelopeFromBody(event, newTestDSN(t), sentAt, body)
if err != nil {
t.Fatal(err)
}
got := b.String()
want := `{"event_id":"b81c5be4d31e48959103a1f878a1efcb","sent_at":"1970-01-01T00:00:00Z","dsn":"http://[email protected]/sentry/1","sdk":{"name":"sentry.go","version":"0.0.1"}}
{"type":"event","length":35}
{"type":"event","fields":"omitted"}
`
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("Envelope mismatch (-want +got):\n%s", diff)
}
}
func TestEnvelopeFromTransactionBody(t *testing.T) {
event := newTestEvent(transactionType)
sentAt := time.Unix(0, 0).UTC()
body := json.RawMessage(`{"type":"transaction","fields":"omitted"}`)
b, err := envelopeFromBody(event, newTestDSN(t), sentAt, body)
if err != nil {
t.Fatal(err)
}
got := b.String()
want := `{"event_id":"b81c5be4d31e48959103a1f878a1efcb","sent_at":"1970-01-01T00:00:00Z","dsn":"http://[email protected]/sentry/1","sdk":{"name":"sentry.go","version":"0.0.1"}}
{"type":"transaction","length":41}
{"type":"transaction","fields":"omitted"}
`
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("Envelope mismatch (-want +got):\n%s", diff)
}
}
func TestEnvelopeFromEventWithAttachments(t *testing.T) {
event := newTestEvent(eventType)
event.Attachments = []*Attachment{
// Empty content-type and payload
{
Filename: "empty.txt",
},
// Non-empty content-type and payload
{
Filename: "non-empty.txt",
ContentType: "text/html",
Payload: []byte("<h1>Look, HTML</h1>"),
},
}
sentAt := time.Unix(0, 0).UTC()
body := json.RawMessage(`{"type":"event","fields":"omitted"}`)
b, err := envelopeFromBody(event, newTestDSN(t), sentAt, body)
if err != nil {
t.Fatal(err)
}
got := b.String()
want := `{"event_id":"b81c5be4d31e48959103a1f878a1efcb","sent_at":"1970-01-01T00:00:00Z","dsn":"http://[email protected]/sentry/1","sdk":{"name":"sentry.go","version":"0.0.1"}}
{"type":"event","length":35}
{"type":"event","fields":"omitted"}
{"type":"attachment","length":0,"filename":"empty.txt"}
{"type":"attachment","length":19,"filename":"non-empty.txt","content_type":"text/html"}
<h1>Look, HTML</h1>
`
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("Envelope mismatch (-want +got):\n%s", diff)
}
}
func TestEnvelopeFromTransactionWithProfile(t *testing.T) {
event := newTestEvent(transactionType)
event.sdkMetaData.transactionProfile = &profileInfo{
Trace: &profileTrace{
Frames: []*Frame{
{
Function: "func",
Module: "module",
Filename: "file.go",
Lineno: 42,
Colno: 24,
},
},
Samples: []profileSample{
{
ElapsedSinceStartNS: 10,
StackID: 2,
ThreadID: 3,
},
},
Stacks: []profileStack{{0}},
ThreadMetadata: map[uint64]*profileThreadMetadata{
1: {Name: "GO 1"},
},
},
Transaction: profileTransaction{
ActiveThreadID: 1,
DurationNS: 2,
ID: "3",
Name: "tx-name",
TraceID: "trace-id",
},
}
sentAt := time.Unix(0, 0).UTC()
body := json.RawMessage(`{"type":"transaction","fields":"omitted"}`)
b, err := envelopeFromBody(event, newTestDSN(t), sentAt, body)
if err != nil {
t.Fatal(err)
}
got := b.String()
want := `{"event_id":"b81c5be4d31e48959103a1f878a1efcb","sent_at":"1970-01-01T00:00:00Z","dsn":"http://[email protected]/sentry/1","sdk":{"name":"sentry.go","version":"0.0.1"}}
{"type":"transaction","length":41}
{"type":"transaction","fields":"omitted"}
{"type":"profile","length":618}
{"device":{"architecture":"","classification":"","locale":"","manufacturer":"","model":""},"event_id":"","os":{"build_number":"","name":"","version":""},"platform":"","release":"","dist":"","runtime":{"name":"","version":""},"timestamp":"0001-01-01T00:00:00Z","profile":{"frames":[{"function":"func","module":"module","filename":"file.go","lineno":42,"colno":24,"in_app":false}],"samples":[{"elapsed_since_start_ns":10,"stack_id":2,"thread_id":3}],"stacks":[[0]],"thread_metadata":{"1":{"name":"GO 1"}}},"transaction":{"active_thread_id":1,"duration_ns":2,"id":"3","name":"tx-name","trace_id":"trace-id"},"version":""}
`
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("Envelope mismatch (-want +got):\n%s", diff)
}
}
func TestEnvelopeFromCheckInEvent(t *testing.T) {
event := newTestEvent(checkInType)
event.CheckIn = &CheckIn{
MonitorSlug: "test-slug",
ID: "123c5be4d31e48959103a1f878a1efcb",
Status: CheckInStatusOK,
}
event.MonitorConfig = &MonitorConfig{
Schedule: IntervalSchedule(1, MonitorScheduleUnitHour),
CheckInMargin: 10,
MaxRuntime: 5000,
Timezone: "Asia/Singapore",
}
sentAt := time.Unix(0, 0).UTC()
body := getRequestBodyFromEvent(event)
b, err := envelopeFromBody(event, newTestDSN(t), sentAt, body)
if err != nil {
t.Fatal(err)
}
got := b.String()
want := `{"event_id":"b81c5be4d31e48959103a1f878a1efcb","sent_at":"1970-01-01T00:00:00Z","dsn":"http://[email protected]/sentry/1","sdk":{"name":"sentry.go","version":"0.0.1"}}
{"type":"check_in","length":232}
{"check_in_id":"123c5be4d31e48959103a1f878a1efcb","monitor_slug":"test-slug","status":"ok","monitor_config":{"schedule":{"type":"interval","value":1,"unit":"hour"},"checkin_margin":10,"max_runtime":5000,"timezone":"Asia/Singapore"}}
`
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("Envelope mismatch (-want +got):\n%s", diff)
}
}
func TestGetRequestFromEvent(t *testing.T) {
testCases := []struct {
testName string
// input
event *Event
// output
apiURL string
}{
{
testName: "Sample Event",
event: newTestEvent(eventType),
apiURL: "https://host/path/api/42/envelope/",
},
{
testName: "Transaction",
event: newTestEvent(transactionType),
apiURL: "https://host/path/api/42/envelope/",
},
}
for _, test := range testCases {
test := test
dsn, err := NewDsn("https://key@host/path/42")
if err != nil {
t.Fatal(err)
}
t.Run(test.testName, func(t *testing.T) {
req, err := getRequestFromEvent(context.TODO(), test.event, dsn)
if err != nil {
t.Fatal(err)
}
if req.Method != http.MethodPost {
t.Errorf("Request %v using http method: %s, supposed to use method: %s", req, req.Method, http.MethodPost)
}
if req.URL.String() != test.apiURL {
t.Errorf("Incorrect API URL. want: %s, got: %s", test.apiURL, req.URL.String())
}
userAgent := fmt.Sprintf("%s/%s", test.event.Sdk.Name, test.event.Sdk.Version)
if ua := req.UserAgent(); ua != userAgent {
t.Errorf("got User-Agent = %q, want %q", ua, userAgent)
}
contentType := "application/x-sentry-envelope"
if ct := req.Header.Get("Content-Type"); ct != contentType {
t.Errorf("got Content-Type = %q, want %q", ct, contentType)
}
xSentryAuth := "Sentry sentry_version=7, sentry_client=sentry.go/0.0.1, sentry_key=key"
if auth := req.Header.Get("X-Sentry-Auth"); !strings.HasPrefix(auth, xSentryAuth) {
t.Errorf("got X-Sentry-Auth = %q, want %q", auth, xSentryAuth)
}
})
}
}
// A testHTTPServer counts events sent to it. It requires a call to Unblock
// before incrementing its internal counter and sending a response to the HTTP
// client. This allows for coordinating the execution flow when needed.
type testHTTPServer struct {
*httptest.Server
// eventCounter counts the number of events processed by the server.
eventCounter *uint64
// ch is used to block/unblock the server on demand.
ch chan bool
}
func newTestHTTPServer(t *testing.T) *testHTTPServer {
ch := make(chan bool)
eventCounter := new(uint64)
handler := func(w http.ResponseWriter, r *http.Request) {
var event struct {
EventID string `json:"event_id"`
}
dec := json.NewDecoder(r.Body)
err := dec.Decode(&event)
if err != nil {
t.Fatal(err)
}
// Block until signal to continue.
<-ch
count := atomic.AddUint64(eventCounter, 1)
t.Logf("[SERVER] {%.4s} event received (#%d)", event.EventID, count)
}
return &testHTTPServer{
Server: httptest.NewTLSServer(http.HandlerFunc(handler)),
eventCounter: eventCounter,
ch: ch,
}
}
func (ts *testHTTPServer) EventCount() uint64 {
return atomic.LoadUint64(ts.eventCounter)
}
func (ts *testHTTPServer) Unblock() {
ts.ch <- true
}
func TestHTTPTransport(t *testing.T) {
server := newTestHTTPServer(t)
defer server.Close()
transport := NewHTTPTransport()
transport.Configure(ClientOptions{
Dsn: fmt.Sprintf("https://test@%s/1", server.Listener.Addr()),
HTTPClient: server.Client(),
})
// Helpers
transportSendTestEvent := func(t *testing.T) (id string) {
t.Helper()
e := NewEvent()
id = uuid()
e.EventID = EventID(id)
transport.SendEvent(e)
t.Logf("[CLIENT] {%.4s} event sent", e.EventID)
return id
}
transportMustFlush := func(t *testing.T, id string) {
t.Helper()
ok := transport.Flush(testutils.FlushTimeout())
if !ok {
t.Fatalf("[CLIENT] {%.4s} Flush() timed out", id)
}
}
serverEventCountMustBe := func(t *testing.T, n uint64) {
t.Helper()
count := server.EventCount()
if count != n {
t.Fatalf("[SERVER] event count = %d, want %d", count, n)
}
}
testSendSingleEvent := func(t *testing.T) {
// Sending a single event should increase the server event count by
// exactly one.
initialCount := server.EventCount()
id := transportSendTestEvent(t)
// Server is blocked waiting for us, right now count must not have
// changed yet.
serverEventCountMustBe(t, initialCount)
// After unblocking the server, Flush must guarantee that the server
// event count increased by one.
server.Unblock()
transportMustFlush(t, id)
serverEventCountMustBe(t, initialCount+1)
}
// Actual tests
t.Run("SendSingleEvent", testSendSingleEvent)
t.Run("FlushMultipleTimes", func(t *testing.T) {
// Flushing multiple times should not increase the server event count.
initialCount := server.EventCount()
for i := 0; i < 10; i++ {
transportMustFlush(t, fmt.Sprintf("loop%d", i))
}
serverEventCountMustBe(t, initialCount)
})
t.Run("ConcurrentSendAndFlush", func(t *testing.T) {
// It should be safe to send events and flush concurrently.
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
testSendSingleEvent(t)
}()
go func() {
defer wg.Done()
transportMustFlush(t, "from goroutine")
}()
wg.Wait()
})
}
// httptraceRoundTripper implements http.RoundTripper by wrapping
// http.DefaultTransport and keeps track of whether TCP connections have been
// reused for every request.
//
// For simplicity, httptraceRoundTripper is not safe for concurrent use.
type httptraceRoundTripper struct {
reusedConn []bool
}
func (rt *httptraceRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
trace := &httptrace.ClientTrace{
GotConn: func(connInfo httptrace.GotConnInfo) {
rt.reusedConn = append(rt.reusedConn, connInfo.Reused)
},
}
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
return http.DefaultTransport.RoundTrip(req)
}
func testKeepAlive(t *testing.T, tr Transport) {
// event is a test event. It is empty because here we only care about
// the reuse of TCP connections between client and server, not the
// specific contents of the event itself.
event := &Event{}
// largeResponse controls whether the test server should simulate an
// unexpectedly large response from Relay -- the SDK should not try to
// consume arbitrarily large responses.
largeResponse := false
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Simulates a response from Relay. The event_id is arbitrary,
// it doesn't matter for this test.
fmt.Fprintln(w, `{"id":"ec71d87189164e79ab1e61030c183af0"}`)
if largeResponse {
fmt.Fprintln(w, strings.Repeat(" ", maxDrainResponseBytes))
}
}))
defer srv.Close()
dsn := strings.Replace(srv.URL, "//", "//pubkey@", 1) + "/1"
rt := &httptraceRoundTripper{}
tr.Configure(ClientOptions{
Dsn: dsn,
HTTPTransport: rt,
})
reqCount := 0
checkLastConnReuse := func(reused bool) {
t.Helper()
reqCount++
if !tr.Flush(testutils.FlushTimeout()) {
t.Fatal("Flush timed out")
}
if len(rt.reusedConn) != reqCount {
t.Fatalf("unexpected number of requests: got %d, want %d", len(rt.reusedConn), reqCount)
}
if rt.reusedConn[reqCount-1] != reused {
if reused {
t.Fatal("TCP connection not reused")
}
t.Fatal("unexpected TCP connection reuse")
}
}
// First event creates a new TCP connection.
tr.SendEvent(event)
checkLastConnReuse(false)
// Next events reuse the TCP connection.
for i := 0; i < 10; i++ {
tr.SendEvent(event)
checkLastConnReuse(true)
}
// If server responses are too large, the SDK should close the
// connection instead of consuming an arbitrarily large number of bytes.
largeResponse = true
// Next event, first one to get a large response, reuses the connection.
tr.SendEvent(event)
checkLastConnReuse(true)
// All future events create a new TCP connection.
for i := 0; i < 10; i++ {
tr.SendEvent(event)
checkLastConnReuse(false)
}
}
func TestKeepAlive(t *testing.T) {
t.Run("AsyncTransport", func(t *testing.T) {
testKeepAlive(t, NewHTTPTransport())
})
t.Run("SyncTransport", func(t *testing.T) {
testKeepAlive(t, NewHTTPSyncTransport())
})
}
func TestRateLimiting(t *testing.T) {
t.Run("AsyncTransport", func(t *testing.T) {
testRateLimiting(t, NewHTTPTransport())
})
t.Run("SyncTransport", func(t *testing.T) {
testRateLimiting(t, NewHTTPSyncTransport())
})
}
func testRateLimiting(t *testing.T, tr Transport) {
errorEvent := &Event{}
transactionEvent := &Event{Type: transactionType}
var errorEventCount, transactionEventCount uint64
writeRateLimits := func(w http.ResponseWriter, s string) {
w.Header().Add("Retry-After", "50")
w.Header().Add("X-Sentry-Rate-Limits", s)
w.WriteHeader(http.StatusTooManyRequests)
fmt.Fprint(w, `{"id":"636205708f6846c8821e6576a9d05921"}`)
}
// Test server that simulates responses with rate limits.
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := io.ReadAll(r.Body)
if err != nil {
panic(err)
}
if bytes.Contains(b, []byte("transaction")) {
atomic.AddUint64(&transactionEventCount, 1)
writeRateLimits(w, "20:transaction")
} else {
atomic.AddUint64(&errorEventCount, 1)
writeRateLimits(w, "50:error")
}
}))
defer srv.Close()
dsn := strings.Replace(srv.URL, "//", "//pubkey@", 1) + "/1"
tr.Configure(ClientOptions{
Dsn: dsn,
})
// Send several errors and transactions concurrently.
//
// Because the server always returns a rate limit for the payload type
// in the request, the expectation is that, for both errors and
// transactions, the first event is sent successfully, and then all
// others are discarded before hitting the server.
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
for i := 0; i < 10; i++ {
tr.SendEvent(errorEvent)
}
}()
go func() {
defer wg.Done()
for i := 0; i < 10; i++ {
tr.SendEvent(transactionEvent)
}
}()
wg.Wait()
if !tr.Flush(testutils.FlushTimeout()) {
t.Fatal("Flush timed out")
}
// Only one event of each kind should have hit the transport, all other
// events discarded because of rate limiting.
if n := atomic.LoadUint64(&errorEventCount); n != 1 {
t.Errorf("got errorEvent = %d, want %d", n, 1)
}
if n := atomic.LoadUint64(&transactionEventCount); n != 1 {
t.Errorf("got transactionEvent = %d, want %d", n, 1)
}
}
func TestEnvelopeFromMetricBody(t *testing.T) {
event := newTestEvent(metricType)
event.Metrics = append(event.Metrics,
NewCounterMetric("counter", Second(), map[string]string{"foo": "bar", "route": "GET /foo"}, 1597790835, 1.0),
NewDistributionMetric("distribution", Second(), map[string]string{"$foo$": "%bar%"}, 1597790835, 1.0),
NewGaugeMetric("gauge", Second(), map[string]string{"föö": "bär"}, 1597790835, 1.0),
NewSetMetric[int]("set", Second(), map[string]string{"%{key}": "$value$"}, 1597790835, 1),
NewCounterMetric("no_tags", Second(), nil, 1597790835, 1.0),
)
sentAt := time.Unix(0, 0).UTC()
body := getRequestBodyFromEvent(event)
b, err := envelopeFromBody(event, newTestDSN(t), sentAt, body)
if err != nil {
t.Fatal(err)
}
got := b.String()
want := `{"event_id":"b81c5be4d31e48959103a1f878a1efcb","sent_at":"1970-01-01T00:00:00Z","dsn":"http://[email protected]/sentry/1","sdk":{"name":"sentry.go","version":"0.0.1"}}
{"type":"statsd","length":218}
counter@second:1|c|#foo:bar,route:GET /foo|T1597790835
distribution@second:1|d|#_foo_:bar|T1597790835
gauge@second:1:1:1:1:1|g|#f_:br|T1597790835
set@second:1|s|#_key_:$value$|T1597790835
no_tags@second:1|c|T1597790835
`
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("Envelope mismatch (-want +got):\n%s", diff)
}
}