-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathclient_reports_test.go
More file actions
105 lines (95 loc) · 2.78 KB
/
client_reports_test.go
File metadata and controls
105 lines (95 loc) · 2.78 KB
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
package sentry
import (
"bytes"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/getsentry/sentry-go/internal/ratelimit"
"github.com/getsentry/sentry-go/internal/testutils"
"github.com/getsentry/sentry-go/report"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
// TestClientReports_Integration tests that client reports are properly generated
// and sent when events are dropped for various reasons.
func TestClientReports_Integration(t *testing.T) {
var receivedBodies [][]byte
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
receivedBodies = append(receivedBodies, body)
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"id":"test-event-id"}`))
}))
defer srv.Close()
dsn := strings.Replace(srv.URL, "//", "//test@", 1) + "/1"
hub := CurrentHub().Clone()
c, err := NewClient(ClientOptions{
Dsn: dsn,
DisableClientReports: false,
SampleRate: 1.0,
BeforeSend: func(event *Event, _ *EventHint) *Event {
if event.Message == "drop-me" {
return nil
}
return event
},
})
if err != nil {
t.Fatalf("Init failed: %v", err)
}
hub.BindClient(c)
defer hub.Flush(testutils.FlushTimeout())
// second client with disabled reports shouldn't affect the first
_, _ = NewClient(ClientOptions{
Dsn: testDsn,
DisableClientReports: true,
})
// simulate dropped events for report outcomes
hub.CaptureMessage("drop-me")
scope := NewScope()
scope.AddEventProcessor(func(event *Event, _ *EventHint) *Event {
if event.Message == "processor-drop" {
return nil
}
return event
})
hub.WithScope(func(s *Scope) {
s.eventProcessors = scope.eventProcessors
hub.CaptureMessage("processor-drop")
})
hub.CaptureMessage("hi") // send an event to capture the report along with it
if !hub.Flush(testutils.FlushTimeout()) {
t.Fatal("Flush timed out")
}
var got report.ClientReport
found := false
for _, b := range receivedBodies {
for _, line := range bytes.Split(b, []byte("\n")) {
if json.Unmarshal(line, &got) == nil && len(got.DiscardedEvents) > 0 {
found = true
break
}
}
if found {
break
}
}
if !found {
t.Fatal("no client report found in envelope bodies")
}
if got.Timestamp.IsZero() {
t.Error("client report missing timestamp")
}
want := []report.DiscardedEvent{
{Reason: report.ReasonBeforeSend, Category: ratelimit.CategoryError, Quantity: 1},
{Reason: report.ReasonEventProcessor, Category: ratelimit.CategoryError, Quantity: 1},
}
if diff := cmp.Diff(want, got.DiscardedEvents, cmpopts.SortSlices(func(a, b report.DiscardedEvent) bool {
return a.Reason < b.Reason
})); diff != "" {
t.Errorf("DiscardedEvents mismatch (-want +got):\n%s", diff)
}
}