-
Notifications
You must be signed in to change notification settings - Fork 213
/
traces_profiler_test.go
154 lines (142 loc) · 4.29 KB
/
traces_profiler_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
package sentry
import (
"io"
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func testTraceProfiling(t *testing.T, rate float64) (*Span, *Event) {
ticker := setupProfilerTestTicker(io.Discard)
defer restoreProfilerTicker()
transport := &TransportMock{}
ctx := NewTestContext(ClientOptions{
Transport: transport,
EnableTracing: true,
TracesSampleRate: 1.0,
ProfilesSampleRate: rate,
Environment: "env",
Release: "rel",
Dist: "dist",
})
span := StartSpan(ctx, "top")
ticker.Tick()
span.Finish()
require.Equal(t, 1, len(transport.events))
return span, transport.events[0]
}
func TestTraceProfiling(t *testing.T) {
// Disable the automatically started global profiler after the test has finished.
defer func() {
startProfilerOnce = sync.Once{}
if globalProfiler != nil {
globalProfiler.Stop(true)
globalProfiler = nil
}
}()
var require = require.New(t)
require.Nil(globalProfiler)
var timeBeforeStarting = time.Now()
span, event := testTraceProfiling(t, 1.0)
require.Equal(transactionType, event.Type)
require.NotNil(event.sdkMetaData.transactionProfile)
var profileInfo = event.sdkMetaData.transactionProfile
require.Equal("go", profileInfo.Platform)
require.Equal(event.Environment, profileInfo.Environment)
require.Equal(event.Release, profileInfo.Release)
require.Equal(event.Dist, profileInfo.Dist)
require.GreaterOrEqual(profileInfo.Timestamp, timeBeforeStarting)
require.LessOrEqual(profileInfo.Timestamp, time.Now())
require.Equal(event.EventID, profileInfo.Transaction.ID)
require.Greater(profileInfo.Transaction.ActiveThreadID, uint64(0))
require.Equal(span.TraceID.String(), profileInfo.Transaction.TraceID)
validateProfile(t, profileInfo.Trace, span.EndTime.Sub(span.StartTime))
require.NotNil(globalProfiler)
}
func TestTraceProfilingDisabled(t *testing.T) {
if testing.Short() {
t.Skip("Skipping in short mode because of the timeout we wait for in Tick().")
}
var require = require.New(t)
_, event := testTraceProfiling(t, 0)
require.Equal(transactionType, event.Type)
require.Nil(event.sdkMetaData.transactionProfile)
}
func TestUpdateFromEvent(t *testing.T) {
var require = require.New(t)
t.Parallel()
cases := map[string]struct {
event *Event
original *profileInfo
expected *profileInfo
}{
"basic": {
event: &Event{
Environment: "test",
Platform: "go",
Release: "v1.0.0",
Dist: "dist",
EventID: "12345",
Contexts: map[string]Context{
"runtime": map[string]any{"name": "go", "version": "1.16"},
"os": map[string]any{"name": "linux"},
"device": map[string]any{"arch": "x86_64"},
},
},
expected: &profileInfo{
Environment: "test",
Platform: "go",
Release: "v1.0.0",
Dist: "dist",
Transaction: profileTransaction{ID: "12345"},
Runtime: profileRuntime{Name: "go", Version: "1.16"},
OS: profileOS{Name: "linux"},
Device: profileDevice{Architecture: "x86_64"},
},
original: &profileInfo{},
},
"values from original": {
event: &Event{
Environment: "test",
Platform: "go",
Release: "v1.0.0",
Dist: "dist",
EventID: "12345",
Contexts: map[string]Context{
"runtime": map[string]any{"name": "go"},
"os": map[string]any{"name": "linux"},
"device": map[string]any{"arch": 86},
},
},
original: &profileInfo{
Environment: "original",
Platform: "original",
Release: "original",
Dist: "original",
Transaction: profileTransaction{ID: "original"},
Runtime: profileRuntime{Name: "original", Version: "original"},
OS: profileOS{Name: "original"},
Device: profileDevice{Architecture: "original"},
},
expected: &profileInfo{
Environment: "test",
Platform: "go",
Release: "v1.0.0",
Dist: "dist",
Transaction: profileTransaction{ID: "12345"},
Runtime: profileRuntime{Name: "go", Version: "original"},
OS: profileOS{Name: "linux"},
Device: profileDevice{Architecture: "original"},
},
},
}
for name, tc := range cases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
info := tc.original
info.UpdateFromEvent(tc.event)
require.Equal(tc.expected, info)
})
}
}