-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwire_test.go
More file actions
182 lines (169 loc) · 5.49 KB
/
Copy pathwire_test.go
File metadata and controls
182 lines (169 loc) · 5.49 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
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
package groundcover
import (
"encoding/json"
"runtime/debug"
"testing"
"time"
)
func testResource() resource {
return resource{
serviceName: "checkout",
env: "prod",
namespace: "shop",
cluster: "c1",
release: "1.0.0",
userAgent: sdkName + "/1.0.0",
startTime: time.Unix(0, 0),
attrs: map[string]string{
"telemetry.sdk.name": sdkName,
"host.name": "host-1",
"service.name": "checkout", // session-level: must be excluded from metadata
"k8s.namespace.name": "shop", // session-level: must be excluded from metadata
},
}
}
func TestEncodeBatchShape(t *testing.T) {
e := &Event{
ID: "evt-1",
Timestamp: time.Unix(1, 0),
Type: "exception",
Level: LevelError,
ErrorHandled: true,
ErrorType: "*errors.errorString",
ErrorMessage: "boom",
Fingerprint: "fp",
User: User{ID: "u-1", Organization: "acme"},
Stacktrace: []Frame{{Function: "main.run", File: "/app/main.go", Line: 10, InApp: true}},
Attributes: Attributes{"gc.test_id": "needle", "amount": 9.5},
}
body, err := encodeBatch([]*Event{e}, testResource())
if err != nil {
t.Fatalf("encodeBatch: %v", err)
}
var p wirePayload
if err := json.Unmarshal(body, &p); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if p.SessionAttributes["service.name"] != "checkout" || p.SessionAttributes["env"] != "prod" {
t.Fatalf("session spine wrong: %+v", p.SessionAttributes)
}
if _, ok := p.SessionAttributes["session_start_time"]; !ok {
t.Fatal("session_start_time missing")
}
ev := p.Events[0]
if ev.Timestamp != time.Unix(1, 0).UnixNano() {
t.Fatalf("timestamp not nanoseconds: %d", ev.Timestamp)
}
if len(ev.Attributes.ErrorStacktrace) != 1 || ev.Attributes.ErrorStacktrace[0].Filename != "/app/main.go" {
t.Fatalf("frames wrong: %+v", ev.Attributes.ErrorStacktrace)
}
md := ev.Attributes.ErrorMetadata
if md["gc.test_id"] != "needle" || md["amount"] != 9.5 {
t.Fatalf("custom attrs missing from metadata: %+v", md)
}
if md["user.id"] != "u-1" || md["user.organization"] != "acme" {
t.Fatalf("identity missing from metadata: %+v", md)
}
if md["telemetry.sdk.name"] != sdkName || md["host.name"] != "host-1" {
t.Fatalf("detailed resource attrs missing from metadata: %+v", md)
}
if _, present := md["service.name"]; present {
t.Fatal("session-level key service.name must not be duplicated into metadata")
}
if _, present := md["k8s.namespace.name"]; present {
t.Fatal("session-level key k8s.namespace.name must not be duplicated into metadata")
}
if md["level"] != string(LevelError) {
t.Fatalf("level missing from metadata: %+v", md["level"])
}
}
func TestEncodeBatchIncludesTitle(t *testing.T) {
e := &Event{
Type: "exception",
Timestamp: time.Unix(1, 0),
Level: LevelError,
ErrorType: "*net.OpError",
ErrorMessage: "connection refused",
Title: "*net.OpError: connection refused",
}
body, err := encodeBatch([]*Event{e}, testResource())
if err != nil {
t.Fatalf("encodeBatch: %v", err)
}
var p wirePayload
if err := json.Unmarshal(body, &p); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if got := p.Events[0].Attributes.ErrorMetadata["gc.title"]; got != "*net.OpError: connection refused" {
t.Fatalf("gc.title not on the wire: %v", got)
}
}
func TestEstimateSizeAccountsForNestedAttributes(t *testing.T) {
plain := &Event{Type: "exception", ErrorMessage: "x"}
rich := &Event{
Type: "exception",
ErrorMessage: "x",
Stacktrace: []Frame{{Function: "f", File: "a.go"}},
Attributes: Attributes{
"nested": map[string]any{"a": "value", "b": []any{1, 2, "three"}},
"list": []any{"x", "y"},
"scalar": 12345,
"empty": nil,
},
}
if estimateSize(rich) <= estimateSize(plain) {
t.Fatalf("nested attributes should increase the estimate (%d vs %d)", estimateSize(rich), estimateSize(plain))
}
}
func TestEstimateValueSize(t *testing.T) {
if estimateValueSize("hello") != len("hello") {
t.Fatal("string size should be its length")
}
if estimateValueSize(true) != scalarSizeEstimate {
t.Fatal("scalar size should be the scalar estimate")
}
nested := map[string]any{"k": "vv"}
if got := estimateValueSize(nested); got <= len("k") {
t.Fatalf("map size should include keys and values, got %d", got)
}
if got := estimateValueSize(Attributes{"k": "vv"}); got <= len("k") {
t.Fatalf("Attributes size should include keys and values, got %d", got)
}
if estimateValueSize([]any{"a", "bb"}) < 2+len("a")+len("bb") {
t.Fatal("slice size should sum element sizes")
}
}
func TestVersionNonEmpty(t *testing.T) {
if Version() == "" {
t.Fatal("Version() must not be empty")
}
}
func TestModulePathMatchesMainModule(t *testing.T) {
// version.go lives in the module root package, so modulePath must equal
// the module path exactly. Compare against the test binary's main module
// from build info rather than a hardcoded string so the test keeps
// working after a module rename or fork.
bi, ok := debug.ReadBuildInfo()
if !ok || bi.Main.Path == "" {
t.Skip("no build info available")
}
if got := modulePath(); got != bi.Main.Path {
t.Errorf("modulePath() = %q, want main module path %q", got, bi.Main.Path)
}
}
func TestNormalizeModuleVersion(t *testing.T) {
tests := []struct {
in string
want string
}{
{"v0.1.1", "0.1.1"},
{"0.1.1", "0.1.1"},
{"(devel)", ""},
{"", ""},
}
for _, tc := range tests {
if got := normalizeModuleVersion(tc.in); got != tc.want {
t.Errorf("normalizeModuleVersion(%q) = %q, want %q", tc.in, got, tc.want)
}
}
}