-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessor_test.go
More file actions
253 lines (217 loc) · 6.46 KB
/
Copy pathprocessor_test.go
File metadata and controls
253 lines (217 loc) · 6.46 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
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
// Copyright 2024 tailsampling-go contributors
// Licensed under the Apache License, Version 2.0
package tailsampling_test
import (
"context"
"strings"
"sync"
"testing"
"time"
tailsampling "github.com/lukashes/tailsampling-go"
"github.com/lukashes/tailsampling-go/policies"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)
// testExporter collects exported spans for testing
type testExporter struct {
mu sync.Mutex
spans []sdktrace.ReadOnlySpan
}
func (t *testExporter) ExportSpans(ctx context.Context, spans []sdktrace.ReadOnlySpan) error {
t.mu.Lock()
defer t.mu.Unlock()
t.spans = append(t.spans, spans...)
return nil
}
func (t *testExporter) Shutdown(ctx context.Context) error {
return nil
}
func (t *testExporter) GetSpans() []sdktrace.ReadOnlySpan {
t.mu.Lock()
defer t.mu.Unlock()
return t.spans
}
func TestLatencyPolicy(t *testing.T) {
exporter := &testExporter{}
latencyPolicy := policies.NewLatency("latency", 100, 0)
processor, err := tailsampling.NewTailSamplingProcessor(
tailsampling.WithDecisionWait(100*time.Millisecond),
tailsampling.WithNumTraces(100),
tailsampling.WithPolicies(latencyPolicy),
tailsampling.WithExporter(exporter),
)
if err != nil {
t.Fatalf("failed to create processor: %v", err)
}
tp := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithSpanProcessor(processor),
)
otel.SetTracerProvider(tp)
tracer := tp.Tracer("test")
// Fast trace - should NOT be sampled
_, span1 := tracer.Start(context.Background(), "fast")
time.Sleep(50 * time.Millisecond)
span1.End()
// Slow trace - SHOULD be sampled
_, span2 := tracer.Start(context.Background(), "slow")
time.Sleep(150 * time.Millisecond)
span2.End()
// Wait for decision (DecisionWait = 100ms, ticker = 50ms)
time.Sleep(500 * time.Millisecond)
if err := tp.Shutdown(context.Background()); err != nil {
t.Fatalf("shutdown failed: %v", err)
}
// Check exported spans
spans := exporter.GetSpans()
if len(spans) != 1 {
t.Fatalf("expected 1 exported span, got %d", len(spans))
}
if spans[0].Name() != "slow" {
t.Errorf("expected 'slow' span, got '%s'", spans[0].Name())
}
}
func TestStatusCodePolicy(t *testing.T) {
exporter := &testExporter{}
statusPolicy, _ := policies.NewStatusCode("errors", []string{"ERROR"})
processor, err := tailsampling.NewTailSamplingProcessor(
tailsampling.WithDecisionWait(100*time.Millisecond),
tailsampling.WithNumTraces(100),
tailsampling.WithPolicies(statusPolicy),
tailsampling.WithExporter(exporter),
)
if err != nil {
t.Fatalf("failed to create processor: %v", err)
}
tp := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithSpanProcessor(processor),
)
otel.SetTracerProvider(tp)
tracer := tp.Tracer("test")
// OK trace - should NOT be sampled
_, span1 := tracer.Start(context.Background(), "ok-span")
span1.End()
// Error trace - SHOULD be sampled
_, span2 := tracer.Start(context.Background(), "error-span")
span2.SetStatus(codes.Error, "test error")
span2.End()
// Wait for decision (DecisionWait = 100ms, ticker = 50ms)
time.Sleep(500 * time.Millisecond)
if err := tp.Shutdown(context.Background()); err != nil {
t.Fatalf("shutdown failed: %v", err)
}
// Check that error trace was sampled and OK trace was not
metrics := processor.GetMetrics()
if metrics.PolicySampled != 1 {
t.Errorf("expected 1 sampled trace, got %d", metrics.PolicySampled)
}
if metrics.PolicyNotSampled != 1 {
t.Errorf("expected 1 not-sampled trace, got %d", metrics.PolicyNotSampled)
}
}
func TestDropPendingTracesOnShutdown(t *testing.T) {
exporter := &testExporter{}
statusPolicy, _ := policies.NewStatusCode("all", []string{"ERROR", "OK", "UNSET"})
// DropPendingTracesOnShutdown = false
processor, err := tailsampling.NewTailSamplingProcessor(
tailsampling.WithDecisionWait(10*time.Second), // Long wait
tailsampling.WithNumTraces(100),
tailsampling.WithPolicies(statusPolicy),
tailsampling.WithExporter(exporter),
tailsampling.WithDropPendingTracesOnShutdown(false),
)
if err != nil {
t.Fatalf("failed to create processor: %v", err)
}
tp := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithSpanProcessor(processor),
)
otel.SetTracerProvider(tp)
tracer := tp.Tracer("test")
// Create span
_, span := tracer.Start(context.Background(), "pending-span")
span.End()
// Shutdown immediately (before DecisionWait)
if err := tp.Shutdown(context.Background()); err != nil {
t.Fatalf("shutdown failed: %v", err)
}
// Span should be exported despite not waiting
spans := exporter.GetSpans()
if len(spans) != 1 {
t.Errorf("expected 1 span (processed on shutdown), got %d", len(spans))
}
}
func TestConfigValidation(t *testing.T) {
exporter := &testExporter{}
policy := policies.NewLatency("latency", 100, 0)
tests := []struct {
name string
opts []tailsampling.Option
want string
}{
{
name: "no exporter",
opts: []tailsampling.Option{
tailsampling.WithPolicies(policy),
},
want: "no exporter configured",
},
{
name: "no policies",
opts: []tailsampling.Option{
tailsampling.WithExporter(exporter),
},
want: "no policies configured",
},
{
name: "zero DecisionWait",
opts: []tailsampling.Option{
tailsampling.WithExporter(exporter),
tailsampling.WithPolicies(policy),
tailsampling.WithDecisionWait(0),
},
want: "DecisionWait must be positive",
},
{
name: "DecisionWait too large",
opts: []tailsampling.Option{
tailsampling.WithExporter(exporter),
tailsampling.WithPolicies(policy),
tailsampling.WithDecisionWait(48 * time.Hour),
},
want: "DecisionWait too large",
},
{
name: "zero NumTraces",
opts: []tailsampling.Option{
tailsampling.WithExporter(exporter),
tailsampling.WithPolicies(policy),
tailsampling.WithNumTraces(0),
},
want: "NumTraces must be positive",
},
{
name: "NumTraces too large",
opts: []tailsampling.Option{
tailsampling.WithExporter(exporter),
tailsampling.WithPolicies(policy),
tailsampling.WithNumTraces(20_000_000),
},
want: "NumTraces too large",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := tailsampling.NewTailSamplingProcessor(tt.opts...)
if err == nil {
t.Fatal("expected error, got nil")
}
if !strings.Contains(err.Error(), tt.want) {
t.Errorf("expected error containing %q, got %q", tt.want, err.Error())
}
})
}
}