From 344a0d2aba7e8a9363bce1aa8247c5769cba0480 Mon Sep 17 00:00:00 2001 From: Jonah Back Date: Thu, 1 Feb 2024 07:38:58 -0800 Subject: [PATCH] fix(datadogreceiver): set AppVersion on payload so it propagates correctly (#30225) **Description:** This PR sets the AppVersion property for the default fallthrough case if it exists on any spans in the request. This allows it to properly propagate through to Datadog. Currently, if using a library such as datadog/dd-trace-dotnet , the datadog service version property (dd_version) does not propagate through to datadog at all. The bug that this fixes is that the Datadog version resource attribute is currently not flowing through. **Link to tracking Issue:** Closes #30526 **Testing:** Local testing using delve to confirm expected behavior, added existing version tag to tests. Deployed patch internally on OpenTelemetry collector and observed desired behavior. **Documentation:** --- .chloggen/fix_dd-app-version.yaml | 27 +++++++++++++++++++++ receiver/datadogreceiver/translator.go | 24 ++++++++++++++++-- receiver/datadogreceiver/translator_test.go | 11 ++++++--- 3 files changed, 57 insertions(+), 5 deletions(-) create mode 100755 .chloggen/fix_dd-app-version.yaml diff --git a/.chloggen/fix_dd-app-version.yaml b/.chloggen/fix_dd-app-version.yaml new file mode 100755 index 000000000000..4272a90755c3 --- /dev/null +++ b/.chloggen/fix_dd-app-version.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: datadogreceiver + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Set AppVersion to allow Datadog version property to transform properly to service.version resource attribute + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [30225] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] diff --git a/receiver/datadogreceiver/translator.go b/receiver/datadogreceiver/translator.go index 0eda02b1174e..113b25841cc3 100644 --- a/receiver/datadogreceiver/translator.go +++ b/receiver/datadogreceiver/translator.go @@ -230,11 +230,15 @@ func handlePayload(req *http.Request) (tp []*pb.TracerPayload, err error) { return nil, err } + traceChunks := traceChunksFromTraces(traces) + appVersion := appVersionFromTraceChunks(traceChunks) + tracerPayload := &pb.TracerPayload{ LanguageName: req.Header.Get("Datadog-Meta-Lang"), LanguageVersion: req.Header.Get("Datadog-Meta-Lang-Version"), TracerVersion: req.Header.Get("Datadog-Meta-Tracer-Version"), - Chunks: traceChunksFromTraces(traces), + Chunks: traceChunks, + AppVersion: appVersion, } tracerPayloads = append(tracerPayloads, tracerPayload) @@ -269,11 +273,14 @@ func handlePayload(req *http.Request) (tp []*pb.TracerPayload, err error) { if err = decodeRequest(req, &traces); err != nil { return nil, err } + traceChunks := traceChunksFromTraces(traces) + appVersion := appVersionFromTraceChunks(traceChunks) tracerPayload := &pb.TracerPayload{ LanguageName: req.Header.Get("Datadog-Meta-Lang"), LanguageVersion: req.Header.Get("Datadog-Meta-Lang-Version"), TracerVersion: req.Header.Get("Datadog-Meta-Tracer-Version"), - Chunks: traceChunksFromTraces(traces), + Chunks: traceChunks, + AppVersion: appVersion, } tracerPayloads = append(tracerPayloads, tracerPayload) } @@ -342,6 +349,19 @@ func traceChunksFromTraces(traces pb.Traces) []*pb.TraceChunk { return traceChunks } +func appVersionFromTraceChunks(traces []*pb.TraceChunk) string { + appVersion := "" + for _, trace := range traces { + for _, span := range trace.Spans { + if span != nil && span.Meta["version"] != "" { + appVersion = span.Meta["version"] + return appVersion + } + } + } + return appVersion +} + func getMediaType(req *http.Request) string { mt, _, err := mime.ParseMediaType(req.Header.Get("Content-Type")) if err != nil { diff --git a/receiver/datadogreceiver/translator_test.go b/receiver/datadogreceiver/translator_test.go index f15d84a55aa9..c65f876d30d7 100644 --- a/receiver/datadogreceiver/translator_test.go +++ b/receiver/datadogreceiver/translator_test.go @@ -33,6 +33,8 @@ var data = [2]any{ 9: "value whatever", 10: "sql", 11: "service.name", + 12: "1.0.1", + 13: "version", }, 1: [][][12]any{ { @@ -51,6 +53,7 @@ var data = [2]any{ 0: 1, 2: 3, 11: 6, + 13: 12, }, map[any]float64{ 5: 1.2, @@ -89,11 +92,13 @@ func TestTracePayloadV05Unmarshalling(t *testing.T) { assert.Equal(t, 1, translated.SpanCount(), "Span Count wrong") span := translated.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0) assert.NotNil(t, span) - assert.Equal(t, 7, span.Attributes().Len(), "missing attributes") + assert.Equal(t, 8, span.Attributes().Len(), "missing attributes") value, exists := span.Attributes().Get("service.name") + serviceVersionValue, _ := span.Attributes().Get("service.version") assert.True(t, exists, "service.name missing") assert.Equal(t, "my-service", value.AsString(), "service.name attribute value incorrect") assert.Equal(t, "my-name", span.Name()) + assert.Equal(t, "1.0.1", serviceVersionValue.AsString()) spanResource, _ := span.Attributes().Get("dd.span.Resource") assert.Equal(t, "my-resource", spanResource.Str()) } @@ -115,7 +120,7 @@ func TestTracePayloadV07Unmarshalling(t *testing.T) { translated := translatedPayloads[0] span := translated.GetChunks()[0].GetSpans()[0] assert.NotNil(t, span) - assert.Equal(t, 4, len(span.GetMeta()), "missing attributes") + assert.Equal(t, 5, len(span.GetMeta()), "missing attributes") value, exists := span.GetMeta()["service.name"] assert.True(t, exists, "service.name missing") assert.Equal(t, "my-service", value, "service.name attribute value incorrect") @@ -155,7 +160,7 @@ func TestTracePayloadApiV02Unmarshalling(t *testing.T) { span := translated.Chunks[0].Spans[0] assert.NotNil(t, span) - assert.Equal(t, 4, len(span.Meta), "missing attributes") + assert.Equal(t, 5, len(span.Meta), "missing attributes") assert.Equal(t, "my-service", span.Meta["service.name"]) assert.Equal(t, "my-name", span.Name) assert.Equal(t, "my-resource", span.Resource)