-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathotel_util.go
483 lines (438 loc) · 16 KB
/
otel_util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package traceutil
import (
"context"
"encoding/binary"
"strings"
"github.com/DataDog/datadog-agent/pkg/trace/log"
"github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/attributes"
"github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/attributes/source"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
semconv117 "go.opentelemetry.io/collector/semconv/v1.17.0"
semconv "go.opentelemetry.io/collector/semconv/v1.6.1"
"go.opentelemetry.io/otel/attribute"
)
// Util functions for converting OTel semantics to DD semantics.
var (
// SignalTypeSet is the OTel attribute set for traces.
SignalTypeSet = attribute.NewSet(attribute.String("signal", "traces"))
)
const (
// TagStatusCode is the tag key for http status code.
TagStatusCode = "http.status_code"
)
// IndexOTelSpans iterates over the input OTel spans and returns 3 maps:
// OTel spans indexed by span ID, OTel resources indexed by span ID, OTel instrumentation scopes indexed by span ID.
// Skips spans with invalid trace ID or span ID. If there are multiple spans with the same (non-zero) span ID, the last one wins.
func IndexOTelSpans(traces ptrace.Traces) (map[pcommon.SpanID]ptrace.Span, map[pcommon.SpanID]pcommon.Resource, map[pcommon.SpanID]pcommon.InstrumentationScope) {
spanByID := make(map[pcommon.SpanID]ptrace.Span)
resByID := make(map[pcommon.SpanID]pcommon.Resource)
scopeByID := make(map[pcommon.SpanID]pcommon.InstrumentationScope)
rspanss := traces.ResourceSpans()
for i := 0; i < rspanss.Len(); i++ {
rspans := rspanss.At(i)
res := rspans.Resource()
for j := 0; j < rspans.ScopeSpans().Len(); j++ {
libspans := rspans.ScopeSpans().At(j)
for k := 0; k < libspans.Spans().Len(); k++ {
span := libspans.Spans().At(k)
if span.TraceID().IsEmpty() || span.SpanID().IsEmpty() {
continue
}
spanByID[span.SpanID()] = span
resByID[span.SpanID()] = res
scopeByID[span.SpanID()] = libspans.Scope()
}
}
}
return spanByID, resByID, scopeByID
}
// GetTopLevelOTelSpans returns the span IDs of the top level OTel spans.
func GetTopLevelOTelSpans(spanByID map[pcommon.SpanID]ptrace.Span, resByID map[pcommon.SpanID]pcommon.Resource, topLevelByKind bool) map[pcommon.SpanID]struct{} {
topLevelSpans := make(map[pcommon.SpanID]struct{})
for spanID, span := range spanByID {
if span.ParentSpanID().IsEmpty() {
// case 1: root span
topLevelSpans[spanID] = struct{}{}
continue
}
if topLevelByKind {
// New behavior for computing top level OTel spans, see computeTopLevelAndMeasured in pkg/trace/api/otlp.go
spanKind := span.Kind()
if spanKind == ptrace.SpanKindServer || spanKind == ptrace.SpanKindConsumer {
// span is a server-side span, mark as top level
topLevelSpans[spanID] = struct{}{}
}
continue
}
// Otherwise, fall back to old behavior in ComputeTopLevel
parentSpan, ok := spanByID[span.ParentSpanID()]
if !ok {
// case 2: parent span not in the same chunk, presumably it belongs to another service
topLevelSpans[spanID] = struct{}{}
continue
}
svc := GetOTelService(span, resByID[spanID], true)
parentSvc := GetOTelService(parentSpan, resByID[parentSpan.SpanID()], true)
if svc != parentSvc {
// case 3: parent is not in the same service
topLevelSpans[spanID] = struct{}{}
}
}
return topLevelSpans
}
// GetOTelAttrVal returns the matched value as a string in the input map with the given keys.
// If there are multiple keys present, the first matched one is returned.
// If normalize is true, normalize the return value with NormalizeTagValue.
func GetOTelAttrVal(attrs pcommon.Map, normalize bool, keys ...string) string {
val := ""
for _, key := range keys {
attrval, exists := attrs.Get(key)
if exists {
val = attrval.AsString()
break
}
}
if normalize {
val = NormalizeTagValue(val)
}
return val
}
// GetOTelAttrValInResAndSpanAttrs returns the matched value as a string in the OTel resource attributes and span attributes with the given keys.
// If there are multiple keys present, the first matched one is returned.
// If the key is present in both resource attributes and span attributes, resource attributes take precedence.
// If normalize is true, normalize the return value with NormalizeTagValue.
func GetOTelAttrValInResAndSpanAttrs(span ptrace.Span, res pcommon.Resource, normalize bool, keys ...string) string {
if val := GetOTelAttrVal(res.Attributes(), normalize, keys...); val != "" {
return val
}
return GetOTelAttrVal(span.Attributes(), normalize, keys...)
}
// GetOTelSpanType returns the DD span type based on OTel span kind and attributes.
func GetOTelSpanType(span ptrace.Span, res pcommon.Resource) string {
typ := GetOTelAttrValInResAndSpanAttrs(span, res, false, "span.type")
if typ != "" {
return typ
}
switch span.Kind() {
case ptrace.SpanKindServer:
typ = "web"
case ptrace.SpanKindClient:
db := GetOTelAttrValInResAndSpanAttrs(span, res, true, semconv.AttributeDBSystem)
if db == "redis" || db == "memcached" {
typ = "cache"
} else if db != "" {
typ = "db"
} else {
typ = "http"
}
default:
typ = "custom"
}
return typ
}
// GetOTelService returns the DD service name based on OTel span and resource attributes.
func GetOTelService(span ptrace.Span, res pcommon.Resource, normalize bool) string {
// No need to normalize with NormalizeTagValue since we will do NormalizeService later
svc := GetOTelAttrValInResAndSpanAttrs(span, res, false, semconv.AttributeServiceName)
if svc == "" {
svc = "otlpresourcenoservicename"
}
if normalize {
newsvc, err := NormalizeService(svc, "")
switch err {
case ErrTooLong:
log.Debugf("Fixing malformed trace. Service is too long (reason:service_truncate), truncating span.service to length=%d: %s", MaxServiceLen, svc)
case ErrInvalid:
log.Debugf("Fixing malformed trace. Service is invalid (reason:service_invalid), replacing invalid span.service=%s with fallback span.service=%s", svc, newsvc)
}
svc = newsvc
}
return svc
}
// GetOTelResourceV1 returns the DD resource name based on OTel span and resource attributes.
func GetOTelResourceV1(span ptrace.Span, res pcommon.Resource) (resName string) {
resName = GetOTelAttrValInResAndSpanAttrs(span, res, false, "resource.name")
if resName == "" {
if m := GetOTelAttrValInResAndSpanAttrs(span, res, false, "http.request.method", semconv.AttributeHTTPMethod); m != "" {
// use the HTTP method + route (if available)
resName = m
if route := GetOTelAttrValInResAndSpanAttrs(span, res, false, semconv.AttributeHTTPRoute); route != "" {
resName = resName + " " + route
}
} else if m := GetOTelAttrValInResAndSpanAttrs(span, res, false, semconv.AttributeMessagingOperation); m != "" {
resName = m
// use the messaging operation
if dest := GetOTelAttrValInResAndSpanAttrs(span, res, false, semconv.AttributeMessagingDestination, semconv117.AttributeMessagingDestinationName); dest != "" {
resName = resName + " " + dest
}
} else if m := GetOTelAttrValInResAndSpanAttrs(span, res, false, semconv.AttributeRPCMethod); m != "" {
resName = m
// use the RPC method
if svc := GetOTelAttrValInResAndSpanAttrs(span, res, false, semconv.AttributeRPCService); m != "" {
// ...and service if available
resName = resName + " " + svc
}
} else {
resName = span.Name()
}
}
if len(resName) > MaxResourceLen {
resName = resName[:MaxResourceLen]
}
return
}
// GetOTelResourceV2 returns the DD resource name based on OTel span and resource attributes.
func GetOTelResourceV2(span ptrace.Span, res pcommon.Resource) (resName string) {
defer func() {
if len(resName) > MaxResourceLen {
resName = resName[:MaxResourceLen]
}
}()
if m := GetOTelAttrValInResAndSpanAttrs(span, res, false, "resource.name"); m != "" {
resName = m
return
}
if m := GetOTelAttrValInResAndSpanAttrs(span, res, false, "http.request.method", semconv.AttributeHTTPMethod); m != "" {
if m == "_OTHER" {
m = "HTTP"
}
// use the HTTP method + route (if available)
resName = m
if span.Kind() == ptrace.SpanKindServer {
if route := GetOTelAttrValInResAndSpanAttrs(span, res, false, semconv.AttributeHTTPRoute); route != "" {
resName = resName + " " + route
}
}
return
}
if m := GetOTelAttrValInResAndSpanAttrs(span, res, false, semconv.AttributeMessagingOperation); m != "" {
resName = m
// use the messaging operation
if dest := GetOTelAttrValInResAndSpanAttrs(span, res, false, semconv.AttributeMessagingDestination, semconv117.AttributeMessagingDestinationName); dest != "" {
resName = resName + " " + dest
}
return
}
if m := GetOTelAttrValInResAndSpanAttrs(span, res, false, semconv.AttributeRPCMethod); m != "" {
resName = m
// use the RPC method
if svc := GetOTelAttrValInResAndSpanAttrs(span, res, false, semconv.AttributeRPCService); m != "" {
// ...and service if available
resName = resName + " " + svc
}
return
}
resName = span.Name()
return
}
// GetOTelOperationNameV2 returns the DD operation name based on OTel span and resource attributes and given configs.
func GetOTelOperationNameV2(
span ptrace.Span,
) string {
if operationName := GetOTelAttrVal(span.Attributes(), false, "operation.name"); operationName != "" {
return operationName
}
isClient := span.Kind() == ptrace.SpanKindClient
isServer := span.Kind() == ptrace.SpanKindServer
// http
if method := GetOTelAttrVal(span.Attributes(), false, "http.request.method", semconv.AttributeHTTPMethod); method != "" {
if isServer {
return "http.server.request"
}
if isClient {
return "http.client.request"
}
}
// database
if v := GetOTelAttrVal(span.Attributes(), true, semconv.AttributeDBSystem); v != "" && isClient {
return v + ".query"
}
// messaging
system := GetOTelAttrVal(span.Attributes(), true, semconv.AttributeMessagingSystem)
op := GetOTelAttrVal(span.Attributes(), true, semconv.AttributeMessagingOperation)
if system != "" && op != "" {
switch span.Kind() {
case ptrace.SpanKindClient, ptrace.SpanKindServer, ptrace.SpanKindConsumer, ptrace.SpanKindProducer:
return system + "." + op
}
}
// RPC & AWS
rpcValue := GetOTelAttrVal(span.Attributes(), true, semconv.AttributeRPCSystem)
isRPC := rpcValue != ""
isAws := isRPC && (rpcValue == "aws-api")
// AWS client
if isAws && isClient {
if service := GetOTelAttrVal(span.Attributes(), true, semconv.AttributeRPCService); service != "" {
return "aws." + service + ".request"
}
return "aws.client.request"
}
// RPC client
if isRPC && isClient {
return rpcValue + ".client.request"
}
// RPC server
if isRPC && isServer {
return rpcValue + ".server.request"
}
// FAAS client
provider := GetOTelAttrVal(span.Attributes(), true, semconv.AttributeFaaSInvokedProvider)
invokedName := GetOTelAttrVal(span.Attributes(), true, semconv.AttributeFaaSInvokedName)
if provider != "" && invokedName != "" && isClient {
return provider + "." + invokedName + ".invoke"
}
// FAAS server
trigger := GetOTelAttrVal(span.Attributes(), true, semconv.AttributeFaaSTrigger)
if trigger != "" && isServer {
return trigger + ".invoke"
}
// GraphQL
if GetOTelAttrVal(span.Attributes(), true, "graphql.operation.type") != "" {
return "graphql.server.request"
}
// if nothing matches, checking for generic http server/client
protocol := GetOTelAttrVal(span.Attributes(), true, "network.protocol.name")
if isServer {
if protocol != "" {
return protocol + ".server.request"
}
return "server.request"
} else if isClient {
if protocol != "" {
return protocol + ".client.request"
}
return "client.request"
}
if span.Kind() != ptrace.SpanKindUnspecified {
return span.Kind().String()
}
return ptrace.SpanKindInternal.String()
}
// GetOTelOperationNameV1 returns the DD operation name based on OTel span and resource attributes and given configs.
func GetOTelOperationNameV1(
span ptrace.Span,
res pcommon.Resource,
lib pcommon.InstrumentationScope,
spanNameAsResourceName bool,
spanNameRemappings map[string]string,
normalize bool) string {
// No need to normalize with NormalizeTagValue since we will do NormalizeName later
name := GetOTelAttrValInResAndSpanAttrs(span, res, false, "operation.name")
if name == "" {
if spanNameAsResourceName {
name = span.Name()
} else {
name = strings.ToLower(span.Kind().String())
if lib.Name() != "" {
name = lib.Name() + "." + name
} else {
name = "opentelemetry." + name
}
}
}
if v, ok := spanNameRemappings[name]; ok {
name = v
}
if normalize {
normalizeName, err := NormalizeName(name)
switch err {
case ErrEmpty:
log.Debugf("Fixing malformed trace. Name is empty (reason:span_name_empty), setting span.name=%s", normalizeName)
case ErrTooLong:
log.Debugf("Fixing malformed trace. Name is too long (reason:span_name_truncate), truncating span.name to length=%d", MaxServiceLen)
case ErrInvalid:
log.Debugf("Fixing malformed trace. Name is invalid (reason:span_name_invalid), setting span.name=%s", normalizeName)
}
name = normalizeName
}
return name
}
// GetOtelSource returns the source based on OTel span and resource attributes.
func GetOtelSource(span ptrace.Span, res pcommon.Resource, tr *attributes.Translator) (source.Source, bool) {
ctx := context.Background()
src, srcok := tr.ResourceToSource(ctx, res, SignalTypeSet)
if !srcok {
if v := GetOTelAttrValInResAndSpanAttrs(span, res, false, "_dd.hostname"); v != "" {
src = source.Source{Kind: source.HostnameKind, Identifier: v}
srcok = true
}
}
return src, srcok
}
// GetOTelHostname returns the DD hostname based on OTel span and resource attributes.
func GetOTelHostname(span ptrace.Span, res pcommon.Resource, tr *attributes.Translator, fallbackHost string) string {
src, srcok := GetOtelSource(span, res, tr)
if srcok {
switch src.Kind {
case source.HostnameKind:
return src.Identifier
default:
// We are not on a hostname (serverless), hence the hostname is empty
return ""
}
} else {
// fallback hostname from Agent conf.Hostname
return fallbackHost
}
}
// GetOTelStatusCode returns the DD status code of the OTel span.
func GetOTelStatusCode(span ptrace.Span) uint32 {
if code, ok := span.Attributes().Get("http.response.status_code"); ok {
return uint32(code.Int())
}
if code, ok := span.Attributes().Get(semconv.AttributeHTTPStatusCode); ok {
return uint32(code.Int())
}
return 0
}
// GetOTelContainerTags returns a list of DD container tags in the OTel resource attributes.
// Tags are always normalized.
func GetOTelContainerTags(rattrs pcommon.Map, tagKeys []string) []string {
var containerTags []string
containerTagsMap := attributes.ContainerTagsFromResourceAttributes(rattrs)
for _, key := range tagKeys {
if mappedKey, ok := attributes.ContainerMappings[key]; ok {
// If the key has a mapping in ContainerMappings, use the mapped key
if val, ok := containerTagsMap[mappedKey]; ok {
t := NormalizeTag(mappedKey + ":" + val)
containerTags = append(containerTags, t)
}
} else {
// Otherwise populate as additional container tags
if val := GetOTelAttrVal(rattrs, false, key); val != "" {
t := NormalizeTag(key + ":" + val)
containerTags = append(containerTags, t)
}
}
}
return containerTags
}
// OTelTraceIDToUint64 converts an OTel trace ID to an uint64
func OTelTraceIDToUint64(b [16]byte) uint64 {
return binary.BigEndian.Uint64(b[len(b)-8:])
}
// OTelSpanIDToUint64 converts an OTel span ID to an uint64
func OTelSpanIDToUint64(b [8]byte) uint64 {
return binary.BigEndian.Uint64(b[:])
}
var spanKindNames = map[ptrace.SpanKind]string{
ptrace.SpanKindUnspecified: "unspecified",
ptrace.SpanKindInternal: "internal",
ptrace.SpanKindServer: "server",
ptrace.SpanKindClient: "client",
ptrace.SpanKindProducer: "producer",
ptrace.SpanKindConsumer: "consumer",
}
// OTelSpanKindName converts the given SpanKind to a valid Datadog span kind name.
func OTelSpanKindName(k ptrace.SpanKind) string {
name, ok := spanKindNames[k]
if !ok {
return "unspecified"
}
return name
}