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-support-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: Support for span links in Datadog receiver

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

# (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: 3 additions & 2 deletions pkg/translator/zipkin/zipkinv2/to_translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func zSpanToInternal(zspan *zipkinmodel.SpanModel, tags map[string]string, dest
dest.SetKind(zipkinKindToSpanKind(zspan.Kind, tags))

populateSpanStatus(tags, dest.Status())
if err := zTagsToSpanLinks(tags, dest.Links()); err != nil {
if err := TagsToSpanLinks(tags, dest.Links()); err != nil {
return err
}

Expand Down Expand Up @@ -183,7 +183,8 @@ func zipkinKindToSpanKind(kind zipkinmodel.Kind, tags map[string]string) ptrace.
}
}

func zTagsToSpanLinks(tags map[string]string, dest ptrace.SpanLinkSlice) error {
// TagsToSpanLinks translates span tags into ptrace.SpanLinkSlice.
func TagsToSpanLinks(tags map[string]string, dest ptrace.SpanLinkSlice) error {
for i := 0; i < 128; i++ {
key := fmt.Sprintf("otlp.link.%d", i)
val, ok := tags[key]
Expand Down
162 changes: 162 additions & 0 deletions pkg/translator/zipkin/zipkinv2/to_translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
package zipkinv2

import (
"encoding/hex"
"strconv"
"testing"
"time"

zipkinmodel "github.com/openzipkin/zipkin-go/model"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"

Expand Down Expand Up @@ -262,3 +265,162 @@ func TestV2SpanWithoutTimestampGetsTag(t *testing.T) {
assert.True(t, mapContainedKey)
assert.True(t, wasAbsent.Bool())
}

func TestTagsToSpanLinks(t *testing.T) {
tests := []struct {
name string
tags map[string]string
expectedLinks []ptrace.SpanLink
expectedError bool
}{
{
name: "Single valid link",
tags: map[string]string{
"otlp.link.0": "0102030405060708090a0b0c0d0e0f10|0102030405060708|state|{\"attr1\":\"value1\",\"attr2\":123}|0",
},
expectedLinks: []ptrace.SpanLink{
func() ptrace.SpanLink {
link := ptrace.NewSpanLink()
traceID, _ := hex.DecodeString("0102030405060708090a0b0c0d0e0f10")
link.SetTraceID(pcommon.TraceID(traceID))
spanID, _ := hex.DecodeString("0102030405060708")
link.SetSpanID(pcommon.SpanID(spanID))
link.TraceState().FromRaw("state")
attrs := link.Attributes()
attrs.PutStr("attr1", "value1")
attrs.PutInt("attr2", 123)
link.SetDroppedAttributesCount(0)
return link
}(),
},
expectedError: false,
},
{
name: "Multiple valid links",
tags: map[string]string{
"otlp.link.0": "0102030405060708090a0b0c0d0e0f10|0102030405060708|state1|{\"attr1\":\"value1\"}|0",
"otlp.link.1": "1112131415161718191a1b1c1d1e1f20|1112131415161718|state2|{\"attr2\":456}|1",
},
expectedLinks: []ptrace.SpanLink{
func() ptrace.SpanLink {
link := ptrace.NewSpanLink()
traceID, _ := hex.DecodeString("0102030405060708090a0b0c0d0e0f10")
link.SetTraceID(pcommon.TraceID(traceID))
spanID, _ := hex.DecodeString("0102030405060708")
link.SetSpanID(pcommon.SpanID(spanID))
link.TraceState().FromRaw("state1")
attrs := link.Attributes()
attrs.PutStr("attr1", "value1")
link.SetDroppedAttributesCount(0)
return link
}(),
func() ptrace.SpanLink {
link := ptrace.NewSpanLink()
traceID, _ := hex.DecodeString("1112131415161718191a1b1c1d1e1f20")
link.SetTraceID(pcommon.TraceID(traceID))
spanID, _ := hex.DecodeString("1112131415161718")
link.SetSpanID(pcommon.SpanID(spanID))
link.TraceState().FromRaw("state2")
attrs := link.Attributes()
attrs.PutInt("attr2", 456)
link.SetDroppedAttributesCount(1)
return link
}(),
},
expectedError: false,
},
{
name: "Invalid format (too few parts)",
tags: map[string]string{
"otlp.link.0": "invalid|value",
},
expectedLinks: []ptrace.SpanLink{},
expectedError: false,
},
{
name: "Invalid hex in trace ID",
tags: map[string]string{
"otlp.link.0": "nothex|0102030405060708|state|{\"attr1\":\"value1\"}|0",
},
expectedLinks: nil,
expectedError: true,
},
{
name: "Invalid dropped attributes count",
tags: map[string]string{
"otlp.link.0": "0102030405060708090a0b0c0d0e0f10|0102030405060708|state|{\"attr1\":\"value1\"}|notanumber",
},
expectedLinks: nil,
expectedError: true,
},
{
name: "Attributes with pipe character",
tags: map[string]string{
"otlp.link.0": "0102030405060708090a0b0c0d0e0f10|0102030405060708|state|{\"attr1\":\"value1|with|pipes\"}|0",
},
expectedLinks: []ptrace.SpanLink{
func() ptrace.SpanLink {
link := ptrace.NewSpanLink()
traceID, _ := hex.DecodeString("0102030405060708090a0b0c0d0e0f10")
link.SetTraceID(pcommon.TraceID(traceID))
spanID, _ := hex.DecodeString("0102030405060708")
link.SetSpanID(pcommon.SpanID(spanID))
link.TraceState().FromRaw("state")
attrs := link.Attributes()
attrs.PutStr("attr1", "value1|with|pipes")
link.SetDroppedAttributesCount(0)
return link
}(),
},
expectedError: false,
},
{
name: "No links in tags",
tags: map[string]string{},
expectedLinks: []ptrace.SpanLink{},
expectedError: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dest := ptrace.NewSpanLinkSlice()
err := TagsToSpanLinks(tt.tags, dest)
if tt.expectedError {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.Equal(t, len(tt.expectedLinks), dest.Len())

for i, expected := range tt.expectedLinks {
link := dest.At(i)

assert.Equal(t, expected.TraceID(), link.TraceID(), "TraceID mismatch")
assert.Equal(t, expected.SpanID(), link.SpanID(), "SpanID mismatch")
assert.Equal(t, expected.TraceState().AsRaw(), link.TraceState().AsRaw())

assert.Equal(t, expected.DroppedAttributesCount(), link.DroppedAttributesCount())

// Verify attributes
expectedAttrs := expected.Attributes()
actualAttrs := link.Attributes()
assert.Equal(t, expectedAttrs.Len(), actualAttrs.Len(), "Attributes length mismatch")

expectedAttrs.Range(func(k string, v pcommon.Value) bool {
actualVal, ok := actualAttrs.Get(k)
assert.True(t, ok, "Attribute %s not found in link", k)
assert.Equal(t, v, actualVal, "Attribute %s value mismatch", k)
return true
})
}

// Verify that link tags are removed
for i := 0; i < 128; i++ {
key := "otlp.link." + strconv.Itoa(i)
_, ok := tt.tags[key]
assert.False(t, ok, "Expected tag %s to be deleted", key)
}
})
}
}
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
- `otlp.link.%d`: This receiver supports `otlp.link.%d` attributes, which are used to create Span Links.
The `%d` is a placeholder for the link index. The value of the attribute is a span context serialized as a string separated by `|`, in the format of `trace_id|span_id|tracestate|json_attributes|dropped_attributes_count`.
Examples can be found [here](../../pkg/translator/zipkin/zipkinv2/to_translator_test.go).

### Datadog's API support

**Traces**
Expand Down
10 changes: 7 additions & 3 deletions receiver/datadogreceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.112.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/exp/metrics v0.112.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/sharedcomponent v0.112.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/zipkin v0.112.0
github.com/stretchr/testify v1.9.0
github.com/tinylib/msgp v1.2.2
github.com/vmihailenco/msgpack/v5 v5.4.1
Expand Down Expand Up @@ -42,7 +43,7 @@ require (
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cihub/seelog v0.0.0-20170130134532-f561c5e57575 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
Expand All @@ -66,11 +67,12 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil v0.112.0 // indirect
github.com/openzipkin/zipkin-go v0.4.3 // indirect
github.com/outcaste-io/ristretto v0.2.1 // indirect
github.com/philhofer/fwd v1.1.3-0.20240916144458-20a13a1f6b7c // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/power-devops/perfstat v0.0.0-20220216144756-c35f1ee13d7c // indirect
github.com/rs/cors v1.11.1 // indirect
github.com/secure-systems-lab/go-securesystemslib v0.7.0 // indirect
Expand Down Expand Up @@ -108,7 +110,7 @@ require (
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
golang.org/x/tools v0.24.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240822170219-fc7c04adadcd // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect
google.golang.org/grpc v1.67.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand All @@ -130,4 +132,6 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden =>

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

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

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal => ../../internal/coreinternal
12 changes: 8 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 @@ -22,6 +22,7 @@ import (
semconv "go.opentelemetry.io/collector/semconv/v1.16.0"
"google.golang.org/protobuf/proto"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/zipkin/zipkinv2"
"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()

_ = zipkinv2.TagsToSpanLinks(span.GetMeta(), newSpan.Links())

newSpan.SetTraceID(uInt64ToTraceID(0, span.TraceID))
newSpan.SetSpanID(uInt64ToSpanID(span.SpanID))
newSpan.SetStartTimestamp(pcommon.Timestamp(span.Start))
Expand Down