From 3cc4857fd89420d3043a1f6643942420f670e11a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Thu, 31 Oct 2024 10:20:09 +0100 Subject: [PATCH] opentracing: Make schemaURL and scope attributes as identifying for Tracer (#5931) Fixes https://github.com/open-telemetry/opentelemetry-go/issues/5928 --- CHANGELOG.md | 1 + bridge/opentracing/provider.go | 5 ++++ bridge/opentracing/provider_test.go | 42 +++++++++++++++++++++-------- 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eab1919f403..95238cb0b55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Support scope attributes and make them as identifying for `Tracer` in `go.opentelemetry.io/otel` and `go.opentelemetry.io/otel/sdk/trace`. (#5924) - Support scope attributes and make them as identifying for `Meter` in `go.opentelemetry.io/otel` and `go.opentelemetry.io/otel/sdk/metric`. (#5926) - Support scope attributes and make them as identifying for `Logger` in `go.opentelemetry.io/otel` and `go.opentelemetry.io/otel/sdk/log`. (#5925) +- Make schema URL and scope attributes as identifying for `Tracer` in `go.opentelemetry.io/otel/bridge/opentracing`. (#5931) diff --git a/bridge/opentracing/provider.go b/bridge/opentracing/provider.go index b069753f7e9..3f093e75fcf 100644 --- a/bridge/opentracing/provider.go +++ b/bridge/opentracing/provider.go @@ -6,6 +6,7 @@ package opentracing // import "go.opentelemetry.io/otel/bridge/opentracing" import ( "sync" + "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/trace" "go.opentelemetry.io/otel/trace/embedded" ) @@ -38,6 +39,8 @@ func NewTracerProvider(bridge *BridgeTracer, provider trace.TracerProvider) *Tra type wrappedTracerKey struct { name string version string + schema string + attrs attribute.Set } // Tracer creates a WrappedTracer that wraps the OpenTelemetry tracer for each call to @@ -51,6 +54,8 @@ func (p *TracerProvider) Tracer(name string, opts ...trace.TracerOption) trace.T key := wrappedTracerKey{ name: name, version: c.InstrumentationVersion(), + schema: c.SchemaURL(), + attrs: c.InstrumentationAttributes(), } if t, ok := p.tracers[key]; ok { diff --git a/bridge/opentracing/provider_test.go b/bridge/opentracing/provider_test.go index 0560e0230b5..7836624925d 100644 --- a/bridge/opentracing/provider_test.go +++ b/bridge/opentracing/provider_test.go @@ -6,6 +6,7 @@ package opentracing import ( "testing" + "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/bridge/opentracing/internal" "go.opentelemetry.io/otel/trace" "go.opentelemetry.io/otel/trace/embedded" @@ -58,18 +59,37 @@ func TestTracerProvider(t *testing.T) { }) t.Run("Repeated requests to create a tracer should provide the existing tracer", func(t *testing.T) { - tracer1 := provider.Tracer(foobar) - assertMockTracerName(t, tracer1, foobar) - tracer2 := provider.Tracer(foobar) - assertMockTracerName(t, tracer2, foobar) - tracer3 := provider.Tracer(bazbar) - assertMockTracerName(t, tracer3, bazbar) - - if tracer1 != tracer2 { - t.Errorf("expected the same tracer, got different tracers") + tracerFns := []func() trace.Tracer{ + func() trace.Tracer { + return provider.Tracer(foobar) + }, + func() trace.Tracer { + return provider.Tracer(bazbar) + }, + func() trace.Tracer { + return provider.Tracer(foobar, trace.WithSchemaURL("https://opentelemetry.io/schemas/1.2.0")) + }, + func() trace.Tracer { + return provider.Tracer(foobar, trace.WithInstrumentationAttributes(attribute.String("foo", "bar"))) + }, + func() trace.Tracer { + return provider.Tracer(foobar, trace.WithSchemaURL("https://opentelemetry.io/schemas/1.2.0"), trace.WithInstrumentationAttributes(attribute.String("foo", "bar"))) + }, } - if tracer1 == tracer3 || tracer2 == tracer3 { - t.Errorf("expected different tracers, got the same tracer") + + for i, fn1 := range tracerFns { + for j, fn2 := range tracerFns { + tracer1, tracer2 := fn1(), fn2() + if i == j { + if tracer1 != tracer2 { + t.Errorf("expected the same tracer, got different tracers; i=%d j=%d", i, j) + } + } else { + if tracer1 == tracer2 { + t.Errorf("expected different tracers, got the same tracer; i=%d j=%d", i, j) + } + } + } } }) }