1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-14 10:13:10 +02:00
opentelemetry-go/exporters/trace/zipkin/model.go
Krzesimir Nowak d9210f5676
Zipkin exporter (#495)
* Add zipkin exporter

The zipkin exporter implements the SpanBatcher interface. It follows
the current-at-the-time-of-writing document about conversion from
OpenTelemetry span data to Zipkin spans. Which means that endpoint
information is not yet filled.

* Fix typo in docs

* Add a zipkin example

This sends span information to a locally running zipkin collector.
Currently I have a problem getting the collector to show me the spans
after accepting them with HTTP 202. Not sure if this is because of
missing endpoint information.

* Make gitignore consistent

The fixed paths should be prefixed with a slash. The "relative" paths
mean that git will ignore all the files that end with the path.

* Add tests for zipkin exporter
2020-03-11 14:49:02 -07:00

132 lines
3.5 KiB
Go

package zipkin
import (
"encoding/binary"
"encoding/json"
"fmt"
zkmodel "github.com/openzipkin/zipkin-go/model"
"go.opentelemetry.io/otel/api/core"
"go.opentelemetry.io/otel/api/trace"
export "go.opentelemetry.io/otel/sdk/export/trace"
)
func toZipkinSpanModels(batch []*export.SpanData) []zkmodel.SpanModel {
models := make([]zkmodel.SpanModel, 0, len(batch))
for _, data := range batch {
models = append(models, toZipkinSpanModel(data))
}
return models
}
func toZipkinSpanModel(data *export.SpanData) zkmodel.SpanModel {
return zkmodel.SpanModel{
SpanContext: toZipkinSpanContext(data),
Name: data.Name,
Kind: toZipkinKind(data.SpanKind),
Timestamp: data.StartTime,
Duration: data.EndTime.Sub(data.StartTime),
Shared: false,
LocalEndpoint: nil, // *Endpoint
RemoteEndpoint: nil, // *Endpoint
Annotations: toZipkinAnnotations(data.MessageEvents),
Tags: toZipkinTags(data),
}
}
func toZipkinSpanContext(data *export.SpanData) zkmodel.SpanContext {
return zkmodel.SpanContext{
TraceID: toZipkinTraceID(data.SpanContext.TraceID),
ID: toZipkinID(data.SpanContext.SpanID),
ParentID: toZipkinParentID(data.ParentSpanID),
Debug: false,
Sampled: nil,
Err: nil,
}
}
func toZipkinTraceID(traceID core.TraceID) zkmodel.TraceID {
return zkmodel.TraceID{
High: binary.BigEndian.Uint64(traceID[:8]),
Low: binary.BigEndian.Uint64(traceID[8:]),
}
}
func toZipkinID(spanID core.SpanID) zkmodel.ID {
return zkmodel.ID(binary.BigEndian.Uint64(spanID[:]))
}
func toZipkinParentID(spanID core.SpanID) *zkmodel.ID {
if spanID.IsValid() {
id := toZipkinID(spanID)
return &id
}
return nil
}
func toZipkinKind(kind trace.SpanKind) zkmodel.Kind {
switch kind {
case trace.SpanKindUnspecified:
return zkmodel.Undetermined
case trace.SpanKindInternal:
// The spec says we should set the kind to nil, but
// the model does not allow that.
return zkmodel.Undetermined
case trace.SpanKindServer:
return zkmodel.Server
case trace.SpanKindClient:
return zkmodel.Client
case trace.SpanKindProducer:
return zkmodel.Producer
case trace.SpanKindConsumer:
return zkmodel.Consumer
}
return zkmodel.Undetermined
}
func toZipkinAnnotations(events []export.Event) []zkmodel.Annotation {
if len(events) == 0 {
return nil
}
annotations := make([]zkmodel.Annotation, 0, len(events))
for _, event := range events {
value := event.Name
if len(event.Attributes) > 0 {
jsonString := attributesToJSONMapString(event.Attributes)
if jsonString != "" {
value = fmt.Sprintf("%s: %s", event.Name, jsonString)
}
}
annotations = append(annotations, zkmodel.Annotation{
Timestamp: event.Time,
Value: value,
})
}
return annotations
}
func attributesToJSONMapString(attributes []core.KeyValue) string {
m := make(map[string]interface{}, len(attributes))
for _, attribute := range attributes {
m[(string)(attribute.Key)] = attribute.Value.AsInterface()
}
// if an error happens, the result will be an empty string
jsonBytes, _ := json.Marshal(m)
return (string)(jsonBytes)
}
func toZipkinTags(data *export.SpanData) map[string]string {
// +2 for status code and for status message
m := make(map[string]string, len(data.Attributes)+2)
for _, kv := range data.Attributes {
m[(string)(kv.Key)] = kv.Value.Emit()
}
if v, ok := m["error"]; ok && v == "false" {
delete(m, "error")
}
m["ot.status_code"] = data.StatusCode.String()
m["ot.status_description"] = data.StatusMessage
return m
}