Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0b61fd7

Browse files
committedOct 21, 2024·
Register funcs to support environment
1 parent e8225ec commit 0b61fd7

File tree

2 files changed

+51
-23
lines changed

2 files changed

+51
-23
lines changed
 

‎instrumentation/opentelemetry/init.go

+25-16
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import (
4343

4444
var (
4545
batchTimeout = time.Duration(200) * time.Millisecond
46-
traceProviders map[string]*sdktrace.TracerProvider
46+
traceProviders map[string]map[string]*sdktrace.TracerProvider
4747
globalSampler sdktrace.Sampler
4848
initialized = false
4949
enabled = false
@@ -306,7 +306,7 @@ func InitWithSpanProcessorWrapper(cfg *config.AgentConfig, wrapper SpanProcessor
306306

307307
otel.SetTextMapPropagator(makePropagator(cfg.PropagationFormats))
308308

309-
traceProviders = make(map[string]*sdktrace.TracerProvider)
309+
traceProviders = make(map[string]map[string]*sdktrace.TracerProvider)
310310
globalSampler = sampler
311311
initialized = true
312312

@@ -324,14 +324,17 @@ func InitWithSpanProcessorWrapper(cfg *config.AgentConfig, wrapper SpanProcessor
324324
return func() {
325325
mu.Lock()
326326
defer mu.Unlock()
327-
for serviceName, tracerProvider := range traceProviders {
328-
err := tracerProvider.Shutdown(context.Background())
329-
if err != nil {
330-
log.Printf("error while shutting down tracer provider: %v\n", err)
327+
for environment, serviceTraceProviders := range traceProviders {
328+
for _, tracerProvider := range serviceTraceProviders {
329+
err := tracerProvider.Shutdown(context.Background())
330+
if err != nil {
331+
log.Printf("error while shutting down tracer provider: %v\n", err)
332+
}
331333
}
332-
delete(traceProviders, serviceName)
334+
delete(traceProviders, environment)
333335
}
334-
traceProviders = map[string]*sdktrace.TracerProvider{}
336+
337+
traceProviders = map[string]map[string]*sdktrace.TracerProvider{}
335338
err := tp.Shutdown(context.Background())
336339
if err != nil {
337340
log.Printf("error while shutting down default tracer provider: %v\n", err)
@@ -359,14 +362,14 @@ func createResources(serviceName string, resources map[string]string,
359362
return retValues
360363
}
361364

362-
// RegisterService creates tracerprovider for a new service and returns a func which can be used to create spans and the TracerProvider
363-
func RegisterService(serviceName string, resourceAttributes map[string]string) (sdk.StartSpan, trace.TracerProvider, error) {
364-
return RegisterServiceWithSpanProcessorWrapper(serviceName, resourceAttributes, nil, versionInfoAttributes)
365+
// RegisterService creates tracerprovider for a new service in given environment and returns a func which can be used to create spans and the TracerProvider
366+
func RegisterService(environment string, serviceName string, resourceAttributes map[string]string) (sdk.StartSpan, trace.TracerProvider, error) {
367+
return RegisterServiceWithSpanProcessorWrapper(environment, serviceName, resourceAttributes, nil, versionInfoAttributes)
365368
}
366369

367-
// RegisterServiceWithSpanProcessorWrapper creates a tracerprovider for a new service with a wrapper over opentelemetry span processor
370+
// RegisterServiceWithSpanProcessorWrapper creates a tracerprovider for a new service in given environment with a wrapper over opentelemetry span processor
368371
// and returns a func which can be used to create spans and the TracerProvider
369-
func RegisterServiceWithSpanProcessorWrapper(serviceName string, resourceAttributes map[string]string,
372+
func RegisterServiceWithSpanProcessorWrapper(environment string, serviceName string, resourceAttributes map[string]string,
370373
wrapper SpanProcessorWrapper, versionInfoAttrs []attribute.KeyValue) (sdk.StartSpan, trace.TracerProvider, error) {
371374
mu.Lock()
372375
defer mu.Unlock()
@@ -378,8 +381,10 @@ func RegisterServiceWithSpanProcessorWrapper(serviceName string, resourceAttribu
378381
return NoopStartSpan, noop.NewTracerProvider(), nil
379382
}
380383

381-
if _, ok := traceProviders[serviceName]; ok {
382-
return nil, noop.NewTracerProvider(), fmt.Errorf("service %v already initialized", serviceName)
384+
if stp, ok := traceProviders[environment]; ok {
385+
if _, present := stp[serviceName]; present {
386+
return nil, noop.NewTracerProvider(), fmt.Errorf("environment %v service %v already initialized", environment, serviceName)
387+
}
383388
}
384389

385390
exporter, err := exporterFactory()
@@ -408,7 +413,11 @@ func RegisterServiceWithSpanProcessorWrapper(serviceName string, resourceAttribu
408413
sdktrace.WithResource(resources),
409414
)
410415

411-
traceProviders[serviceName] = tp
416+
if _, ok := traceProviders[environment]; !ok {
417+
traceProviders[environment] = make(map[string]*sdktrace.TracerProvider)
418+
}
419+
420+
traceProviders[environment][serviceName] = tp
412421
return startSpan(func() trace.TracerProvider {
413422
return tp
414423
}), tp, nil

‎instrumentation/opentelemetry/init_test.go

+26-7
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,38 @@ func ExampleRegisterService() {
3838
shutdown := Init(cfg)
3939
defer shutdown()
4040

41-
_, _, err := RegisterService("custom_service", map[string]string{"test1": "val1"})
41+
_, _, err := RegisterService("custom_env", "custom_service", map[string]string{"test1": "val1"})
4242
if err != nil {
4343
log.Fatalf("Error while initializing service: %v", err)
4444
}
4545
}
4646

47+
func TestMultiRegisterService(t *testing.T) {
48+
cfg := config.Load()
49+
cfg.ServiceName = config.String("my_example_svc")
50+
cfg.DataCapture.HttpHeaders.Request = config.Bool(true)
51+
cfg.Reporting.TraceReporterType = config.TraceReporterType_LOGGING
52+
53+
shutdown := Init(cfg)
54+
defer shutdown()
55+
56+
_, _, err := RegisterService("custom_env", "custom_service", map[string]string{"test1": "val1"})
57+
assert.Nil(t, err)
58+
59+
_, _, err = RegisterService("custom_env", "custom_service_2", map[string]string{"test2": "val2"})
60+
assert.Nil(t, err)
61+
62+
_, _, err = RegisterService("custom_env_1", "custom_service", map[string]string{"test3": "val3"})
63+
assert.Nil(t, err)
64+
}
65+
4766
func TestInitDisabledAgent(t *testing.T) {
4867
cfg := config.Load()
4968
cfg.Enabled = config.Bool(false)
5069
shutdown := Init(cfg)
5170
defer shutdown()
5271

53-
startSpan, tp, err := RegisterService("test_service", nil)
72+
startSpan, tp, err := RegisterService("custom_env", "test_service", nil)
5473
require.NoError(t, err)
5574
assert.Equal(t, noop.NewTracerProvider(), tp)
5675
_, s, _ := startSpan(context.Background(), "test_span", nil)
@@ -80,7 +99,7 @@ func TestOtlpService(t *testing.T) {
8099
shutdown := Init(cfg)
81100
defer shutdown()
82101

83-
startSpan, tp, err := RegisterService("custom_service", map[string]string{"test1": "val1"})
102+
startSpan, tp, err := RegisterService("custom_env", "custom_service", map[string]string{"test1": "val1"})
84103
_, s, _ := startSpan(context.Background(), "test_span", nil)
85104
assert.False(t, s.IsNoop())
86105
assert.NotEqual(t, noop.NewTracerProvider(), tp)
@@ -102,7 +121,7 @@ func TestGrpcLoadBalancingConfig(t *testing.T) {
102121
defer shutdown()
103122

104123
assert.Equal(t, resolver.GetDefaultScheme(), "dns")
105-
_, tp, err := RegisterService("custom_service", map[string]string{"test1": "val1"})
124+
_, tp, err := RegisterService("custom_env", "custom_service", map[string]string{"test1": "val1"})
106125
assert.NotEqual(t, noop.NewTracerProvider(), tp)
107126
if err != nil {
108127
log.Fatalf("Error while initializing service: %v", err)
@@ -165,7 +184,7 @@ func TestMultipleTraceProviders(t *testing.T) {
165184
_, _, spanEnder := StartSpan(context.Background(), "example_span", nil)
166185
spanEnder()
167186

168-
startServiceSpan, tp, err := RegisterService("custom_service", map[string]string{"test1": "val1"})
187+
startServiceSpan, tp, err := RegisterService("custom_env", "custom_service", map[string]string{"test1": "val1"})
169188
assert.NoError(t, err)
170189
assert.NotNil(t, startServiceSpan)
171190
assert.True(t, initialized)
@@ -208,7 +227,7 @@ func TestMultipleTraceProvidersCallAfterShutdown(t *testing.T) {
208227
assert.True(t, initialized)
209228
assert.Equal(t, 0, len(traceProviders))
210229

211-
startServiceSpan, _, err := RegisterService("custom_service", map[string]string{"test1": "val1"})
230+
startServiceSpan, _, err := RegisterService("custom_env", "custom_service", map[string]string{"test1": "val1"})
212231
assert.NoError(t, err)
213232
assert.NotNil(t, startServiceSpan)
214233
assert.True(t, initialized)
@@ -379,7 +398,7 @@ func TestInitWithSpanProcessorWrapper(t *testing.T) {
379398
assert.Equal(t, 2, wrapper.onEndCount)
380399

381400
// test wrapper is called for spans created by service trace provider
382-
startSpan, _, err := RegisterServiceWithSpanProcessorWrapper("custom_service", map[string]string{"test1": "val1"}, wrapper,
401+
startSpan, _, err := RegisterServiceWithSpanProcessorWrapper("custom_env", "custom_service", map[string]string{"test1": "val1"}, wrapper,
383402
versionInfoAttributes)
384403
if err != nil {
385404
log.Fatalf("Error while initializing service: %v", err)

0 commit comments

Comments
 (0)
Please sign in to comment.