Skip to content

Commit

Permalink
Fix opentracing.Bridge where it miss identifying the spanKind (#3096)
Browse files Browse the repository at this point in the history
* Fix opentracing.Bridge where it was not identifying the spanKinf correctly

* fix test

* changelog

* Keeping backward comppatibillity

* Update CHANGELOG.md

Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>

* Update CHANGELOG.md

Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>
Co-authored-by: Chester Cheung <cheung.zhy.csu@gmail.com>
  • Loading branch information
3 people committed Aug 18, 2022
1 parent d96e8d2 commit b9adb17
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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
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)
})
}
}

0 comments on commit b9adb17

Please sign in to comment.