feat(trace): migrate dd-trace-go v1 to v2 (#7466)

- Upgrade dd-trace-go dependency to v2.2.2
- Separate Zipkin and DataDog code paths for better maintainability
- Add proper tracer shutdown through OnShutdown()
- Replace deprecated opentracer.New() with direct tracer.Start()
- Added tests

Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
This commit is contained in:
Ville Vesilehto
2025-08-21 02:00:21 +03:00
committed by GitHub
parent 43d968331e
commit 2b273d48ab
5 changed files with 242 additions and 36 deletions

View File

@@ -5,6 +5,7 @@ import (
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/coredns/caddy"
@@ -15,9 +16,10 @@ import (
"github.com/coredns/coredns/plugin/test"
"github.com/coredns/coredns/request"
"github.com/DataDog/dd-trace-go/v2/ddtrace/mocktracer"
"github.com/miekg/dns"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/mocktracer"
openTracingMock "github.com/opentracing/opentracing-go/mocktracer"
)
func TestStartup(t *testing.T) {
@@ -83,7 +85,7 @@ func TestTrace(t *testing.T) {
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
w := dnstest.NewRecorder(&test.ResponseWriter{})
m := mocktracer.New()
m := openTracingMock.New()
tr := &trace{
Next: test.HandlerFunc(func(_ context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
if plugin.ClientWrite(tc.status) {
@@ -93,9 +95,9 @@ func TestTrace(t *testing.T) {
}
return tc.status, tc.err
}),
every: 1,
tracer: m,
tagSet: defaultTagSet,
every: 1,
zipkinTracer: m,
tagSet: defaultTagSet,
}
ctx := context.TODO()
if _, err := tr.ServeDNS(ctx, w, tc.question); err != nil && tc.err == nil {
@@ -138,7 +140,7 @@ func TestTrace(t *testing.T) {
func TestTrace_DOH_TraceHeaderExtraction(t *testing.T) {
w := dnstest.NewRecorder(&test.ResponseWriter{})
m := mocktracer.New()
m := openTracingMock.New()
tr := &trace{
Next: test.HandlerFunc(func(_ context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
if plugin.ClientWrite(dns.RcodeSuccess) {
@@ -148,8 +150,8 @@ func TestTrace_DOH_TraceHeaderExtraction(t *testing.T) {
}
return dns.RcodeSuccess, nil
}),
every: 1,
tracer: m,
every: 1,
zipkinTracer: m,
}
q := new(dns.Msg).SetQuestion("example.net.", dns.TypeA)
@@ -166,9 +168,163 @@ func TestTrace_DOH_TraceHeaderExtraction(t *testing.T) {
fs := m.FinishedSpans()
rootCoreDNSspan := fs[1]
rootCoreDNSTraceID := rootCoreDNSspan.Context().(mocktracer.MockSpanContext).TraceID
outsideSpanTraceID := outsideSpan.Context().(mocktracer.MockSpanContext).TraceID
rootCoreDNSTraceID := rootCoreDNSspan.Context().(openTracingMock.MockSpanContext).TraceID
outsideSpanTraceID := outsideSpan.Context().(openTracingMock.MockSpanContext).TraceID
if rootCoreDNSTraceID != outsideSpanTraceID {
t.Errorf("Unexpected traceID: rootSpan.TraceID: want %v, got %v", rootCoreDNSTraceID, outsideSpanTraceID)
}
}
func TestStartup_Datadog(t *testing.T) {
m, err := traceParse(caddy.NewTestController("dns", `trace datadog localhost:8126`))
if err != nil {
t.Errorf("Error parsing test input: %s", err)
return
}
if m.Name() != "trace" {
t.Errorf("Wrong name from GetName: %s", m.Name())
}
// Test that we can start and stop the DataDog tracer without errors
err = m.OnStartup()
if err != nil {
t.Errorf("Error starting DataDog tracing plugin: %s", err)
return
}
if m.tagSet != tagByProvider["datadog"] {
t.Errorf("TagSet for DataDog hasn't been correctly initialized")
}
// Test shutdown
err = m.OnShutdown()
if err != nil {
t.Errorf("Error shutting down DataDog tracing plugin: %s", err)
}
}
func TestTrace_DataDog(t *testing.T) {
// Test the complete DataDog tracing flow using mocktracer
mt := mocktracer.Start()
defer mt.Stop()
cases := []struct {
name string
rcode int
status int
question *dns.Msg
err error
}{
{
name: "NXDOMAIN",
rcode: dns.RcodeNameError,
status: dns.RcodeSuccess,
question: new(dns.Msg).SetQuestion("example.org.", dns.TypeA),
},
{
name: "NOERROR",
rcode: dns.RcodeSuccess,
status: dns.RcodeSuccess,
question: new(dns.Msg).SetQuestion("example.net.", dns.TypeCNAME),
},
{
name: "SERVFAIL with error",
rcode: dns.RcodeServerFailure,
status: dns.RcodeSuccess,
question: new(dns.Msg).SetQuestion("example.net.", dns.TypeA),
err: errors.New("test error"),
},
}
datadogTagSet := tagByProvider["datadog"]
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
// Reset spans for each test
mt.Reset()
w := dnstest.NewRecorder(&test.ResponseWriter{})
tr := &trace{
Next: test.HandlerFunc(func(_ context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
if plugin.ClientWrite(tc.status) {
m := new(dns.Msg)
m.SetRcode(r, tc.rcode)
w.WriteMsg(m)
}
return tc.status, tc.err
}),
every: 1,
EndpointType: "datadog",
tagSet: datadogTagSet,
}
ctx := context.TODO()
if _, err := tr.ServeDNS(ctx, w, tc.question); err != nil && tc.err == nil {
t.Fatalf("Error during tr.ServeDNS(ctx, w, %v): %v", tc.question, err)
}
spans := mt.FinishedSpans()
if len(spans) == 0 {
t.Fatal("Expected at least one span, got none")
}
// Find the DNS span
var dnsSpan *mocktracer.Span
for _, span := range spans {
if span.OperationName() == defaultTopLevelSpanName {
dnsSpan = span
break
}
}
if dnsSpan == nil {
t.Fatal("Could not find DNS span with operation name 'servedns'")
}
req := request.Request{W: w, Req: tc.question}
// Test DataDog-specific tags
if dnsSpan.Tag(datadogTagSet.Name) != req.Name() {
t.Errorf("Unexpected span tag: span.Tag(%v): want %v, got %v",
datadogTagSet.Name, req.Name(), dnsSpan.Tag(datadogTagSet.Name))
}
if dnsSpan.Tag(datadogTagSet.Type) != req.Type() {
t.Errorf("Unexpected span tag: span.Tag(%v): want %v, got %v",
datadogTagSet.Type, req.Type(), dnsSpan.Tag(datadogTagSet.Type))
}
if dnsSpan.Tag(datadogTagSet.Proto) != req.Proto() {
t.Errorf("Unexpected span tag: span.Tag(%v): want %v, got %v",
datadogTagSet.Proto, req.Proto(), dnsSpan.Tag(datadogTagSet.Proto))
}
if dnsSpan.Tag(datadogTagSet.Remote) != req.IP() {
t.Errorf("Unexpected span tag: span.Tag(%v): want %v, got %v",
datadogTagSet.Remote, req.IP(), dnsSpan.Tag(datadogTagSet.Remote))
}
if dnsSpan.Tag(datadogTagSet.Rcode) != rcode.ToString(tc.rcode) {
t.Errorf("Unexpected span tag: span.Tag(%v): want %v, got %v",
datadogTagSet.Rcode, rcode.ToString(tc.rcode), dnsSpan.Tag(datadogTagSet.Rcode))
}
// Test DataDog v2 error handling
if tc.err != nil {
errorMsg := dnsSpan.Tag("error.message")
if errorMsg == nil {
t.Error("Expected error.message tag to be set")
} else if !strings.Contains(errorMsg.(string), "test error") {
t.Errorf("Expected error.message to contain 'test error', got %v", errorMsg)
}
// Check error type tag
errorType := dnsSpan.Tag("error.type")
if errorType == nil {
t.Error("Expected error.type tag to be set")
}
}
// Verify trace ID exists (mocktracer uses uint64)
traceID := dnsSpan.TraceID()
if traceID == 0 {
t.Error("Expected non-zero trace ID")
}
})
}
}