| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package zipkin // import "go.opentelemetry.io/otel/exporters/zipkin" |
| 5 |
|
| 6 |
import ( |
| 7 |
"encoding/binary" |
| 8 |
"encoding/json" |
| 9 |
"fmt" |
| 10 |
"net" |
| 11 |
"strconv" |
| 12 |
"strings" |
| 13 |
|
| 14 |
zkmodel "github.com/openzipkin/zipkin-go/model" |
| 15 |
|
| 16 |
"go.opentelemetry.io/otel/attribute" |
| 17 |
"go.opentelemetry.io/otel/codes" |
| 18 |
"go.opentelemetry.io/otel/sdk/resource" |
| 19 |
tracesdk "go.opentelemetry.io/otel/sdk/trace" |
| 20 |
semconv120 "go.opentelemetry.io/otel/semconv/v1.20.0" |
| 21 |
semconv121 "go.opentelemetry.io/otel/semconv/v1.21.0" |
| 22 |
semconv125 "go.opentelemetry.io/otel/semconv/v1.25.0" |
| 23 |
semconv138 "go.opentelemetry.io/otel/semconv/v1.38.0" |
| 24 |
semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 25 |
"go.opentelemetry.io/otel/trace" |
| 26 |
) |
| 27 |
|
| 28 |
const ( |
| 29 |
keyPeerHostname attribute.Key = "peer.hostname" |
| 30 |
keyPeerAddress attribute.Key = "peer.address" |
| 31 |
) |
| 32 |
|
| 33 |
var defaultServiceName string |
| 34 |
|
| 35 |
func init() { |
| 36 |
// fetch service.name from default resource for backup |
| 37 |
defaultResource := resource.Default() |
| 38 |
if value, exists := defaultResource.Set().Value(semconv.ServiceNameKey); exists { |
| 39 |
defaultServiceName = value.AsString() |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
// SpanModels converts OpenTelemetry spans into Zipkin model spans. |
| 44 |
// This is used for exporting to Zipkin compatible tracing services. |
| 45 |
func SpanModels(batch []tracesdk.ReadOnlySpan) []zkmodel.SpanModel { |
| 46 |
models := make([]zkmodel.SpanModel, 0, len(batch)) |
| 47 |
for _, data := range batch { |
| 48 |
models = append(models, toZipkinSpanModel(data)) |
| 49 |
} |
| 50 |
return models |
| 51 |
} |
| 52 |
|
| 53 |
func getServiceName(attrs []attribute.KeyValue) string { |
| 54 |
for _, kv := range attrs { |
| 55 |
if kv.Key == semconv.ServiceNameKey { |
| 56 |
return kv.Value.AsString() |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
return defaultServiceName |
| 61 |
} |
| 62 |
|
| 63 |
func toZipkinSpanModel(data tracesdk.ReadOnlySpan) zkmodel.SpanModel { |
| 64 |
return zkmodel.SpanModel{ |
| 65 |
SpanContext: toZipkinSpanContext(data), |
| 66 |
Name: data.Name(), |
| 67 |
Kind: toZipkinKind(data.SpanKind()), |
| 68 |
Timestamp: data.StartTime(), |
| 69 |
Duration: data.EndTime().Sub(data.StartTime()), |
| 70 |
Shared: false, |
| 71 |
LocalEndpoint: &zkmodel.Endpoint{ |
| 72 |
ServiceName: getServiceName(data.Resource().Attributes()), |
| 73 |
}, |
| 74 |
RemoteEndpoint: toZipkinRemoteEndpoint(data), |
| 75 |
Annotations: toZipkinAnnotations(data.Events()), |
| 76 |
Tags: toZipkinTags(data), |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
func toZipkinSpanContext(data tracesdk.ReadOnlySpan) zkmodel.SpanContext { |
| 81 |
return zkmodel.SpanContext{ |
| 82 |
TraceID: toZipkinTraceID(data.SpanContext().TraceID()), |
| 83 |
ID: toZipkinID(data.SpanContext().SpanID()), |
| 84 |
ParentID: toZipkinParentID(data.Parent().SpanID()), |
| 85 |
Debug: false, |
| 86 |
Sampled: nil, |
| 87 |
Err: nil, |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
func toZipkinTraceID(traceID trace.TraceID) zkmodel.TraceID { |
| 92 |
return zkmodel.TraceID{ |
| 93 |
High: binary.BigEndian.Uint64(traceID[:8]), |
| 94 |
Low: binary.BigEndian.Uint64(traceID[8:]), |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
func toZipkinID(spanID trace.SpanID) zkmodel.ID { |
| 99 |
return zkmodel.ID(binary.BigEndian.Uint64(spanID[:])) |
| 100 |
} |
| 101 |
|
| 102 |
func toZipkinParentID(spanID trace.SpanID) *zkmodel.ID { |
| 103 |
if spanID.IsValid() { |
| 104 |
id := toZipkinID(spanID) |
| 105 |
return &id |
| 106 |
} |
| 107 |
return nil |
| 108 |
} |
| 109 |
|
| 110 |
func toZipkinKind(kind trace.SpanKind) zkmodel.Kind { |
| 111 |
switch kind { |
| 112 |
case trace.SpanKindUnspecified: |
| 113 |
return zkmodel.Undetermined |
| 114 |
case trace.SpanKindInternal: |
| 115 |
// The spec says we should set the kind to nil, but |
| 116 |
// the model does not allow that. |
| 117 |
return zkmodel.Undetermined |
| 118 |
case trace.SpanKindServer: |
| 119 |
return zkmodel.Server |
| 120 |
case trace.SpanKindClient: |
| 121 |
return zkmodel.Client |
| 122 |
case trace.SpanKindProducer: |
| 123 |
return zkmodel.Producer |
| 124 |
case trace.SpanKindConsumer: |
| 125 |
return zkmodel.Consumer |
| 126 |
} |
| 127 |
return zkmodel.Undetermined |
| 128 |
} |
| 129 |
|
| 130 |
func toZipkinAnnotations(events []tracesdk.Event) []zkmodel.Annotation { |
| 131 |
if len(events) == 0 { |
| 132 |
return nil |
| 133 |
} |
| 134 |
annotations := make([]zkmodel.Annotation, 0, len(events)) |
| 135 |
for _, event := range events { |
| 136 |
value := event.Name |
| 137 |
if len(event.Attributes) > 0 { |
| 138 |
jsonString := attributesToJSONMapString(event.Attributes) |
| 139 |
if jsonString != "" { |
| 140 |
value = fmt.Sprintf("%s: %s", event.Name, jsonString) |
| 141 |
} |
| 142 |
} |
| 143 |
annotations = append(annotations, zkmodel.Annotation{ |
| 144 |
Timestamp: event.Time, |
| 145 |
Value: value, |
| 146 |
}) |
| 147 |
} |
| 148 |
return annotations |
| 149 |
} |
| 150 |
|
| 151 |
func attributesToJSONMapString(attributes []attribute.KeyValue) string { |
| 152 |
m := make(map[string]any, len(attributes)) |
| 153 |
for _, a := range attributes { |
| 154 |
m[string(a.Key)] = a.Value.AsInterface() |
| 155 |
} |
| 156 |
// if an error happens, the result will be an empty string |
| 157 |
jsonBytes, _ := json.Marshal(m) |
| 158 |
return string(jsonBytes) |
| 159 |
} |
| 160 |
|
| 161 |
// attributeToStringPair serializes each attribute to a string pair. |
| 162 |
func attributeToStringPair(kv attribute.KeyValue) (string, string) { |
| 163 |
switch kv.Value.Type() { |
| 164 |
// For slice attributes, serialize as JSON list string. |
| 165 |
case attribute.BOOLSLICE: |
| 166 |
data, _ := json.Marshal(kv.Value.AsBoolSlice()) |
| 167 |
return string(kv.Key), string(data) |
| 168 |
case attribute.INT64SLICE: |
| 169 |
data, _ := json.Marshal(kv.Value.AsInt64Slice()) |
| 170 |
return string(kv.Key), string(data) |
| 171 |
case attribute.FLOAT64SLICE: |
| 172 |
data, _ := json.Marshal(kv.Value.AsFloat64Slice()) |
| 173 |
return string(kv.Key), string(data) |
| 174 |
case attribute.STRINGSLICE: |
| 175 |
data, _ := json.Marshal(kv.Value.AsStringSlice()) |
| 176 |
return string(kv.Key), string(data) |
| 177 |
default: |
| 178 |
return string(kv.Key), kv.Value.Emit() |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
// extraZipkinTagsLen is a count of tags that may be added to every outgoing span. |
| 183 |
var extraZipkinTagsLen = len([]attribute.Key{ |
| 184 |
semconv.OTelStatusCodeKey, |
| 185 |
semconv.OTelScopeNameKey, |
| 186 |
semconv.OTelScopeVersionKey, |
| 187 |
}) |
| 188 |
|
| 189 |
func toZipkinTags(data tracesdk.ReadOnlySpan) map[string]string { |
| 190 |
attr := data.Attributes() |
| 191 |
resourceAttr := data.Resource().Attributes() |
| 192 |
m := make(map[string]string, len(attr)+len(resourceAttr)+extraZipkinTagsLen) |
| 193 |
for _, kv := range attr { |
| 194 |
k, v := attributeToStringPair(kv) |
| 195 |
m[k] = v |
| 196 |
} |
| 197 |
for _, kv := range resourceAttr { |
| 198 |
k, v := attributeToStringPair(kv) |
| 199 |
m[k] = v |
| 200 |
} |
| 201 |
|
| 202 |
if data.Status().Code != codes.Unset { |
| 203 |
// Zipkin expect to receive uppercase status values |
| 204 |
// rather than default capitalized ones. |
| 205 |
m[string(semconv.OTelStatusCodeKey)] = strings.ToUpper(data.Status().Code.String()) |
| 206 |
} |
| 207 |
|
| 208 |
if data.Status().Code == codes.Error { |
| 209 |
m["error"] = data.Status().Description |
| 210 |
} else { |
| 211 |
delete(m, "error") |
| 212 |
} |
| 213 |
|
| 214 |
if is := data.InstrumentationScope(); is.Name != "" { |
| 215 |
m[string(semconv.OTelScopeNameKey)] = is.Name |
| 216 |
if is.Version != "" { |
| 217 |
m[string(semconv.OTelScopeVersionKey)] = is.Version |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
if len(m) == 0 { |
| 222 |
return nil |
| 223 |
} |
| 224 |
|
| 225 |
return m |
| 226 |
} |
| 227 |
|
| 228 |
// Rank determines selection order for remote endpoint. See the specification |
| 229 |
// https://github.com/open-telemetry/opentelemetry-specification/blob/v1.28.0/specification/trace/sdk_exporters/zipkin.md#otlp---zipkin |
| 230 |
var remoteEndpointKeyRank = map[attribute.Key]int{ |
| 231 |
semconv138.PeerServiceKey: 1, |
| 232 |
semconv.ServerAddressKey: 2, |
| 233 |
semconv120.NetPeerNameKey: 3, |
| 234 |
semconv.NetworkPeerAddressKey: 4, |
| 235 |
semconv121.ServerSocketDomainKey: 5, |
| 236 |
semconv121.ServerSocketAddressKey: 6, |
| 237 |
semconv120.NetSockPeerNameKey: 7, |
| 238 |
semconv120.NetSockPeerAddrKey: 8, |
| 239 |
keyPeerHostname: 9, |
| 240 |
keyPeerAddress: 10, |
| 241 |
semconv125.DBNameKey: 11, |
| 242 |
} |
| 243 |
|
| 244 |
func toZipkinRemoteEndpoint(data tracesdk.ReadOnlySpan) *zkmodel.Endpoint { |
| 245 |
// Should be set only for client or producer kind |
| 246 |
if sk := data.SpanKind(); sk != trace.SpanKindClient && sk != trace.SpanKindProducer { |
| 247 |
return nil |
| 248 |
} |
| 249 |
|
| 250 |
attr := data.Attributes() |
| 251 |
var endpointAttr attribute.KeyValue |
| 252 |
for _, kv := range attr { |
| 253 |
rank, ok := remoteEndpointKeyRank[kv.Key] |
| 254 |
if !ok { |
| 255 |
continue |
| 256 |
} |
| 257 |
|
| 258 |
currentKeyRank, ok := remoteEndpointKeyRank[endpointAttr.Key] |
| 259 |
if ok && rank < currentKeyRank { |
| 260 |
endpointAttr = kv |
| 261 |
} else if !ok { |
| 262 |
endpointAttr = kv |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
if endpointAttr.Key == "" { |
| 267 |
return nil |
| 268 |
} |
| 269 |
|
| 270 |
v := endpointAttr.Value.AsString() |
| 271 |
|
| 272 |
switch endpointAttr.Key { |
| 273 |
case semconv.NetworkPeerAddressKey: |
| 274 |
return remoteEndpointPeerIPWithPort(v, semconv.NetworkPeerPortKey, attr) |
| 275 |
case semconv121.ServerSocketAddressKey: |
| 276 |
return remoteEndpointPeerIPWithPort(v, semconv121.ServerSocketPortKey, attr) |
| 277 |
case semconv120.NetSockPeerAddrKey: |
| 278 |
return remoteEndpointPeerIPWithPort(v, semconv121.NetSockPeerPortKey, attr) |
| 279 |
} |
| 280 |
|
| 281 |
return &zkmodel.Endpoint{ |
| 282 |
ServiceName: v, |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
func remoteEndpointPeerIPWithPort(peerIP string, portKey attribute.Key, attrs []attribute.KeyValue) *zkmodel.Endpoint { |
| 287 |
ip := net.ParseIP(peerIP) |
| 288 |
if ip == nil { |
| 289 |
return nil |
| 290 |
} |
| 291 |
|
| 292 |
endpoint := &zkmodel.Endpoint{} |
| 293 |
// Determine if IPv4 or IPv6 |
| 294 |
if ip.To4() != nil { |
| 295 |
endpoint.IPv4 = ip |
| 296 |
} else { |
| 297 |
endpoint.IPv6 = ip |
| 298 |
} |
| 299 |
|
| 300 |
for _, kv := range attrs { |
| 301 |
if kv.Key == portKey { |
| 302 |
port, _ := strconv.ParseUint(kv.Value.Emit(), 10, 16) |
| 303 |
endpoint.Port = uint16(port) // nolint: gosec // Bit size of 16 checked above. |
| 304 |
return endpoint |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
return endpoint |
| 309 |
} |