From 586a0bb9122e875ec2d0ed772533abfdd9ea8ec0 Mon Sep 17 00:00:00 2001 From: Rachel Yang Date: Fri, 8 Nov 2024 11:30:07 -0500 Subject: [PATCH] ddtrace/tracer: fix trace_agent_url on startup log (#2949) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Dario Castañé --- ddtrace/tracer/log.go | 9 ++++- ddtrace/tracer/log_test.go | 75 ++++++++++++++++++++++++++++++++++++++ ddtrace/tracer/option.go | 4 ++ 3 files changed, 87 insertions(+), 1 deletion(-) diff --git a/ddtrace/tracer/log.go b/ddtrace/tracer/log.go index f64d2dadee..79b538e958 100644 --- a/ddtrace/tracer/log.go +++ b/ddtrace/tracer/log.go @@ -105,6 +105,13 @@ func logStartup(t *tracer) { injectorNames = "custom" extractorNames = "custom" } + // Determine the agent URL to use in the logs + var agentURL string + if t.config.originalAgentURL != nil && t.config.originalAgentURL.Scheme == "unix" { + agentURL = t.config.originalAgentURL.String() + } else { + agentURL = t.config.transport.endpoint() + } info := startupInfo{ Date: time.Now().Format(time.RFC3339), @@ -115,7 +122,7 @@ func logStartup(t *tracer) { LangVersion: runtime.Version(), Env: t.config.env, Service: t.config.serviceName, - AgentURL: t.config.transport.endpoint(), + AgentURL: agentURL, Debug: t.config.debug, AnalyticsEnabled: !math.IsNaN(globalconfig.AnalyticsRate()), SampleRate: fmt.Sprintf("%f", t.rulesSampling.traces.globalRate), diff --git a/ddtrace/tracer/log_test.go b/ddtrace/tracer/log_test.go index 3bc2998569..a0c0d0597c 100644 --- a/ddtrace/tracer/log_test.go +++ b/ddtrace/tracer/log_test.go @@ -8,6 +8,7 @@ package tracer import ( "fmt" "math" + "regexp" "testing" "gopkg.in/DataDog/dd-trace-go.v1/ddtrace" @@ -261,3 +262,77 @@ func setup(t *testing.T, customProp Propagator) string { require.Len(t, tp.Logs(), 2) return tp.Logs()[1] } + +func findLogEntry(logs []string, pattern string) (string, bool) { + for _, log := range logs { + if matched, _ := regexp.MatchString(pattern, log); matched { + return log, true + } + } + return "", false +} + +func TestAgentURL(t *testing.T) { + assert := assert.New(t) + tp := new(log.RecordLogger) + tracer := newTracer(WithLogger(tp), WithUDS("var/run/datadog/apm.socket")) + defer tracer.Stop() + tp.Reset() + tp.Ignore("appsec: ", telemetry.LogPrefix) + logStartup(tracer) + logEntry, found := findLogEntry(tp.Logs(), `"agent_url":"unix://var/run/datadog/apm.socket"`) + if !found { + t.Fatal("Expected to find log entry") + } + assert.Regexp(`"agent_url":"unix://var/run/datadog/apm.socket"`, logEntry) +} + +func TestAgentURLFromEnv(t *testing.T) { + assert := assert.New(t) + t.Setenv("DD_TRACE_AGENT_URL", "unix://var/run/datadog/apm.socket") + tp := new(log.RecordLogger) + tracer := newTracer(WithLogger(tp)) + defer tracer.Stop() + tp.Reset() + tp.Ignore("appsec: ", telemetry.LogPrefix) + logStartup(tracer) + logEntry, found := findLogEntry(tp.Logs(), `"agent_url":"unix://var/run/datadog/apm.socket"`) + if !found { + t.Fatal("Expected to find log entry") + } + assert.Regexp(`"agent_url":"unix://var/run/datadog/apm.socket"`, logEntry) +} + +func TestInvalidAgentURL(t *testing.T) { + assert := assert.New(t) + // invalid socket URL + t.Setenv("DD_TRACE_AGENT_URL", "var/run/datadog/apm.socket") + tp := new(log.RecordLogger) + tracer := newTracer(WithLogger(tp)) + defer tracer.Stop() + tp.Reset() + tp.Ignore("appsec: ", telemetry.LogPrefix) + logStartup(tracer) + logEntry, found := findLogEntry(tp.Logs(), `"agent_url":"http://localhost:8126/v0.4/traces"`) + if !found { + t.Fatal("Expected to find log entry") + } + // assert that it is the default URL + assert.Regexp(`"agent_url":"http://localhost:8126/v0.4/traces"`, logEntry) +} + +func TestAgentURLConflict(t *testing.T) { + assert := assert.New(t) + t.Setenv("DD_TRACE_AGENT_URL", "unix://var/run/datadog/apm.socket") + tp := new(log.RecordLogger) + tracer := newTracer(WithLogger(tp), WithUDS("var/run/datadog/apm.socket"), WithAgentAddr("localhost:8126")) + defer tracer.Stop() + tp.Reset() + tp.Ignore("appsec: ", telemetry.LogPrefix) + logStartup(tracer) + logEntry, found := findLogEntry(tp.Logs(), `"agent_url":"http://localhost:8126/v0.4/traces"`) + if !found { + t.Fatal("Expected to find log entry") + } + assert.Regexp(`"agent_url":"http://localhost:8126/v0.4/traces"`, logEntry) +} diff --git a/ddtrace/tracer/option.go b/ddtrace/tracer/option.go index 8415b29ee5..e9bb4bf120 100644 --- a/ddtrace/tracer/option.go +++ b/ddtrace/tracer/option.go @@ -160,6 +160,9 @@ type config struct { // agentURL is the agent URL that receives traces from the tracer. agentURL *url.URL + // originalAgentURL is the agent URL that receives traces from the tracer and does not get changed. + originalAgentURL *url.URL + // serviceMappings holds a set of service mappings to dynamically rename services serviceMappings map[string]string @@ -457,6 +460,7 @@ func newConfig(opts ...StartOption) *config { if c.agentURL == nil { c.agentURL = internal.AgentURLFromEnv() } + c.originalAgentURL = c.agentURL // Preserve the original agent URL for logging if c.httpClient == nil { if c.agentURL.Scheme == "unix" { // If we're connecting over UDS we can just rely on the agent to provide the hostname