@@ -116,6 +116,8 @@ def _run_patch_mechanism_tests(self):
116116 """
117117 self ._test_botocore_installed_flag ()
118118 self ._reset_mocks ()
119+ self ._test_resource_detector_patches ()
120+ self ._reset_mocks ()
119121
120122 def _test_unpatched_botocore_instrumentation (self ):
121123 # Kinesis
@@ -352,6 +354,56 @@ def _test_patched_bedrock_agent_instrumentation(self):
352354 self .assertEqual (len (bedrock_agent_success_attributes ), 1 )
353355 self .assertEqual (bedrock_agent_success_attributes [attribute_tuple [0 ]], attribute_tuple [1 ])
354356
357+ def _test_resource_detector_patches (self ):
358+ """Test that resource detector patches are applied and work correctly"""
359+ import opentelemetry .sdk .extension .aws .resource .ec2 as ec2_resource
360+ import opentelemetry .sdk .extension .aws .resource .eks as eks_resource
361+
362+ # Test that the functions were patched
363+ self .assertIsNotNone (ec2_resource ._aws_http_request )
364+ self .assertIsNotNone (eks_resource ._aws_http_request )
365+
366+ # Test EC2 patched function
367+ with patch ("amazon.opentelemetry.distro.patches._resource_detector_patches.urlopen" ) as mock_urlopen :
368+ mock_response = MagicMock ()
369+ mock_response .read .return_value = b'{"test": "ec2-data"}'
370+ mock_urlopen .return_value .__enter__ .return_value = mock_response
371+
372+ result = ec2_resource ._aws_http_request ("GET" , "/test/path" , {"X-Test" : "header" })
373+ self .assertEqual (result , '{"test": "ec2-data"}' )
374+
375+ # Verify the request was made correctly
376+ args , kwargs = mock_urlopen .call_args
377+ request = args [0 ]
378+ self .assertEqual (request .full_url , "http://169.254.169.254/test/path" )
379+ self .assertEqual (request .headers , {"X-test" : "header" })
380+ self .assertEqual (kwargs ["timeout" ], 5 )
381+
382+ # Test EKS patched function
383+ with patch ("amazon.opentelemetry.distro.patches._resource_detector_patches.urlopen" ) as mock_urlopen , patch (
384+ "amazon.opentelemetry.distro.patches._resource_detector_patches.ssl.create_default_context"
385+ ) as mock_ssl :
386+ mock_response = MagicMock ()
387+ mock_response .read .return_value = b'{"test": "eks-data"}'
388+ mock_urlopen .return_value .__enter__ .return_value = mock_response
389+
390+ mock_context = MagicMock ()
391+ mock_ssl .return_value = mock_context
392+
393+ result = eks_resource ._aws_http_request ("GET" , "/api/v1/test" , "Bearer token123" )
394+ self .assertEqual (result , '{"test": "eks-data"}' )
395+
396+ # Verify the request was made correctly
397+ args , kwargs = mock_urlopen .call_args
398+ request = args [0 ]
399+ self .assertEqual (request .full_url , "https://kubernetes.default.svc/api/v1/test" )
400+ self .assertEqual (request .headers , {"Authorization" : "Bearer token123" })
401+ self .assertEqual (kwargs ["timeout" ], 5 )
402+ self .assertEqual (kwargs ["context" ], mock_context )
403+
404+ # Verify SSL context was created with correct CA file
405+ mock_ssl .assert_called_once_with (cafile = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt" )
406+
355407 def _reset_mocks (self ):
356408 for method_patch in self .method_patches .values ():
357409 method_patch .reset_mock ()
0 commit comments