-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathinitializers_test.go
More file actions
375 lines (326 loc) · 10.2 KB
/
initializers_test.go
File metadata and controls
375 lines (326 loc) · 10.2 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package core
import (
"context"
"testing"
"time"
"github.com/go-coldbrew/core/config"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
experimental "google.golang.org/grpc/experimental/opentelemetry"
grpcotel "google.golang.org/grpc/stats/opentelemetry"
)
func TestBuildOTELResource(t *testing.T) {
oldRes := otelResource
otelResource = nil // force rebuild
defer func() { otelResource = oldRes }()
r, err := buildOTELResource("test-service", "v1.0.0")
if err != nil {
t.Fatalf("buildOTELResource() error: %v", err)
}
if r == nil {
t.Fatal("buildOTELResource() returned nil resource")
}
// Verify shared var is set
if otelResource == nil {
t.Fatal("otelResource should be set after buildOTELResource()")
}
// Verify service.name attribute exists
found := false
for _, attr := range r.Attributes() {
if string(attr.Key) == "service.name" && attr.Value.AsString() == "test-service" {
found = true
break
}
}
if !found {
t.Error("resource should contain service.name=test-service")
}
}
func TestSetupOpenTelemetry_StoresTracerProvider(t *testing.T) {
// Save and restore all globals mutated by SetupOpenTelemetry.
oldTP := otelTracerProvider
oldRes := otelResource
oldGlobalTP := otel.GetTracerProvider()
oldGlobalProp := otel.GetTextMapPropagator()
defer func() {
otelTracerProvider = oldTP
otelResource = oldRes
otel.SetTracerProvider(oldGlobalTP)
otel.SetTextMapPropagator(oldGlobalProp)
}()
otelTracerProvider = nil
otelResource = nil
// Use a real OTLP endpoint that will fail to connect but won't error during setup
// (the batcher exporter is async, so the exporter setup succeeds even with invalid endpoints)
err := SetupOpenTelemetry(OTLPConfig{
ServiceName: "test-service",
Endpoint: "localhost:4317",
Insecure: true,
})
if err != nil {
t.Fatalf("SetupOpenTelemetry() error: %v", err)
}
if otelTracerProvider == nil {
t.Fatal("otelTracerProvider should be set after SetupOpenTelemetry()")
}
if otelResource == nil {
t.Fatal("otelResource should be set after SetupOpenTelemetry()")
}
// Clean up
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
_ = otelTracerProvider.Shutdown(ctx)
}
func TestSetupOTELMetrics(t *testing.T) {
// Reset globals
oldRes := otelResource
defer func() { otelResource = oldRes }()
// Build a resource first
_, err := buildOTELResource("test-metrics", "v1.0.0")
if err != nil {
t.Fatalf("buildOTELResource() error: %v", err)
}
// Save and restore global MeterProvider.
oldGlobalMP := otel.GetMeterProvider()
defer otel.SetMeterProvider(oldGlobalMP)
mp, err := SetupOTELMetrics(OTLPConfig{
Endpoint: "localhost:4317",
Insecure: true,
}, 60*time.Second)
if err != nil {
t.Fatalf("SetupOTELMetrics() error: %v", err)
}
if mp == nil {
t.Fatal("SetupOTELMetrics() returned nil MeterProvider")
}
// Verify it's a concrete *sdkmetric.MeterProvider
var _ *sdkmetric.MeterProvider = mp
// Shutdown may fail because no OTLP collector is running — that's expected.
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
_ = mp.Shutdown(ctx)
}
func TestSetupOTELMetrics_NoEndpoint(t *testing.T) {
// Ensure resource exists so test validates endpoint check, not resource fallback.
oldRes := otelResource
defer func() { otelResource = oldRes }()
if _, err := buildOTELResource("test-no-endpoint", "v1.0.0"); err != nil {
t.Fatalf("buildOTELResource() error: %v", err)
}
_, err := SetupOTELMetrics(OTLPConfig{}, 60*time.Second)
if err == nil {
t.Fatal("SetupOTELMetrics with empty endpoint should error")
}
}
func TestSetupOTELMetrics_SharedResource(t *testing.T) {
// Save and restore globals
oldRes := otelResource
oldGlobalMP := otel.GetMeterProvider()
defer func() {
otelResource = oldRes
otel.SetMeterProvider(oldGlobalMP)
}()
// Set up tracing first to populate shared resource
otelResource = nil
_, err := buildOTELResource("shared-svc", "v2.0.0")
if err != nil {
t.Fatalf("buildOTELResource() error: %v", err)
}
mp, err := SetupOTELMetrics(OTLPConfig{
Endpoint: "localhost:4317",
Insecure: true,
}, 60*time.Second)
if err != nil {
t.Fatalf("SetupOTELMetrics() error: %v", err)
}
defer func() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
mp.Shutdown(ctx)
}()
// The MeterProvider should have been created with the shared resource.
// We can't inspect the resource directly, but we verify the MeterProvider is non-nil
// and otelResource was used (not rebuilt).
found := false
for _, attr := range otelResource.Attributes() {
if string(attr.Key) == "service.name" && attr.Value.AsString() == "shared-svc" {
found = true
break
}
}
if !found {
t.Error("shared resource should contain service.name=shared-svc")
}
}
func TestBuildOTELOptions_MethodAttributeFilter(t *testing.T) {
// Save and restore global OTel state.
oldTP := otel.GetTracerProvider()
oldProp := otel.GetTextMapPropagator()
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.NeverSample()))
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(
propagation.TraceContext{},
propagation.Baggage{},
))
defer func() {
tp.Shutdown(context.Background())
otel.SetTracerProvider(oldTP)
otel.SetTextMapPropagator(oldProp)
}()
oldMP := otelMeterProvider
otelMeterProvider = nil
defer func() { otelMeterProvider = oldMP }()
opts := buildOTELOptions()
// MethodAttributeFilter should filter health/ready/reflection
filter := opts.MetricsOptions.MethodAttributeFilter
if filter == nil {
t.Fatal("MethodAttributeFilter should be set")
}
// Filtered methods — ColdBrew's default filter matches substrings
// "healthcheck", "readycheck", "serverreflectioninfo" (case-insensitive).
for _, method := range []string{
"/myservice.v1.MyService/HealthCheck",
"/myservice.v1.MyService/ReadyCheck",
"/grpc.reflection.v1alpha.ServerReflection/ServerReflectionInfo",
} {
if filter(method) {
t.Errorf("MethodAttributeFilter(%q) = true, want false", method)
}
}
// Normal methods (should return true — not filtered)
for _, method := range []string{
"/mypackage.MyService/MyMethod",
"/users.v1.UserService/GetUser",
"/grpc.health.v1.Health/Check", // standard gRPC health check is NOT filtered (no contiguous "healthcheck" substring)
} {
if !filter(method) {
t.Errorf("MethodAttributeFilter(%q) = false, want true", method)
}
}
// TraceOptions should have provider and propagator
if opts.TraceOptions.TracerProvider == nil {
t.Error("TraceOptions.TracerProvider should be set")
}
if opts.TraceOptions.TextMapPropagator == nil {
t.Error("TraceOptions.TextMapPropagator should be set")
}
}
func TestBuildOTELOptions_WithMeterProvider(t *testing.T) {
oldTP := otel.GetTracerProvider()
oldProp := otel.GetTextMapPropagator()
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.NeverSample()))
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.TraceContext{})
defer func() {
tp.Shutdown(context.Background())
otel.SetTracerProvider(oldTP)
otel.SetTextMapPropagator(oldProp)
}()
// Set a MeterProvider
oldMP := otelMeterProvider
otelMeterProvider = sdkmetric.NewMeterProvider()
defer func() {
otelMeterProvider.Shutdown(context.Background())
otelMeterProvider = oldMP
}()
opts := buildOTELOptions()
if opts.MetricsOptions.MeterProvider == nil {
t.Error("MetricsOptions.MeterProvider should be set when otelMeterProvider is non-nil")
}
}
func TestProcessConfig_NativeOTEL(t *testing.T) {
// Save and restore all globals mutated by processConfig.
oldSet := otelGRPCOptionsSet
oldOpts := otelGRPCOptions
oldTP := otelTracerProvider
oldRes := otelResource
oldUseLegacy := otelUseLegacy
oldMP := otelMeterProvider
oldGlobalTP := otel.GetTracerProvider()
oldGlobalProp := otel.GetTextMapPropagator()
defer func() {
otelGRPCOptionsSet = oldSet
otelGRPCOptions = oldOpts
otelTracerProvider = oldTP
otelResource = oldRes
otelUseLegacy = oldUseLegacy
otelMeterProvider = oldMP
otel.SetTracerProvider(oldGlobalTP)
otel.SetTextMapPropagator(oldGlobalProp)
}()
otelGRPCOptionsSet = false
c := &cb{
config: config.Config{
DisableSignalHandler: true,
DisableAutoMaxProcs: true,
OTLPEndpoint: "localhost:4317",
OTLPInsecure: true,
AppName: "test-native",
},
}
c.processConfig()
if !otelGRPCOptionsSet {
t.Error("otelGRPCOptionsSet should be true after processConfig with default config")
}
if otelMeterProvider != nil {
t.Error("otelMeterProvider should be nil when EnableOTELMetrics is false")
}
// Clean up TracerProvider
if otelTracerProvider != nil {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
otelTracerProvider.Shutdown(ctx)
}
}
func TestProcessConfig_LegacyFallback(t *testing.T) {
oldSet := otelGRPCOptionsSet
oldUseLegacy := otelUseLegacy
defer func() {
otelGRPCOptionsSet = oldSet
otelUseLegacy = oldUseLegacy
}()
otelGRPCOptionsSet = false
c := &cb{
config: config.Config{
DisableSignalHandler: true,
DisableAutoMaxProcs: true,
OTELUseLegacyInstrumentation: true,
},
}
c.processConfig()
if otelGRPCOptionsSet {
t.Error("otelGRPCOptionsSet should be false when OTELUseLegacyInstrumentation is true")
}
}
func TestProcessConfig_UserSetOTELOptions(t *testing.T) {
oldSet := otelGRPCOptionsSet
oldOpts := otelGRPCOptions
oldUseLegacy := otelUseLegacy
defer func() {
otelGRPCOptionsSet = oldSet
otelGRPCOptions = oldOpts
otelUseLegacy = oldUseLegacy
}()
// Simulate user calling SetOTELOptions during init
tp := sdktrace.NewTracerProvider()
defer tp.Shutdown(context.Background())
customOpts := grpcotel.Options{
TraceOptions: experimental.TraceOptions{
TracerProvider: tp,
},
}
SetOTELOptions(customOpts)
c := &cb{
config: config.Config{
DisableSignalHandler: true,
DisableAutoMaxProcs: true,
},
}
c.processConfig()
// Verify user options were not overwritten
if otelGRPCOptions.TraceOptions.TracerProvider != tp {
t.Error("processConfig should not overwrite user-provided OTELOptions")
}
}