diff --git a/tracer.go b/tracer.go index d37f500..f236888 100644 --- a/tracer.go +++ b/tracer.go @@ -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 { diff --git a/tracer_test.go b/tracer_test.go index 3cf13ea..cb50898 100644 --- a/tracer_test.go +++ b/tracer_test.go @@ -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" @@ -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) @@ -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) }