Skip to content

Commit

Permalink
Merge pull request #145 from openzipkin-contrib/tests_fix_span_kind
Browse files Browse the repository at this point in the history
tests: adds span kind test.
  • Loading branch information
jcchavezs authored Aug 16, 2019
2 parents 1c852e0 + d067c67 commit 98eca45
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
10 changes: 9 additions & 1 deletion tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,15 @@ func parseTagsAsZipkinOptions(t map[string]interface{}) []zipkin.SpanOption {
default:
kind = fmt.Sprintf("%v", kindVal)
}
zopts = append(zopts, zipkin.Kind(model.Kind(strings.ToUpper(kind))))
mKind := model.Kind(strings.ToUpper(kind))
if mKind == model.Client ||
mKind == model.Server ||
mKind == model.Producer ||
mKind == model.Consumer {
zopts = append(zopts, zipkin.Kind(mKind))
} else {
tags["span.kind"] = kind
}
}

if val, ok := t[string(ext.PeerService)]; ok {
Expand Down
52 changes: 47 additions & 5 deletions tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package zipkintracer
import (
"testing"

"github.com/openzipkin/zipkin-go/model"
"github.com/openzipkin/zipkin-go/reporter"
"github.com/openzipkin/zipkin-go/reporter/recorder"

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

func TestOTKindTagIsParsedSuccessfuly(t *testing.T) {
tagCases := []map[string]interface{}{
{string(ext.SpanKind): "server"},
{"span.kind": "server"},
{"span.kind": ext.SpanKindRPCServerEnum},
}
for _, tags := range tagCases {
opts := parseTagsAsZipkinOptions(tags)

rec := recorder.NewReporter()
tr, _ := zipkin.NewTracer(rec)
sp := tr.StartSpan("test", opts...)
sp.Finish()
spans := rec.Flush()
if want, have := 1, len(spans); want != have {
t.Fatalf("unexpected number of spans, want %d, have %d", want, have)
}

if want, have := model.Server, spans[0].Kind; want != have {
t.Errorf("unexpected kind value, want %s, have %s", want, have)
}
}
}

func TestOTKindTagIsCantBeParsed(t *testing.T) {
tags := map[string]interface{}{"span.kind": "banana"}
opts := parseTagsAsZipkinOptions(tags)

rec := recorder.NewReporter()
tr, _ := zipkin.NewTracer(rec)
sp := tr.StartSpan("test", opts...)
sp.Finish()
spans := rec.Flush()
if want, have := 1, len(spans); want != have {
t.Fatalf("unexpected number of spans, want %d, have %d", want, have)
}

if want, have := model.Undetermined, spans[0].Kind; want != have {
t.Errorf("unexpected kind value, want %s, have %s", want, have)
}

if want, have := "banana", spans[0].Tags["span.kind"]; want != have {
t.Errorf("unexpected tag value, want %s, have %s", want, have)
}
}

func TestOptionsFromOTTags(t *testing.T) {
tags := map[string]interface{}{}
tags[string(ext.SpanKind)] = "server"
tags[string(ext.PeerService)] = "service_a"
tags["key"] = "value"
opts := parseTagsAsZipkinOptions(tags)
Expand All @@ -32,10 +78,6 @@ func TestOptionsFromOTTags(t *testing.T) {
t.Fatalf("unexpected number of spans, want %d, have %d", want, have)
}

if want, have := "SERVER", string(spans[0].Kind); want != have {
t.Errorf("unexpected span kind, want %s, have %s", want, have)
}

if want, have := "service_a", spans[0].RemoteEndpoint.ServiceName; want != have {
t.Errorf("unexpected remote service name, want %s, have %s", want, have)
}
Expand Down

0 comments on commit 98eca45

Please sign in to comment.