|
6 | 6 |
|
7 | 7 | require 'test_helper' |
8 | 8 |
|
| 9 | +describe OpenTelemetry::Resource::Detector::AWS do |
| 10 | + let(:detector) { OpenTelemetry::Resource::Detector::AWS } |
| 11 | + |
| 12 | + RESOURCE = OpenTelemetry::SemanticConventions::Resource |
| 13 | + |
| 14 | + describe '.detect' do |
| 15 | + before do |
| 16 | + WebMock.disable_net_connect! |
| 17 | + # Ensure we stub any potential requests to EC2 metadata service |
| 18 | + # Simulate failed token request |
| 19 | + stub_request(:put, 'http://169.254.169.254/latest/api/token') |
| 20 | + .to_timeout |
| 21 | + |
| 22 | + # Simulate failed identity document request |
| 23 | + stub_request(:get, 'http://169.254.169.254/latest/dynamic/instance-identity/document') |
| 24 | + .with(headers: { 'Accept' => '*/*' }) |
| 25 | + .to_return(status: 404, body: 'Not Found') |
| 26 | + |
| 27 | + # Clear environment variables for ECS and Lambda |
| 28 | + @original_env = ENV.to_hash |
| 29 | + ENV.delete('ECS_CONTAINER_METADATA_URI') |
| 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') |
| 34 | + end |
| 35 | + |
| 36 | + after do |
| 37 | + WebMock.allow_net_connect! |
| 38 | + ENV.replace(@original_env) |
| 39 | + end |
| 40 | + |
| 41 | + def assert_detection_result(detectors) |
| 42 | + resource = detector.detect(detectors) |
| 43 | + attributes = resource.attribute_enumerator.to_h |
| 44 | + |
| 45 | + _(resource).must_be_instance_of(OpenTelemetry::SDK::Resources::Resource) |
| 46 | + _(attributes).must_equal({}) |
| 47 | + end |
| 48 | + |
| 49 | + it 'returns an empty resource with no detectors' do |
| 50 | + assert_detection_result([]) |
| 51 | + end |
| 52 | + |
| 53 | + it 'returns an empty resource when EC2 detection fails' do |
| 54 | + assert_detection_result([:ec2]) |
| 55 | + end |
| 56 | + |
| 57 | + it 'returns an empty resource when ECS detection fails' do |
| 58 | + assert_detection_result([:ecs]) |
| 59 | + end |
| 60 | + |
| 61 | + it 'returns an empty resource when Lambda detection fails' do |
| 62 | + assert_detection_result([:lambda]) |
| 63 | + end |
| 64 | + |
| 65 | + it 'returns an empty resource with unknown detector' do |
| 66 | + assert_detection_result([:unknown]) |
| 67 | + end |
| 68 | + |
| 69 | + it 'returns an empty resource with multiple detectors when all fail' do |
| 70 | + assert_detection_result(%i[ec2 ecs unknown]) |
| 71 | + end |
| 72 | + |
| 73 | + describe 'with successful EC2 detection' do |
| 74 | + let(:token) { 'ec2-token-value' } |
| 75 | + let(:identity_document) do |
| 76 | + { |
| 77 | + 'instanceId' => 'i-1234567890abcdef0', |
| 78 | + 'instanceType' => 'm5.xlarge', |
| 79 | + 'accountId' => '123456789012', |
| 80 | + 'region' => 'us-west-2', |
| 81 | + 'availabilityZone' => 'us-west-2b' |
| 82 | + } |
| 83 | + end |
| 84 | + let(:hostname) { 'ip-10-0-0-1.ec2.internal' } |
| 85 | + |
| 86 | + before do |
| 87 | + # Stub successful token request (IMDSv2) |
| 88 | + stub_request(:put, 'http://169.254.169.254/latest/api/token') |
| 89 | + .to_return(status: 200, body: token) |
| 90 | + |
| 91 | + # Stub successful identity document request |
| 92 | + stub_request(:get, 'http://169.254.169.254/latest/dynamic/instance-identity/document') |
| 93 | + .to_return(status: 200, body: identity_document.to_json) |
| 94 | + |
| 95 | + # Stub successful hostname request |
| 96 | + stub_request(:get, 'http://169.254.169.254/latest/meta-data/hostname') |
| 97 | + .with(headers: { 'X-aws-ec2-metadata-token' => token }) |
| 98 | + .to_return(status: 200, body: hostname) |
| 99 | + end |
| 100 | + |
| 101 | + it 'detects EC2 resources when specified' do |
| 102 | + resource = detector.detect([:ec2]) |
| 103 | + attributes = resource.attribute_enumerator.to_h |
| 104 | + |
| 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) |
| 113 | + end |
| 114 | + |
| 115 | + describe 'with succesefful ECS detection' do |
| 116 | + let(:metadata_uri_v4) { 'http://169.254.170.2/v4' } |
| 117 | + let(:container_metadata) do |
| 118 | + { |
| 119 | + 'ContainerARN' => 'arn:aws:ecs:us-west-2:123456789012:container/container-id', |
| 120 | + 'LogDriver' => 'awslogs', |
| 121 | + 'LogOptions' => { |
| 122 | + 'awslogs-region' => 'us-west-2', |
| 123 | + 'awslogs-group' => 'my-log-group', |
| 124 | + 'awslogs-stream' => 'my-log-stream' |
| 125 | + } |
| 126 | + } |
| 127 | + end |
| 128 | + |
| 129 | + let(:task_metadata) do |
| 130 | + { |
| 131 | + 'Cluster' => 'my-cluster', |
| 132 | + 'TaskARN' => 'arn:aws:ecs:us-west-2:123456789012:task/task-id', |
| 133 | + 'Family' => 'my-task-family', |
| 134 | + 'Revision' => '1', |
| 135 | + 'LaunchType' => 'FARGATE' |
| 136 | + } |
| 137 | + end |
| 138 | + |
| 139 | + before do |
| 140 | + ENV['ECS_CONTAINER_METADATA_URI_V4'] = metadata_uri_v4 |
| 141 | + |
| 142 | + # Stub container metadata endpoint |
| 143 | + stub_request(:get, metadata_uri_v4) |
| 144 | + .to_return(status: 200, body: container_metadata.to_json) |
| 145 | + |
| 146 | + # Stub task metadata endpoint |
| 147 | + stub_request(:get, "#{metadata_uri_v4}/task") |
| 148 | + .to_return(status: 200, body: task_metadata.to_json) |
| 149 | + end |
| 150 | + |
| 151 | + it 'detects ECS resources when specified' do |
| 152 | + # Properly stub the fetch_container_id method |
| 153 | + OpenTelemetry::Resource::Detector::AWS::ECS.stub :fetch_container_id, '0123456789abcdef' * 4 do |
| 154 | + # Also stub the hostname method |
| 155 | + Socket.stub :gethostname, 'test-container' do |
| 156 | + resource = detector.detect([:ecs]) |
| 157 | + attributes = resource.attribute_enumerator.to_h |
| 158 | + |
| 159 | + _(attributes[RESOURCE::CLOUD_PROVIDER]).must_equal('aws') |
| 160 | + _(attributes[RESOURCE::CLOUD_PLATFORM]).must_equal('aws_ecs') |
| 161 | + end |
| 162 | + end |
| 163 | + end |
| 164 | + |
| 165 | + it 'returns combined resources when multiple detectors are used' do |
| 166 | + # Stub the container ID and hostname methods |
| 167 | + OpenTelemetry::Resource::Detector::AWS::ECS.stub :fetch_container_id, '0123456789abcdef' * 4 do |
| 168 | + Socket.stub :gethostname, 'test-container' do |
| 169 | + # Mock EC2 detector to return a simple resource |
| 170 | + ec2_resource = OpenTelemetry::SDK::Resources::Resource.create({ 'ec2.instance.id' => 'i-1234567890abcdef0' }) |
| 171 | + OpenTelemetry::Resource::Detector::AWS::EC2.stub :detect, ec2_resource do |
| 172 | + resource = detector.detect(%i[ec2 ecs]) |
| 173 | + attributes = resource.attribute_enumerator.to_h |
| 174 | + |
| 175 | + # Should include attributes from both detectors |
| 176 | + _(attributes[RESOURCE::CLOUD_PROVIDER]).must_equal('aws') |
| 177 | + _(attributes[RESOURCE::CLOUD_PLATFORM]).must_equal('aws_ecs') |
| 178 | + _(attributes['ec2.instance.id']).must_equal('i-1234567890abcdef0') |
| 179 | + end |
| 180 | + end |
| 181 | + end |
| 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 |
| 226 | + end |
| 227 | + end |
| 228 | + end |
| 229 | +end |
| 230 | + |
9 | 231 | describe OpenTelemetry::Resource::Detector::AWS::EKS do |
10 | 232 | let(:detector) { OpenTelemetry::Resource::Detector::AWS::EKS } |
11 | 233 | let(:token_path) { OpenTelemetry::Resource::Detector::AWS::EKS::TOKEN_PATH } |
|
0 commit comments