Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SpanKind support for badger #6376

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
4 changes: 0 additions & 4 deletions cmd/jaeger/internal/integration/badger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ func TestBadgerStorage(t *testing.T) {
StorageIntegration: integration.StorageIntegration{
SkipArchiveTest: true,
CleanUp: purge,

// TODO: remove this once badger supports returning spanKind from GetOperations
// Cf https://github.com/jaegertracing/jaeger/issues/1922
GetOperationsMissingSpanKind: true,
},
}
s.e2eInitialize(t, "badger")
Expand Down
4 changes: 3 additions & 1 deletion model/converter/thrift/zipkin/to_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,9 @@
func (toDomain) getSpanKindTag(annotations []*zipkincore.Annotation) (model.KeyValue, bool) {
for _, a := range annotations {
if spanKind, ok := coreAnnotations[a.Value]; ok {
return model.SpanKindTag(model.SpanKind(spanKind)), true
if k, err := model.SpanKindFromString(spanKind); err == nil {
return model.SpanKindTag(k), true
}

Check warning on line 397 in model/converter/thrift/zipkin/to_domain.go

View check run for this annotation

Codecov / codecov/patch

model/converter/thrift/zipkin/to_domain.go#L395-L397

Added lines #L395 - L397 were not covered by tests
}
}
return model.KeyValue{}, false
Expand Down
12 changes: 6 additions & 6 deletions model/converter/thrift/zipkin/to_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ func TestToDomainMultipleSpanKinds(t *testing.T) {
json string
tagFirstKey string
tagSecondKey string
tagFirstVal model.SpanKind
tagSecondVal model.SpanKind
tagFirstVal string
tagSecondVal string
}{
{
json: `[{ "trace_id": -1, "id": 31, "annotations": [
Expand All @@ -117,8 +117,8 @@ func TestToDomainMultipleSpanKinds(t *testing.T) {
]}]`,
tagFirstKey: model.SpanKindKey,
tagSecondKey: model.SpanKindKey,
tagFirstVal: model.SpanKindClient,
tagSecondVal: model.SpanKindServer,
tagFirstVal: model.SpanKindClient.String(),
tagSecondVal: model.SpanKindServer.String(),
},
{
json: `[{ "trace_id": -1, "id": 31, "annotations": [
Expand All @@ -128,8 +128,8 @@ func TestToDomainMultipleSpanKinds(t *testing.T) {
]}]`,
tagFirstKey: model.SpanKindKey,
tagSecondKey: model.SpanKindKey,
tagFirstVal: model.SpanKindServer,
tagSecondVal: model.SpanKindClient,
tagFirstVal: model.SpanKindServer.String(),
tagSecondVal: model.SpanKindClient.String(),
},
}

Expand Down
68 changes: 57 additions & 11 deletions model/keyvalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,70 @@
SamplerParamKey = "sampler.param"
)

type SpanKind string
type SpanKind int

const (
SpanKindClient SpanKind = "client"
SpanKindServer SpanKind = "server"
SpanKindProducer SpanKind = "producer"
SpanKindConsumer SpanKind = "consumer"
SpanKindInternal SpanKind = "internal"
SpanKindUnspecified SpanKind = ""
SpanKindUnspecified SpanKind = iota
SpanKindClient
SpanKindServer
SpanKindProducer
SpanKindConsumer
SpanKindInternal
)

var toSpanKind = map[string]SpanKind{
"client": SpanKindClient,
"server": SpanKindServer,
"producer": SpanKindProducer,
"consumer": SpanKindConsumer,
"internal": SpanKindInternal,
"": SpanKindUnspecified,
}

func SpanKindFromString(kind string) (SpanKind, error) {
switch SpanKind(kind) {
case SpanKindClient, SpanKindServer, SpanKindProducer, SpanKindConsumer, SpanKindInternal, SpanKindUnspecified:
return SpanKind(kind), nil
if sKind, ok := toSpanKind[kind]; ok {
return sKind, nil
}
return SpanKindUnspecified, fmt.Errorf("unknown span kind %s", kind)

Check warning on line 58 in model/keyvalue.go

View check run for this annotation

Codecov / codecov/patch

model/keyvalue.go#L58

Added line #L58 was not covered by tests
}

func (sk SpanKind) String() string {
switch sk {
case SpanKindClient:
return "client"
case SpanKindServer:
return "server"
case SpanKindProducer:
return "producer"
case SpanKindConsumer:
return "consumer"
case SpanKindInternal:
return "internal"

Check warning on line 72 in model/keyvalue.go

View check run for this annotation

Codecov / codecov/patch

model/keyvalue.go#L67-L72

Added lines #L67 - L72 were not covered by tests
default:
return ""
}
}

func (sk SpanKind) Validate() bool {
switch sk {
case SpanKindClient, SpanKindServer, SpanKindProducer, SpanKindConsumer, SpanKindInternal:
return true

Check warning on line 81 in model/keyvalue.go

View check run for this annotation

Codecov / codecov/patch

model/keyvalue.go#L78-L81

Added lines #L78 - L81 were not covered by tests
default:
return SpanKindUnspecified, fmt.Errorf("unknown span kind %q", kind)
return false

Check warning on line 83 in model/keyvalue.go

View check run for this annotation

Codecov / codecov/patch

model/keyvalue.go#L83

Added line #L83 was not covered by tests
}
}

func GetSpanKindFromStringNumericString(s string) SpanKind {
kind, err := strconv.Atoi(s)
if err != nil {
return SpanKindUnspecified
}
sKind := SpanKind(kind)
ok := sKind.Validate()
if !ok {
return SpanKindUnspecified

Check warning on line 95 in model/keyvalue.go

View check run for this annotation

Codecov / codecov/patch

model/keyvalue.go#L87-L95

Added lines #L87 - L95 were not covered by tests
}
return sKind

Check warning on line 97 in model/keyvalue.go

View check run for this annotation

Codecov / codecov/patch

model/keyvalue.go#L97

Added line #L97 was not covered by tests
}

// KeyValues is a type alias that exposes convenience functions like Sort, FindByKey.
Expand Down
4 changes: 2 additions & 2 deletions model/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
}

func SpanKindTag(kind SpanKind) KeyValue {
return String(SpanKindKey, string(kind))
return String(SpanKindKey, kind.String())

Check warning on line 61 in model/span.go

View check run for this annotation

Codecov / codecov/patch

model/span.go#L61

Added line #L61 was not covered by tests
}

// Hash implements Hash from Hashable.
Expand All @@ -72,7 +72,7 @@
// HasSpanKind returns true if the span has a `span.kind` tag set to `kind`.
func (s *Span) HasSpanKind(kind SpanKind) bool {
if tag, ok := KeyValues(s.Tags).FindByKey(SpanKindKey); ok {
return tag.AsString() == string(kind)
return tag.AsString() == kind.String()

Check warning on line 75 in model/span.go

View check run for this annotation

Codecov / codecov/patch

model/span.go#L75

Added line #L75 was not covered by tests
}
return false
}
Expand Down
2 changes: 1 addition & 1 deletion model/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func TestGetSpanKind(t *testing.T) {
assert.Equal(t, model.SpanKindUnspecified, spanKind)
assert.False(t, found)

span = makeSpan(model.SpanKindTag("client"))
span = makeSpan(model.SpanKindTag(model.SpanKindClient))
spanKind, found = span.GetSpanKind()
assert.Equal(t, model.SpanKindClient, spanKind)
assert.True(t, found)
Expand Down
Loading
Loading