Skip to content

Commit

Permalink
exporter(jaeger): change jaeger process tags to core.KeyValue (open-t…
Browse files Browse the repository at this point in the history
  • Loading branch information
paivagustavo authored and rghetia committed Oct 17, 2019
1 parent 5e409de commit 11bdacf
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 63 deletions.
8 changes: 8 additions & 0 deletions exporter/trace/jaeger/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import (
"context"
"log"

"go.opentelemetry.io/api/core"
"go.opentelemetry.io/api/key"

apitrace "go.opentelemetry.io/api/trace"
"go.opentelemetry.io/exporter/trace/jaeger"
"go.opentelemetry.io/sdk/trace"
Expand All @@ -34,6 +37,11 @@ func main() {
jaeger.WithCollectorEndpoint("http://localhost:14268/api/traces"),
jaeger.WithProcess(jaeger.Process{
ServiceName: "trace-demo",
Tags: []core.KeyValue{
key.String("exporter", "jaeger"),
key.Float64("float", 312.23),
key.Bytes("bytes", []byte("byte array")),
},
}),
)
if err != nil {
Expand Down
74 changes: 19 additions & 55 deletions exporter/trace/jaeger/jaeger.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,12 @@ func NewExporter(endpointOption EndpointOption, opts ...Option) (*Exporter, erro
if service == "" {
service = defaultServiceName
}
tags := make([]*gen.Tag, len(o.Process.Tags))
for i, tag := range o.Process.Tags {
tags[i] = attributeToTag(tag.key, tag.value)
tags := make([]*gen.Tag, 0, len(o.Process.Tags))
for _, tag := range o.Process.Tags {
t := keyValueToTag(tag)
if t != nil {
tags = append(tags, t)
}
}
e := &Exporter{
uploader: uploader,
Expand Down Expand Up @@ -128,14 +131,7 @@ type Process struct {
ServiceName string

// Tags are added to Jaeger Process exports
Tags []Tag
}

// Tag defines a key-value pair
// It is limited to the possible conversions to *jaeger.Tag by attributeToTag
type Tag struct {
key string
value interface{}
Tags []core.KeyValue
}

// Exporter is an implementation of trace.Exporter that uploads spans to Jaeger.
Expand Down Expand Up @@ -165,8 +161,10 @@ func (e *Exporter) ExportSpan(ctx context.Context, d *export.SpanData) {
func spanDataToThrift(data *export.SpanData) *gen.Span {
tags := make([]*gen.Tag, 0, len(data.Attributes))
for _, kv := range data.Attributes {
tag := coreAttributeToTag(kv)
tags = append(tags, tag)
tag := keyValueToTag(kv)
if tag != nil {
tags = append(tags, tag)
}
}

tags = append(tags, getInt64Tag("status.code", int64(data.Status)),
Expand All @@ -183,7 +181,7 @@ func spanDataToThrift(data *export.SpanData) *gen.Span {
for _, a := range data.MessageEvents {
fields := make([]*gen.Tag, 0, len(a.Attributes))
for _, kv := range a.Attributes {
tag := coreAttributeToTag(kv)
tag := keyValueToTag(kv)
if tag != nil {
fields = append(fields, tag)
}
Expand Down Expand Up @@ -222,7 +220,7 @@ func spanDataToThrift(data *export.SpanData) *gen.Span {
}
}

func coreAttributeToTag(kv core.KeyValue) *gen.Tag {
func keyValueToTag(kv core.KeyValue) *gen.Tag {
var tag *gen.Tag
switch kv.Value.Type {
case core.STRING:
Expand All @@ -249,6 +247,12 @@ func coreAttributeToTag(kv core.KeyValue) *gen.Tag {
VDouble: &kv.Value.Float64,
VType: gen.TagType_DOUBLE,
}
case core.BYTES:
tag = &gen.Tag{
Key: string(kv.Key),
VBinary: kv.Value.Bytes,
VType: gen.TagType_BINARY,
}
}
return tag
}
Expand Down Expand Up @@ -277,46 +281,6 @@ func getBoolTag(k string, b bool) *gen.Tag {
}
}

// TODO(rghetia): remove interface{}. see https://github.com/open-telemetry/opentelemetry-go/pull/112/files#r321444786
func attributeToTag(key string, a interface{}) *gen.Tag {
var tag *gen.Tag
switch value := a.(type) {
case bool:
tag = &gen.Tag{
Key: key,
VBool: &value,
VType: gen.TagType_BOOL,
}
case string:
tag = &gen.Tag{
Key: key,
VStr: &value,
VType: gen.TagType_STRING,
}
case int64:
tag = &gen.Tag{
Key: key,
VLong: &value,
VType: gen.TagType_LONG,
}
case int32:
v := int64(value)
tag = &gen.Tag{
Key: key,
VLong: &v,
VType: gen.TagType_LONG,
}
case float64:
v := float64(value)
tag = &gen.Tag{
Key: key,
VDouble: &v,
VType: gen.TagType_DOUBLE,
}
}
return tag
}

// Flush waits for exported trace spans to be uploaded.
//
// This is useful if your program is ending and you do not want to lose recent spans.
Expand Down
17 changes: 9 additions & 8 deletions exporter/trace/jaeger/jaeger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"testing"
"time"

"go.opentelemetry.io/api/key"

apitrace "go.opentelemetry.io/api/trace"

"github.com/google/go-cmp/cmp"
Expand All @@ -42,6 +44,7 @@ func Test_spanDataToThrift(t *testing.T) {
keyValue := "value"
statusCodeValue := int64(2)
doubleValue := float64(123.456)
bytesValue := []byte("byte array")
boolTrue := true
statusMessage := "Unknown"

Expand Down Expand Up @@ -69,14 +72,11 @@ func Test_spanDataToThrift(t *testing.T) {
},
},
Attributes: []core.KeyValue{
{
Key: core.Key("key"),
Value: core.Value{Type: core.STRING, String: keyValue},
},
{
Key: core.Key("double"),
Value: core.Value{Type: core.FLOAT64, Float64: doubleValue},
},
key.String("key", keyValue),
key.Float64("double", doubleValue),
key.Bytes("bytes", bytesValue),
// Jaeger doesn't handle Uint tags, this should be ignored.
key.Uint64("ignored", 123),
},
// TODO: [rghetia] add events test after event is concrete type.
Status: codes.Unknown,
Expand All @@ -91,6 +91,7 @@ func Test_spanDataToThrift(t *testing.T) {
Tags: []*gen.Tag{
{Key: "double", VType: gen.TagType_DOUBLE, VDouble: &doubleValue},
{Key: "key", VType: gen.TagType_STRING, VStr: &keyValue},
{Key: "bytes", VType: gen.TagType_BINARY, VBinary: bytesValue},
{Key: "error", VType: gen.TagType_BOOL, VBool: &boolTrue},
{Key: "status.code", VType: gen.TagType_LONG, VLong: &statusCodeValue},
{Key: "status.message", VType: gen.TagType_STRING, VStr: &statusMessage},
Expand Down

0 comments on commit 11bdacf

Please sign in to comment.