@@ -16,8 +16,8 @@ import (
16
16
semconv "go.opentelemetry.io/collector/semconv/v1.27.0"
17
17
)
18
18
19
- func insertAttrIfMissing (sattr pcommon.Map , key string , value any ) (err error ) {
20
- if _ , ok := sattr .Get (key ); ! ok {
19
+ func ( tp * tracesProcessor ) insertAttrIfMissingOrShouldOverride (sattr pcommon.Map , key string , value any ) (err error ) {
20
+ if _ , ok := sattr .Get (key ); tp . overrideIncomingDatadogFields || ! ok {
21
21
switch v := value .(type ) {
22
22
case string :
23
23
sattr .PutStr (key , v )
@@ -37,74 +37,73 @@ func (tp *tracesProcessor) processTraces(ctx context.Context, td ptrace.Traces)
37
37
otelres := rspan .Resource ()
38
38
rattr := otelres .Attributes ()
39
39
for j := 0 ; j < rspan .ScopeSpans ().Len (); j ++ {
40
- libspans := rspan .ScopeSpans ().At (j )
41
- for k := 0 ; k < libspans .Spans ().Len (); k ++ {
42
- otelspan := libspans .Spans ().At (k )
43
- sattr := otelspan .Attributes ()
44
- if tp .overrideIncomingDatadogFields {
45
- sattr .RemoveIf (func (k string , _ pcommon.Value ) bool {
46
- return strings .HasPrefix (k , "datadog." )
47
- })
48
- if ddHostname , ok := rattr .Get ("datadog.host.name" ); ok && ddHostname .AsString () == "" {
49
- rattr .Remove ("datadog.host.name" )
50
- }
51
- }
52
- if err = insertAttrIfMissing (sattr , "datadog.service" , traceutil .GetOTelService (otelres , true )); err != nil {
40
+ if service := traceutil .GetOTelService (otelres , true ); service != "" {
41
+ if err = tp .insertAttrIfMissingOrShouldOverride (rattr , "datadog.service" , service ); err != nil {
53
42
return ptrace.Traces {}, err
54
43
}
55
- if err = insertAttrIfMissing (sattr , "datadog.name" , traceutil .GetOTelOperationNameV2 (otelspan )); err != nil {
44
+ }
45
+ if serviceVersion , ok := otelres .Attributes ().Get (semconv .AttributeServiceVersion ); ok {
46
+ if err = tp .insertAttrIfMissingOrShouldOverride (rattr , "datadog.version" , serviceVersion .AsString ()); err != nil {
56
47
return ptrace.Traces {}, err
57
48
}
58
- if err = insertAttrIfMissing (sattr , "datadog.resource" , traceutil .GetOTelResourceV2 (otelspan , otelres )); err != nil {
49
+ }
50
+ if env := traceutil .GetOTelEnv (otelres ); env != "" {
51
+ if err = tp .insertAttrIfMissingOrShouldOverride (rattr , "datadog.env" , env ); err != nil {
59
52
return ptrace.Traces {}, err
60
53
}
61
- if err = insertAttrIfMissing (sattr , "datadog.type" , traceutil .GetOTelSpanType (otelspan , otelres )); err != nil {
54
+ }
55
+
56
+ if tp .overrideIncomingDatadogFields {
57
+ rattr .Remove ("datadog.host.name" )
58
+ }
59
+
60
+ if src , ok := tp .attrsTranslator .ResourceToSource (ctx , otelres , traceutil .SignalTypeSet , nil ); ok && src .Kind == source .HostnameKind {
61
+ if err = tp .insertAttrIfMissingOrShouldOverride (rattr , "datadog.host.name" , src .Identifier ); err != nil {
62
62
return ptrace.Traces {}, err
63
63
}
64
- if src , ok := tp .attrsTranslator .ResourceToSource (ctx , otelres , traceutil .SignalTypeSet , nil ); ok && src .Kind == source .HostnameKind {
65
- if err = insertAttrIfMissing (otelres .Attributes (), "datadog.host.name" , src .Identifier ); err != nil {
66
- return ptrace.Traces {}, err
67
- }
68
- }
64
+ }
69
65
70
- spanKind := otelspan .Kind ()
71
- if err = insertAttrIfMissing (sattr , "datadog.span.kind" , traceutil .OTelSpanKindName (spanKind )); err != nil {
66
+ libspans := rspan .ScopeSpans ().At (j )
67
+ for k := 0 ; k < libspans .Spans ().Len (); k ++ {
68
+ otelspan := libspans .Spans ().At (k )
69
+ sattr := otelspan .Attributes ()
70
+
71
+ if err = tp .insertAttrIfMissingOrShouldOverride (sattr , "datadog.name" , traceutil .GetOTelOperationNameV2 (otelspan )); err != nil {
72
72
return ptrace.Traces {}, err
73
73
}
74
- if env := traceutil .GetOTelEnv (otelres ); env != "" {
75
- if err = insertAttrIfMissing (sattr , "datadog.env" , env ); err != nil {
76
- return ptrace.Traces {}, err
77
- }
74
+ if err = tp .insertAttrIfMissingOrShouldOverride (sattr , "datadog.resource" , traceutil .GetOTelResourceV2 (otelspan , otelres )); err != nil {
75
+ return ptrace.Traces {}, err
78
76
}
79
- if serviceVersion , ok := otelres .Attributes ().Get (semconv .AttributeServiceVersion ); ok {
80
- if err = insertAttrIfMissing (sattr , "datadog.version" , serviceVersion .AsString ()); err != nil {
81
- return ptrace.Traces {}, err
82
- }
77
+ if err = tp .insertAttrIfMissingOrShouldOverride (sattr , "datadog.type" , traceutil .GetOTelSpanType (otelspan , otelres )); err != nil {
78
+ return ptrace.Traces {}, err
79
+ }
80
+ spanKind := otelspan .Kind ()
81
+ if err = tp .insertAttrIfMissingOrShouldOverride (sattr , "datadog.span.kind" , traceutil .OTelSpanKindName (spanKind )); err != nil {
82
+ return ptrace.Traces {}, err
83
83
}
84
-
85
84
metaMap := make (map [string ]string )
86
85
code := traceutil .GetOTelStatusCode (otelspan )
87
86
if code != 0 {
88
- if err = insertAttrIfMissing (sattr , "datadog.http_status_code" , fmt .Sprintf ("%d" , code )); err != nil {
87
+ if err = tp . insertAttrIfMissingOrShouldOverride (sattr , "datadog.http_status_code" , fmt .Sprintf ("%d" , code )); err != nil {
89
88
return ptrace.Traces {}, err
90
89
}
91
90
}
92
91
ddError := int64 (status2Error (otelspan .Status (), otelspan .Events (), metaMap ))
93
- if err = insertAttrIfMissing (sattr , "datadog.error" , ddError ); err != nil {
92
+ if err = tp . insertAttrIfMissingOrShouldOverride (sattr , "datadog.error" , ddError ); err != nil {
94
93
return ptrace.Traces {}, err
95
94
}
96
95
if metaMap ["error.msg" ] != "" {
97
- if err = insertAttrIfMissing (sattr , "datadog.error.msg" , metaMap ["error.msg" ]); err != nil {
96
+ if err = tp . insertAttrIfMissingOrShouldOverride (sattr , "datadog.error.msg" , metaMap ["error.msg" ]); err != nil {
98
97
return ptrace.Traces {}, err
99
98
}
100
99
}
101
100
if metaMap ["error.type" ] != "" {
102
- if err = insertAttrIfMissing (sattr , "datadog.error.type" , metaMap ["error.type" ]); err != nil {
101
+ if err = tp . insertAttrIfMissingOrShouldOverride (sattr , "datadog.error.type" , metaMap ["error.type" ]); err != nil {
103
102
return ptrace.Traces {}, err
104
103
}
105
104
}
106
105
if metaMap ["error.stack" ] != "" {
107
- if err = insertAttrIfMissing (sattr , "datadog.error.stack" , metaMap ["error.stack" ]); err != nil {
106
+ if err = tp . insertAttrIfMissingOrShouldOverride (sattr , "datadog.error.stack" , metaMap ["error.stack" ]); err != nil {
108
107
return ptrace.Traces {}, err
109
108
}
110
109
}
0 commit comments