9
9
describe OpenTelemetry ::Resource ::Detector ::AWS do
10
10
let ( :detector ) { OpenTelemetry ::Resource ::Detector ::AWS }
11
11
12
+ RESOURCE = OpenTelemetry ::SemanticConventions ::Resource
13
+
12
14
describe '.detect' do
13
15
before do
14
16
WebMock . disable_net_connect!
22
24
. with ( headers : { 'Accept' => '*/*' } )
23
25
. to_return ( status : 404 , body : 'Not Found' )
24
26
25
- # Clear environment variables for ECS
27
+ # Clear environment variables for ECS and Lambda
26
28
@original_env = ENV . to_hash
27
29
ENV . delete ( 'ECS_CONTAINER_METADATA_URI' )
28
30
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' )
29
34
end
30
35
31
36
after do
@@ -53,6 +58,10 @@ def assert_detection_result(detectors)
53
58
assert_detection_result ( [ :ecs ] )
54
59
end
55
60
61
+ it 'returns an empty resource when Lambda detection fails' do
62
+ assert_detection_result ( [ :lambda ] )
63
+ end
64
+
56
65
it 'returns an empty resource with unknown detector' do
57
66
assert_detection_result ( [ :unknown ] )
58
67
end
@@ -93,14 +102,14 @@ def assert_detection_result(detectors)
93
102
resource = detector . detect ( [ :ec2 ] )
94
103
attributes = resource . attribute_enumerator . to_h
95
104
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 )
104
113
end
105
114
106
115
describe 'with succesefful ECS detection' do
@@ -147,8 +156,8 @@ def assert_detection_result(detectors)
147
156
resource = detector . detect ( [ :ecs ] )
148
157
attributes = resource . attribute_enumerator . to_h
149
158
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' )
152
161
end
153
162
end
154
163
end
@@ -164,13 +173,56 @@ def assert_detection_result(detectors)
164
173
attributes = resource . attribute_enumerator . to_h
165
174
166
175
# 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' )
169
178
_ ( attributes [ 'ec2.instance.id' ] ) . must_equal ( 'i-1234567890abcdef0' )
170
179
end
171
180
end
172
181
end
173
182
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
174
226
end
175
227
end
176
228
end
0 commit comments