|
| 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 | +require 'socket' |
| 10 | +require 'opentelemetry/common' |
| 11 | + |
| 12 | +module OpenTelemetry |
| 13 | + module Resource |
| 14 | + module Detector |
| 15 | + module AWS |
| 16 | + # ECS contains detect class method for determining the ECS resource attributes |
| 17 | + module ECS |
| 18 | + extend self |
| 19 | + |
| 20 | + # Container ID length from cgroup file |
| 21 | + CONTAINER_ID_LENGTH = 64 |
| 22 | + |
| 23 | + # HTTP request timeout in seconds |
| 24 | + HTTP_TIMEOUT = 5 |
| 25 | + |
| 26 | + # Create a constant for resource semantic conventions |
| 27 | + RESOURCE = OpenTelemetry::SemanticConventions::Resource |
| 28 | + |
| 29 | + def detect |
| 30 | + # Return empty resource if not running on ECS |
| 31 | + metadata_uri = ENV.fetch('ECS_CONTAINER_METADATA_URI', nil) |
| 32 | + metadata_uri_v4 = ENV.fetch('ECS_CONTAINER_METADATA_URI_V4', nil) |
| 33 | + |
| 34 | + return OpenTelemetry::SDK::Resources::Resource.create({}) if metadata_uri.nil? && metadata_uri_v4.nil? |
| 35 | + |
| 36 | + resource_attributes = {} |
| 37 | + container_id = fetch_container_id |
| 38 | + |
| 39 | + # Base ECS resource attributes |
| 40 | + resource_attributes[RESOURCE::CLOUD_PROVIDER] = 'aws' |
| 41 | + resource_attributes[RESOURCE::CLOUD_PLATFORM] = 'aws_ecs' |
| 42 | + resource_attributes[RESOURCE::CONTAINER_NAME] = Socket.gethostname |
| 43 | + resource_attributes[RESOURCE::CONTAINER_ID] = container_id unless container_id.empty? |
| 44 | + |
| 45 | + # If v4 endpoint is not available, return basic resource |
| 46 | + return OpenTelemetry::SDK::Resources::Resource.create(resource_attributes) if metadata_uri_v4.nil? |
| 47 | + |
| 48 | + begin |
| 49 | + # Fetch container and task metadata |
| 50 | + container_metadata = JSON.parse(http_get(metadata_uri_v4.to_s)) |
| 51 | + task_metadata = JSON.parse(http_get("#{metadata_uri_v4}/task")) |
| 52 | + |
| 53 | + task_arn = task_metadata['TaskARN'] |
| 54 | + base_arn = task_arn[0..task_arn.rindex(':') - 1] |
| 55 | + |
| 56 | + cluster = task_metadata['Cluster'] |
| 57 | + cluster_arn = cluster.start_with?('arn:') ? cluster : "#{base_arn}:cluster/#{cluster}" |
| 58 | + |
| 59 | + # Set ECS-specific attributes |
| 60 | + resource_attributes[RESOURCE::AWS_ECS_CONTAINER_ARN] = container_metadata['ContainerARN'] |
| 61 | + resource_attributes[RESOURCE::AWS_ECS_CLUSTER_ARN] = cluster_arn |
| 62 | + resource_attributes[RESOURCE::AWS_ECS_LAUNCHTYPE] = task_metadata['LaunchType'].downcase |
| 63 | + resource_attributes[RESOURCE::AWS_ECS_TASK_ARN] = task_arn |
| 64 | + resource_attributes[RESOURCE::AWS_ECS_TASK_FAMILY] = task_metadata['Family'] |
| 65 | + resource_attributes[RESOURCE::AWS_ECS_TASK_REVISION] = task_metadata['Revision'] |
| 66 | + |
| 67 | + # Add logging attributes if awslogs is used |
| 68 | + logs_attributes = get_logs_resource(container_metadata) |
| 69 | + resource_attributes.merge!(logs_attributes) |
| 70 | + rescue StandardError => e |
| 71 | + OpenTelemetry.logger.debug("ECS resource detection failed: #{e.message}") |
| 72 | + return OpenTelemetry::SDK::Resources::Resource.create({}) |
| 73 | + end |
| 74 | + |
| 75 | + # Filter out nil or empty values |
| 76 | + resource_attributes.delete_if { |_key, value| value.nil? || value.empty? } |
| 77 | + OpenTelemetry::SDK::Resources::Resource.create(resource_attributes) |
| 78 | + end |
| 79 | + |
| 80 | + private |
| 81 | + |
| 82 | + # Fetches container ID from /proc/self/cgroup file |
| 83 | + # |
| 84 | + # @return [String] The container ID or empty string if not found |
| 85 | + def fetch_container_id |
| 86 | + begin |
| 87 | + File.open('/proc/self/cgroup', 'r') do |file| |
| 88 | + file.each_line do |line| |
| 89 | + line = line.strip |
| 90 | + # Look for container ID (64 chars) at the end of the line |
| 91 | + return line[-CONTAINER_ID_LENGTH..-1] if line.length > CONTAINER_ID_LENGTH |
| 92 | + end |
| 93 | + end |
| 94 | + rescue Errno::ENOENT => e |
| 95 | + OpenTelemetry.logger.debug("Failed to get container ID on ECS: #{e.message}") |
| 96 | + end |
| 97 | + |
| 98 | + '' |
| 99 | + end |
| 100 | + |
| 101 | + # Extracting logging-related resource attributes |
| 102 | + # |
| 103 | + # @param container_metadata [Hash] Container metadata from ECS metadata endpoint |
| 104 | + # @returhn [Hash] Resource attributes for logging configuration |
| 105 | + def get_logs_resource(container_metadata) |
| 106 | + log_attributes = {} |
| 107 | + |
| 108 | + if container_metadata['LogDriver'] == 'awslogs' |
| 109 | + log_options = container_metadata['LogOptions'] |
| 110 | + |
| 111 | + if log_options |
| 112 | + logs_region = log_options['awslogs-region'] |
| 113 | + logs_group_name = log_options['awslogs-group'] |
| 114 | + logs_stream_name = log_options['awslogs-stream'] |
| 115 | + |
| 116 | + container_arn = container_metadata['ContainerARN'] |
| 117 | + |
| 118 | + # Parse region from ARN if not specified in log options |
| 119 | + if logs_region.nil? || logs_region.empty? |
| 120 | + region_match = container_arn.match(/arn:aws:ecs:([^:]+):.*/) |
| 121 | + logs_region = region_match[1] if region_match |
| 122 | + end |
| 123 | + |
| 124 | + # Parse account ID from ARN |
| 125 | + account_match = container_arn.match(/arn:aws:ecs:[^:]+:([^:]+):.*/) |
| 126 | + aws_account = account_match[1] if account_match |
| 127 | + |
| 128 | + logs_group_arn = nil |
| 129 | + logs_stream_arn = nil |
| 130 | + |
| 131 | + if logs_region && aws_account |
| 132 | + logs_group_arn = "arn:aws:logs:#{logs_region}:#{aws_account}:log-group:#{logs_group_name}" if logs_group_name |
| 133 | + |
| 134 | + logs_stream_arn = "arn:aws:logs:#{logs_region}:#{aws_account}:log-group:#{logs_group_name}:log-stream:#{logs_stream_name}" if logs_stream_name && logs_group_name |
| 135 | + end |
| 136 | + |
| 137 | + log_attributes[RESOURCE::AWS_LOG_GROUP_NAMES] = [logs_group_name].compact |
| 138 | + log_attributes[RESOURCE::AWS_LOG_GROUP_ARNS] = [logs_group_arn].compact |
| 139 | + log_attributes[RESOURCE::AWS_LOG_STREAM_NAMES] = [logs_stream_name].compact |
| 140 | + log_attributes[RESOURCE::AWS_LOG_STREAM_ARNS] = [logs_stream_arn].compact |
| 141 | + else |
| 142 | + OpenTelemetry.logger.debug("The metadata endpoint v4 has returned 'awslogs' as 'LogDriver', but there is no 'LogOptions' data") |
| 143 | + end |
| 144 | + end |
| 145 | + |
| 146 | + log_attributes |
| 147 | + end |
| 148 | + |
| 149 | + # Makes an HTTP GET request to the specified URL |
| 150 | + # |
| 151 | + # @param url [String] The URL to request |
| 152 | + # @return [String] The response body |
| 153 | + def http_get(url) |
| 154 | + uri = URI.parse(url) |
| 155 | + request = Net::HTTP::Get.new(uri) |
| 156 | + |
| 157 | + http = Net::HTTP.new(uri.host, uri.port) |
| 158 | + http.open_timeout = HTTP_TIMEOUT |
| 159 | + http.read_timeout = HTTP_TIMEOUT |
| 160 | + |
| 161 | + OpenTelemetry::Common::Utilities.untraced do |
| 162 | + response = http.request(request) |
| 163 | + raise "HTTP request failed with status #{response.code}" unless response.is_a?(Net::HTTPSuccess) |
| 164 | + |
| 165 | + response.body |
| 166 | + end |
| 167 | + end |
| 168 | + end |
| 169 | + end |
| 170 | + end |
| 171 | + end |
| 172 | +end |
0 commit comments