Skip to content

Commit

Permalink
[receiver/datadog] add datadog span and trace id (#23058)
Browse files Browse the repository at this point in the history
**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.>
  • Loading branch information
povilasv authored Aug 4, 2023
1 parent 64a630a commit 29f6acf
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
20 changes: 20 additions & 0 deletions .chloggen/dd-receiver-span-id.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use this changelog template to create an entry for release notes.
# If your change doesn't affect end users, such as a test fix or a tooling change,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# 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: "add datadog trace and span id"

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [23057]

# (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:
16 changes: 15 additions & 1 deletion receiver/datadogreceiver/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io"
"mime"
"net/http"
"strconv"
"strings"
"sync"

Expand All @@ -22,6 +23,18 @@ import (

const (
datadogSpanKindKey = "span.kind"
// The datadog trace id
//
// Type: string
// Requirement Level: Optional
// Examples: '6249785623524942554'
attributeDatadogTraceID = "datadog.trace.id"
// The datadog span id
//
// Type: string
// Requirement Level: Optional
// Examples: '228114450199004348'
attributeDatadogSpanID = "datadog.span.id"
)

func upsertHeadersAttributes(req *http.Request, attrs pcommon.Map) {
Expand Down Expand Up @@ -89,7 +102,8 @@ func toTraces(payload *pb.TracerPayload, req *http.Request) ptrace.Traces {
if span.Error > 0 {
newSpan.Status().SetCode(ptrace.StatusCodeError)
}

newSpan.Attributes().PutStr(attributeDatadogSpanID, strconv.FormatUint(span.SpanID, 10))
newSpan.Attributes().PutStr(attributeDatadogTraceID, strconv.FormatUint(span.TraceID, 10))
for k, v := range span.GetMeta() {
if k = translateDataDogKeyToOtel(k); len(k) > 0 {
newSpan.Attributes().PutStr(k, v)
Expand Down
8 changes: 4 additions & 4 deletions receiver/datadogreceiver/translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ 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, 5, span.Attributes().Len(), "missing tags")
assert.Equal(t, 7, span.Attributes().Len(), "missing attributes")
value, exists := span.Attributes().Get("service.name")
assert.True(t, exists, "service.name missing")
assert.Equal(t, "my-service", value.AsString(), "service.name tag value incorrect")
assert.Equal(t, "my-service", value.AsString(), "service.name attribute value incorrect")
assert.Equal(t, "my-name", span.Name())
spanResource, _ := span.Attributes().Get("dd.span.Resource")
assert.Equal(t, "my-resource", spanResource.Str())
Expand All @@ -103,10 +103,10 @@ func TestTracePayloadV07Unmarshalling(t *testing.T) {
translated, _ := handlePayload(req)
span := translated.GetChunks()[0].GetSpans()[0]
assert.NotNil(t, span)
assert.Equal(t, 4, len(span.GetMeta()), "missing tags")
assert.Equal(t, 4, 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 tag value incorrect")
assert.Equal(t, "my-service", value, "service.name attribute value incorrect")
assert.Equal(t, "my-name", span.GetName())
assert.Equal(t, "my-resource", span.GetResource())
}
Expand Down

0 comments on commit 29f6acf

Please sign in to comment.