Skip to content
Merged
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
27 changes: 27 additions & 0 deletions .chloggen/datadog-receiver-span-links.yaml
Original file line number Diff line number Diff line change
@@ -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: 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: Implement support for span links

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

# (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: [user]
5 changes: 5 additions & 0 deletions receiver/datadogreceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ https://github.com/open-telemetry/opentelemetry-collector/tree/main/config/confi

- `dd.span.Resource`: The datadog resource name (as distinct from the span name)

### Optional Attributes

- `_dd.span_links`: This receiver supports DD Agent's `_dd.span_links` attribute for span links creation, as produced by Datadog's tracing libraries.
Format example can be found [here](./internal/translator/traces_translator_test.go).

### Datadog's API support

**Traces**
Expand Down
16 changes: 8 additions & 8 deletions receiver/datadogreceiver/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/open-telemetry/opentelemetry-collector-contrib/receiver/datadogreceiver

go 1.22.0
go 1.22.7

require (
github.com/DataDog/agent-payload/v5 v5.0.144
Expand All @@ -27,6 +27,7 @@ require (
go.opentelemetry.io/collector/receiver v0.119.1-0.20250210123122-44b3eeda354c
go.opentelemetry.io/collector/receiver/receivertest v0.119.1-0.20250210123122-44b3eeda354c
go.opentelemetry.io/collector/semconv v0.119.1-0.20250210123122-44b3eeda354c
go.opentelemetry.io/otel/trace v1.34.0
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.27.0
google.golang.org/protobuf v1.36.5
Expand Down Expand Up @@ -103,14 +104,13 @@ require (
go.opentelemetry.io/otel/metric v1.34.0 // indirect
go.opentelemetry.io/otel/sdk v1.34.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.34.0 // indirect
go.opentelemetry.io/otel/trace v1.34.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
golang.org/x/net v0.34.0 // indirect
golang.org/x/oauth2 v0.24.0 // indirect
golang.org/x/sys v0.29.0 // indirect
golang.org/x/sys v0.30.0 // indirect
golang.org/x/text v0.22.0 // indirect
golang.org/x/time v0.8.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241202173237-19429a94021a // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250115164207-1a7da9e5054f // indirect
google.golang.org/grpc v1.70.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand All @@ -125,10 +125,10 @@ retract (

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/exp/metrics => ../../internal/exp/metrics

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest => ../../pkg/pdatatest

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden => ../../pkg/golden

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil => ../../pkg/pdatautil

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal => ../../internal/coreinternal

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden => ../../pkg/golden

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest => ../../pkg/pdatatest
8 changes: 4 additions & 4 deletions receiver/datadogreceiver/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
semconv "go.opentelemetry.io/collector/semconv/v1.16.0"
"go.opentelemetry.io/otel/trace"
"google.golang.org/protobuf/proto"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/datadogreceiver/internal/translator/header"
Expand Down Expand Up @@ -98,6 +99,8 @@ func ToTraces(payload *pb.TracerPayload, req *http.Request) ptrace.Traces {
}
newSpan := slice.AppendEmpty()

_ = tagsToSpanLinks(span.GetMeta(), newSpan.Links())

newSpan.SetTraceID(uInt64ToTraceID(0, span.TraceID))
newSpan.SetSpanID(uInt64ToSpanID(span.SpanID))
newSpan.SetStartTimestamp(pcommon.Timestamp(span.Start))
Expand Down Expand Up @@ -165,6 +168,57 @@ func ToTraces(payload *pb.TracerPayload, req *http.Request) ptrace.Traces {
return results
}

// DDSpanLink represents the structure of each JSON object
type DDSpanLink struct {
TraceID string `json:"trace_id"`
SpanID string `json:"span_id"`
Tracestate string `json:"tracestate"`
Attributes map[string]any `json:"attributes"`
}

func tagsToSpanLinks(tags map[string]string, dest ptrace.SpanLinkSlice) error {
key := "_dd.span_links"
val, ok := tags[key]
if !ok {
return nil
}
delete(tags, key)

var spans []DDSpanLink
err := json.Unmarshal([]byte(val), &spans)
if err != nil {
return err
}

for i := 0; i < len(spans); i++ {
span := spans[i]
link := dest.AppendEmpty()

// Convert trace id.
Comment thread
lopes-felipe marked this conversation as resolved.
rawTrace, errTrace := trace.TraceIDFromHex(span.TraceID)
if errTrace != nil {
return errTrace
}
link.SetTraceID(pcommon.TraceID(rawTrace))

// Convert span id.
rawSpan, errSpan := trace.SpanIDFromHex(span.SpanID)
if errSpan != nil {
return errSpan
}
link.SetSpanID(pcommon.SpanID(rawSpan))

link.TraceState().FromRaw(span.Tracestate)

err = link.Attributes().FromRaw(span.Attributes)
if err != nil {
return err
}
}

return nil
}

var bufferPool = sync.Pool{
New: func() any {
return new(bytes.Buffer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ var data = [2]any{
11: "service.name",
12: "1.0.1",
13: "version",
14: "_dd.span_links",
15: `[{"attributes":{"attr1":"val1","attr2":"val2"},"span_id":"70666bf9dee4a3fe","trace_id":"0eacdb57bebc935038bf5b4802ccabd5","tracestate":"dd=k:v"}]`,
},
1: [][][12]any{
{
Expand All @@ -57,6 +59,7 @@ var data = [2]any{
2: 3,
11: 6,
13: 12,
14: 15,
},
map[any]float64{
5: 1.2,
Expand Down Expand Up @@ -107,6 +110,13 @@ func TestTracePayloadV05Unmarshalling(t *testing.T) {
numericAttributeValue, _ := span.Attributes().Get("numeric_attribute")
numericAttributeFloat, _ := strconv.ParseFloat(numericAttributeValue.AsString(), 64)
assert.Equal(t, 1.2, numericAttributeFloat)

spanLink := span.Links().At(0)
assert.Equal(t, "70666bf9dee4a3fe", spanLink.SpanID().String())
assert.Equal(t, "0eacdb57bebc935038bf5b4802ccabd5", spanLink.TraceID().String())
assert.Equal(t, "dd=k:v", spanLink.TraceState().AsRaw())
spanLinkAttrVal, _ := spanLink.Attributes().Get("attr1")
assert.Equal(t, "val1", spanLinkAttrVal.Str())
}

func TestTracePayloadV07Unmarshalling(t *testing.T) {
Expand All @@ -126,7 +136,7 @@ func TestTracePayloadV07Unmarshalling(t *testing.T) {
translated := translatedPayloads[0]
span := translated.GetChunks()[0].GetSpans()[0]
assert.NotNil(t, span)
assert.Len(t, span.GetMeta(), 5, "missing attributes")
assert.Len(t, span.GetMeta(), 6, "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")
Expand Down Expand Up @@ -166,7 +176,7 @@ func TestTracePayloadApiV02Unmarshalling(t *testing.T) {
span := translated.Chunks[0].Spans[0]

assert.NotNil(t, span)
assert.Len(t, span.Meta, 5, "missing attributes")
assert.Len(t, span.Meta, 6, "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)
Expand Down