Skip to content

[processor/datadogsemantics] Make defaults explicit and add test #39596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions processor/datadogsemanticsprocessor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,37 @@ func (tp *tracesProcessor) processTraces(ctx context.Context, td ptrace.Traces)
otelres := rspan.Resource()
rattr := otelres.Attributes()
for j := 0; j < rspan.ScopeSpans().Len(); j++ {
if service := traceutil.GetOTelService(otelres, true); service != "" {
if err = tp.insertAttrIfMissingOrShouldOverride(rattr, "datadog.service", service); err != nil {
return ptrace.Traces{}, err
}
// Note: default value from GetOTelService is "otlpresourcenoservicename"
if err = tp.insertAttrIfMissingOrShouldOverride(rattr, "datadog.service", traceutil.GetOTelService(otelres, true)); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

am I understanding correctly that this change, if otelres is blank, then traceutil.GetOTelService will return "otlpresourcenoservicename" and that's what datadog.service will be set to?
https://github.com/DataDog/datadog-agent/blob/0d44e61fa623ef2572ba8d7fbe30e2632540d8aa/pkg/trace/traceutil/otel_util.go#L276
Worth adding a link to the function or noting the default setting in the comments here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alternatively, you could always make that an exportable constant in pkg/trace/traceutil and set service equal to the value before calling GetOTelService.

I just think we should try to have the pattern for each attribute follow a similar pattern to the maximum extent practical, so the code is easy to understand later

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I added a comment explaining the default

return ptrace.Traces{}, err
}
if serviceVersion, ok := otelres.Attributes().Get(semconv.AttributeServiceVersion); ok {
if err = tp.insertAttrIfMissingOrShouldOverride(rattr, "datadog.version", serviceVersion.AsString()); err != nil {
return ptrace.Traces{}, err
}

serviceVersion := ""
if serviceVersionAttr, ok := otelres.Attributes().Get(semconv.AttributeServiceVersion); ok {
serviceVersion = serviceVersionAttr.AsString()
}
if env := traceutil.GetOTelEnv(otelres); env != "" {
if err = tp.insertAttrIfMissingOrShouldOverride(rattr, "datadog.env", env); err != nil {
return ptrace.Traces{}, err
}
if err = tp.insertAttrIfMissingOrShouldOverride(rattr, "datadog.version", serviceVersion); err != nil {
return ptrace.Traces{}, err
}

env := "default"
if envFromAttr := traceutil.GetOTelEnv(otelres); envFromAttr != "" {
env = envFromAttr
}
if err = tp.insertAttrIfMissingOrShouldOverride(rattr, "datadog.env", env); err != nil {
return ptrace.Traces{}, err
}

if tp.overrideIncomingDatadogFields {
rattr.Remove("datadog.host.name")
}

datadogHostName := ""
if src, ok := tp.attrsTranslator.ResourceToSource(ctx, otelres, traceutil.SignalTypeSet, nil); ok && src.Kind == source.HostnameKind {
if err = tp.insertAttrIfMissingOrShouldOverride(rattr, "datadog.host.name", src.Identifier); err != nil {
return ptrace.Traces{}, err
}
datadogHostName = src.Identifier
}
if err = tp.insertAttrIfMissingOrShouldOverride(rattr, "datadog.host.name", datadogHostName); err != nil {
return ptrace.Traces{}, err
}

libspans := rspan.ScopeSpans().At(j)
Expand Down Expand Up @@ -92,17 +99,13 @@ func (tp *tracesProcessor) processTraces(ctx context.Context, td ptrace.Traces)
if err = tp.insertAttrIfMissingOrShouldOverride(sattr, "datadog.error", ddError); err != nil {
return ptrace.Traces{}, err
}
if metaMap["error.msg"] != "" {
if ddError == 1 {
if err = tp.insertAttrIfMissingOrShouldOverride(sattr, "datadog.error.msg", metaMap["error.msg"]); err != nil {
return ptrace.Traces{}, err
}
}
if metaMap["error.type"] != "" {
if err = tp.insertAttrIfMissingOrShouldOverride(sattr, "datadog.error.type", metaMap["error.type"]); err != nil {
return ptrace.Traces{}, err
}
}
if metaMap["error.stack"] != "" {
if err = tp.insertAttrIfMissingOrShouldOverride(sattr, "datadog.error.stack", metaMap["error.stack"]); err != nil {
return ptrace.Traces{}, err
}
Expand Down
78 changes: 78 additions & 0 deletions processor/datadogsemanticsprocessor/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,84 @@ func TestBasicTranslation(t *testing.T) {
assertKeyInAttributesMatchesValue(t, sattr, "datadog.error.stack", "overridden-stack")
},
},
{
name: "overrideIncomingDatadogFields even if override would be empty",
overrideIncomingDatadogFields: true,
in: []testutil.OTLPResourceSpan{
{
LibName: "libname",
LibVersion: "1.2",
Attributes: map[string]any{
"service.name": "",
"resource.name": "",
"deployment.environment.name": "",
"host.name": "",
"service.version": "",
"datadog.env": "specified-host-name",
"datadog.host.name": "specified-host-name",
"datadog.version": "specified-version",
},
Spans: []*testutil.OTLPSpan{
{
Events: []testutil.OTLPSpanEvent{
{
Timestamp: 66,
Name: "exception",
Attributes: map[string]any{
semconv.AttributeExceptionMessage: "",
semconv.AttributeExceptionType: "",
semconv.AttributeExceptionStacktrace: "",
},
Dropped: 4,
},
},
StatusCode: ptrace.StatusCodeError,
StatusMsg: "overridden-error-msg",
TraceID: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
SpanID: [8]byte{0, 1, 2, 3, 4, 5, 6, 7},
ParentID: [8]byte{0, 0, 0, 0, 0, 0, 0, 1},
Kind: ptrace.SpanKindServer,
Attributes: map[string]any{
"datadog.service": "specified-service",
"datadog.resource": "specified-resource",
"datadog.name": "specified-operation",
"datadog.type": "specified-type",
"datadog.host.name": "specified-hostname",
"datadog.span.kind": "specified-span-kind",
"datadog.env": "specified-env",
"datadog.http_status_code": "500",
"datadog.error": 1,
"datadog.error.msg": "specified-error-msg",
"datadog.error.type": "specified-error-type",
"datadog.error.stack": "specified-error-stack",
semconv.AttributeHTTPStatusCode: 200,
},
},
},
},
},
fn: func(out *ptrace.Traces) {
rs := out.ResourceSpans().At(0)
rattr := rs.Resource().Attributes()
assertKeyInAttributesMatchesValue(t, rattr, "datadog.service", "otlpresourcenoservicename")
assertKeyInAttributesMatchesValue(t, rattr, "datadog.env", "default")
assertKeyInAttributesMatchesValue(t, rattr, "datadog.version", "")
assertKeyInAttributesMatchesValue(t, rattr, "datadog.host.name", "")

span := rs.ScopeSpans().At(0).Spans().At(0)
sattr := span.Attributes()
assertKeyInAttributesMatchesValue(t, sattr, "datadog.name", "server.request")
assertKeyInAttributesMatchesValue(t, sattr, "datadog.resource", "")
assertKeyInAttributesMatchesValue(t, sattr, "datadog.type", "web")
assertKeyInAttributesMatchesValue(t, sattr, "datadog.span.kind", "server")
assertKeyInAttributesMatchesValue(t, sattr, "datadog.http_status_code", "200")
ddError, _ := sattr.Get("datadog.error")
require.Equal(t, int64(1), ddError.Int())
assertKeyInAttributesMatchesValue(t, sattr, "datadog.error.msg", "")
assertKeyInAttributesMatchesValue(t, sattr, "datadog.error.type", "")
assertKeyInAttributesMatchesValue(t, sattr, "datadog.error.stack", "")
},
},
{
name: "dont override incoming Datadog fields",
overrideIncomingDatadogFields: false,
Expand Down
Loading