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

Fix opentracing.Bridge where it miss identifying the spanKind #3096

Merged
merged 6 commits into from Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -13,6 +13,8 @@ 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)
hanyuancheung marked this conversation as resolved.
Show resolved Hide resolved
## [1.9.0/0.0.3] - 2022-08-01

### Added
Expand Down
22 changes: 12 additions & 10 deletions bridge/opentracing/bridge.go
Expand Up @@ -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 {
Expand Down
43 changes: 43 additions & 0 deletions bridge/opentracing/bridge_test.go
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
})
}
}