Skip to content

Commit

Permalink
[WIP] Replace OT trace with otel trace spans type to span model (#4614
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 type to span model

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

---------

Signed-off-by: Afzal Ansari <[email protected]>
Signed-off-by: Afzal <[email protected]>
Co-authored-by: Afzal <[email protected]>
  • Loading branch information
afzal442 and afzalbin64 authored Aug 3, 2023
1 parent d46c41b commit 312746b
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 33 deletions.
15 changes: 8 additions & 7 deletions cmd/query/app/apiv3/otlp_translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"go.opentelemetry.io/collector/pdata/ptrace"
semconv "go.opentelemetry.io/collector/semconv/v1.5.0"
"go.opentelemetry.io/otel/trace"

"github.com/jaegertracing/jaeger/model"
commonv1 "github.com/jaegertracing/jaeger/proto-gen/otel/common/v1"
Expand Down Expand Up @@ -109,7 +110,7 @@ func jSpanToOTLP(jSpan *model.Span) (*tracev1.Span, resource, instrumentationLib
Events: jLogsToOTLP(jSpan.GetLogs()),
Links: jReferencesToOTLP(jSpan.GetReferences(), jSpan.ParentSpanID()),
Status: status,
Kind: tracev1.Span_SPAN_KIND_INTERNAL,
Kind: tracev1.Span_SPAN_KIND_UNSPECIFIED,
}
if kind, found := jSpan.GetSpanKind(); found {
s.Kind = jSpanKindToInternal(kind)
Expand Down Expand Up @@ -333,17 +334,17 @@ func getStatusCodeFromHTTPStatusTag(tag model.KeyValue) (int, error) {
return int(statusCodeFromHTTP(statusCode)), nil
}

func jSpanKindToInternal(spanKind string) tracev1.Span_SpanKind {
func jSpanKindToInternal(spanKind trace.SpanKind) tracev1.Span_SpanKind {
switch spanKind {
case "client":
case trace.SpanKindClient:
return tracev1.Span_SPAN_KIND_CLIENT
case "server":
case trace.SpanKindServer:
return tracev1.Span_SPAN_KIND_SERVER
case "producer":
case trace.SpanKindProducer:
return tracev1.Span_SPAN_KIND_PRODUCER
case "consumer":
case trace.SpanKindConsumer:
return tracev1.Span_SPAN_KIND_CONSUMER
case "internal":
case trace.SpanKindInternal:
return tracev1.Span_SPAN_KIND_INTERNAL
}
return tracev1.Span_SPAN_KIND_UNSPECIFIED
Expand Down
15 changes: 8 additions & 7 deletions cmd/query/app/apiv3/otlp_translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/stretchr/testify/assert"
semconv "go.opentelemetry.io/collector/semconv/v1.5.0"
"go.opentelemetry.io/otel/trace"

"github.com/jaegertracing/jaeger/model"
commonv1 "github.com/jaegertracing/jaeger/proto-gen/otel/common/v1"
Expand Down Expand Up @@ -151,35 +152,35 @@ func TestTranslateSpan(t *testing.T) {

func TestTranslateSpanKind(t *testing.T) {
tests := []struct {
kind string
kind trace.SpanKind
otelSpanKind v1.Span_SpanKind
}{
{
kind: "client",
kind: trace.SpanKindClient,
otelSpanKind: v1.Span_SPAN_KIND_CLIENT,
},
{
kind: "server",
kind: trace.SpanKindServer,
otelSpanKind: v1.Span_SPAN_KIND_SERVER,
},
{
kind: "producer",
kind: trace.SpanKindProducer,
otelSpanKind: v1.Span_SPAN_KIND_PRODUCER,
},
{
kind: "consumer",
kind: trace.SpanKindConsumer,
otelSpanKind: v1.Span_SPAN_KIND_CONSUMER,
},
{
kind: "internal",
kind: trace.SpanKindInternal,
otelSpanKind: v1.Span_SPAN_KIND_INTERNAL,
},
{
otelSpanKind: v1.Span_SPAN_KIND_UNSPECIFIED,
},
}
for _, test := range tests {
t.Run(test.kind, func(t *testing.T) {
t.Run(test.kind.String(), func(t *testing.T) {
otelSpanKind := jSpanKindToInternal(test.kind)
assert.Equal(t, test.otelSpanKind, otelSpanKind)
})
Expand Down
32 changes: 22 additions & 10 deletions model/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"io"
"strconv"

"github.com/opentracing/opentracing-go/ext"
"github.com/uber/jaeger-client-go"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
)

Expand All @@ -34,12 +34,22 @@ const (
FirehoseFlag = Flags(8)

samplerType = "sampler.type"
keySpanKind = "span.kind"
samplerTypeUnknown = "unknown"
)

// Flags is a bit map of flags for a span
type Flags uint32

// Map from string to trace.SpanKind.
var toSpanKind = map[string]trace.SpanKind{
"client": trace.SpanKindClient,
"server": trace.SpanKindServer,
"producer": trace.SpanKindProducer,
"consumer": trace.SpanKindConsumer,
"internal": trace.SpanKindInternal,
}

// Hash implements Hash from Hashable.
func (s *Span) Hash(w io.Writer) (err error) {
// gob is not the most efficient way, but it ensures we don't miss any fields.
Expand All @@ -49,19 +59,21 @@ func (s *Span) Hash(w io.Writer) (err error) {
}

// HasSpanKind returns true if the span has a `span.kind` tag set to `kind`.
func (s *Span) HasSpanKind(kind ext.SpanKindEnum) bool {
if tag, ok := KeyValues(s.Tags).FindByKey(string(ext.SpanKind)); ok {
return tag.AsString() == string(kind)
func (s *Span) HasSpanKind(kind trace.SpanKind) bool {
if tag, ok := KeyValues(s.Tags).FindByKey(keySpanKind); ok {
return tag.AsString() == kind.String()
}
return false
}

// GetSpanKind returns value of `span.kind` tag and whether the tag can be found
func (s *Span) GetSpanKind() (spanKind string, found bool) {
if tag, ok := KeyValues(s.Tags).FindByKey(string(ext.SpanKind)); ok {
return tag.AsString(), true
func (s *Span) GetSpanKind() (spanKind trace.SpanKind, found bool) {
if tag, ok := KeyValues(s.Tags).FindByKey(keySpanKind); ok {
if kind, ok := toSpanKind[tag.AsString()]; ok {
return kind, true
}
}
return "", false
return trace.SpanKindUnspecified, false
}

// GetSamplerType returns the sampler type for span
Expand All @@ -79,13 +91,13 @@ func (s *Span) GetSamplerType() string {
// IsRPCClient returns true if the span represents a client side of an RPC,
// as indicated by the `span.kind` tag set to `client`.
func (s *Span) IsRPCClient() bool {
return s.HasSpanKind(ext.SpanKindRPCClientEnum)
return s.HasSpanKind(trace.SpanKindClient)
}

// IsRPCServer returns true if the span represents a server side of an RPC,
// as indicated by the `span.kind` tag set to `server`.
func (s *Span) IsRPCServer() bool {
return s.HasSpanKind(ext.SpanKindRPCServerEnum)
return s.HasSpanKind(trace.SpanKindServer)
}

// NormalizeTimestamps changes all timestamps in this span to UTC.
Expand Down
10 changes: 6 additions & 4 deletions model/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (

"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/proto"
"github.com/opentracing/opentracing-go/ext"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"

"github.com/jaegertracing/jaeger/model"
Expand Down Expand Up @@ -122,6 +122,8 @@ var (
}
)

const keySpanKind = "span.kind"

func TestSpanIDMarshalJSON(t *testing.T) {
for _, testCase := range testCasesSpanID {
expected := fmt.Sprintf(`{"traceId":"AAAAAAAAAAAAAAAAAAAAAA==","spanId":"%s"}`, testCase.b64)
Expand Down Expand Up @@ -188,7 +190,7 @@ func TestSpanIDUnmarshalJSONErrors(t *testing.T) {
func TestIsRPCClientServer(t *testing.T) {
span1 := &model.Span{
Tags: model.KeyValues{
model.String(string(ext.SpanKind), string(ext.SpanKindRPCClientEnum)),
model.String(keySpanKind, trace.SpanKindClient.String()),
},
}
assert.True(t, span1.IsRPCClient())
Expand Down Expand Up @@ -227,12 +229,12 @@ func TestIsFirehoseEnabled(t *testing.T) {
func TestGetSpanKind(t *testing.T) {
span := makeSpan(model.String("sampler.type", "lowerbound"))
spanKind, found := span.GetSpanKind()
assert.Equal(t, "", spanKind)
assert.Equal(t, "unspecified", spanKind.String())
assert.Equal(t, false, found)

span = makeSpan(model.String("span.kind", "client"))
spanKind, found = span.GetSpanKind()
assert.Equal(t, "client", spanKind)
assert.Equal(t, "client", spanKind.String())
assert.Equal(t, true, found)
}

Expand Down
2 changes: 1 addition & 1 deletion plugin/storage/cassandra/spanstore/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func (s *SpanWriter) writeIndexes(span *model.Span, ds *dbmodel.Span) error {
spanKind, _ := span.GetSpanKind()
if err := s.saveServiceNameAndOperationName(dbmodel.Operation{
ServiceName: ds.ServiceName,
SpanKind: spanKind,
SpanKind: spanKind.String(),
OperationName: ds.OperationName,
}); err != nil {
// should this be a soft failure?
Expand Down
2 changes: 1 addition & 1 deletion plugin/storage/cassandra/spanstore/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ func TestStorageMode_IndexOnly_FirehoseSpan(t *testing.T) {
assert.Equal(t, "planet-express", serviceWritten.Load())
assert.Equal(t, dbmodel.Operation{
ServiceName: "planet-express",
SpanKind: "",
SpanKind: "unspecified",
OperationName: "package-delivery",
}, operationWritten.Load())
}, StoreIndexesOnly())
Expand Down
2 changes: 1 addition & 1 deletion plugin/storage/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (s *StorageIntegration) testGetOperations(t *testing.T) {
}
} else {
expected = []spanstore.Operation{
{Name: "example-operation-1"},
{Name: "example-operation-1", SpanKind: "unspecified"},
{Name: "example-operation-3", SpanKind: "server"},
{Name: "example-operation-4", SpanKind: "client"},
}
Expand Down
2 changes: 1 addition & 1 deletion plugin/storage/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (st *Store) WriteSpan(ctx context.Context, span *model.Span) error {
spanKind, _ := span.GetSpanKind()
operation := spanstore.Operation{
Name: span.OperationName,
SpanKind: spanKind,
SpanKind: spanKind.String(),
}

if _, ok := m.operations[span.Process.ServiceName][operation]; !ok {
Expand Down
2 changes: 1 addition & 1 deletion plugin/storage/memory/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ var childSpan2 = &model.Span{
OperationName: "childOperationName",
Tags: model.KeyValues{
model.String("tagKey", "tagValue"),
model.String("span.kind", "local"),
model.String("span.kind", "internal"),
},
Logs: []model.Log{
{
Expand Down

0 comments on commit 312746b

Please sign in to comment.