Skip to content

Commit 29f6acf

Browse files
authored
[receiver/datadog] add datadog span and trace id (#23058)
**Description:** <Describe what has changed.> <!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> Add datadog trace id and span id so we cancorrelate traces received by datadogreceiver with logs. Datadog libraries automatically provide datadog span ids and trace ids with the logs for various languages: - https://docs.datadoghq.com/tracing/other_telemetry/connect_logs_and_traces/python/ -https://docs.datadoghq.com/tracing/other_telemetry/connect_logs_and_traces/java?tab=log4j2 Example log for python: ``` 2023-06-05 06:01:30,459 INFO [__main__] [test-enhanced.py:44] [dd.service= dd.env= dd.version= dd.trace_id=6249785623524942554 dd.span_id=2 28114450199004348] - Starting a new Game! ``` **Link to tracking Issue:** #23057 **Testing:** <Describe what testing was performed and which tests were added.> - local testing with python app - updated unit tests - **Documentation:** <Describe the documentation added.>
1 parent 64a630a commit 29f6acf

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

.chloggen/dd-receiver-span-id.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Use this changelog template to create an entry for release notes.
2+
# If your change doesn't affect end users, such as a test fix or a tooling change,
3+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
4+
5+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
6+
change_type: enhancement
7+
8+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
9+
component: datadogreceiver
10+
11+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
12+
note: "add datadog trace and span id"
13+
14+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
15+
issues: [23057]
16+
17+
# (Optional) One or more lines of additional information to render under the primary note.
18+
# These lines will be padded with 2 spaces and then inserted directly into the document.
19+
# Use pipe (|) for multiline entries.
20+
subtext:

receiver/datadogreceiver/translator.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"io"
1111
"mime"
1212
"net/http"
13+
"strconv"
1314
"strings"
1415
"sync"
1516

@@ -22,6 +23,18 @@ import (
2223

2324
const (
2425
datadogSpanKindKey = "span.kind"
26+
// The datadog trace id
27+
//
28+
// Type: string
29+
// Requirement Level: Optional
30+
// Examples: '6249785623524942554'
31+
attributeDatadogTraceID = "datadog.trace.id"
32+
// The datadog span id
33+
//
34+
// Type: string
35+
// Requirement Level: Optional
36+
// Examples: '228114450199004348'
37+
attributeDatadogSpanID = "datadog.span.id"
2538
)
2639

2740
func upsertHeadersAttributes(req *http.Request, attrs pcommon.Map) {
@@ -89,7 +102,8 @@ func toTraces(payload *pb.TracerPayload, req *http.Request) ptrace.Traces {
89102
if span.Error > 0 {
90103
newSpan.Status().SetCode(ptrace.StatusCodeError)
91104
}
92-
105+
newSpan.Attributes().PutStr(attributeDatadogSpanID, strconv.FormatUint(span.SpanID, 10))
106+
newSpan.Attributes().PutStr(attributeDatadogTraceID, strconv.FormatUint(span.TraceID, 10))
93107
for k, v := range span.GetMeta() {
94108
if k = translateDataDogKeyToOtel(k); len(k) > 0 {
95109
newSpan.Attributes().PutStr(k, v)

receiver/datadogreceiver/translator_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ func TestTracePayloadV05Unmarshalling(t *testing.T) {
7474
assert.Equal(t, 1, translated.SpanCount(), "Span Count wrong")
7575
span := translated.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0)
7676
assert.NotNil(t, span)
77-
assert.Equal(t, 5, span.Attributes().Len(), "missing tags")
77+
assert.Equal(t, 7, span.Attributes().Len(), "missing attributes")
7878
value, exists := span.Attributes().Get("service.name")
7979
assert.True(t, exists, "service.name missing")
80-
assert.Equal(t, "my-service", value.AsString(), "service.name tag value incorrect")
80+
assert.Equal(t, "my-service", value.AsString(), "service.name attribute value incorrect")
8181
assert.Equal(t, "my-name", span.Name())
8282
spanResource, _ := span.Attributes().Get("dd.span.Resource")
8383
assert.Equal(t, "my-resource", spanResource.Str())
@@ -103,10 +103,10 @@ func TestTracePayloadV07Unmarshalling(t *testing.T) {
103103
translated, _ := handlePayload(req)
104104
span := translated.GetChunks()[0].GetSpans()[0]
105105
assert.NotNil(t, span)
106-
assert.Equal(t, 4, len(span.GetMeta()), "missing tags")
106+
assert.Equal(t, 4, len(span.GetMeta()), "missing attributes")
107107
value, exists := span.GetMeta()["service.name"]
108108
assert.True(t, exists, "service.name missing")
109-
assert.Equal(t, "my-service", value, "service.name tag value incorrect")
109+
assert.Equal(t, "my-service", value, "service.name attribute value incorrect")
110110
assert.Equal(t, "my-name", span.GetName())
111111
assert.Equal(t, "my-resource", span.GetResource())
112112
}

0 commit comments

Comments
 (0)