@@ -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