Skip to content

Commit

Permalink
add tests and additional rpc resource details
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmustin committed Dec 18, 2020
1 parent 2dc4a3a commit f34c7ee
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 6 deletions.
18 changes: 13 additions & 5 deletions exporter/datadogexporter/translate_traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"encoding/hex"
"fmt"
"strconv"
"strings"

"github.com/DataDog/datadog-agent/pkg/trace/exportable/pb"
"go.opentelemetry.io/collector/consumer/pdata"
Expand Down Expand Up @@ -382,18 +381,18 @@ func getDatadogSpanName(s pdata.Span, datadogTags map[string]string) string {
// The spec has changed over time and, depending on the original exporter, IL Name could represented a few different ways
// so we try to account for all permutations
if ilnOtlp, okOtlp := datadogTags[tracetranslator.TagInstrumentationName]; okOtlp {
return utils.NormalizeSpanName(fmt.Sprintf("%s.%s", ilnOtlp, strings.TrimPrefix(s.Kind().String(), "SPAN_KIND_")))
return utils.NormalizeSpanName(fmt.Sprintf("%s.%s", ilnOtlp, utils.NormalizeSpanKind(s.Kind())))
}

if ilnOtelCur, okOtelCur := datadogTags[currentILNameTag]; okOtelCur {
return utils.NormalizeSpanName(fmt.Sprintf("%s.%s", ilnOtelCur, strings.TrimPrefix(s.Kind().String(), "SPAN_KIND_")))
return utils.NormalizeSpanName(fmt.Sprintf("%s.%s", ilnOtelCur, utils.NormalizeSpanKind(s.Kind())))
}

if ilnOtelOld, okOtelOld := datadogTags[oldILNameTag]; okOtelOld {
return utils.NormalizeSpanName(fmt.Sprintf("%s.%s", ilnOtelOld, strings.TrimPrefix(s.Kind().String(), "SPAN_KIND_")))
return utils.NormalizeSpanName(fmt.Sprintf("%s.%s", ilnOtelOld, utils.NormalizeSpanKind(s.Kind())))
}

return utils.NormalizeSpanName(fmt.Sprintf("%s.%s", "opentelemetry", strings.TrimPrefix(s.Kind().String(), "SPAN_KIND_")))
return utils.NormalizeSpanName(fmt.Sprintf("%s.%s", "opentelemetry", utils.NormalizeSpanKind(s.Kind())))
}

func getDatadogResourceName(s pdata.Span, datadogTags map[string]string) string {
Expand Down Expand Up @@ -423,6 +422,15 @@ func getDatadogResourceName(s pdata.Span, datadogTags map[string]string) string
return msgOperation
}

// add resource convention for rpc services , method+service, fallback to just method if no service attribute
if rpcMethod, rpcMethodOk := datadogTags[conventions.AttributeRPCMethod]; rpcMethodOk {
if rpcService, rpcServiceOk := datadogTags[conventions.AttributeRPCService]; rpcServiceOk {
return fmt.Sprintf("%s %s", rpcMethod, rpcService)
}

return rpcMethod
}

return s.Name()
}

Expand Down
75 changes: 74 additions & 1 deletion exporter/datadogexporter/translate_traces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,74 @@ func TestSpanResourceTranslationMessaging(t *testing.T) {
assert.Equal(t, "Default Name", resourceNameDefault)
}

// ensure that the datadog span name uses IL name +kind whenn available and falls back to opetelemetry + kind
// ensure that datadog span resource naming uses messaging operation even when destination is not available
func TestSpanResourceTranslationMessagingFallback(t *testing.T) {
span := pdata.NewSpan()
span.SetKind(pdata.SpanKindSERVER)
span.SetName("Default Name")

ddHTTPTags := map[string]string{
"messaging.operation": "receive",
}

ddNotHTTPTags := map[string]string{
"other": "GET",
}

resourceNameHTTP := getDatadogResourceName(span, ddHTTPTags)

resourceNameDefault := getDatadogResourceName(span, ddNotHTTPTags)

assert.Equal(t, "receive", resourceNameHTTP)
assert.Equal(t, "Default Name", resourceNameDefault)
}

