Skip to content

Commit

Permalink
merge core annotations
Browse files Browse the repository at this point in the history
Signed-off-by: Pavol Loffay <[email protected]>
  • Loading branch information
pavolloffay committed Oct 25, 2017
1 parent 756ea36 commit c53f31b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 42 deletions.
54 changes: 23 additions & 31 deletions model/converter/thrift/zipkin/to_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,12 @@ const (
)

var (
coreClientAnnotations = map[string]string{
coreAnnotations = map[string]string{
zipkincore.CLIENT_RECV: string(ext.SpanKindRPCClientEnum),
zipkincore.CLIENT_SEND: string(ext.SpanKindRPCClientEnum),
}
coreServerAnnotations = map[string]string{
zipkincore.SERVER_RECV: string(ext.SpanKindRPCServerEnum),
zipkincore.SERVER_SEND: string(ext.SpanKindRPCServerEnum),
}
coreAnnotations = map[string]string{}

// Some tags on Zipkin spans really describe the process emitting them rather than an individual span.
// Once all clients are upgraded to use native Jaeger model, this won't be happenning, but for now
Expand All @@ -66,15 +63,6 @@ var (
IPTagName = "ip"
)

func init() {
for k, v := range coreClientAnnotations {
coreAnnotations[k] = v
}
for k, v := range coreServerAnnotations {
coreAnnotations[k] = v
}
}

// ToDomain transforms a trace in zipkin.thrift format into model.Trace.
// The transformation assumes that all spans have the same Trace ID.
// A valid model.Trace is always returned, even when there are errors.
Expand Down Expand Up @@ -121,17 +109,17 @@ func (td toDomain) ToDomainSpan(zSpan *zipkincore.Span) ([]*model.Span, error) {
return jSpans, err
}

// isClientAndServerSpan determines whether span represents both client and server processing
func (td toDomain) isClientAndServerSpan(zSpan *zipkincore.Span) bool {
var cOk bool
var sOk bool
var csOk bool
var srOk bool
for _, ann := range zSpan.Annotations {
if !cOk {
_, cOk = coreClientAnnotations[ann.Value]
if ann.Value == "cs" {
csOk = true
} else if ann.Value == "sr" {
srOk = true
}
if !sOk {
_, sOk = coreServerAnnotations[ann.Value]
}
if cOk && sOk {
if csOk && srOk {
return true
}
}
Expand All @@ -141,7 +129,9 @@ func (td toDomain) isClientAndServerSpan(zSpan *zipkincore.Span) bool {
// transformSpan transforms a zipkin span into a Jaeger span
func (td toDomain) transformSpan(zSpan *zipkincore.Span) []*model.Span {
tags := td.getTags(zSpan.BinaryAnnotations, td.isSpanTag)
if spanKindTag, ok := td.getSpanKindTag(zSpan.Annotations); ok {
var spanKindTag model.KeyValue
spanKindTag, ok := td.getSpanKindTag(zSpan.Annotations)
if ok {
tags = append(tags, spanKindTag)
}
var traceIDHigh, parentID int64
Expand All @@ -165,15 +155,20 @@ func (td toDomain) transformSpan(zSpan *zipkincore.Span) []*model.Span {
}}

if td.isClientAndServerSpan(zSpan) {
// The first converted span is a client span, because in getSpanKindTag we first check for client annotations
// Therefore now we add only server span part
result = append(result, &model.Span{
// if the span is client and server we split it into two separate spans
s := &model.Span{
TraceID: model.TraceID{High: uint64(traceIDHigh), Low: uint64(zSpan.TraceID)},
SpanID: model.SpanID(zSpan.ID),
OperationName: zSpan.Name,
ParentSpanID: model.SpanID(parentID),
Tags: []model.KeyValue{model.String(ext.SpanKindRPCServer.Key, string(ext.SpanKindRPCServer.Value.(ext.SpanKindEnum)))},
})
}
// if the first span is a client span we create server span and vice-versa.
if spanKindTag.VStr == string(ext.SpanKindRPCClient.Value.(ext.SpanKindEnum)) {
s.Tags = []model.KeyValue{model.String(ext.SpanKindRPCServer.Key, string(ext.SpanKindRPCServer.Value.(ext.SpanKindEnum)))}
} else {
s.Tags = []model.KeyValue{model.String(ext.SpanKindRPCClient.Key, string(ext.SpanKindRPCClient.Value.(ext.SpanKindEnum)))}
}
result = append(result, s)
}
return result
}
Expand Down Expand Up @@ -356,10 +351,7 @@ func (td toDomain) getLogFields(annotation *zipkincore.Annotation) []model.KeyVa

func (td toDomain) getSpanKindTag(annotations []*zipkincore.Annotation) (model.KeyValue, bool) {
for _, a := range annotations {
if spanKind, ok := coreClientAnnotations[a.Value]; ok {
return model.String(string(ext.SpanKind), spanKind), true
}
if spanKind, ok := coreServerAnnotations[a.Value]; ok {
if spanKind, ok := coreAnnotations[a.Value]; ok {
return model.String(string(ext.SpanKind), spanKind), true
}
}
Expand Down
43 changes: 32 additions & 11 deletions model/converter/thrift/zipkin/to_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/uber/jaeger/model"
z "github.com/uber/jaeger/thrift-gen/zipkincore"
)
Expand Down Expand Up @@ -86,20 +88,39 @@ func TestToDomainServiceNameInBinAnnotation(t *testing.T) {
}

func TestToDomainMultipleSpanKinds(t *testing.T) {
zSpans := getZipkinSpans(t, `[{ "trace_id": -1, "id": 31,
tests := []struct {
json string
tagFirst opentracing.Tag
tagSecond opentracing.Tag
}{
{json: `[{ "trace_id": -1, "id": 31,
"annotations": [{"value": "cs", "host": {"service_name": "bar", "ipv4": 23456}},
{"value": "sr", "host": {"service_name": "bar", "ipv4": 23456}}] }]`)
trace, err := ToDomain(zSpans)
require.Nil(t, err)
{"value": "sr", "host": {"service_name": "bar", "ipv4": 23456}}] }]`,
tagFirst: ext.SpanKindRPCClient,
tagSecond: ext.SpanKindRPCServer,
},
{json: `[{ "trace_id": -1, "id": 31,
"annotations": [{"value": "sr", "host": {"service_name": "bar", "ipv4": 23456}},
{"value": "cs", "host": {"service_name": "bar", "ipv4": 23456}}] }]`,
tagFirst: ext.SpanKindRPCServer,
tagSecond: ext.SpanKindRPCClient,
},
}

for _, test := range tests {
fmt.Println(test.json)
trace, err := ToDomain(getZipkinSpans(t, test.json))
require.Nil(t, err)

assert.Equal(t, 2, len(trace.Spans))
assert.Equal(t, 1, trace.Spans[0].Tags.Len())
assert.Equal(t, "span.kind", trace.Spans[0].Tags[0].Key)
assert.Equal(t, "client", trace.Spans[0].Tags[0].VStr)
assert.Equal(t, 2, len(trace.Spans))
assert.Equal(t, 1, trace.Spans[0].Tags.Len())
assert.Equal(t, test.tagFirst.Key, trace.Spans[0].Tags[0].Key)
assert.Equal(t, string(test.tagFirst.Value.(ext.SpanKindEnum)), trace.Spans[0].Tags[0].VStr)

assert.Equal(t, 1, trace.Spans[1].Tags.Len())
assert.Equal(t, "span.kind", trace.Spans[1].Tags[0].Key)
assert.Equal(t, "server", trace.Spans[1].Tags[0].VStr)
assert.Equal(t, 1, trace.Spans[1].Tags.Len())
assert.Equal(t, test.tagSecond.Key, trace.Spans[1].Tags[0].Key)
assert.Equal(t, string(test.tagSecond.Value.(ext.SpanKindEnum)), trace.Spans[1].Tags[0].VStr)
}
}

func TestInvalidAnnotationTypeError(t *testing.T) {
Expand Down

0 comments on commit c53f31b

Please sign in to comment.