Skip to content

Commit 98eca45

Browse files
authored
Merge pull request #145 from openzipkin-contrib/tests_fix_span_kind
tests: adds span kind test.
2 parents 1c852e0 + d067c67 commit 98eca45

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

tracer.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,15 @@ func parseTagsAsZipkinOptions(t map[string]interface{}) []zipkin.SpanOption {
9292
default:
9393
kind = fmt.Sprintf("%v", kindVal)
9494
}
95-
zopts = append(zopts, zipkin.Kind(model.Kind(strings.ToUpper(kind))))
95+
mKind := model.Kind(strings.ToUpper(kind))
96+
if mKind == model.Client ||
97+
mKind == model.Server ||
98+
mKind == model.Producer ||
99+
mKind == model.Consumer {
100+
zopts = append(zopts, zipkin.Kind(mKind))
101+
} else {
102+
tags["span.kind"] = kind
103+
}
96104
}
97105

98106
if val, ok := t[string(ext.PeerService)]; ok {

tracer_test.go

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package zipkintracer
33
import (
44
"testing"
55

6+
"github.com/openzipkin/zipkin-go/model"
67
"github.com/openzipkin/zipkin-go/reporter"
78
"github.com/openzipkin/zipkin-go/reporter/recorder"
89

@@ -16,9 +17,54 @@ func newTracer(r reporter.Reporter, opts ...zipkin.TracerOption) opentracing.Tra
1617
return Wrap(tr)
1718
}
1819

20+
func TestOTKindTagIsParsedSuccessfuly(t *testing.T) {
21+
tagCases := []map[string]interface{}{
22+
{string(ext.SpanKind): "server"},
23+
{"span.kind": "server"},
24+
{"span.kind": ext.SpanKindRPCServerEnum},
25+
}
26+
for _, tags := range tagCases {
27+
opts := parseTagsAsZipkinOptions(tags)
28+
29+
rec := recorder.NewReporter()
30+
tr, _ := zipkin.NewTracer(rec)
31+
sp := tr.StartSpan("test", opts...)
32+
sp.Finish()
33+
spans := rec.Flush()
34+
if want, have := 1, len(spans); want != have {
35+
t.Fatalf("unexpected number of spans, want %d, have %d", want, have)
36+
}
37+
38+
if want, have := model.Server, spans[0].Kind; want != have {
39+
t.Errorf("unexpected kind value, want %s, have %s", want, have)
40+
}
41+
}
42+
}
43+
44+
func TestOTKindTagIsCantBeParsed(t *testing.T) {
45+
tags := map[string]interface{}{"span.kind": "banana"}
46+
opts := parseTagsAsZipkinOptions(tags)
47+
48+
rec := recorder.NewReporter()
49+
tr, _ := zipkin.NewTracer(rec)
50+
sp := tr.StartSpan("test", opts...)
51+
sp.Finish()
52+
spans := rec.Flush()
53+
if want, have := 1, len(spans); want != have {
54+
t.Fatalf("unexpected number of spans, want %d, have %d", want, have)
55+
}
56+
57+
if want, have := model.Undetermined, spans[0].Kind; want != have {
58+
t.Errorf("unexpected kind value, want %s, have %s", want, have)
59+
}
60+
61+
if want, have := "banana", spans[0].Tags["span.kind"]; want != have {
62+
t.Errorf("unexpected tag value, want %s, have %s", want, have)
63+
}
64+
}
65+
1966
func TestOptionsFromOTTags(t *testing.T) {
2067
tags := map[string]interface{}{}
21-
tags[string(ext.SpanKind)] = "server"
2268
tags[string(ext.PeerService)] = "service_a"
2369
tags["key"] = "value"
2470
opts := parseTagsAsZipkinOptions(tags)
@@ -32,10 +78,6 @@ func TestOptionsFromOTTags(t *testing.T) {
3278
t.Fatalf("unexpected number of spans, want %d, have %d", want, have)
3379
}
3480

35-
if want, have := "SERVER", string(spans[0].Kind); want != have {
36-
t.Errorf("unexpected span kind, want %s, have %s", want, have)
37-
}
38-
3981
if want, have := "service_a", spans[0].RemoteEndpoint.ServiceName; want != have {
4082
t.Errorf("unexpected remote service name, want %s, have %s", want, have)
4183
}

0 commit comments

Comments
 (0)