2021-06-12 20:39:22 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package zipkin // import "go.opentelemetry.io/otel/exporters/zipkin"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
2022-10-28 06:22:18 +02:00
|
|
|
"strings"
|
2021-06-12 20:39:22 +02:00
|
|
|
|
2021-11-13 18:35:04 +02:00
|
|
|
zkmodel "github.com/openzipkin/zipkin-go/model"
|
|
|
|
|
2021-06-12 20:39:22 +02:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/codes"
|
|
|
|
"go.opentelemetry.io/otel/sdk/resource"
|
|
|
|
tracesdk "go.opentelemetry.io/otel/sdk/trace"
|
2023-08-07 18:30:40 +02:00
|
|
|
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
|
2021-11-13 18:35:04 +02:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2021-06-12 20:39:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
keyInstrumentationLibraryName = "otel.library.name"
|
|
|
|
keyInstrumentationLibraryVersion = "otel.library.version"
|
|
|
|
|
|
|
|
keyPeerHostname attribute.Key = "peer.hostname"
|
|
|
|
keyPeerAddress attribute.Key = "peer.address"
|
|
|
|
)
|
|
|
|
|
|
|
|
var defaultServiceName string
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// fetch service.name from default resource for backup
|
|
|
|
defaultResource := resource.Default()
|
|
|
|
if value, exists := defaultResource.Set().Value(semconv.ServiceNameKey); exists {
|
|
|
|
defaultServiceName = value.AsString()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-24 00:30:15 +02:00
|
|
|
// SpanModels converts OpenTelemetry spans into Zipkin model spans.
|
|
|
|
// This is used for exporting to Zipkin compatible tracing services.
|
|
|
|
func SpanModels(batch []tracesdk.ReadOnlySpan) []zkmodel.SpanModel {
|
2021-06-12 20:39:22 +02:00
|
|
|
models := make([]zkmodel.SpanModel, 0, len(batch))
|
|
|
|
for _, data := range batch {
|
|
|
|
models = append(models, toZipkinSpanModel(data))
|
|
|
|
}
|
|
|
|
return models
|
|
|
|
}
|
|
|
|
|
|
|
|
func getServiceName(attrs []attribute.KeyValue) string {
|
|
|
|
for _, kv := range attrs {
|
|
|
|
if kv.Key == semconv.ServiceNameKey {
|
|
|
|
return kv.Value.AsString()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultServiceName
|
|
|
|
}
|
|
|
|
|
|
|
|
func toZipkinSpanModel(data tracesdk.ReadOnlySpan) 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: &zkmodel.Endpoint{
|
|
|
|
ServiceName: getServiceName(data.Resource().Attributes()),
|
|
|
|
},
|
|
|
|
RemoteEndpoint: toZipkinRemoteEndpoint(data),
|
|
|
|
Annotations: toZipkinAnnotations(data.Events()),
|
|
|
|
Tags: toZipkinTags(data),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func toZipkinSpanContext(data tracesdk.ReadOnlySpan) zkmodel.SpanContext {
|
|
|
|
return zkmodel.SpanContext{
|
|
|
|
TraceID: toZipkinTraceID(data.SpanContext().TraceID()),
|
|
|
|
ID: toZipkinID(data.SpanContext().SpanID()),
|
|
|
|
ParentID: toZipkinParentID(data.Parent().SpanID()),
|
|
|
|
Debug: false,
|
|
|
|
Sampled: nil,
|
|
|
|
Err: nil,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func toZipkinTraceID(traceID trace.TraceID) zkmodel.TraceID {
|
|
|
|
return zkmodel.TraceID{
|
|
|
|
High: binary.BigEndian.Uint64(traceID[:8]),
|
|
|
|
Low: binary.BigEndian.Uint64(traceID[8:]),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func toZipkinID(spanID trace.SpanID) zkmodel.ID {
|
|
|
|
return zkmodel.ID(binary.BigEndian.Uint64(spanID[:]))
|
|
|
|
}
|
|
|
|
|
|
|
|
func toZipkinParentID(spanID trace.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 []tracesdk.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 []attribute.KeyValue) string {
|
|
|
|
m := make(map[string]interface{}, len(attributes))
|
2022-05-19 22:15:07 +02:00
|
|
|
for _, a := range attributes {
|
|
|
|
m[(string)(a.Key)] = a.Value.AsInterface()
|
2021-06-12 20:39:22 +02:00
|
|
|
}
|
|
|
|
// if an error happens, the result will be an empty string
|
|
|
|
jsonBytes, _ := json.Marshal(m)
|
|
|
|
return (string)(jsonBytes)
|
|
|
|
}
|
|
|
|
|
2022-04-25 22:22:49 +02:00
|
|
|
// attributeToStringPair serializes each attribute to a string pair.
|
2022-02-09 20:26:45 +02:00
|
|
|
func attributeToStringPair(kv attribute.KeyValue) (string, string) {
|
|
|
|
switch kv.Value.Type() {
|
|
|
|
// For slice attributes, serialize as JSON list string.
|
|
|
|
case attribute.BOOLSLICE:
|
2022-05-19 22:15:07 +02:00
|
|
|
data, _ := json.Marshal(kv.Value.AsBoolSlice())
|
|
|
|
return (string)(kv.Key), (string)(data)
|
2022-02-09 20:26:45 +02:00
|
|
|
case attribute.INT64SLICE:
|
2022-05-19 22:15:07 +02:00
|
|
|
data, _ := json.Marshal(kv.Value.AsInt64Slice())
|
|
|
|
return (string)(kv.Key), (string)(data)
|
2022-02-09 20:26:45 +02:00
|
|
|
case attribute.FLOAT64SLICE:
|
2022-05-19 22:15:07 +02:00
|
|
|
data, _ := json.Marshal(kv.Value.AsFloat64Slice())
|
|
|
|
return (string)(kv.Key), (string)(data)
|
2022-02-09 20:26:45 +02:00
|
|
|
case attribute.STRINGSLICE:
|
2022-05-19 22:15:07 +02:00
|
|
|
data, _ := json.Marshal(kv.Value.AsStringSlice())
|
|
|
|
return (string)(kv.Key), (string)(data)
|
2022-02-09 20:26:45 +02:00
|
|
|
default:
|
|
|
|
return (string)(kv.Key), kv.Value.Emit()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 22:22:49 +02:00
|
|
|
// extraZipkinTags are those that may be added to every outgoing span.
|
2021-06-12 20:39:22 +02:00
|
|
|
var extraZipkinTags = []string{
|
|
|
|
"otel.status_code",
|
|
|
|
keyInstrumentationLibraryName,
|
|
|
|
keyInstrumentationLibraryVersion,
|
|
|
|
}
|
|
|
|
|
|
|
|
func toZipkinTags(data tracesdk.ReadOnlySpan) map[string]string {
|
|
|
|
attr := data.Attributes()
|
2022-02-09 20:26:45 +02:00
|
|
|
resourceAttr := data.Resource().Attributes()
|
|
|
|
m := make(map[string]string, len(attr)+len(resourceAttr)+len(extraZipkinTags))
|
2021-06-12 20:39:22 +02:00
|
|
|
for _, kv := range attr {
|
2022-02-09 20:26:45 +02:00
|
|
|
k, v := attributeToStringPair(kv)
|
|
|
|
m[k] = v
|
|
|
|
}
|
|
|
|
for _, kv := range resourceAttr {
|
|
|
|
k, v := attributeToStringPair(kv)
|
|
|
|
m[k] = v
|
2021-06-12 20:39:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if data.Status().Code != codes.Unset {
|
2022-10-28 06:22:18 +02:00
|
|
|
// Zipkin expect to receive uppercase status values
|
|
|
|
// rather than default capitalized ones.
|
|
|
|
m["otel.status_code"] = strings.ToUpper(data.Status().Code.String())
|
2021-06-12 20:39:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if data.Status().Code == codes.Error {
|
|
|
|
m["error"] = data.Status().Description
|
|
|
|
} else {
|
|
|
|
delete(m, "error")
|
|
|
|
}
|
|
|
|
|
2022-07-06 20:55:46 +02:00
|
|
|
if is := data.InstrumentationScope(); is.Name != "" {
|
|
|
|
m[keyInstrumentationLibraryName] = is.Name
|
|
|
|
if is.Version != "" {
|
|
|
|
m[keyInstrumentationLibraryVersion] = is.Version
|
2021-06-12 20:39:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(m) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rank determines selection order for remote endpoint. See the specification
|
2023-05-09 17:45:39 +02:00
|
|
|
// https://github.com/open-telemetry/opentelemetry-specification/blob/v1.20.0/specification/trace/sdk_exporters/zipkin.md#otlp---zipkin
|
2021-06-12 20:39:22 +02:00
|
|
|
var remoteEndpointKeyRank = map[attribute.Key]int{
|
2023-01-10 20:57:28 +02:00
|
|
|
semconv.PeerServiceKey: 0,
|
|
|
|
semconv.NetPeerNameKey: 1,
|
|
|
|
semconv.NetSockPeerNameKey: 2,
|
|
|
|
semconv.NetSockPeerAddrKey: 3,
|
|
|
|
keyPeerHostname: 4,
|
|
|
|
keyPeerAddress: 5,
|
|
|
|
semconv.DBNameKey: 6,
|
2021-06-12 20:39:22 +02:00
|
|
|
}
|
|
|
|
|
2022-05-19 22:15:07 +02:00
|
|
|
func toZipkinRemoteEndpoint(data tracesdk.ReadOnlySpan) *zkmodel.Endpoint {
|
2021-06-12 20:39:22 +02:00
|
|
|
// Should be set only for client or producer kind
|
|
|
|
if sk := data.SpanKind(); sk != trace.SpanKindClient && sk != trace.SpanKindProducer {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
attr := data.Attributes()
|
|
|
|
var endpointAttr attribute.KeyValue
|
|
|
|
for _, kv := range attr {
|
|
|
|
rank, ok := remoteEndpointKeyRank[kv.Key]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
currentKeyRank, ok := remoteEndpointKeyRank[endpointAttr.Key]
|
|
|
|
if ok && rank < currentKeyRank {
|
|
|
|
endpointAttr = kv
|
|
|
|
} else if !ok {
|
|
|
|
endpointAttr = kv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if endpointAttr.Key == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-10 20:57:28 +02:00
|
|
|
if endpointAttr.Key != semconv.NetSockPeerAddrKey &&
|
2021-06-12 20:39:22 +02:00
|
|
|
endpointAttr.Value.Type() == attribute.STRING {
|
|
|
|
return &zkmodel.Endpoint{
|
|
|
|
ServiceName: endpointAttr.Value.AsString(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return remoteEndpointPeerIPWithPort(endpointAttr.Value.AsString(), attr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handles `net.peer.ip` remote endpoint separately (should include `net.peer.ip`
|
|
|
|
// as well, if available).
|
|
|
|
func remoteEndpointPeerIPWithPort(peerIP string, attrs []attribute.KeyValue) *zkmodel.Endpoint {
|
|
|
|
ip := net.ParseIP(peerIP)
|
|
|
|
if ip == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoint := &zkmodel.Endpoint{}
|
|
|
|
// Determine if IPv4 or IPv6
|
|
|
|
if ip.To4() != nil {
|
|
|
|
endpoint.IPv4 = ip
|
|
|
|
} else {
|
|
|
|
endpoint.IPv6 = ip
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, kv := range attrs {
|
2023-01-10 20:57:28 +02:00
|
|
|
if kv.Key == semconv.NetSockPeerPortKey {
|
2021-06-12 20:39:22 +02:00
|
|
|
port, _ := strconv.ParseUint(kv.Value.Emit(), 10, 16)
|
|
|
|
endpoint.Port = uint16(port)
|
|
|
|
return endpoint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return endpoint
|
|
|
|
}
|