|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Copyright The OpenTelemetry Authors |
| 4 | +# |
| 5 | +# SPDX-License-Identifier: Apache-2.0 |
| 6 | + |
| 7 | +require 'net/http' |
| 8 | +require 'json' |
| 9 | + |
| 10 | +module OpenTelemetry |
| 11 | + module Resource |
| 12 | + module Detector |
| 13 | + module AWS |
| 14 | + # EC2 contains detect class method for determining EC2 resource attributes |
| 15 | + module EC2 |
| 16 | + extend self |
| 17 | + |
| 18 | + # EC2 metadata service endpoints and constants |
| 19 | + EC2_METADATA_HOST = '169.254.169.254' |
| 20 | + TOKEN_ENDPOINT = '/latest/api/token' |
| 21 | + IDENTITY_DOCUMENT_ENDPOINT = '/latest/dynamic/instance-identity/document' |
| 22 | + HOSTNAME_ENDPOINT = '/latest/meta-data/hostname' |
| 23 | + |
| 24 | + TOKEN_HEADER = 'X-aws-ec2-metadata-token' |
| 25 | + TOKEN_TTL_HEADER = 'X-aws-ec2-metadata-token-ttl-seconds' |
| 26 | + TOKEN_TTL_VALUE = '60' |
| 27 | + |
| 28 | + # Timeout in seconds for HTTP requests |
| 29 | + HTTP_TIMEOUT = 1 |
| 30 | + |
| 31 | + def detect |
| 32 | + # Placeholder for EC2 implementation |
| 33 | + resource_attributes = {} |
| 34 | + |
| 35 | + begin |
| 36 | + # Get IMDSv2 token - this will fail quickly if not on EC2 |
| 37 | + token = fetch_token |
| 38 | + return OpenTelemetry::SDK::Resources::Resource.create({}) if token.nil? |
| 39 | + |
| 40 | + # Get instance identity document which contains most metadata |
| 41 | + identity = fetch_identity_document(token) |
| 42 | + return OpenTelemetry::SDK::Resources::Resource.create({}) if identity.nil? |
| 43 | + |
| 44 | + hostname = fetch_hostname(token) |
| 45 | + |
| 46 | + # Set resource attributes from the identity document |
| 47 | + resource_attributes[OpenTelemetry::SemanticConventions::Resource::CLOUD_PROVIDER] = 'aws' |
| 48 | + resource_attributes[OpenTelemetry::SemanticConventions::Resource::CLOUD_PLATFORM] = 'aws_ec2' |
| 49 | + resource_attributes[OpenTelemetry::SemanticConventions::Resource::CLOUD_ACCOUNT_ID] = identity['accountId'] |
| 50 | + resource_attributes[OpenTelemetry::SemanticConventions::Resource::CLOUD_REGION] = identity['region'] |
| 51 | + resource_attributes[OpenTelemetry::SemanticConventions::Resource::CLOUD_AVAILABILITY_ZONE] = identity['availabilityZone'] |
| 52 | + |
| 53 | + resource_attributes[OpenTelemetry::SemanticConventions::Resource::HOST_ID] = identity['instanceId'] |
| 54 | + resource_attributes[OpenTelemetry::SemanticConventions::Resource::HOST_TYPE] = identity['instanceType'] |
| 55 | + resource_attributes[OpenTelemetry::SemanticConventions::Resource::HOST_NAME] = hostname |
| 56 | + rescue StandardError => e |
| 57 | + OpenTelemetry.logger.debug("EC2 resource detection failed: #{e.message}") |
| 58 | + return OpenTelemetry::SDK::Resources::Resource.create({}) |
| 59 | + end |
| 60 | + |
| 61 | + # Filter out nil or empty values |
| 62 | + resource_attributes.delete_if { |_key, value| value.nil? || value.empty? } |
| 63 | + OpenTelemetry::SDK::Resources::Resource.create(resource_attributes) |
| 64 | + end |
| 65 | + |
| 66 | + private |
| 67 | + |
| 68 | + # Fetches an IMDSv2 token from the EC2 metadata service |
| 69 | + # |
| 70 | + # @return [String, nil] The token or nil if the request failed |
| 71 | + def fetch_token |
| 72 | + uri = URI.parse("http://#{EC2_METADATA_HOST}#{TOKEN_ENDPOINT}") |
| 73 | + request = Net::HTTP::Put.new(uri) |
| 74 | + request[TOKEN_TTL_HEADER] = TOKEN_TTL_VALUE |
| 75 | + |
| 76 | + response = make_request(uri, request) |
| 77 | + return nil unless response.is_a?(Net::HTTPSuccess) |
| 78 | + |
| 79 | + response.body |
| 80 | + end |
| 81 | + |
| 82 | + # Fetches the instance identity document which contains EC2 instance metadata |
| 83 | + # |
| 84 | + # @param token [String] IMDSv2 token |
| 85 | + # @return [Hash, nil] Parsed identity document or nil if the request failed |
| 86 | + def fetch_identity_document(token) |
| 87 | + uri = URI.parse("http://#{EC2_METADATA_HOST}#{IDENTITY_DOCUMENT_ENDPOINT}") |
| 88 | + request = Net::HTTP::Get.new(uri) |
| 89 | + request[TOKEN_HEADER] = token |
| 90 | + |
| 91 | + response = make_request(uri, request) |
| 92 | + return nil unless response.is_a?(Net::HTTPSuccess) |
| 93 | + |
| 94 | + begin |
| 95 | + JSON.parse(response.body) |
| 96 | + rescue JSON::ParserError |
| 97 | + nil |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + # Fetches the EC2 instance hostname |
| 102 | + # |
| 103 | + # @param token [String] IMDSv2 token |
| 104 | + # @return [String, nil] The hostname or nil if the request failed |
| 105 | + def fetch_hostname(token) |
| 106 | + uri = URI.parse("http://#{EC2_METADATA_HOST}#{HOSTNAME_ENDPOINT}") |
| 107 | + request = Net::HTTP::Get.new(uri) |
| 108 | + request[TOKEN_HEADER] = token |
| 109 | + |
| 110 | + response = make_request(uri, request) |
| 111 | + return nil unless response.is_a?(Net::HTTPSuccess) |
| 112 | + |
| 113 | + response.body |
| 114 | + end |
| 115 | + |
| 116 | + # Makes an HTTP request with timeout handling |
| 117 | + # |
| 118 | + # @param uri [URI] The request URI |
| 119 | + # @param request [Net::HTTP::Request] The request to perform |
| 120 | + # @return [Net::HTTPResponse, nil] The response or nil if the request failed |
| 121 | + def make_request(uri, request) |
| 122 | + http = Net::HTTP.new(uri.host, uri.port) |
| 123 | + http.open_timeout = HTTP_TIMEOUT |
| 124 | + http.read_timeout = HTTP_TIMEOUT |
| 125 | + |
| 126 | + begin |
| 127 | + http.request(request) |
| 128 | + rescue StandardError => e |
| 129 | + OpenTelemetry.logger.debug("EC2 metadata service request failed: #{e.message}") |
| 130 | + nil |
| 131 | + end |
| 132 | + end |
| 133 | + end |
| 134 | + end |
| 135 | + end |
| 136 | + end |
| 137 | +end |
0 commit comments