Skip to content

Commit c94ddc1

Browse files
authored
fix: panic on auto propagator (#3920)
fix: panic on auto propagator panic: duplicate registration: "skipper-debug" [recovered, repanicked] test: add test coverage to otel package Signed-off-by: Sandor Szuecs <[email protected]>
1 parent dabeafb commit c94ddc1

2 files changed

Lines changed: 139 additions & 9 deletions

File tree

otel/otel.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ func newSpanExporter(ctx context.Context, o *Options) (trace.SpanExporter, error
181181
return skipperDebugSpanExporter(ctx)
182182
} else {
183183
log.Debugf("Configuring span exporter using environment variables")
184-
autoexport.RegisterSpanExporter("skipper-debug", skipperDebugSpanExporter)
185184
return autoexport.NewSpanExporter(ctx)
186185
}
187186
}

otel/otel_test.go

Lines changed: 139 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,149 @@ package otel
22

33
import (
44
"context"
5+
"os"
56
"testing"
67
)
78

89
func TestOtel(t *testing.T) {
9-
shutdown, err := Init(context.Background(), &Options{})
10-
if err != nil {
11-
t.Fatalf("Failed to init OTel: %v", err)
12-
}
13-
14-
err = shutdown(context.Background())
15-
if err != nil {
16-
t.Fatalf("Failed to shutdown OTel: %v", err)
10+
for _, tt := range []struct {
11+
name string
12+
opt *Options
13+
env map[string]string
14+
errString string
15+
}{
16+
{
17+
name: "test otel default",
18+
opt: &Options{},
19+
},
20+
{
21+
name: "test otel initialized",
22+
opt: &Options{Initialized: true},
23+
},
24+
{
25+
name: "test otel exporter otlp with endpoint",
26+
opt: &Options{
27+
TracesExporter: "otlp",
28+
ExporterOtlp: ExporterOtlp{
29+
Endpoint: "http://otlp-exporter.example",
30+
},
31+
},
32+
},
33+
{
34+
name: "test otel exporter otlp with endpoint and protcol grpc",
35+
opt: &Options{
36+
TracesExporter: "otlp",
37+
ExporterOtlp: ExporterOtlp{
38+
Endpoint: "http://otlp-exporter.example",
39+
Protocol: "grpc",
40+
},
41+
},
42+
},
43+
{
44+
name: "test otel exporter otlp with endpoint and protcol http/protobuf",
45+
opt: &Options{
46+
TracesExporter: "otlp",
47+
ExporterOtlp: ExporterOtlp{
48+
Endpoint: "http://otlp-exporter.example",
49+
Protocol: "http/protobuf",
50+
},
51+
},
52+
},
53+
{
54+
name: "test otel exporter otlp with endpoint and protcol unknown",
55+
opt: &Options{
56+
TracesExporter: "otlp",
57+
ExporterOtlp: ExporterOtlp{
58+
Endpoint: "http://otlp-exporter.example",
59+
Protocol: "unknown",
60+
},
61+
},
62+
errString: "invalid OTLP protocol unknown - should be one of ['grpc', 'http/protobuf']",
63+
},
64+
{
65+
name: "test otel exporter otlp without endpoint",
66+
opt: &Options{TracesExporter: "otlp"},
67+
errString: "OTLP endpoint is required",
68+
},
69+
{
70+
name: "test otel exporter console",
71+
opt: &Options{TracesExporter: "console"},
72+
},
73+
{
74+
name: "test otel exporter skipper-debug",
75+
opt: &Options{TracesExporter: "skipper-debug"},
76+
},
77+
{
78+
name: "test otel exporter auto",
79+
opt: &Options{TracesExporter: "auto"},
80+
env: map[string]string{
81+
"OTEL_TRACES_EXPORTER": "console",
82+
},
83+
},
84+
{
85+
name: "test otel exporter auto with batchers",
86+
opt: &Options{
87+
TracesExporter: "auto",
88+
BatchSpanProcessor: BatchSpanProcessor{
89+
ScheduleDelay: 1,
90+
ExportTimeout: 1,
91+
MaxQueueSize: 1,
92+
MaxExportBatchSize: 1,
93+
},
94+
},
95+
env: map[string]string{
96+
"OTEL_TRACES_EXPORTER": "console",
97+
},
98+
},
99+
{
100+
name: "test otel exporter console with resources",
101+
opt: &Options{
102+
TracesExporter: "console",
103+
ResourceAttributes: map[string]string{
104+
"foo": "bar",
105+
},
106+
},
107+
},
108+
{
109+
name: "test otel exporter console with propagator none",
110+
opt: &Options{
111+
TracesExporter: "console",
112+
Propagators: []string{"none"},
113+
},
114+
},
115+
{
116+
name: "test otel exporter console with propagator baggage",
117+
opt: &Options{
118+
TracesExporter: "console",
119+
Propagators: []string{"baggage"},
120+
},
121+
}} {
122+
t.Run(tt.name, func(t *testing.T) {
123+
for k, v := range tt.env {
124+
if err := os.Setenv(k, v); err != nil {
125+
t.Fatalf("Failed to set env: %q -> %q: %v", k, v, err)
126+
}
127+
}
128+
shutdown, err := Init(context.Background(), tt.opt)
129+
if err != nil {
130+
if tt.errString == "" {
131+
t.Fatalf("Failed to init OTel want no error, got: %v", err)
132+
} else {
133+
if tt.errString != err.Error() {
134+
t.Fatalf("Failed to get error want: %q, got %q", tt.errString, err.Error())
135+
}
136+
return
137+
}
138+
} else {
139+
if tt.errString != "" {
140+
t.Fatalf("Failed to get wanted error: %q", tt.errString)
141+
}
142+
}
143+
err = shutdown(context.Background())
144+
if err != nil {
145+
t.Fatalf("Failed to shutdown OTel: %v", err)
146+
}
147+
})
17148
}
18149

19150
}

0 commit comments

Comments
 (0)