@@ -703,6 +703,82 @@ def customize_exporter_test(
703
703
for key in config .keys ():
704
704
os .environ .pop (key , None )
705
705
706
+ def test_validate_logs_headers_infers_from_resource_attributes (self ):
707
+ """Test that _validate_logs_headers can infer headers from OTEL_RESOURCE_ATTRIBUTES"""
708
+ from amazon .opentelemetry .distro .aws_opentelemetry_configurator import (
709
+ _validate_logs_headers ,
710
+ _parse_resource_attributes ,
711
+ )
712
+
713
+ # Test _parse_resource_attributes function
714
+ os .environ ["OTEL_RESOURCE_ATTRIBUTES" ] = (
715
+ "service.name=TestService,aws.log.group.names=/aws/genesis/TestAgent,aws.log.stream.names=test-stream"
716
+ )
717
+ attrs = _parse_resource_attributes ()
718
+ self .assertEqual (attrs ["service.name" ], "TestService" )
719
+ self .assertEqual (attrs ["aws.log.group.names" ], "/aws/genesis/TestAgent" )
720
+ self .assertEqual (attrs ["aws.log.stream.names" ], "test-stream" )
721
+
722
+ # Test inference when AGENT_OBSERVABILITY_ENABLED is true and OTEL_EXPORTER_OTLP_LOGS_HEADERS is not set
723
+ os .environ ["AGENT_OBSERVABILITY_ENABLED" ] = "true"
724
+ os .environ ["OTEL_RESOURCE_ATTRIBUTES" ] = (
725
+ "service.name=TestService,aws.log.group.names=/aws/genesis/TestAgent,aws.log.stream.names=test-stream"
726
+ )
727
+ os .environ .pop ("OTEL_EXPORTER_OTLP_LOGS_HEADERS" , None )
728
+
729
+ result = _validate_logs_headers ()
730
+ self .assertTrue (result .is_valid )
731
+ self .assertEqual (result .log_group , "/aws/genesis/TestAgent" )
732
+ self .assertEqual (result .log_stream , "test-stream" )
733
+
734
+ # Clean up
735
+ os .environ .pop ("AGENT_OBSERVABILITY_ENABLED" , None )
736
+ os .environ .pop ("OTEL_RESOURCE_ATTRIBUTES" , None )
737
+
738
+ def test_validate_logs_headers_explicit_takes_priority (self ):
739
+ """Test that explicit OTEL_EXPORTER_OTLP_LOGS_HEADERS takes priority over inference"""
740
+ from amazon .opentelemetry .distro .aws_opentelemetry_configurator import _validate_logs_headers
741
+
742
+ # Set both explicit headers and resource attributes
743
+ os .environ ["AGENT_OBSERVABILITY_ENABLED" ] = "true"
744
+ os .environ ["OTEL_RESOURCE_ATTRIBUTES" ] = (
745
+ "aws.log.group.names=/aws/genesis/InferredAgent,aws.log.stream.names=inferred-stream"
746
+ )
747
+ os .environ ["OTEL_EXPORTER_OTLP_LOGS_HEADERS" ] = (
748
+ "x-aws-log-group=/aws/genesis/ExplicitAgent,x-aws-log-stream=explicit-stream"
749
+ )
750
+
751
+ result = _validate_logs_headers ()
752
+ self .assertTrue (result .is_valid )
753
+ # Should use explicit headers, not inferred ones
754
+ self .assertEqual (result .log_group , "/aws/genesis/ExplicitAgent" )
755
+ self .assertEqual (result .log_stream , "explicit-stream" )
756
+
757
+ # Clean up
758
+ os .environ .pop ("AGENT_OBSERVABILITY_ENABLED" , None )
759
+ os .environ .pop ("OTEL_RESOURCE_ATTRIBUTES" , None )
760
+ os .environ .pop ("OTEL_EXPORTER_OTLP_LOGS_HEADERS" , None )
761
+
762
+ def test_validate_logs_headers_no_inference_when_agent_observability_disabled (self ):
763
+ """Test that no inference happens when AGENT_OBSERVABILITY_ENABLED is false"""
764
+ from amazon .opentelemetry .distro .aws_opentelemetry_configurator import _validate_logs_headers
765
+
766
+ # Set resource attributes but keep agent observability disabled
767
+ os .environ ["AGENT_OBSERVABILITY_ENABLED" ] = "false"
768
+ os .environ ["OTEL_RESOURCE_ATTRIBUTES" ] = (
769
+ "aws.log.group.names=/aws/genesis/TestAgent,aws.log.stream.names=test-stream"
770
+ )
771
+ os .environ .pop ("OTEL_EXPORTER_OTLP_LOGS_HEADERS" , None )
772
+
773
+ result = _validate_logs_headers ()
774
+ self .assertFalse (result .is_valid )
775
+ self .assertIsNone (result .log_group )
776
+ self .assertIsNone (result .log_stream )
777
+
778
+ # Clean up
779
+ os .environ .pop ("AGENT_OBSERVABILITY_ENABLED" , None )
780
+ os .environ .pop ("OTEL_RESOURCE_ATTRIBUTES" , None )
781
+
706
782
707
783
def validate_distro_environ ():
708
784
tc : TestCase = TestCase ()
0 commit comments