Skip to content

Commit e2b272e

Browse files
authored
Merge branch 'main' into david.ogbureke/add-lambda-readme
2 parents f182b1a + e334908 commit e2b272e

File tree

4 files changed

+97
-15
lines changed

4 files changed

+97
-15
lines changed

internal/appsec/listener/httpsec/roundtripper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (feature *DownwardRequestFeature) OnStart(op *httpsec.RoundTripOperation, a
7171
builder := addresses.NewAddressesBuilder().
7272
WithDownwardURL(args.URL).
7373
WithDownwardMethod(args.Method).
74-
WithDownwardRequestHeaders(args.Headers)
74+
WithDownwardRequestHeaders(headersToLower(args.Headers))
7575

7676
// Increment the span metric for downward requests
7777
op.HandlerOp.ContextOperation.GetMetricsInstance().SumDownstreamRequestsCalls.Add(1)

internal/appsec/testdata/api10.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@
413413
{
414414
"address": "server.io.net.request.headers",
415415
"key_path": [
416-
"Witness"
416+
"witness"
417417
]
418418
}
419419
],

internal/config/config.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -166,24 +166,24 @@ func loadConfig() *Config {
166166
}
167167
}
168168

169-
hostname, err := os.Hostname()
170-
if err != nil {
171-
log.Warn("unable to look up hostname: %s", err.Error())
172-
cfg.hostnameLookupError = err
169+
// Hostname lookup, if DD_TRACE_REPORT_HOSTNAME is true
170+
// If the hostname lookup fails, an error is set and the hostname is not reported
171+
// The tracer will fail to start if the hostname lookup fails when it is explicitly configured
172+
// to report the hostname.
173+
if provider.getBool("DD_TRACE_REPORT_HOSTNAME", false) {
174+
hostname, err := os.Hostname()
175+
if err != nil {
176+
log.Warn("unable to look up hostname: %s", err.Error())
177+
cfg.hostnameLookupError = err
178+
}
179+
cfg.hostname = hostname
180+
cfg.reportHostname = true
173181
}
174-
175-
// Always read DD_TRACE_REPORT_HOSTNAME for telemetry tracking
176-
reportHostnameFromEnv := provider.getBool("DD_TRACE_REPORT_HOSTNAME", false)
177-
178-
// Check if DD_TRACE_SOURCE_HOSTNAME was explicitly set
182+
// Check if DD_TRACE_SOURCE_HOSTNAME was explicitly set, it takes precedence over the hostname lookup
179183
if sourceHostname, ok := env.Lookup("DD_TRACE_SOURCE_HOSTNAME"); ok {
180184
// Explicitly configured hostname - always report it
181185
cfg.hostname = sourceHostname
182186
cfg.reportHostname = true
183-
} else if err == nil {
184-
// Auto-detected hostname - only report if DD_TRACE_REPORT_HOSTNAME=true
185-
cfg.hostname = hostname
186-
cfg.reportHostname = reportHostnameFromEnv
187187
}
188188

189189
return cfg

internal/config/config_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,3 +431,85 @@ func TestSetServiceMappingReportsFullList(t *testing.T) {
431431
sort.Strings(parts)
432432
assert.Equal(t, []string{"a:3", "b:2"}, parts)
433433
}
434+
435+
func TestHostnameConfiguration(t *testing.T) {
436+
t.Run("default behavior - hostname empty when not configured", func(t *testing.T) {
437+
resetGlobalState()
438+
defer resetGlobalState()
439+
440+
cfg := Get()
441+
require.NotNil(t, cfg)
442+
443+
assert.Empty(t, cfg.Hostname(), "Hostname should be empty by default")
444+
assert.False(t, cfg.ReportHostname(), "ReportHostname should be false by default")
445+
})
446+
447+
t.Run("DD_TRACE_REPORT_HOSTNAME=true enables hostname lookup", func(t *testing.T) {
448+
resetGlobalState()
449+
defer resetGlobalState()
450+
451+
t.Setenv("DD_TRACE_REPORT_HOSTNAME", "true")
452+
453+
cfg := Get()
454+
require.NotNil(t, cfg)
455+
456+
assert.NotEmpty(t, cfg.Hostname(), "Hostname should be set when DD_TRACE_REPORT_HOSTNAME=true")
457+
assert.True(t, cfg.ReportHostname(), "ReportHostname should be true when DD_TRACE_REPORT_HOSTNAME=true")
458+
assert.NoError(t, cfg.HostnameLookupError(), "HostnameLookupError should be nil on successful lookup")
459+
})
460+
461+
t.Run("DD_TRACE_REPORT_HOSTNAME=false keeps hostname empty", func(t *testing.T) {
462+
resetGlobalState()
463+
defer resetGlobalState()
464+
465+
t.Setenv("DD_TRACE_REPORT_HOSTNAME", "false")
466+
467+
cfg := Get()
468+
require.NotNil(t, cfg)
469+
470+
assert.Empty(t, cfg.Hostname(), "Hostname should be empty when DD_TRACE_REPORT_HOSTNAME=false")
471+
assert.False(t, cfg.ReportHostname(), "ReportHostname should be false when DD_TRACE_REPORT_HOSTNAME=false")
472+
})
473+
474+
t.Run("DD_TRACE_SOURCE_HOSTNAME sets explicit hostname", func(t *testing.T) {
475+
resetGlobalState()
476+
defer resetGlobalState()
477+
478+
t.Setenv("DD_TRACE_SOURCE_HOSTNAME", "custom-hostname")
479+
480+
cfg := Get()
481+
require.NotNil(t, cfg)
482+
483+
assert.Equal(t, "custom-hostname", cfg.Hostname(), "Hostname should match DD_TRACE_SOURCE_HOSTNAME")
484+
assert.True(t, cfg.ReportHostname(), "ReportHostname should be true when DD_TRACE_SOURCE_HOSTNAME is set")
485+
})
486+
487+
t.Run("DD_TRACE_SOURCE_HOSTNAME takes precedence over DD_TRACE_REPORT_HOSTNAME", func(t *testing.T) {
488+
resetGlobalState()
489+
defer resetGlobalState()
490+
491+
t.Setenv("DD_TRACE_REPORT_HOSTNAME", "true")
492+
t.Setenv("DD_TRACE_SOURCE_HOSTNAME", "override-hostname")
493+
494+
cfg := Get()
495+
require.NotNil(t, cfg)
496+
497+
assert.Equal(t, "override-hostname", cfg.Hostname(), "DD_TRACE_SOURCE_HOSTNAME should take precedence")
498+
assert.True(t, cfg.ReportHostname(), "ReportHostname should be true")
499+
})
500+
501+
t.Run("empty DD_TRACE_SOURCE_HOSTNAME is used when explicitly set", func(t *testing.T) {
502+
resetGlobalState()
503+
defer resetGlobalState()
504+
505+
t.Setenv("DD_TRACE_REPORT_HOSTNAME", "true")
506+
t.Setenv("DD_TRACE_SOURCE_HOSTNAME", "")
507+
508+
cfg := Get()
509+
require.NotNil(t, cfg)
510+
511+
// Empty string explicitly set should override the looked-up hostname
512+
assert.Empty(t, cfg.Hostname(), "Empty DD_TRACE_SOURCE_HOSTNAME should override hostname lookup")
513+
assert.True(t, cfg.ReportHostname(), "ReportHostname should be true when DD_TRACE_SOURCE_HOSTNAME is explicitly set")
514+
})
515+
}

0 commit comments

Comments
 (0)