1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-10 09:50:58 +02:00

exporter(jaeger): change jaeger process tags to core.KeyValue (#219)

This commit is contained in:
Gustavo Silva Paiva 2019-10-17 13:59:30 -03:00 committed by rghetia
parent 5e409de1aa
commit 11bdacf7b9
3 changed files with 36 additions and 63 deletions

View File

@ -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"
@ -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 {

View File

@ -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,
@ -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.
@ -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)),
@ -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)
}
@ -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:
@ -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
}
@ -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.

View File

@ -19,6 +19,8 @@ import (
"testing"
"time"
"go.opentelemetry.io/api/key"
apitrace "go.opentelemetry.io/api/trace"
"github.com/google/go-cmp/cmp"
@ -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"
@ -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,
@ -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},