Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use opaque key to identify trace-provider #241

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions instrumentation/opentelemetry/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@

resources, err := resource.New(
context.Background(),
resource.WithAttributes(createResources(cfg.GetServiceName().GetValue(), cfg.ResourceAttributes,
resource.WithAttributes(createResources(resourceAttrsWithServiceName(cfg.ResourceAttributes, cfg.GetServiceName().GetValue()),
versionInfoAttrs)...),
)
if err != nil {
Expand Down Expand Up @@ -351,12 +351,12 @@
return func() {
mu.Lock()
defer mu.Unlock()
for serviceName, tracerProvider := range traceProviders {
for key, tracerProvider := range traceProviders {
err := tracerProvider.Shutdown(context.Background())
if err != nil {
log.Printf("error while shutting down tracer provider: %v\n", err)
}
delete(traceProviders, serviceName)
delete(traceProviders, key)
}
traceProviders = map[string]*sdktrace.TracerProvider{}
err := tp.Shutdown(context.Background())
Expand All @@ -371,10 +371,9 @@
}
}

func createResources(serviceName string, resources map[string]string,
func createResources(resources map[string]string,
versionInfo []attribute.KeyValue) []attribute.KeyValue {
retValues := []attribute.KeyValue{
semconv.ServiceNameKey.String(serviceName),
semconv.TelemetrySDKLanguageGo,
}

Expand All @@ -386,14 +385,14 @@
return retValues
}

// RegisterService creates tracerprovider for a new service and returns a func which can be used to create spans and the TracerProvider
func RegisterService(serviceName string, resourceAttributes map[string]string) (sdk.StartSpan, trace.TracerProvider, error) {
return RegisterServiceWithSpanProcessorWrapper(serviceName, resourceAttributes, nil, versionInfoAttributes)
// RegisterService creates tracerprovider for a new service (represented via a unique key) and returns a func which can be used to create spans and the TracerProvider
func RegisterService(key string, resourceAttributes map[string]string) (sdk.StartSpan, trace.TracerProvider, error) {
return RegisterServiceWithSpanProcessorWrapper(key, resourceAttributes, nil, versionInfoAttributes)
}

// RegisterServiceWithSpanProcessorWrapper creates a tracerprovider for a new service with a wrapper over opentelemetry span processor
// RegisterServiceWithSpanProcessorWrapper creates a tracerprovider for a new service (represented via a unique key) with a wrapper over opentelemetry span processor
// and returns a func which can be used to create spans and the TracerProvider
func RegisterServiceWithSpanProcessorWrapper(serviceName string, resourceAttributes map[string]string,
func RegisterServiceWithSpanProcessorWrapper(key string, resourceAttributes map[string]string,
wrapper SpanProcessorWrapper, versionInfoAttrs []attribute.KeyValue) (sdk.StartSpan, trace.TracerProvider, error) {
mu.Lock()
defer mu.Unlock()
Expand All @@ -405,8 +404,8 @@
return NoopStartSpan, noop.NewTracerProvider(), nil
}

if _, ok := traceProviders[serviceName]; ok {
return nil, noop.NewTracerProvider(), fmt.Errorf("service %v already initialized", serviceName)
if _, ok := traceProviders[key]; ok {
return nil, noop.NewTracerProvider(), fmt.Errorf("key %v is already used for initialization", key)

Check warning on line 408 in instrumentation/opentelemetry/init.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/opentelemetry/init.go#L408

Added line #L408 was not covered by tests
}

exporter, err := exporterFactory()
Expand All @@ -424,7 +423,7 @@

resources, err := resource.New(
context.Background(),
resource.WithAttributes(createResources(serviceName, resourceAttributes, versionInfoAttrs)...),
resource.WithAttributes(createResources(resourceAttributes, versionInfoAttrs)...),
)
if err != nil {
log.Fatal(err)
Expand All @@ -435,7 +434,7 @@
sdktrace.WithResource(resources),
)

traceProviders[serviceName] = tp
traceProviders[key] = tp
return startSpan(func() trace.TracerProvider {
return tp
}), tp, nil
Expand All @@ -453,7 +452,7 @@
}
periodicReader := metric.NewPeriodicReader(metricsExporter)

resourceKvps := createResources(cfg.GetServiceName().GetValue(), cfg.ResourceAttributes, versionInfoAttrs)
resourceKvps := createResources(resourceAttrsWithServiceName(cfg.ResourceAttributes, cfg.GetServiceName().GetValue()), versionInfoAttrs)
resourceKvps = append(resourceKvps, identifier.ServiceInstanceKeyValue)
metricResources, err := resource.New(context.Background(), resource.WithAttributes(resourceKvps...))
if err != nil {
Expand Down Expand Up @@ -492,6 +491,17 @@
(cfg.GetTelemetry() != nil && cfg.GetTelemetry().GetMetricsEnabled().GetValue()) // metrics enabled
}

func resourceAttrsWithServiceName(resourceMap map[string]string, serviceName string) map[string]string {
thugrock7 marked this conversation as resolved.
Show resolved Hide resolved
if resourceMap == nil {
resourceMap = make(map[string]string)
}
if _, ok := resourceMap["service.name"]; !ok && (len(serviceName) > 0) {
thugrock7 marked this conversation as resolved.
Show resolved Hide resolved
resourceMap["service.name"] = serviceName
}

return resourceMap
}

// SpanProcessorWrapper wraps otel span processor
// and is responsible to delegate calls to the wrapped processor
type SpanProcessorWrapper interface {
Expand Down
2 changes: 1 addition & 1 deletion instrumentation/opentelemetry/init_additional.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func InitAsAdditional(cfg *config.AgentConfig) (trace.SpanProcessor, func()) {
if cfg.GetServiceName().GetValue() != "" {
resource, err := resource.New(
context.Background(),
resource.WithAttributes(createResources(cfg.GetServiceName().GetValue(), cfg.ResourceAttributes, versionInfoAttributes)...),
resource.WithAttributes(createResources(resourceAttrsWithServiceName(cfg.ResourceAttributes, cfg.GetServiceName().GetValue()), versionInfoAttributes)...),
)
if err != nil {
log.Fatal(err)
Expand Down
Loading