Skip to content

Commit

Permalink
Store log payloads as tags; fix "component" tag handling (jaegertraci…
Browse files Browse the repository at this point in the history
  • Loading branch information
yurishkuro authored Aug 20, 2016
1 parent ee3f52f commit 6ef2c7c
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 13 deletions.
60 changes: 47 additions & 13 deletions thrift_span.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ const (
allowPackedNumbers = false
)

var (
logPayloadLabels = makeLogPayloadLabels(100)
)

func makeLogPayloadLabels(size int) []string {
labels := make([]string, size)
for i := 0; i < size; i++ {
labels[i] = fmt.Sprintf("log_payload_%d", i)
}
return labels
}

// buildThriftSpan builds thrift span based on internal span.
func buildThriftSpan(span *span) *z.Span {
parentID := int64(span.context.parentID)
Expand Down Expand Up @@ -125,55 +137,77 @@ func buildBinaryAnnotations(span *span, endpoint *z.Endpoint) []*z.BinaryAnnotat
annotations = append(annotations, anno)
}
if !span.isRPC() {
// TODO(yurishkuro) deal with local component name
// https://github.com/opentracing/opentracing.github.io/issues/75
componentName := endpoint.ServiceName
for _, tag := range span.tags {
if tag.key == string(ext.Component) {
componentName = stringify(tag.value)
break
}
}
local := &z.BinaryAnnotation{
Key: z.LOCAL_COMPONENT,
Value: []byte(componentName),
AnnotationType: z.AnnotationType_STRING,
Host: endpoint}
annotations = append(annotations, local)
}
for i := range span.tags {
if anno := buildBinaryAnnotation(&span.tags[i], nil); anno != nil {
for _, tag := range span.tags {
if anno := buildBinaryAnnotation(tag.key, tag.value, nil); anno != nil {
annotations = append(annotations, anno)
}
}
for i, log := range span.logs {
if log.Payload != nil {
label := "log_payload"
if i < len(logPayloadLabels) {
label = logPayloadLabels[i]
}
if anno := buildBinaryAnnotation(label, log.Payload, nil); anno != nil {
annotations = append(annotations, anno)
}
}
}
return annotations
}

func buildBinaryAnnotation(tag *tag, endpoint *z.Endpoint) *z.BinaryAnnotation {
bann := &z.BinaryAnnotation{Key: tag.key, Host: endpoint}
if value, ok := tag.value.(string); ok {
func buildBinaryAnnotation(key string, val interface{}, endpoint *z.Endpoint) *z.BinaryAnnotation {
bann := &z.BinaryAnnotation{Key: key, Host: endpoint}
if value, ok := val.(string); ok {
bann.Value = []byte(truncateString(value))
bann.AnnotationType = z.AnnotationType_STRING
} else if value, ok := tag.value.([]byte); ok {
} else if value, ok := val.([]byte); ok {
if len(value) > maxAnnotationLength {
value = value[:maxAnnotationLength]
}
bann.Value = value
bann.AnnotationType = z.AnnotationType_BYTES
} else if value, ok := tag.value.(int32); ok && allowPackedNumbers {
} else if value, ok := val.(int32); ok && allowPackedNumbers {
bann.Value = int32ToBytes(value)
bann.AnnotationType = z.AnnotationType_I32
} else if value, ok := tag.value.(int64); ok && allowPackedNumbers {
} else if value, ok := val.(int64); ok && allowPackedNumbers {
bann.Value = int64ToBytes(value)
bann.AnnotationType = z.AnnotationType_I64
} else if value, ok := tag.value.(int); ok && allowPackedNumbers {
} else if value, ok := val.(int); ok && allowPackedNumbers {
bann.Value = int64ToBytes(int64(value))
bann.AnnotationType = z.AnnotationType_I64
} else if value, ok := tag.value.(bool); ok {
} else if value, ok := val.(bool); ok {
bann.Value = []byte{boolToByte(value)}
bann.AnnotationType = z.AnnotationType_BOOL
} else {
value := fmt.Sprintf("%+v", tag.value)
value := stringify(val)
bann.Value = []byte(truncateString(value))
bann.AnnotationType = z.AnnotationType_STRING
}
return bann
}

func stringify(value interface{}) string {
if s, ok := value.(string); ok {
return s
}
return fmt.Sprintf("%+v", value)
}

func truncateString(value string) string {
// we ignore the problem of utf8 runes possibly being sliced in the middle,
// as it is rather expensive to iterate through each tag just to find rune
Expand Down
66 changes: 66 additions & 0 deletions thrift_span_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jaeger

import (
"fmt"
"testing"

"github.com/opentracing/opentracing-go"
Expand Down Expand Up @@ -50,3 +51,68 @@ func TestThriftForceSampled(t *testing.T) {
thriftSpan := buildThriftSpan(sp)
assert.True(t, thriftSpan.Debug)
}

func TestThriftSpanLogs(t *testing.T) {
tracer, closer := NewTracer("DOOP",
NewConstSampler(true),
NewNullReporter())
defer closer.Close()

sp := tracer.StartSpan("s1").(*span)
payload := "luggage"
n := len(logPayloadLabels)
m := n + 5
for i := 0; i < m; i++ {
sp.LogEventWithPayload(fmt.Sprintf("event%d", i), payload)
}
thriftSpan := buildThriftSpan(sp)
var (
logs int
numberedPayloads int
plainPayloads int
)
for i := 0; i < m; i++ {
for _, anno := range thriftSpan.Annotations {
if anno.Value == fmt.Sprintf("event%d", i) {
logs++
}
}
for _, anno := range thriftSpan.BinaryAnnotations {
if anno.Key == fmt.Sprintf("log_payload_%d", i) {
numberedPayloads++
}
}
}
for _, anno := range thriftSpan.BinaryAnnotations {
if anno.Key == "log_payload" {
plainPayloads++
}
}
assert.Equal(t, m, logs, "Each log must create Annotation")
assert.Equal(t, n, numberedPayloads, "Each log must create numbered BinaryAnnotation")
assert.Equal(t, m-n, plainPayloads, "Each log over %d must create unnumbered BinaryAnnotation", n)
}

func TestThriftLocalComponentSpan(t *testing.T) {
tracer, closer := NewTracer("DOOP",
NewConstSampler(true),
NewNullReporter())
defer closer.Close()

sp := tracer.StartSpan("s1").(*span)
sp.Finish()
thriftSpan := buildThriftSpan(sp)

anno := thriftSpan.BinaryAnnotations[len(thriftSpan.BinaryAnnotations)-1]
assert.Equal(t, "lc", anno.Key)
assert.EqualValues(t, "DOOP", anno.Value, "Without COMPONENT tag the value is service name")

sp = tracer.StartSpan("s1").(*span)
ext.Component.Set(sp, "c1")
sp.Finish()
thriftSpan = buildThriftSpan(sp)

anno = thriftSpan.BinaryAnnotations[len(thriftSpan.BinaryAnnotations)-2]
assert.Equal(t, "lc", anno.Key)
assert.EqualValues(t, "c1", anno.Value, "Value of COMPONENT tag")
}

0 comments on commit 6ef2c7c

Please sign in to comment.