Skip to content

Commit

Permalink
Use Zipkin annotations if the timestamp is zero
Browse files Browse the repository at this point in the history
  • Loading branch information
geobeau committed Feb 13, 2019
1 parent f445c1d commit 7daa0a6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
33 changes: 30 additions & 3 deletions model/converter/thrift/zipkin/to_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,17 @@ func (td toDomain) transformSpan(zSpan *zipkincore.Span) []*model.Span {
}

flags := td.getFlags(zSpan)
// TODO StartTime and Duration could theoretically be defined only via cs/cr/sr/ss annotations.

startTime, duration := td.getStartTimeAndDuration(zSpan)

result := []*model.Span{{
TraceID: traceID,
SpanID: model.NewSpanID(uint64(zSpan.ID)),
OperationName: zSpan.Name,
References: refs,
Flags: flags,
StartTime: model.EpochMicrosecondsAsTime(uint64(zSpan.GetTimestamp())),
Duration: model.MicrosecondsAsDuration(uint64(zSpan.GetDuration())),
StartTime: model.EpochMicrosecondsAsTime(uint64(startTime)),
Duration: model.MicrosecondsAsDuration(uint64(duration)),
Tags: tags,
Logs: td.getLogs(zSpan.Annotations),
}}
Expand Down Expand Up @@ -188,6 +190,31 @@ func (td toDomain) getFlags(zSpan *zipkincore.Span) model.Flags {
return f
}

// Get a correct start time to use for the span if it's not set directly
func (td toDomain) getStartTimeAndDuration(zSpan *zipkincore.Span) (int64, int64) {
timestamp := zSpan.GetTimestamp()
duration := zSpan.GetDuration()
if timestamp == 0 {
cs := td.findAnnotation(zSpan, zipkincore.CLIENT_SEND)
sr := td.findAnnotation(zSpan, zipkincore.SERVER_RECV)
if cs != nil {
timestamp = cs.Timestamp
cr := td.findAnnotation(zSpan, zipkincore.CLIENT_RECV)
if cr != nil {
duration = cr.Timestamp - cs.Timestamp
}
} else if sr != nil {
timestamp = sr.Timestamp
ss := td.findAnnotation(zSpan, zipkincore.SERVER_SEND)
if ss != nil {
duration = ss.Timestamp - sr.Timestamp
}
}
}
return timestamp, duration
}


// generateProcess takes a Zipkin Span and produces a model.Process.
// An optional error may also be returned, but it is not fatal.
func (td toDomain) generateProcess(zSpan *zipkincore.Span) (*model.Process, error) {
Expand Down
22 changes: 22 additions & 0 deletions model/converter/thrift/zipkin/to_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,28 @@ func TestToDomainServiceNameInBinAnnotation(t *testing.T) {
assert.Equal(t, "bar", trace.Spans[0].Process.ServiceName)
}

func TestToDomainWithDurationFromServerAnnotations(t *testing.T) {
zSpans := getZipkinSpans(t, `[{ "trace_id": -1, "id": 31, "annotations": [
{"value": "sr", "timestamp": 1, "host": {"service_name": "bar", "ipv4": 23456}},
{"value": "ss", "timestamp": 10, "host": {"service_name": "bar", "ipv4": 23456}}
]}]`)
trace, err := ToDomain(zSpans)
require.Nil(t, err)
assert.Equal(t, 1000, int(trace.Spans[0].StartTime.Nanosecond()))
assert.Equal(t, 9000, int(trace.Spans[0].Duration))
}

func TestToDomainWithDurationFromClientAnnotations(t *testing.T) {
zSpans := getZipkinSpans(t, `[{ "trace_id": -1, "id": 31, "annotations": [
{"value": "cs", "timestamp": 1, "host": {"service_name": "bar", "ipv4": 23456}},
{"value": "cr", "timestamp": 10, "host": {"service_name": "bar", "ipv4": 23456}}
]}]`)
trace, err := ToDomain(zSpans)
require.Nil(t, err)
assert.Equal(t, 1000, int(trace.Spans[0].StartTime.Nanosecond()))
assert.Equal(t, 9000, int(trace.Spans[0].Duration))
}

func TestToDomainMultipleSpanKinds(t *testing.T) {
tests := []struct {
json string
Expand Down

0 comments on commit 7daa0a6

Please sign in to comment.