diff --git a/CHANGELOG.md b/CHANGELOG.md index a28332f3fbc..3dd5926bfda 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Support Go 1.19. Include compatibility testing and document support. (#3077) +### Fixed + +- Fix misidentification of OpenTelemetry `SpanKind` in OpenTracing bridge (`go.opentelemetry.io/otel/bridge/opentracing`). (#3096) + ## [1.9.0/0.0.3] - 2022-08-01 ### Added diff --git a/bridge/opentracing/bridge.go b/bridge/opentracing/bridge.go index 2922b9869d5..967aaa20d4b 100644 --- a/bridge/opentracing/bridge.go +++ b/bridge/opentracing/bridge.go @@ -502,17 +502,19 @@ func otTagsToOTelAttributesKindAndError(tags map[string]interface{}) ([]attribut for k, v := range tags { switch k { case string(otext.SpanKind): + sk := v if s, ok := v.(string); ok { - switch strings.ToLower(s) { - case "client": - kind = trace.SpanKindClient - case "server": - kind = trace.SpanKindServer - case "producer": - kind = trace.SpanKindProducer - case "consumer": - kind = trace.SpanKindConsumer - } + sk = otext.SpanKindEnum(strings.ToLower(s)) + } + switch sk { + case otext.SpanKindRPCClientEnum: + kind = trace.SpanKindClient + case otext.SpanKindRPCServerEnum: + kind = trace.SpanKindServer + case otext.SpanKindProducerEnum: + kind = trace.SpanKindProducer + case otext.SpanKindConsumerEnum: + kind = trace.SpanKindConsumer } case string(otext.Error): if b, ok := v.(bool); ok && b { diff --git a/bridge/opentracing/bridge_test.go b/bridge/opentracing/bridge_test.go index a64d6f73919..7286bd77823 100644 --- a/bridge/opentracing/bridge_test.go +++ b/bridge/opentracing/bridge_test.go @@ -22,9 +22,11 @@ import ( "testing" ot "github.com/opentracing/opentracing-go" + "github.com/opentracing/opentracing-go/ext" "github.com/stretchr/testify/assert" "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/bridge/opentracing/internal" "go.opentelemetry.io/otel/propagation" "go.opentelemetry.io/otel/trace" ) @@ -425,3 +427,44 @@ func TestBridgeTracer_StartSpan(t *testing.T) { }) } } + +func Test_otTagsToOTelAttributesKindAndError(t *testing.T) { + tracer := internal.NewMockTracer() + sc := &bridgeSpanContext{} + + testCases := []struct { + name string + opt []ot.StartSpanOption + expected trace.SpanKind + }{ + { + name: "client", + opt: []ot.StartSpanOption{ext.SpanKindRPCClient}, + expected: trace.SpanKindClient, + }, + { + name: "server", + opt: []ot.StartSpanOption{ext.RPCServerOption(sc)}, + expected: trace.SpanKindServer, + }, + { + name: "client string", + opt: []ot.StartSpanOption{ot.Tag{Key: "span.kind", Value: "client"}}, + expected: trace.SpanKindClient, + }, + { + name: "server string", + opt: []ot.StartSpanOption{ot.Tag{Key: "span.kind", Value: "server"}}, + expected: trace.SpanKindServer, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + b, _ := NewTracerPair(tracer) + + s := b.StartSpan(tc.name, tc.opt...) + assert.Equal(t, s.(*bridgeSpan).otelSpan.(*internal.MockSpan).SpanKind, tc.expected) + }) + } +}