Skip to content

Commit

Permalink
[WIP] Replace OT const wth OTEL trace.span for zipkin comp (#4625)
Browse files Browse the repository at this point in the history
## Which problem is this PR solving?
* Part of #3381 
* This PR adds `otel trace` spans const type to domain model

## Short description of the changes
- Replace OT const with `otel trace` spans const i.e. `ext.SpanKind..`
with `trace.SpanKind..`

Signed-off-by: Afzal <94980910+afzalbin64@users.noreply.github.com>
Co-authored-by: Afzal <94980910+afzalbin64@users.noreply.github.com>
  • Loading branch information
afzal442 and afzalbin64 authored Aug 7, 2023
1 parent 369219c commit 078051c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
32 changes: 19 additions & 13 deletions model/converter/thrift/zipkin/to_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"errors"
"fmt"

"github.com/opentracing/opentracing-go/ext"
"go.opentelemetry.io/otel/trace"

"github.com/jaegertracing/jaeger/model"
"github.com/jaegertracing/jaeger/thrift-gen/zipkincore"
Expand All @@ -32,14 +32,20 @@ import (
const (
// UnknownServiceName is serviceName we give to model.Process if we cannot find it anywhere in a Zipkin span
UnknownServiceName = "unknown-service-name"
keySpanKind = "span.kind"
component = "component"
peerservice = "peer.service"
peerHostIPv4 = "peer.ipv4"
peerHostIPv6 = "peer.ipv6"
peerPort = "peer.port"
)

var (
coreAnnotations = map[string]string{
zipkincore.SERVER_RECV: string(ext.SpanKindRPCServerEnum),
zipkincore.SERVER_SEND: string(ext.SpanKindRPCServerEnum),
zipkincore.CLIENT_RECV: string(ext.SpanKindRPCClientEnum),
zipkincore.CLIENT_SEND: string(ext.SpanKindRPCClientEnum),
zipkincore.SERVER_RECV: trace.SpanKindServer.String(),
zipkincore.SERVER_SEND: trace.SpanKindServer.String(),
zipkincore.CLIENT_RECV: trace.SpanKindClient.String(),
zipkincore.CLIENT_SEND: trace.SpanKindClient.String(),
}

// Some tags on Zipkin spans really describe the process emitting them rather than an individual span.
Expand Down Expand Up @@ -165,13 +171,13 @@ func (td toDomain) transformSpan(zSpan *zipkincore.Span) []*model.Span {
}
// if the first span is a client span we create server span and vice-versa.
if result[0].IsRPCClient() {
s.Tags = []model.KeyValue{model.String(string(ext.SpanKind), string(ext.SpanKindRPCServerEnum))}
s.Tags = []model.KeyValue{model.String(keySpanKind, trace.SpanKindServer.String())}
s.StartTime = model.EpochMicrosecondsAsTime(uint64(sr.Timestamp))
if ss := td.findAnnotation(zSpan, zipkincore.SERVER_SEND); ss != nil {
s.Duration = model.MicrosecondsAsDuration(uint64(ss.Timestamp - sr.Timestamp))
}
} else {
s.Tags = []model.KeyValue{model.String(string(ext.SpanKind), string(ext.SpanKindRPCClientEnum))}
s.Tags = []model.KeyValue{model.String(keySpanKind, trace.SpanKindClient.String())}
s.StartTime = model.EpochMicrosecondsAsTime(uint64(cs.Timestamp))
if cr := td.findAnnotation(zSpan, zipkincore.CLIENT_RECV); cr != nil {
s.Duration = model.MicrosecondsAsDuration(uint64(cr.Timestamp - cs.Timestamp))
Expand Down Expand Up @@ -286,7 +292,7 @@ func (td toDomain) getTags(binAnnotations []*zipkincore.BinaryAnnotation, tagInc
switch annotation.Key {
case zipkincore.LOCAL_COMPONENT:
value := string(annotation.Value)
tag := model.String(string(ext.Component), value)
tag := model.String(component, value)
retMe = append(retMe, tag)
case zipkincore.SERVER_ADDR, zipkincore.CLIENT_ADDR, zipkincore.MESSAGE_ADDR:
retMe = td.getPeerTags(annotation.Host, retMe)
Expand Down Expand Up @@ -385,7 +391,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 := coreAnnotations[a.Value]; ok {
return model.String(string(ext.SpanKind), spanKind), true
return model.String(keySpanKind, spanKind), true
}
}
return model.KeyValue{}, false
Expand All @@ -395,19 +401,19 @@ func (td toDomain) getPeerTags(endpoint *zipkincore.Endpoint, tags []model.KeyVa
if endpoint == nil {
return tags
}
tags = append(tags, model.String(string(ext.PeerService), endpoint.ServiceName))
tags = append(tags, model.String(peerservice, endpoint.ServiceName))
if endpoint.Ipv4 != 0 {
ipv4 := int64(uint32(endpoint.Ipv4))
tags = append(tags, model.Int64(string(ext.PeerHostIPv4), ipv4))
tags = append(tags, model.Int64(peerHostIPv4, ipv4))
}
if endpoint.Ipv6 != nil {
// Zipkin defines Ipv6 field as: "IPv6 host address packed into 16 bytes. Ex Inet6Address.getBytes()".
// https://github.com/openzipkin/zipkin-api/blob/master/thrift/zipkinCore.thrift#L305
tags = append(tags, model.Binary(string(ext.PeerHostIPv6), endpoint.Ipv6))
tags = append(tags, model.Binary(peerHostIPv6, endpoint.Ipv6))
}
if endpoint.Port != 0 {
port := int64(uint16(endpoint.Port))
tags = append(tags, model.Int64(string(ext.PeerPort), port))
tags = append(tags, model.Int64(peerPort, port))
}
return tags
}
31 changes: 18 additions & 13 deletions model/converter/thrift/zipkin/to_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ import (
"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/proto"
"github.com/kr/pretty"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/trace"

"github.com/jaegertracing/jaeger/model"
z "github.com/jaegertracing/jaeger/thrift-gen/zipkincore"
Expand Down Expand Up @@ -116,27 +115,33 @@ func TestToDomainWithDurationFromClientAnnotations(t *testing.T) {

func TestToDomainMultipleSpanKinds(t *testing.T) {
tests := []struct {
json string
tagFirst opentracing.Tag
tagSecond opentracing.Tag
json string
tagFirstKey string
tagSecondKey string
tagFirstVal trace.SpanKind
tagSecondVal trace.SpanKind
}{
{
json: `[{ "trace_id": -1, "id": 31, "annotations": [
{"value": "cs", "host": {"service_name": "bar", "ipv4": 23456}},
{"value": "sr", "timestamp": 1, "host": {"service_name": "bar", "ipv4": 23456}},
{"value": "ss", "timestamp": 2, "host": {"service_name": "bar", "ipv4": 23456}}
]}]`,
tagFirst: ext.SpanKindRPCClient,
tagSecond: ext.SpanKindRPCServer,
tagFirstKey: keySpanKind,
tagSecondKey: keySpanKind,
tagFirstVal: trace.SpanKindClient,
tagSecondVal: trace.SpanKindServer,
},
{
json: `[{ "trace_id": -1, "id": 31, "annotations": [
{"value": "sr", "host": {"service_name": "bar", "ipv4": 23456}},
{"value": "cs", "timestamp": 1, "host": {"service_name": "bar", "ipv4": 23456}},
{"value": "cr", "timestamp": 2, "host": {"service_name": "bar", "ipv4": 23456}}
]}]`,
tagFirst: ext.SpanKindRPCServer,
tagSecond: ext.SpanKindRPCClient,
tagFirstKey: keySpanKind,
tagSecondKey: keySpanKind,
tagFirstVal: trace.SpanKindServer,
tagSecondVal: trace.SpanKindClient,
},
}

Expand All @@ -146,13 +151,13 @@ func TestToDomainMultipleSpanKinds(t *testing.T) {

assert.Equal(t, 2, len(trace.Spans))
assert.Equal(t, 1, len(trace.Spans[0].Tags))
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, test.tagFirstKey, trace.Spans[0].Tags[0].Key)
assert.Equal(t, test.tagFirstVal.String(), trace.Spans[0].Tags[0].VStr)

assert.Equal(t, 1, len(trace.Spans[1].Tags))
assert.Equal(t, test.tagSecond.Key, trace.Spans[1].Tags[0].Key)
assert.Equal(t, test.tagSecondKey, trace.Spans[1].Tags[0].Key)
assert.Equal(t, time.Duration(1000), trace.Spans[1].Duration)
assert.Equal(t, string(test.tagSecond.Value.(ext.SpanKindEnum)), trace.Spans[1].Tags[0].VStr)
assert.Equal(t, test.tagSecondVal.String(), trace.Spans[1].Tags[0].VStr)
}
}

Expand Down

0 comments on commit 078051c

Please sign in to comment.