Skip to content

Commit

Permalink
ddtrace/tracer: fix trace_agent_url on startup log (#2949)
Browse files Browse the repository at this point in the history
Co-authored-by: Dario Castañé <[email protected]>
  • Loading branch information
rachelyangdog and darccio authored Nov 8, 2024
1 parent 2991277 commit 586a0bb
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
9 changes: 8 additions & 1 deletion ddtrace/tracer/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand Down
75 changes: 75 additions & 0 deletions ddtrace/tracer/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package tracer
import (
"fmt"
"math"
"regexp"
"testing"

"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
Expand Down Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions ddtrace/tracer/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 586a0bb

Please sign in to comment.