99describe OpenTelemetry ::Resource ::Detector ::AWS do
1010 let ( :detector ) { OpenTelemetry ::Resource ::Detector ::AWS }
1111
12+ RESOURCE = OpenTelemetry ::SemanticConventions ::Resource
13+
1214 describe '.detect' do
1315 before do
1416 WebMock . disable_net_connect!
2224 . with ( headers : { 'Accept' => '*/*' } )
2325 . to_return ( status : 404 , body : 'Not Found' )
2426
25- # Clear environment variables for ECS
27+ # Clear environment variables for ECS and Lambda
2628 @original_env = ENV . to_hash
2729 ENV . delete ( 'ECS_CONTAINER_METADATA_URI' )
2830 ENV . delete ( 'ECS_CONTAINER_METADATA_URI_V4' )
31+ ENV . delete ( 'AWS_LAMBDA_FUNCTION_NAME' )
32+ ENV . delete ( 'AWS_LAMBDA_FUNCTION_VERSION' )
33+ ENV . delete ( 'AWS_LAMBDA_LOG_STREAM_NAME' )
2934 end
3035
3136 after do
@@ -53,6 +58,10 @@ def assert_detection_result(detectors)
5358 assert_detection_result ( [ :ecs ] )
5459 end
5560
61+ it 'returns an empty resource when Lambda detection fails' do
62+ assert_detection_result ( [ :lambda ] )
63+ end
64+
5665 it 'returns an empty resource with unknown detector' do
5766 assert_detection_result ( [ :unknown ] )
5867 end
@@ -93,14 +102,14 @@ def assert_detection_result(detectors)
93102 resource = detector . detect ( [ :ec2 ] )
94103 attributes = resource . attribute_enumerator . to_h
95104
96- _ ( attributes [ OpenTelemetry :: SemanticConventions :: Resource ::CLOUD_PROVIDER ] ) . must_equal ( 'aws' )
97- _ ( attributes [ OpenTelemetry :: SemanticConventions :: Resource ::CLOUD_PLATFORM ] ) . must_equal ( 'aws_ec2' )
98- _ ( attributes [ OpenTelemetry :: SemanticConventions :: Resource ::CLOUD_ACCOUNT_ID ] ) . must_equal ( '123456789012' )
99- _ ( attributes [ OpenTelemetry :: SemanticConventions :: Resource ::CLOUD_REGION ] ) . must_equal ( 'us-west-2' )
100- _ ( attributes [ OpenTelemetry :: SemanticConventions :: Resource ::CLOUD_AVAILABILITY_ZONE ] ) . must_equal ( 'us-west-2b' )
101- _ ( attributes [ OpenTelemetry :: SemanticConventions :: Resource ::HOST_ID ] ) . must_equal ( 'i-1234567890abcdef0' )
102- _ ( attributes [ OpenTelemetry :: SemanticConventions :: Resource ::HOST_TYPE ] ) . must_equal ( 'm5.xlarge' )
103- _ ( attributes [ OpenTelemetry :: SemanticConventions :: Resource ::HOST_NAME ] ) . must_equal ( hostname )
105+ _ ( attributes [ RESOURCE ::CLOUD_PROVIDER ] ) . must_equal ( 'aws' )
106+ _ ( attributes [ RESOURCE ::CLOUD_PLATFORM ] ) . must_equal ( 'aws_ec2' )
107+ _ ( attributes [ RESOURCE ::CLOUD_ACCOUNT_ID ] ) . must_equal ( '123456789012' )
108+ _ ( attributes [ RESOURCE ::CLOUD_REGION ] ) . must_equal ( 'us-west-2' )
109+ _ ( attributes [ RESOURCE ::CLOUD_AVAILABILITY_ZONE ] ) . must_equal ( 'us-west-2b' )
110+ _ ( attributes [ RESOURCE ::HOST_ID ] ) . must_equal ( 'i-1234567890abcdef0' )
111+ _ ( attributes [ RESOURCE ::HOST_TYPE ] ) . must_equal ( 'm5.xlarge' )
112+ _ ( attributes [ RESOURCE ::HOST_NAME ] ) . must_equal ( hostname )
104113 end
105114
106115 describe 'with succesefful ECS detection' do
@@ -147,8 +156,8 @@ def assert_detection_result(detectors)
147156 resource = detector . detect ( [ :ecs ] )
148157 attributes = resource . attribute_enumerator . to_h
149158
150- _ ( attributes [ OpenTelemetry :: SemanticConventions :: Resource ::CLOUD_PROVIDER ] ) . must_equal ( 'aws' )
151- _ ( attributes [ OpenTelemetry :: SemanticConventions :: Resource ::CLOUD_PLATFORM ] ) . must_equal ( 'aws_ecs' )
159+ _ ( attributes [ RESOURCE ::CLOUD_PROVIDER ] ) . must_equal ( 'aws' )
160+ _ ( attributes [ RESOURCE ::CLOUD_PLATFORM ] ) . must_equal ( 'aws_ecs' )
152161 end
153162 end
154163 end
@@ -164,13 +173,56 @@ def assert_detection_result(detectors)
164173 attributes = resource . attribute_enumerator . to_h
165174
166175 # Should include attributes from both detectors
167- _ ( attributes [ OpenTelemetry :: SemanticConventions :: Resource ::CLOUD_PROVIDER ] ) . must_equal ( 'aws' )
168- _ ( attributes [ OpenTelemetry :: SemanticConventions :: Resource ::CLOUD_PLATFORM ] ) . must_equal ( 'aws_ecs' )
176+ _ ( attributes [ RESOURCE ::CLOUD_PROVIDER ] ) . must_equal ( 'aws' )
177+ _ ( attributes [ RESOURCE ::CLOUD_PLATFORM ] ) . must_equal ( 'aws_ecs' )
169178 _ ( attributes [ 'ec2.instance.id' ] ) . must_equal ( 'i-1234567890abcdef0' )
170179 end
171180 end
172181 end
173182 end
183+
184+ describe 'with successful Lambda detection' do
185+ before do
186+ # Set Lambda environment variables
187+ ENV [ 'AWS_LAMBDA_FUNCTION_NAME' ] = 'my-function'
188+ ENV [ 'AWS_LAMBDA_FUNCTION_VERSION' ] = '$LATEST'
189+ ENV [ 'AWS_LAMBDA_LOG_STREAM_NAME' ] = '2025/01/01/[$LATEST]abcdef123456'
190+ ENV [ 'AWS_REGION' ] = 'us-east-1'
191+ ENV [ 'AWS_LAMBDA_FUNCTION_MEMORY_SIZE' ] = '512'
192+ end
193+
194+ it 'detects Lambda resources when specified' do
195+ resource = detector . detect ( [ :lambda ] )
196+ attributes = resource . attribute_enumerator . to_h
197+
198+ _ ( attributes [ RESOURCE ::CLOUD_PROVIDER ] ) . must_equal ( 'aws' )
199+ _ ( attributes [ RESOURCE ::CLOUD_PLATFORM ] ) . must_equal ( 'aws_lambda' )
200+ _ ( attributes [ RESOURCE ::CLOUD_REGION ] ) . must_equal ( 'us-east-1' )
201+ _ ( attributes [ RESOURCE ::FAAS_NAME ] ) . must_equal ( 'my-function' )
202+ _ ( attributes [ RESOURCE ::FAAS_VERSION ] ) . must_equal ( '$LATEST' )
203+ _ ( attributes [ RESOURCE ::FAAS_INSTANCE ] ) . must_equal ( '2025/01/01/[$LATEST]abcdef123456' )
204+ _ ( attributes [ RESOURCE ::FAAS_MAX_MEMORY ] ) . must_equal ( 512 )
205+ end
206+
207+ it 'detects multiple resources when specified' do
208+ # Create a mock EC2 resource
209+ ec2_resource = OpenTelemetry ::SDK ::Resources ::Resource . create ( {
210+ RESOURCE ::HOST_ID => 'i-1234567890abcdef0'
211+ } )
212+
213+ # Stub EC2 detection to return the mock resource
214+ OpenTelemetry ::Resource ::Detector ::AWS ::EC2 . stub :detect , ec2_resource do
215+ resource = detector . detect ( %i[ ec2 lambda ] )
216+ attributes = resource . attribute_enumerator . to_h
217+
218+ # Should include attributes from both detectors
219+ _ ( attributes [ RESOURCE ::CLOUD_PROVIDER ] ) . must_equal ( 'aws' )
220+ _ ( attributes [ RESOURCE ::CLOUD_PLATFORM ] ) . must_equal ( 'aws_lambda' )
221+ _ ( attributes [ RESOURCE ::FAAS_NAME ] ) . must_equal ( 'my-function' )
222+ _ ( attributes [ RESOURCE ::HOST_ID ] ) . must_equal ( 'i-1234567890abcdef0' )
223+ end
224+ end
225+ end
174226 end
175227 end
176228 end
0 commit comments