-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathotel_test.go
More file actions
339 lines (289 loc) · 8.14 KB
/
otel_test.go
File metadata and controls
339 lines (289 loc) · 8.14 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
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
package docker
import (
"context"
"errors"
"testing"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
"go.opentelemetry.io/otel/trace"
"github.com/jasoet/pkg/v2/otel"
)
func TestOTelInstrumentation_AddSpanAttributes(t *testing.T) {
tests := []struct {
name string
enabled bool
attributes []attribute.KeyValue
wantAttrs bool
}{
{
name: "enabled instrumentation adds attributes",
enabled: true,
attributes: []attribute.KeyValue{
attribute.String("container.id", "abc123"),
attribute.String("container.image", "nginx:latest"),
},
wantAttrs: true,
},
{
name: "disabled instrumentation ignores attributes",
enabled: false,
attributes: []attribute.KeyValue{attribute.String("test", "value")},
wantAttrs: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var inst *otelInstrumentation
var exporter *tracetest.InMemoryExporter
var ctx context.Context
if tt.enabled {
// Create in-memory exporter for testing
exporter = tracetest.NewInMemoryExporter()
tp := sdktrace.NewTracerProvider(
sdktrace.WithSyncer(exporter),
)
cfg := &otel.Config{
TracerProvider: tp,
}
inst = newOTelInstrumentation(cfg)
// Start a span so we have something to add attributes to
var span trace.Span
ctx, span = inst.startSpan(context.Background(), "test.operation")
// Add attributes
inst.addSpanAttributes(ctx, tt.attributes...)
// End span to flush to exporter
span.End()
} else {
inst = &otelInstrumentation{enabled: false}
ctx = context.Background()
// Add attributes (should be no-op)
inst.addSpanAttributes(ctx, tt.attributes...)
}
if tt.wantAttrs && exporter != nil {
// Check attributes
spans := exporter.GetSpans()
if len(spans) == 0 {
t.Fatal("expected span to be created")
}
span := spans[0]
attrMap := make(map[attribute.Key]attribute.Value)
for _, attr := range span.Attributes {
attrMap[attr.Key] = attr.Value
}
for _, expectedAttr := range tt.attributes {
if val, ok := attrMap[expectedAttr.Key]; !ok {
t.Errorf("expected attribute %s not found", expectedAttr.Key)
} else if val != expectedAttr.Value {
t.Errorf("attribute %s = %v, want %v", expectedAttr.Key, val, expectedAttr.Value)
}
}
}
})
}
}
func TestOTelInstrumentation_SetSpanStatus(t *testing.T) {
tests := []struct {
name string
enabled bool
code int
description string
wantError bool
}{
{
name: "enabled instrumentation sets error status",
enabled: true,
code: 1,
description: "operation failed",
wantError: true,
},
{
name: "enabled instrumentation sets ok status",
enabled: true,
code: 0,
description: "operation succeeded",
wantError: false,
},
{
name: "disabled instrumentation ignores status",
enabled: false,
code: 1,
description: "ignored",
wantError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var inst *otelInstrumentation
var exporter *tracetest.InMemoryExporter
var ctx context.Context
if tt.enabled {
// Create in-memory exporter for testing
exporter = tracetest.NewInMemoryExporter()
tp := sdktrace.NewTracerProvider(
sdktrace.WithSyncer(exporter),
)
cfg := &otel.Config{
TracerProvider: tp,
}
inst = newOTelInstrumentation(cfg)
// Start a span
var span trace.Span
ctx, span = inst.startSpan(context.Background(), "test.operation")
// Set span status
inst.setSpanStatus(ctx, tt.code, tt.description)
// End span to flush to exporter
span.End()
} else {
inst = &otelInstrumentation{enabled: false}
ctx = context.Background()
// Set span status (should be no-op)
inst.setSpanStatus(ctx, tt.code, tt.description)
}
if tt.enabled && exporter != nil {
spans := exporter.GetSpans()
if len(spans) == 0 {
t.Fatal("expected span to be created")
}
span := spans[0]
if tt.code != 0 {
// Should set Error status via span.SetStatus
if span.Status.Code != codes.Error {
t.Errorf("span status code = %v, want Error", span.Status.Code)
}
if span.Status.Description != tt.description {
t.Errorf("span status description = %v, want %v", span.Status.Description, tt.description)
}
} else {
// Should set Ok status
if span.Status.Code != codes.Ok {
t.Errorf("span status code = %v, want Ok", span.Status.Code)
}
}
}
})
}
}
func TestOTelInstrumentation_RecordError(t *testing.T) {
// Create in-memory exporter and meter reader
exporter := tracetest.NewInMemoryExporter()
tp := sdktrace.NewTracerProvider(
sdktrace.WithSyncer(exporter),
)
reader := sdkmetric.NewManualReader()
mp := sdkmetric.NewMeterProvider(
sdkmetric.WithReader(reader),
)
cfg := &otel.Config{
TracerProvider: tp,
MeterProvider: mp,
}
inst := newOTelInstrumentation(cfg)
ctx, span := inst.startSpan(context.Background(), "test.operation")
// Record an error
testErr := errors.New("test error")
inst.recordError(ctx, "test_error", testErr)
// End span to flush to exporter
span.End()
// Check span attributes
spans := exporter.GetSpans()
if len(spans) == 0 {
t.Fatal("expected span to be created")
}
spanStub := spans[0]
foundErrorType := false
foundErrorMsg := false
for _, attr := range spanStub.Attributes {
if attr.Key == "error.type" && attr.Value.AsString() == "test_error" {
foundErrorType = true
}
if attr.Key == "error.message" && attr.Value.AsString() == testErr.Error() {
foundErrorMsg = true
}
}
if !foundErrorType {
t.Error("expected error.type attribute not found")
}
if !foundErrorMsg {
t.Error("expected error.message attribute not found")
}
// Check metrics
var rm metricdata.ResourceMetrics
if err := reader.Collect(ctx, &rm); err != nil {
t.Fatalf("failed to collect metrics: %v", err)
}
// Verify error counter was incremented
foundCounter := false
for _, sm := range rm.ScopeMetrics {
for _, m := range sm.Metrics {
if m.Name == "docker.container.errors" {
foundCounter = true
}
}
}
if !foundCounter {
t.Error("expected error counter metric not found")
}
}
func TestOTelInstrumentation_IncrementCounter(t *testing.T) {
reader := sdkmetric.NewManualReader()
mp := sdkmetric.NewMeterProvider(
sdkmetric.WithReader(reader),
)
cfg := &otel.Config{
MeterProvider: mp,
}
inst := newOTelInstrumentation(cfg)
ctx := context.Background()
// Test all counter types
counters := []string{
"containers_started",
"containers_stopped",
"containers_terminated",
"containers_restarted",
}
for _, counter := range counters {
inst.incrementCounter(ctx, counter, 1)
}
// Collect metrics
var rm metricdata.ResourceMetrics
if err := reader.Collect(ctx, &rm); err != nil {
t.Fatalf("failed to collect metrics: %v", err)
}
// Verify all counters exist
expectedMetrics := map[string]bool{
"docker.containers.started": false,
"docker.containers.stopped": false,
"docker.containers.terminated": false,
"docker.containers.restarted": false,
}
for _, sm := range rm.ScopeMetrics {
for _, m := range sm.Metrics {
if _, ok := expectedMetrics[m.Name]; ok {
expectedMetrics[m.Name] = true
}
}
}
for name, found := range expectedMetrics {
if !found {
t.Errorf("expected metric %s not found", name)
}
}
}
func TestOTelInstrumentation_Disabled(t *testing.T) {
// Test with nil config
inst := newOTelInstrumentation(nil)
if inst.enabled {
t.Error("expected instrumentation to be disabled with nil config")
}
ctx := context.Background()
// These should all be no-ops and not panic
_, _ = inst.startSpan(ctx, "test")
inst.recordError(ctx, "test", errors.New("test error"))
inst.incrementCounter(ctx, "containers_started", 1)
inst.addSpanAttributes(ctx, attribute.String("test", "value"))
inst.setSpanStatus(ctx, 1, "test")
}