// ensure that datadog span resource naming uses rpc method + rpc service when available
func TestSpanResourceTranslationRpc(t *testing.T) {
span := pdata.NewSpan()
span.SetKind(pdata.SpanKindSERVER)
span.SetName("Default Name")

ddHTTPTags := map[string]string{
"rpc.method": "example_method",
"rpc.service": "example_service",
}

ddNotHTTPTags := map[string]string{
"other": "GET",
}

resourceNameHTTP := getDatadogResourceName(span, ddHTTPTags)

resourceNameDefault := getDatadogResourceName(span, ddNotHTTPTags)

assert.Equal(t, "example_method example_service", resourceNameHTTP)
assert.Equal(t, "Default Name", resourceNameDefault)
}

// ensure that datadog span resource naming uses rpc method even when rpc service is not available
func TestSpanResourceTranslationRpcFallback(t *testing.T) {
span := pdata.NewSpan()
span.SetKind(pdata.SpanKindSERVER)
span.SetName("Default Name")

ddHTTPTags := map[string]string{
"rpc.method": "example_method",
}

ddNotHTTPTags := map[string]string{
"other": "GET",
}

resourceNameHTTP := getDatadogResourceName(span, ddHTTPTags)

resourceNameDefault := getDatadogResourceName(span, ddNotHTTPTags)

assert.Equal(t, "example_method", resourceNameHTTP)
assert.Equal(t, "Default Name", resourceNameDefault)
}

// ensure that the datadog span name uses IL name +kind when available and falls back to opetelemetry + kind
func TestSpanNameTranslation(t *testing.T) {
span := pdata.NewSpan()
span.SetName("Default Name")
Expand All @@ -517,17 +584,23 @@ func TestSpanNameTranslation(t *testing.T) {
"otel.library.name": "@unusual/\\::value",
}

ddIlTagsHyphen := map[string]string{
"otel.library.name": "hyphenated-value",
}

spanNameIl := getDatadogSpanName(span, ddIlTags)
spanNameDefault := getDatadogSpanName(span, ddNoIlTags)
spanNameOld := getDatadogSpanName(span, ddIlTagsOld)
spanNameCur := getDatadogSpanName(span, ddIlTagsCur)
spanNameUnusual := getDatadogSpanName(span, ddIlTagsUnusual)
spanNameHyphen := getDatadogSpanName(span, ddIlTagsHyphen)

assert.Equal(t, strings.ToLower(fmt.Sprintf("%s.%s", "il_name", strings.TrimPrefix(pdata.SpanKindSERVER.String(), "SPAN_KIND_"))), spanNameIl)
assert.Equal(t, strings.ToLower(fmt.Sprintf("%s.%s", "opentelemetry", strings.TrimPrefix(pdata.SpanKindSERVER.String(), "SPAN_KIND_"))), spanNameDefault)
assert.Equal(t, strings.ToLower(fmt.Sprintf("%s.%s", "old_value", strings.TrimPrefix(pdata.SpanKindSERVER.String(), "SPAN_KIND_"))), spanNameOld)
assert.Equal(t, strings.ToLower(fmt.Sprintf("%s.%s", "current_value", strings.TrimPrefix(pdata.SpanKindSERVER.String(), "SPAN_KIND_"))), spanNameCur)
assert.Equal(t, strings.ToLower(fmt.Sprintf("%s.%s", "unusual_value", strings.TrimPrefix(pdata.SpanKindSERVER.String(), "SPAN_KIND_"))), spanNameUnusual)
assert.Equal(t, strings.ToLower(fmt.Sprintf("%s.%s", "hyphenated_value", strings.TrimPrefix(pdata.SpanKindSERVER.String(), "SPAN_KIND_"))), spanNameHyphen)
}

// ensure that the datadog span type gets mapped from span kind
Expand Down
7 changes: 7 additions & 0 deletions exporter/datadogexporter/utils/trace_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package utils
import (
"strings"
"unicode"

"go.opentelemetry.io/collector/consumer/pdata"
)

// constants for tags
Expand Down Expand Up @@ -100,3 +102,8 @@ func NormalizeSpanName(tag string) string {

return s
}

// NormalizeSpanKind returns a span kind with the SPAN_KIND prefix trimmed off
func NormalizeSpanKind(kind pdata.SpanKind) string {
return strings.TrimPrefix(kind.String(), "SPAN_KIND_")
}

0 comments on commit f34c7ee

Please sign in to comment.