diff --git a/lib/solarwinds_apm/support/aws_resource_detector.rb b/lib/solarwinds_apm/support/aws_resource_detector.rb index cb21a90c..0a07f337 100644 --- a/lib/solarwinds_apm/support/aws_resource_detector.rb +++ b/lib/solarwinds_apm/support/aws_resource_detector.rb @@ -6,7 +6,6 @@ # # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -require_relative 'resource_detector/aws/ec2' require_relative 'resource_detector/aws/ecs' require_relative 'resource_detector/aws/eks' require_relative 'resource_detector/aws/lambda' diff --git a/lib/solarwinds_apm/support/resource_detector.rb b/lib/solarwinds_apm/support/resource_detector.rb index de5d4386..a4490095 100644 --- a/lib/solarwinds_apm/support/resource_detector.rb +++ b/lib/solarwinds_apm/support/resource_detector.rb @@ -12,8 +12,8 @@ require 'socket' require 'securerandom' require 'opentelemetry/resource/detector/azure' -require 'opentelemetry/resource/detector/aws/ec2' if RUBY_VERSION >= '3.1.0' # aws resource detector requires ruby >= 3.1.0 require 'opentelemetry/resource/detector/container' +require_relative 'resource_detector/aws/ec2' module SolarWindsAPM # ResourceDetector diff --git a/lib/solarwinds_apm/support/resource_detector/aws/ec2.rb b/lib/solarwinds_apm/support/resource_detector/aws/ec2.rb index 79ea74ed..882225dd 100644 --- a/lib/solarwinds_apm/support/resource_detector/aws/ec2.rb +++ b/lib/solarwinds_apm/support/resource_detector/aws/ec2.rb @@ -1,129 +1,143 @@ # frozen_string_literal: true -# © 2023 SolarWinds Worldwide, LLC. All rights reserved. +# Copyright The OpenTelemetry Authors # -# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at:http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +# SPDX-License-Identifier: Apache-2.0 require 'net/http' -require 'uri' require 'json' +require 'opentelemetry/common' +require 'opentelemetry/semantic_conventions/resource' + +module OpenTelemetry + module Resource + module Detector + module AWS + # EC2 contains detect class method for determining EC2 resource attributes + module EC2 + extend self + + # EC2 metadata service endpoints and constants + EC2_METADATA_HOST = '169.254.169.254' + TOKEN_ENDPOINT = '/latest/api/token' + IDENTITY_DOCUMENT_ENDPOINT = '/latest/dynamic/instance-identity/document' + HOSTNAME_ENDPOINT = '/latest/meta-data/hostname' + + TOKEN_HEADER = 'X-aws-ec2-metadata-token' + TOKEN_TTL_HEADER = 'X-aws-ec2-metadata-token-ttl-seconds' + TOKEN_TTL_VALUE = '60' + + # Timeout in seconds for HTTP requests + HTTP_TIMEOUT = 1 + + # Create a constant for resource semantic conventions + RESOURCE = ::OpenTelemetry::SemanticConventions::Resource + + def detect + # Implementation for EC2 detection supporting both IMDSv1 and IMDSv2 + resource_attributes = {} + + begin + # Attempt to get IMDSv2 token - this will fail if IMDSv2 is not supported + # but we'll still try IMDSv1 in that case + token = fetch_token + + # Get instance identity document which contains most metadata + # Will try with token (IMDSv2) or without token (IMDSv1) + identity = fetch_identity_document(token) || {} + return ::OpenTelemetry::SDK::Resources::Resource.create({}) if identity.empty? + + hostname = fetch_hostname(token) + + # Set resource attributes from the identity document + resource_attributes[RESOURCE::CLOUD_PROVIDER] = 'aws' + resource_attributes[RESOURCE::CLOUD_PLATFORM] = 'aws_ec2' + resource_attributes[RESOURCE::CLOUD_ACCOUNT_ID] = identity['accountId'] + resource_attributes[RESOURCE::CLOUD_REGION] = identity['region'] + resource_attributes[RESOURCE::CLOUD_AVAILABILITY_ZONE] = identity['availabilityZone'] + + resource_attributes[RESOURCE::HOST_ID] = identity['instanceId'] + resource_attributes[RESOURCE::HOST_TYPE] = identity['instanceType'] + resource_attributes[RESOURCE::HOST_NAME] = hostname + rescue StandardError => e + ::OpenTelemetry.handle_error(exception: e, message: 'EC2 resource detection failed') + return ::OpenTelemetry::SDK::Resources::Resource.create({}) + end + + # Filter out nil or empty values + resource_attributes.delete_if { |_key, value| value.nil? || value.empty? } + ::OpenTelemetry::SDK::Resources::Resource.create(resource_attributes) + end -module SolarWindsAPM - module ResourceDetector - module EC2 - module_function - - # EC2 metadata service endpoints and constants - EC2_METADATA_HOST = '169.254.169.254' - TOKEN_ENDPOINT = '/latest/api/token' - IDENTITY_DOCUMENT_ENDPOINT = '/latest/dynamic/instance-identity/document' - HOSTNAME_ENDPOINT = '/latest/meta-data/hostname' - - TOKEN_HEADER = 'X-aws-ec2-metadata-token' - TOKEN_TTL_HEADER = 'X-aws-ec2-metadata-token-ttl-seconds' - TOKEN_TTL_VALUE = '60' - - # Timeout in seconds for HTTP requests - HTTP_TIMEOUT = 1 - - def detect - # Placeholder for EC2 implementation - resource_attributes = {} - - begin - # Get IMDSv2 token - this will fail quickly if not on EC2 - # If token is nil, then assume it's IMDSv1 (no token required for metadata) - token = fetch_token - identity = fetch_identity_document(token) || {} - hostname = fetch_hostname(token) - - # Set resource attributes from the identity document - resource_attributes[::OpenTelemetry::SemanticConventions::Resource::CLOUD_PROVIDER] = 'aws' - resource_attributes[::OpenTelemetry::SemanticConventions::Resource::CLOUD_PLATFORM] = 'aws_ec2' - resource_attributes[::OpenTelemetry::SemanticConventions::Resource::CLOUD_ACCOUNT_ID] = identity['accountId'] - resource_attributes[::OpenTelemetry::SemanticConventions::Resource::CLOUD_REGION] = identity['region'] - resource_attributes[::OpenTelemetry::SemanticConventions::Resource::CLOUD_AVAILABILITY_ZONE] = identity['availabilityZone'] - - resource_attributes[::OpenTelemetry::SemanticConventions::Resource::HOST_ID] = identity['instanceId'] - resource_attributes[::OpenTelemetry::SemanticConventions::Resource::HOST_TYPE] = identity['instanceType'] - resource_attributes[::OpenTelemetry::SemanticConventions::Resource::HOST_NAME] = hostname - rescue StandardError => e - SolarWindsAPM.logger.debug { "EC2 resource detection failed: #{e.message}" } - return ::OpenTelemetry::SDK::Resources::Resource.create({}) - end - - # Filter out nil or empty values - resource_attributes.compact! - ::OpenTelemetry::SDK::Resources::Resource.create(resource_attributes) - end + private - # Fetches an IMDSv2 token from the EC2 metadata service - # - # @return [String, nil] The token or nil if the request failed - def fetch_token - uri = URI.parse("http://#{EC2_METADATA_HOST}#{TOKEN_ENDPOINT}") - request = Net::HTTP::Put.new(uri) - request[TOKEN_TTL_HEADER] = TOKEN_TTL_VALUE + # Fetches an IMDSv2 token from the EC2 metadata service + # + # @return [String, nil] The token or nil if the request failed + def fetch_token + uri = URI.parse("http://#{EC2_METADATA_HOST}#{TOKEN_ENDPOINT}") + request = Net::HTTP::Put.new(uri) + request[TOKEN_TTL_HEADER] = TOKEN_TTL_VALUE - response = make_request(uri, request) - return nil unless response.is_a?(Net::HTTPSuccess) + response = make_request(uri, request) + return nil unless response.is_a?(Net::HTTPSuccess) - response.body - end + response.body + end - # Fetches the instance identity document which contains EC2 instance metadata - # - # @param token [String] IMDSv2 token - # @return [Hash, nil] Parsed identity document or nil if the request failed - def fetch_identity_document(token) - uri = URI.parse("http://#{EC2_METADATA_HOST}#{IDENTITY_DOCUMENT_ENDPOINT}") - request = Net::HTTP::Get.new(uri) - request[TOKEN_HEADER] = token - - response = make_request(uri, request) - return nil unless response.is_a?(Net::HTTPSuccess) - - begin - JSON.parse(response.body) - rescue JSON::ParserError - nil - end - end + # Fetches the instance identity document which contains EC2 instance metadata + # + # @param token [String, nil] IMDSv2 token (optional for IMDSv1) + # @return [Hash, nil] Parsed identity document or nil if the request failed + def fetch_identity_document(token) + uri = URI.parse("http://#{EC2_METADATA_HOST}#{IDENTITY_DOCUMENT_ENDPOINT}") + request = Net::HTTP::Get.new(uri) + request[TOKEN_HEADER] = token if token + + response = make_request(uri, request) + return nil unless response.is_a?(Net::HTTPSuccess) + + begin + JSON.parse(response.body) + rescue JSON::ParserError + nil + end + end - # Fetches the EC2 instance hostname - # - # @param token [String] IMDSv2 token - # @return [String, nil] The hostname or nil if the request failed - def fetch_hostname(token) - uri = URI.parse("http://#{EC2_METADATA_HOST}#{HOSTNAME_ENDPOINT}") - request = Net::HTTP::Get.new(uri) - request[TOKEN_HEADER] = token + # Fetches the EC2 instance hostname + # + # @param token [String, nil] IMDSv2 token (optional for IMDSv1) + # @return [String, nil] The hostname or nil if the request failed + def fetch_hostname(token) + uri = URI.parse("http://#{EC2_METADATA_HOST}#{HOSTNAME_ENDPOINT}") + request = Net::HTTP::Get.new(uri) + request[TOKEN_HEADER] = token if token - response = make_request(uri, request) - return nil unless response.is_a?(Net::HTTPSuccess) + response = make_request(uri, request) + return nil unless response.is_a?(Net::HTTPSuccess) - response.body - end + response.body + end - # Makes an HTTP request with timeout handling - # - # @param uri [URI] The request URI - # @param request [Net::HTTP::Request] The request to perform - # @return [Net::HTTPResponse, nil] The response or nil if the request failed - def make_request(uri, request) - http = Net::HTTP.new(uri.host, uri.port) - http.open_timeout = HTTP_TIMEOUT - http.read_timeout = HTTP_TIMEOUT - - begin - ::OpenTelemetry::Common::Utilities.untraced do - http.request(request) + # Makes an HTTP request with timeout handling + # + # @param uri [URI] The request URI + # @param request [Net::HTTP::Request] The request to perform + # @return [Net::HTTPResponse, nil] The response or nil if the request failed + def make_request(uri, request) + http = Net::HTTP.new(uri.host, uri.port) + http.open_timeout = HTTP_TIMEOUT + http.read_timeout = HTTP_TIMEOUT + + begin + ::OpenTelemetry::Common::Utilities.untraced do + http.request(request) + end + rescue StandardError + ::OpenTelemetry.logger.debug { 'EC2 metadata service request failed' } + nil + end end - rescue StandardError => e - SolarWindsAPM.logger.debug { "EC2 metadata service request failed: #{e.message}" } - nil end end end diff --git a/lib/solarwinds_apm/support/resource_detector/aws/ecs.rb b/lib/solarwinds_apm/support/resource_detector/aws/ecs.rb index 42bc7ecb..490c6759 100644 --- a/lib/solarwinds_apm/support/resource_detector/aws/ecs.rb +++ b/lib/solarwinds_apm/support/resource_detector/aws/ecs.rb @@ -1,155 +1,172 @@ # frozen_string_literal: true -# © 2023 SolarWinds Worldwide, LLC. All rights reserved. +# Copyright The OpenTelemetry Authors # -# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at:http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +# SPDX-License-Identifier: Apache-2.0 require 'net/http' -require 'uri' require 'json' require 'socket' - -module SolarWindsAPM - module ResourceDetector - module ECS - module_function - - CONTAINER_ID_LENGTH = 64 - DEFAULT_CGROUP_PATH = '/proc/self/cgroup' - HTTP_TIMEOUT = 1 - - def detect - ecs_instance = ENV['ECS_CONTAINER_METADATA_URI_V4'] || ENV.fetch('ECS_CONTAINER_METADATA_URI', nil) - unless ecs_instance - SolarWindsAPM.logger.debug { 'AwsEcsDetector: Process is not on ECS' } - return ::OpenTelemetry::SDK::Resources::Resource.create({}) - end - - gather_data - end - - # curl http://169.254.169.254/latest/meta-data/hostname - def gather_data - attribute = { - ::OpenTelemetry::SemanticConventions::Resource::CLOUD_PROVIDER => 'aws', - ::OpenTelemetry::SemanticConventions::Resource::CLOUD_PLATFORM => 'aws_ecs', - ::OpenTelemetry::SemanticConventions::Resource::CONTAINER_ID => resolve_container_id, - ::OpenTelemetry::SemanticConventions::Resource::HOST_NAME => Socket.gethostname - } - - metadata_url = ENV.fetch('ECS_CONTAINER_METADATA_URI_V4', nil) - if metadata_url - container_metadata = get_url_as_json(metadata_url) - task_metadata = get_url_as_json("#{metadata_url}/task") - - merge_metadata(attribute, container_metadata, task_metadata) - end - - attribute.compact! - ::OpenTelemetry::SDK::Resources::Resource.create(attribute) - end - - def resolve_container_id - container_id = nil - begin - raw_data = File.read(DEFAULT_CGROUP_PATH).strip - raw_data.each_line do |line| - if line.length > CONTAINER_ID_LENGTH - container_id = line[-CONTAINER_ID_LENGTH..] - break +require 'opentelemetry/common' +require 'opentelemetry/semantic_conventions/resource' + +module OpenTelemetry + module Resource + module Detector + module AWS + # ECS contains detect class method for determining the ECS resource attributes + module ECS + extend self + + # Container ID length from cgroup file + CONTAINER_ID_LENGTH = 64 + + # HTTP request timeout in seconds + HTTP_TIMEOUT = 5 + + # Create a constant for resource semantic conventions + RESOURCE = ::OpenTelemetry::SemanticConventions::Resource + + def detect + # Return empty resource if not running on ECS + metadata_uri = ENV.fetch('ECS_CONTAINER_METADATA_URI', nil) + metadata_uri_v4 = ENV.fetch('ECS_CONTAINER_METADATA_URI_V4', nil) + + return ::OpenTelemetry::SDK::Resources::Resource.create({}) if metadata_uri.nil? && metadata_uri_v4.nil? + + resource_attributes = {} + container_id = fetch_container_id + + # Base ECS resource attributes + resource_attributes[RESOURCE::CLOUD_PROVIDER] = 'aws' + resource_attributes[RESOURCE::CLOUD_PLATFORM] = 'aws_ecs' + resource_attributes[RESOURCE::CONTAINER_NAME] = Socket.gethostname + resource_attributes[RESOURCE::CONTAINER_ID] = container_id unless container_id.empty? + + # If v4 endpoint is not available, return basic resource + return ::OpenTelemetry::SDK::Resources::Resource.create(resource_attributes) if metadata_uri_v4.nil? + + begin + # Fetch container and task metadata + container_metadata = JSON.parse(http_get(metadata_uri_v4.to_s)) + task_metadata = JSON.parse(http_get("#{metadata_uri_v4}/task")) + + task_arn = task_metadata['TaskARN'] + base_arn = task_arn[0..task_arn.rindex(':') - 1] + + cluster = task_metadata['Cluster'] + cluster_arn = cluster.start_with?('arn:') ? cluster : "#{base_arn}:cluster/#{cluster}" + + # Set ECS-specific attributes + resource_attributes[RESOURCE::AWS_ECS_CONTAINER_ARN] = container_metadata['ContainerARN'] + resource_attributes[RESOURCE::AWS_ECS_CLUSTER_ARN] = cluster_arn + resource_attributes[RESOURCE::AWS_ECS_LAUNCHTYPE] = task_metadata['LaunchType'].downcase + resource_attributes[RESOURCE::AWS_ECS_TASK_ARN] = task_arn + resource_attributes[RESOURCE::AWS_ECS_TASK_FAMILY] = task_metadata['Family'] + resource_attributes[RESOURCE::AWS_ECS_TASK_REVISION] = task_metadata['Revision'] + + # Add logging attributes if awslogs is used + logs_attributes = get_logs_resource(container_metadata) + resource_attributes.merge!(logs_attributes) + rescue StandardError => e + ::OpenTelemetry.handle_error(exception: e, message: 'ECS resource detection failed') + return ::OpenTelemetry::SDK::Resources::Resource.create({}) end - end - rescue StandardError => e - SolarWindsAPM.logger.debug { "AwsEcsDetector failed to read container ID: #{e.message}" } - end - container_id - end - def make_request(uri, request) - http = Net::HTTP.new(uri.host, uri.port) - http.open_timeout = HTTP_TIMEOUT - http.read_timeout = HTTP_TIMEOUT - - begin - ::OpenTelemetry::Common::Utilities.untraced do - http.request(request) + # Filter out nil or empty values + resource_attributes.delete_if { |_key, value| value.nil? || value.empty? } + ::OpenTelemetry::SDK::Resources::Resource.create(resource_attributes) end - rescue StandardError => e - OpenTelemetry.logger.debug { "ECS metadata service request failed: #{e.message}" } - nil - end - end - def merge_metadata(attribute, container_metadata, task_metadata) - if task_metadata - task_arn = task_metadata['TaskARN'] - base_arn = task_arn[0, task_arn.rindex(':')] - cluster = task_metadata['Cluster'] - account_id = get_account_id_from_arn(task_arn) - region = get_region_from_arn(task_arn) - - attribute[::OpenTelemetry::SemanticConventions::Resource::AWS_ECS_CLUSTER_ARN] = cluster.start_with?('arn:') ? cluster : "#{base_arn}:cluster/#{cluster}" - attribute[::OpenTelemetry::SemanticConventions::Resource::AWS_ECS_LAUNCHTYPE] = task_metadata['LaunchType'] - attribute[::OpenTelemetry::SemanticConventions::Resource::AWS_ECS_TASK_ARN] = task_arn - attribute[::OpenTelemetry::SemanticConventions::Resource::AWS_ECS_TASK_FAMILY] = task_metadata['Family'] - attribute[::OpenTelemetry::SemanticConventions::Resource::AWS_ECS_TASK_REVISION] = task_metadata['Revision'] - - attribute[::OpenTelemetry::SemanticConventions::Resource::CLOUD_ACCOUNT_ID] = account_id - attribute[::OpenTelemetry::SemanticConventions::Resource::CLOUD_REGION] = region - - attribute[::OpenTelemetry::SemanticConventions::Resource::CLOUD_AVAILABILITY_ZONE] = task_metadata['AvailabilityZone'] - else - SolarWindsAPM.logger.debug { 'Missing task_metadata from ECS resource detection' } - end + private + + # Fetches container ID from /proc/self/cgroup file + # + # @return [String] The container ID or empty string if not found + def fetch_container_id + begin + File.open('/proc/self/cgroup', 'r') do |file| + file.each_line do |line| + line = line.strip + # Look for container ID (64 chars) at the end of the line + return line[-CONTAINER_ID_LENGTH..] if line.length > CONTAINER_ID_LENGTH + end + end + rescue Errno::ENOENT => e + ::OpenTelemetry.handle_error(exception: e, message: 'Failed to get container ID on ECS') + end - if container_metadata - container_arn = container_metadata['ContainerARN'] - attribute['cloud.resource_id'] = container_arn - attribute[::OpenTelemetry::SemanticConventions::Resource::AWS_ECS_CONTAINER_ARN] = container_metadata['ContainerARN'] - - if container_metadata['LogDriver'] == 'awslogs' || container_metadata['LogOptions'] - log_options = container_metadata['LogOptions'] - log_region = log_options['awslogs-region'] || get_region_from_arn(container_arn) - aws_account_id = get_account_id_from_arn(container_arn) - logs_group_name = log_options['awslogs-region'] - logs_group_arn = "arn:aws:logs:#{log_region}:#{aws_account_id}:log-group:#{logs_group_name}" - logs_stream_name = log_options['awslogs-stream'] - logs_stream_arn = "arn:aws:logs:#{log_region}:#{aws_account_id}:log-group:#{logs_group_name}:log-stream:#{logs_stream_name}" - - attribute[::OpenTelemetry::SemanticConventions::Resource::AWS_LOG_GROUP_NAMES] = [logs_group_name] - attribute[::OpenTelemetry::SemanticConventions::Resource::AWS_LOG_GROUP_ARNS] = [logs_group_arn] - attribute[::OpenTelemetry::SemanticConventions::Resource::AWS_LOG_STREAM_NAMES] = [logs_stream_name] - attribute[::OpenTelemetry::SemanticConventions::Resource::AWS_LOG_STREAM_ARNS] = [logs_stream_arn] - else - SolarWindsAPM.logger.debug { 'Missing log option data in container_metadata from ECS resource detection' } + '' end - else - SolarWindsAPM.logger.debug { 'Missing container_metadata from ECS resource detection' } - end - attribute.compact! - end + # Extracting logging-related resource attributes + # + # @param container_metadata [Hash] Container metadata from ECS metadata endpoint + # @returhn [Hash] Resource attributes for logging configuration + def get_logs_resource(container_metadata) + log_attributes = {} + + if container_metadata['LogDriver'] == 'awslogs' + log_options = container_metadata['LogOptions'] + + if log_options + logs_region = log_options['awslogs-region'] + logs_group_name = log_options['awslogs-group'] + logs_stream_name = log_options['awslogs-stream'] + + container_arn = container_metadata['ContainerARN'] + + # Parse region from ARN if not specified in log options + if logs_region.nil? || logs_region.empty? + region_match = container_arn.match(/arn:aws:ecs:([^:]+):.*/) + logs_region = region_match[1] if region_match + end + + # Parse account ID from ARN + account_match = container_arn.match(/arn:aws:ecs:[^:]+:([^:]+):.*/) + aws_account = account_match[1] if account_match + + logs_group_arn = nil + logs_stream_arn = nil + + if logs_region && aws_account + logs_group_arn = "arn:aws:logs:#{logs_region}:#{aws_account}:log-group:#{logs_group_name}" if logs_group_name + + 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 + end + + log_attributes[RESOURCE::AWS_LOG_GROUP_NAMES] = [logs_group_name].compact + log_attributes[RESOURCE::AWS_LOG_GROUP_ARNS] = [logs_group_arn].compact + log_attributes[RESOURCE::AWS_LOG_STREAM_NAMES] = [logs_stream_name].compact + log_attributes[RESOURCE::AWS_LOG_STREAM_ARNS] = [logs_stream_arn].compact + else + ::OpenTelemetry.handle_error(message: 'The metadata endpoint v4 has returned \'awslogs\' as \'LogDriver\', but there is no \'LogOptions\' data') + end + end - def get_account_id_from_arn(task_arn) - matches = task_arn.to_s.match(/arn:aws:ecs:[^:]+:([^:]+):.*/) - matches.nil? ? nil : matches[1] - end + log_attributes + end - def get_region_from_arn(task_arn) - matches = task_arn.to_s.match(/arn:aws:ecs:([^:]+):.*/) - matches.nil? ? nil : matches[1] - end + # Makes an HTTP GET request to the specified URL + # + # @param url [String] The URL to request + # @return [String] The response body + def http_get(url) + uri = URI.parse(url) + request = Net::HTTP::Get.new(uri) - def get_url_as_json(url) - uri = URI.parse(url) - request = Net::HTTP::Get.new(uri) - response = make_request(uri, request) + http = Net::HTTP.new(uri.host, uri.port) + http.open_timeout = HTTP_TIMEOUT + http.read_timeout = HTTP_TIMEOUT - return nil unless response.is_a?(Net::HTTPSuccess) + ::OpenTelemetry::Common::Utilities.untraced do + response = http.request(request) + raise "HTTP request failed with status #{response.code}" unless response.is_a?(Net::HTTPSuccess) - JSON.parse(response.body) + response.body + end + end + end end end end diff --git a/lib/solarwinds_apm/support/resource_detector/aws/eks.rb b/lib/solarwinds_apm/support/resource_detector/aws/eks.rb index 5ecab347..b45df8bf 100644 --- a/lib/solarwinds_apm/support/resource_detector/aws/eks.rb +++ b/lib/solarwinds_apm/support/resource_detector/aws/eks.rb @@ -1,146 +1,173 @@ # frozen_string_literal: true -# © 2023 SolarWinds Worldwide, LLC. All rights reserved. +# Copyright The OpenTelemetry Authors # -# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at:http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +# SPDX-License-Identifier: Apache-2.0 require 'net/http' -require 'uri' require 'json' -require 'socket' - -module SolarWindsAPM - module ResourceDetector - module EKS - module_function - - K8S_SVC_URL = 'kubernetes.default.svc' - K8S_TOKEN_PATH = '/var/run/secrets/kubernetes.io/serviceaccount/token' - K8S_CERT_PATH = '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt' - AUTH_CONFIGMAP_PATH = '/api/v1/namespaces/kube-system/configmaps/aws-auth' - CW_CONFIGMAP_PATH = '/api/v1/namespaces/amazon-cloudwatch/configmaps/cluster-info' - CONTAINER_ID_LENGTH = 64 - DEFAULT_CGROUP_PATH = '/proc/self/cgroup' - TIMEOUT_MS = 2000 - UTF8_UNICODE = 'utf-8' - - def detect - ::OpenTelemetry::SDK::Resources::Resource.create(gather_data) - end +require 'openssl' +require 'uri' +require 'opentelemetry/common' +require 'opentelemetry/semantic_conventions/resource' + +module OpenTelemetry + module Resource + module Detector + module AWS + # EKS contains detect class method for determining EKS resource attributes + module EKS + extend self + + # Container ID length from cgroup file + CONTAINER_ID_LENGTH = 64 + + # HTTP request timeout in seconds + HTTP_TIMEOUT = 5 + + # Kubernetes token and certificate paths + TOKEN_PATH = '/var/run/secrets/kubernetes.io/serviceaccount/token' + CERT_PATH = '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt' + + # Kubernetes API paths + AWS_AUTH_PATH = '/api/v1/namespaces/kube-system/configmaps/aws-auth' + CLUSTER_INFO_PATH = '/api/v1/namespaces/amazon-cloudwatch/configmaps/cluster-info' + + # Create a constant for resource semantic conventions + RESOURCE = ::OpenTelemetry::SemanticConventions::Resource + + def detect + # Return empty resource if not running on K8s + return ::OpenTelemetry::SDK::Resources::Resource.create({}) unless k8s? + + resource_attributes = {} + + begin + # Get K8s credentials + cred_value = k8s_cred_value + + # Verify this is an EKS cluster + unless eks?(cred_value) + ::OpenTelemetry.logger.debug('Could not confirm process is running on EKS') + return ::OpenTelemetry::SDK::Resources::Resource.create({}) + end + + # Get cluster name and container ID + cluster_name_val = cluster_name(cred_value) + container_id_val = container_id + + if container_id_val.empty? && cluster_name_val.empty? + ::OpenTelemetry.logger.debug('Neither cluster name nor container ID found on EKS process') + return ::OpenTelemetry::SDK::Resources::Resource.create({}) + end + + # Set resource attributes + resource_attributes[RESOURCE::CLOUD_PROVIDER] = 'aws' + resource_attributes[RESOURCE::CLOUD_PLATFORM] = 'aws_eks' + resource_attributes[RESOURCE::K8S_CLUSTER_NAME] = cluster_name_val unless cluster_name_val.empty? + resource_attributes[RESOURCE::CONTAINER_ID] = container_id_val unless container_id_val.empty? + rescue StandardError => e + ::OpenTelemetry.logger.debug("EKS resource detection failed: #{e.message}") + return ::OpenTelemetry::SDK::Resources::Resource.create({}) + end - def gather_data - raise StandardError, 'K8S token path missing.' unless File.exist?(K8S_TOKEN_PATH) && File.readable?(K8S_TOKEN_PATH) + resource_attributes.delete_if { |_key, value| value.nil? || value.empty? } + ::OpenTelemetry::SDK::Resources::Resource.create(resource_attributes) + end - k8scert = File.read(K8S_CERT_PATH) if File.exist?(K8S_CERT_PATH) && File.readable?(K8S_CERT_PATH) + private - raise StandardError, 'Current environment is not in AWS EKS.' unless eks?(k8scert) + # Check if running on K8s + # + # @return [Boolean] true if running on K8s + def k8s? + File.exist?(TOKEN_PATH) && File.exist?(CERT_PATH) + end - container_id = resolve_container_id - cluster_name = get_cluster_name(k8scert) + # Get K8s token + # + # @return [String] K8s token + # @raise [StandardError] if token could not be read + def k8s_cred_value + token = File.read(TOKEN_PATH).strip + "Bearer #{token}" + rescue StandardError => e + ::OpenTelemetry.logger.debug("Failed to get k8s token: #{e.message}") + raise e + end - { - ::OpenTelemetry::SemanticConventions::Resource::CLOUD_PROVIDER => 'aws', - ::OpenTelemetry::SemanticConventions::Resource::CLOUD_PLATFORM => 'aws_eks', - ::OpenTelemetry::SemanticConventions::Resource::K8S_CLUSTER_NAME => cluster_name || nil, - ::OpenTelemetry::SemanticConventions::Resource::CONTAINER_ID => container_id || nil - }.compact! - rescue StandardError => e - SolarWindsAPM.logger.debug { "Gather data for AWS EKS resource detector failed: #{e.message}" } - {} - end + # Check if running on EKS + # + # @param cred_value [String] K8s credentials + # @return [Boolean] true if running on EKS + def eks?(cred_value) + # Just try to to access the aws-auth configmap + # If it exists and we can access it, we're on EKS + aws_http_request(AWS_AUTH_PATH, cred_value) + true + rescue StandardError + false + end - def resolve_container_id - container_id = nil - begin - raw_data = File.read(DEFAULT_CGROUP_PATH, encoding: UTF8_UNICODE).strip - raw_data.each_line do |line| - if line.length > CONTAINER_ID_LENGTH - container_id = line[-CONTAINER_ID_LENGTH..] - break + # Get EKS cluster name + # + # @param cred_value [String] K8s credentials + # @return [String] Cluster name or empty string if not found + def cluster_name(cred_value) + begin + response = aws_http_request(CLUSTER_INFO_PATH, cred_value) + cluster_info = JSON.parse(response) + return cluster_info['data']['cluster.name'] if cluster_info['data'] && cluster_info['data']['cluster.name'] + rescue StandardError => e + ::OpenTelemetry.logger.debug("Cannot get cluster name on EKS: #{e.message}") end + '' end - rescue StandardError => e - SolarWindsAPM.logger.debug { "AwsEksDetector failed to read container ID: #{e.message}" } - end - container_id - end - - def get_cluster_name(cert) - options = { - ca_file: cert, - headers: { - 'Authorization' => k8s_cred_header - }, - hostname: K8S_SVC_URL, - method: 'GET', - path: CW_CONFIGMAP_PATH, - timeout: TIMEOUT_MS / 1000 - } - - cluster_name = nil - response = fetch_string(options) - begin - cluster_name = JSON.parse(response).dig('data', 'cluster.name') - rescue StandardError => e - SolarWindsAPM.logger.debug { "Cannot get cluster name on EKS: #{e.message}" } - end - cluster_name - end - - def eks?(cert) - options = { - ca_cert: cert, - headers: { - 'Authorization' => k8s_cred_header - }, - hostname: K8S_SVC_URL, - method: 'GET', - path: AUTH_CONFIGMAP_PATH, - timeout: TIMEOUT_MS / 1000 - } - - !!fetch_string(options) - end - - def k8s_cred_header - content = File.read(K8S_TOKEN_PATH).strip - "Bearer #{content}" - rescue StandardError => e - SolarWindsAPM.logger.debug { "Unable to read Kubernetes client token: #{e.message}" } - '' - end - - def fetch_string(options) - uri = URI::HTTPS.build(host: options[:hostname], path: options[:path]) - http = Net::HTTP.new(uri.host, uri.port) - http.use_ssl = true - http.verify_mode = OpenSSL::SSL::VERIFY_PEER - http.ca_file = options[:ca_cert] - http.open_timeout = options[:timeout] - http.read_timeout = options[:timeout] - - request = Net::HTTP::Get.new(uri) - - options[:headers]&.each { |key, value| request[key] = value } + # Get container ID from cgroup file + # + # @return [String] Container ID or empty string if not found + def container_id + begin + File.open('/proc/self/cgroup', 'r') do |file| + file.each_line do |line| + line = line.strip + # Look for container ID (64 chars) at the end of the line + return line[-CONTAINER_ID_LENGTH..] if line.length > CONTAINER_ID_LENGTH + end + end + rescue StandardError => e + ::OpenTelemetry.logger.debug("Failed to get container ID on EKS: #{e.message}") + end + '' + end - response = nil - begin - ::OpenTelemetry::Common::Utilities.untraced do - response = http.request(request) + # Make HTTP GET request to K8s API + # + # @param path [String] API path + # @param cred_value [String] Authorization header value + # @return [String] Response body + # @raise [StandardError] if request fails + def aws_http_request(path, cred_value) + uri = URI.parse("https://kubernetes.default.svc#{path}") + http = Net::HTTP.new(uri.host, uri.port) + http.use_ssl = true + http.verify_mode = OpenSSL::SSL::VERIFY_PEER + http.ca_file = CERT_PATH + http.open_timeout = HTTP_TIMEOUT + http.read_timeout = HTTP_TIMEOUT + + request = Net::HTTP::Get.new(uri) + request['Authorization'] = cred_value + + ::OpenTelemetry::Common::Utilities.untraced do + response = http.request(request) + raise "HTTP request failed with status #{response.code}" unless response.is_a?(Net::HTTPSuccess) + + response.body + end end - rescue StandardError => e - raise "EKS metadata API request error: #{e.message}." end - - raise "Failed to load page, status code: #{response.code}" unless response.is_a?(Net::HTTPSuccess) - - response.body - rescue StandardError => e - SolarWindsAPM.logger.debug { "Request failed: #{e.message}" } - nil end end end diff --git a/lib/solarwinds_apm/support/resource_detector/aws/lambda.rb b/lib/solarwinds_apm/support/resource_detector/aws/lambda.rb index 17b64f6b..942c95a5 100644 --- a/lib/solarwinds_apm/support/resource_detector/aws/lambda.rb +++ b/lib/solarwinds_apm/support/resource_detector/aws/lambda.rb @@ -1,52 +1,65 @@ # frozen_string_literal: true -# © 2023 SolarWinds Worldwide, LLC. All rights reserved. +# Copyright The OpenTelemetry Authors # -# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at:http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +# SPDX-License-Identifier: Apache-2.0 -require 'net/http' -require 'uri' -require 'json' -require 'socket' +require 'opentelemetry/semantic_conventions/resource' -module SolarWindsAPM - module ResourceDetector - module Lambda - module_function +module OpenTelemetry + module Resource + module Detector + module AWS + # Lambda contains detect class method for determining Lambda resource attributes + module Lambda + extend self - def detect - attribute = gather_data - ::OpenTelemetry::SDK::Resources::Resource.create(attribute) - end + # Create a constant for resource semantic conventions + RESOURCE = ::OpenTelemetry::SemanticConventions::Resource + + def detect + # Return empty resource if not running on Lambda + return ::OpenTelemetry::SDK::Resources::Resource.create({}) unless lambda_environment? + + resource_attributes = {} - def gather_data - return {} unless ENV['AWS_EXECUTION_ENV'].to_s.start_with?('AWS_Lambda_') + begin + # Set Lambda-specific attributes from environment variables + resource_attributes[RESOURCE::CLOUD_PROVIDER] = 'aws' + resource_attributes[RESOURCE::CLOUD_PLATFORM] = 'aws_lambda' + resource_attributes[RESOURCE::CLOUD_REGION] = ENV.fetch('AWS_REGION', nil) + resource_attributes[RESOURCE::FAAS_NAME] = ENV.fetch('AWS_LAMBDA_FUNCTION_NAME', nil) + resource_attributes[RESOURCE::FAAS_VERSION] = ENV.fetch('AWS_LAMBDA_FUNCTION_VERSION', nil) + resource_attributes[RESOURCE::FAAS_INSTANCE] = ENV.fetch('AWS_LAMBDA_LOG_STREAM_NAME', nil) - region = ENV.fetch('AWS_REGION', nil) - function_name = ENV.fetch('AWS_LAMBDA_FUNCTION_NAME', nil) - function_version = ENV.fetch('AWS_LAMBDA_FUNCTION_VERSION', nil) - memory_size = ENV.fetch('AWS_LAMBDA_FUNCTION_MEMORY_SIZE', nil) + # Convert memory size to integer + resource_attributes[RESOURCE::FAAS_MAX_MEMORY] = ENV['AWS_LAMBDA_FUNCTION_MEMORY_SIZE'].to_i if ENV['AWS_LAMBDA_FUNCTION_MEMORY_SIZE'] + rescue StandardError => e + ::OpenTelemetry.handle_error(exception: e, message: 'Lambda resource detection failed') + return ::OpenTelemetry::SDK::Resources::Resource.create({}) + end - # These environment variables are not available in Lambda SnapStart functions - log_group_name = ENV.fetch('AWS_LAMBDA_LOG_GROUP_NAME', nil) - log_stream_name = ENV.fetch('AWS_LAMBDA_LOG_STREAM_NAME', nil) + # Filter out nil or empty values + # Note: we need to handle integers differently since they don't respond to empty? + resource_attributes.delete_if do |_key, value| + value.nil? || (value.respond_to?(:empty?) && value.empty?) + end - attributes = { - ::OpenTelemetry::SemanticConventions::Resource::CLOUD_PROVIDER => 'aws', - ::OpenTelemetry::SemanticConventions::Resource::CLOUD_PLATFORM => 'aws_lambda', - ::OpenTelemetry::SemanticConventions::Resource::CLOUD_REGION => region, - ::OpenTelemetry::SemanticConventions::Resource::FAAS_NAME => function_name, - ::OpenTelemetry::SemanticConventions::Resource::FAAS_VERSION => function_version, - ::OpenTelemetry::SemanticConventions::Resource::FAAS_MAX_MEMORY => memory_size.to_i * 1024 * 1024 - } + ::OpenTelemetry::SDK::Resources::Resource.create(resource_attributes) + end - attributes[::OpenTelemetry::SemanticConventions::Resource::AWS_LOG_GROUP_NAMES] = [log_group_name] if log_group_name - attributes[::OpenTelemetry::SemanticConventions::Resource::FAAS_INSTANCE] = [log_stream_name] if log_stream_name + private - attributes.compact! - attributes + # Determines if the current environment is AWS Lambda + # + # @return [Boolean] true if running on AWS Lambda + def lambda_environment? + # Check for Lambda-specific environment variables + !ENV['AWS_LAMBDA_FUNCTION_NAME'].nil? && + !ENV['AWS_LAMBDA_FUNCTION_VERSION'].nil? && + !ENV['AWS_LAMBDA_LOG_STREAM_NAME'].nil? + end + end end end end diff --git a/lib/solarwinds_apm/version.rb b/lib/solarwinds_apm/version.rb index b20cafd3..338756ca 100644 --- a/lib/solarwinds_apm/version.rb +++ b/lib/solarwinds_apm/version.rb @@ -14,7 +14,7 @@ module Version MAJOR = 7 # breaking, MINOR = 0 # feature, PATCH = 0 # fix => BFF - PRE = nil + PRE = 'prev1' STRING = [MAJOR, MINOR, PATCH, PRE].compact.join('.') end diff --git a/solarwinds_apm.gemspec b/solarwinds_apm.gemspec index 1a11e15a..fa955975 100644 --- a/solarwinds_apm.gemspec +++ b/solarwinds_apm.gemspec @@ -30,11 +30,10 @@ Gem::Specification.new do |s| s.add_dependency('opentelemetry-exporter-otlp-metrics', '>= 0.3.0') s.add_dependency('opentelemetry-instrumentation-all', '>= 0.31.0') s.add_dependency('opentelemetry-metrics-sdk', '>= 0.2.0') - s.add_dependency('opentelemetry-resource-detector-aws', '>= 0.2.0') - s.add_dependency('opentelemetry-resource-detector-azure', '>= 0.2.0') - s.add_dependency('opentelemetry-resource-detector-container', '>= 0.2.0') + s.add_dependency('opentelemetry-resource-detector-azure', '>= 0.1.0') + s.add_dependency('opentelemetry-resource-detector-container', '>= 0.1.0') s.add_dependency('opentelemetry-sdk', '>= 1.2.0') - s.required_ruby_version = '>= 3.1.0' + s.required_ruby_version = '>= 3.0.0' s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) } end diff --git a/test/minitest_helper.rb b/test/minitest_helper.rb index e26b5509..ca5d835d 100644 --- a/test/minitest_helper.rb +++ b/test/minitest_helper.rb @@ -24,8 +24,6 @@ require './lib/solarwinds_apm/version' require './lib/solarwinds_apm/logger' -require_relative 'support/resource_detector/aws/sample_result' - # simplecov coverage information require 'simplecov' require 'simplecov-console' diff --git a/test/support/resource_detector/aws/beanstalk_test.rb b/test/support/resource_detector/aws/beanstalk_test.rb deleted file mode 100644 index 1b7bc93d..00000000 --- a/test/support/resource_detector/aws/beanstalk_test.rb +++ /dev/null @@ -1,31 +0,0 @@ -# frozen_string_literal: true - -# Copyright (c) 2016 SolarWinds, LLC. -# All rights reserved. - -require 'minitest_helper' -require './lib/solarwinds_apm/support/resource_detector/aws/beanstalk' - -describe 'AWS Beanstalk Resource Detector Test' do - puts "\n\033[1m=== TEST RUN Beanstalk TEST: #{RUBY_VERSION} #{File.basename(__FILE__)} #{Time.now.strftime('%Y-%m-%d %H:%M')} ===\033[0m\n" - - let(:beanstalk_conf_dir) { '/var/elasticbeanstalk/xray' } - let(:beanstalk_conf_path) { '/var/elasticbeanstalk/xray/environment.conf' } - - before do - unless File.exist?(beanstalk_conf_path) - FileUtils.mkdir_p(beanstalk_conf_dir) - File.open(beanstalk_conf_path, 'w') do |file| - file.puts 'beanstalk' - end - end - end - - it 'returns empty beanstalk attributes if the conf file is malformat' do - attributes = SolarWindsAPM::ResourceDetector::Beanstalk.detect - attribute_hash = attributes.instance_variable_get(:@attributes) - - _(attributes).must_be_instance_of(OpenTelemetry::SDK::Resources::Resource) - assert_equal(attribute_hash, {}) - end -end diff --git a/test/support/resource_detector/aws/ec2_test.rb b/test/support/resource_detector/aws/ec2_test.rb deleted file mode 100644 index ac4a627e..00000000 --- a/test/support/resource_detector/aws/ec2_test.rb +++ /dev/null @@ -1,83 +0,0 @@ -# frozen_string_literal: true - -# Copyright (c) 2016 SolarWinds, LLC. -# All rights reserved. - -require 'minitest_helper' -require 'webmock' -require 'webmock/minitest' -require './lib/solarwinds_apm/support/resource_detector/aws/ec2' - -describe 'AWS EC2 Resource Detector Test' do - puts "\n\033[1m=== TEST RUN EC2 TEST: #{RUBY_VERSION} #{File.basename(__FILE__)} #{Time.now.strftime('%Y-%m-%d %H:%M')} ===\033[0m\n" - - let(:ec2_metadata_host) { '169.254.169.254' } - let(:token_path) { '/latest/api/token' } - let(:identity_document_path) { '/latest/dynamic/instance-identity/document' } - let(:hostname_path) { '/latest/meta-data/hostname' } - let(:mock_token) { 'mock-token-123456' } - let(:mock_identity_document) { EC2_IDENTITY_DOC } - let(:mock_hostname) { 'ip-172-12-34-567.mock-west-2.compute.internal' } - let(:expected_resource_attributes) do - { - 'cloud.provider' => 'aws', - 'cloud.platform' => 'aws_ec2', - 'cloud.account.id' => '123456789012', - 'cloud.region' => 'mock-west-2', - 'cloud.availability_zone' => 'mock-west-2a', - 'host.id' => 'i-1234ab56cd7e89f01', - 'host.type' => 't2.micro-mock', - 'host.name' => 'ip-172-12-34-567.mock-west-2.compute.internal' - } - end - - before do - WebMock.disable_net_connect! - - # Stub token request - stub_request(:put, "http://#{ec2_metadata_host}#{token_path}") - .with(headers: { 'X-aws-ec2-metadata-token-ttl-seconds' => '60' }) - .to_return(status: 200, body: mock_token) - - # Stub identity document request - stub_request(:get, "http://#{ec2_metadata_host}#{identity_document_path}") - .with(headers: { 'X-aws-ec2-metadata-token' => mock_token }) - .to_return(status: 200, body: mock_identity_document.to_json) - - # Stub hostname request - stub_request(:get, "http://#{ec2_metadata_host}#{hostname_path}") - .with(headers: { 'X-aws-ec2-metadata-token' => mock_token }) - .to_return(status: 200, body: mock_hostname) - end - - after do - WebMock.reset! - WebMock.allow_net_connect! - end - - it 'returns a resource with EC2 attributes' do - attributes = SolarWindsAPM::ResourceDetector::EC2.detect - attribute_hash = attributes.instance_variable_get(:@attributes) - - _(attributes).must_be_instance_of(OpenTelemetry::SDK::Resources::Resource) - assert_equal(attribute_hash, expected_resource_attributes) - end - - it 'when token request returns error code in IMDSv2, identity is nil' do - # Stub token request - stub_request(:put, "http://#{ec2_metadata_host}#{token_path}") - .with(headers: { 'X-aws-ec2-metadata-token-ttl-seconds' => '60' }) - .to_return(status: 403, body: 'Forbidden') - - stub_request(:get, "http://#{ec2_metadata_host}#{identity_document_path}") - .to_return(status: 403, body: 'Forbidden') - - stub_request(:get, "http://#{ec2_metadata_host}#{hostname_path}") - .to_return(status: 403, body: 'Forbidden') - - attributes = SolarWindsAPM::ResourceDetector::EC2.detect - attribute_hash = attributes.instance_variable_get(:@attributes) - - assert_equal(attribute_hash, { 'cloud.provider' => 'aws', 'cloud.platform' => 'aws_ec2' }) - end -end diff --git a/test/support/resource_detector/aws/ecs_test.rb b/test/support/resource_detector/aws/ecs_test.rb deleted file mode 100644 index cc183b5e..00000000 --- a/test/support/resource_detector/aws/ecs_test.rb +++ /dev/null @@ -1,134 +0,0 @@ -# frozen_string_literal: true - -# Copyright (c) 2016 SolarWinds, LLC. -# All rights reserved. - -require 'minitest_helper' -require 'webmock' -require 'webmock/minitest' -require './lib/solarwinds_apm/support/resource_detector/aws/ecs' - -describe 'AWS ECS Resource Detector Test' do - puts "\n\033[1m=== TEST RUN ECS TEST: #{RUBY_VERSION} #{File.basename(__FILE__)} #{Time.now.strftime('%Y-%m-%d %H:%M')} ===\033[0m\n" - - let(:cgroup_path) { '/proc/self/cgroup' } - let(:sample_json) { ECS_SAMPLE_JSON } - let(:sample_task) { ECS_SAMPLE_TASK } - - let(:expected_resource_attributes) do - { 'cloud.provider' => 'aws', - 'cloud.platform' => 'aws_ecs', - 'aws.ecs.cluster.arn' => 'arn:aws:ecs:us-east-1:123456789012:cluster/MyEmptyCluster', - 'aws.ecs.launchtype' => 'FARGATE', - 'aws.ecs.task.arn' => 'arn:aws:ecs:us-east-1:123456789012:task/MyEmptyCluster/bfa2636268144d039771334145e490c5', - 'aws.ecs.task.family' => 'sample-fargate', - 'aws.ecs.task.revision' => '5', - 'cloud.account.id' => '123456789012', - 'cloud.region' => 'us-east-1', - 'cloud.availability_zone' => 'us-east-1d', - 'cloud.resource_id' => 'arn:aws:ecs:us-west-2:111122223333:container/05966557-f16c-49cb-9352-24b3a0dcd0e1', - 'aws.ecs.container.arn' => 'arn:aws:ecs:us-west-2:111122223333:container/05966557-f16c-49cb-9352-24b3a0dcd0e1', - 'aws.log.group.names' => ['us-west-2'], - 'aws.log.group.arns' => ['arn:aws:logs:us-west-2:111122223333:log-group:us-west-2'], - 'aws.log.stream.names' => ['ecs/curl/cd189a933e5849daa93386466019ab50'], - 'aws.log.stream.arns' => ['arn:aws:logs:us-west-2:111122223333:log-group:us-west-2:log-stream:ecs/curl/cd189a933e5849daa93386466019ab50'] } - end - - before do - WebMock.disable_net_connect! - - unless File.exist?(cgroup_path) - File.open(cgroup_path, 'w') do |file| - file.puts '0::/' - end - end - end - - after do - WebMock.reset! - WebMock.allow_net_connect! - ENV.delete('ECS_CONTAINER_METADATA_URI_V4') - end - - it 'return empty resource attributes if not in ecs env' do - attributes = SolarWindsAPM::ResourceDetector::ECS.detect - attribute_hash = attributes.instance_variable_get(:@attributes) - - _(attributes).must_be_instance_of(OpenTelemetry::SDK::Resources::Resource) - assert_equal(attribute_hash, {}) - end - - it 'returns a resource with ECS attributes' do - ENV['ECS_CONTAINER_METADATA_URI_V4'] = 'http://169.254.170.2/v4/abcd1234-5678-90ef-ghij-klmnopqrstuv' - # Stub token request - stub_request(:get, 'http://169.254.170.2/v4/abcd1234-5678-90ef-ghij-klmnopqrstuv') - .to_return(status: 200, body: sample_json.to_json) - - stub_request(:get, 'http://169.254.170.2/v4/abcd1234-5678-90ef-ghij-klmnopqrstuv/task') - .to_return(status: 200, body: sample_task.to_json) - - attributes = SolarWindsAPM::ResourceDetector::ECS.detect - attribute_hash = attributes.instance_variable_get(:@attributes) - - _(attributes).must_be_instance_of(OpenTelemetry::SDK::Resources::Resource) - assert(attribute_hash['host.name']) - expected_resource_attributes['host.name'] = attribute_hash['host.name'] - assert_equal(attribute_hash, expected_resource_attributes) - end - - it 'returns a resource with ECS attributes without valid container metadata' do - ENV['ECS_CONTAINER_METADATA_URI_V4'] = 'http://169.254.170.2/v4/abcd1234-5678-90ef-ghij-klmnopqrstuv' - # Stub token request - stub_request(:get, 'http://169.254.170.2/v4/abcd1234-5678-90ef-ghij-klmnopqrstuv') - .to_return(status: 403, body: 'Forbidden') - - stub_request(:get, 'http://169.254.170.2/v4/abcd1234-5678-90ef-ghij-klmnopqrstuv/task') - .to_return(status: 200, body: sample_task.to_json) - - attributes = SolarWindsAPM::ResourceDetector::ECS.detect - attribute_hash = attributes.instance_variable_get(:@attributes) - - ['cloud.resource_id', - 'aws.ecs.container.arn', - 'aws.log.group.names', - 'aws.log.group.arns', - 'aws.log.stream.names', - 'aws.log.stream.arns'].each do |key| - expected_resource_attributes.delete(key) - end - - _(attributes).must_be_instance_of(OpenTelemetry::SDK::Resources::Resource) - assert(attribute_hash['host.name']) - expected_resource_attributes['host.name'] = attribute_hash['host.name'] - assert_equal(attribute_hash, expected_resource_attributes) - end - - it 'returns a resource with ECS attributes without valid task metadata' do - ENV['ECS_CONTAINER_METADATA_URI_V4'] = 'http://169.254.170.2/v4/abcd1234-5678-90ef-ghij-klmnopqrstuv' - # Stub token request - stub_request(:get, 'http://169.254.170.2/v4/abcd1234-5678-90ef-ghij-klmnopqrstuv') - .to_return(status: 200, body: sample_json.to_json) - - stub_request(:get, 'http://169.254.170.2/v4/abcd1234-5678-90ef-ghij-klmnopqrstuv/task') - .to_return(status: 403, body: 'Forbidden') - - attributes = SolarWindsAPM::ResourceDetector::ECS.detect - attribute_hash = attributes.instance_variable_get(:@attributes) - - ['aws.ecs.cluster.arn', - 'aws.ecs.launchtype', - 'aws.ecs.task.arn', - 'aws.ecs.task.family', - 'aws.ecs.task.revision', - 'cloud.account.id', - 'cloud.region', - 'cloud.availability_zone'].each do |key| - expected_resource_attributes.delete(key) - end - - _(attributes).must_be_instance_of(OpenTelemetry::SDK::Resources::Resource) - assert(attribute_hash['host.name']) - expected_resource_attributes['host.name'] = attribute_hash['host.name'] - assert_equal(attribute_hash, expected_resource_attributes) - end -end diff --git a/test/support/resource_detector/aws/eks_test.rb b/test/support/resource_detector/aws/eks_test.rb deleted file mode 100644 index 315d0b0d..00000000 --- a/test/support/resource_detector/aws/eks_test.rb +++ /dev/null @@ -1,81 +0,0 @@ -# frozen_string_literal: true - -# Copyright (c) 2016 SolarWinds, LLC. -# All rights reserved. - -require 'minitest_helper' -require 'webmock' -require 'webmock/minitest' -require './lib/solarwinds_apm/support/resource_detector/aws/eks' - -describe 'AWS EKS Resource Detector Test' do - puts "\n\033[1m=== TEST RUN EKS TEST: #{RUBY_VERSION} #{File.basename(__FILE__)} #{Time.now.strftime('%Y-%m-%d %H:%M')} ===\033[0m\n" - - let(:k8s_svc_url) { 'kubernetes.default.svc' } - let(:k8s_dir) { '/var/run/secrets/kubernetes.io/serviceaccount' } - let(:k8s_token_path) { '/var/run/secrets/kubernetes.io/serviceaccount/token' } - let(:k8s_cert_path) { '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt' } - let(:auth_configmap_path) { '/api/v1/namespaces/kube-system/configmaps/aws-auth' } - let(:cw_configmap_path) { '/api/v1/namespaces/amazon-cloudwatch/configmaps/cluster-info' } - let(:cgroup_path) { '/proc/self/cgroup' } - let(:token) { 'k8s_cred_header' } - let(:cert) { 'abcd' } - - let(:config_map) { { 'test' => 'map' } } - let(:cluster_map) { EKS_CLUSTER_MAP } - - before do - WebMock.disable_net_connect! - - unless File.exist?(k8s_token_path) - FileUtils.mkdir_p(k8s_dir) - File.open(k8s_token_path, 'w') do |file| - file.puts token - end - end - - unless File.exist?(k8s_cert_path) - FileUtils.mkdir_p(k8s_dir) - File.open(k8s_cert_path, 'w') do |file| - file.puts cert - end - end - end - - after do - WebMock.reset! - WebMock.allow_net_connect! - end - - it 'returns empty resource with EKS attributes when failed' do - stub_request(:get, "https://#{k8s_svc_url}#{auth_configmap_path}") - .with(headers: { 'Authorization' => "Bearer #{token}" }) - .to_return(status: 403, body: '') - - stub_request(:get, "https://#{k8s_svc_url}#{cw_configmap_path}") - .with(headers: { 'Authorization' => "Bearer #{token}" }) - .to_return(status: 403, body: '') - - attributes = SolarWindsAPM::ResourceDetector::EKS.detect - attribute_hash = attributes.instance_variable_get(:@attributes) - - _(attributes).must_be_instance_of(OpenTelemetry::SDK::Resources::Resource) - assert_equal(attribute_hash, {}) - end - - it 'returns resource with EKS attributes' do - stub_request(:get, "https://#{k8s_svc_url}#{auth_configmap_path}") - .with(headers: { 'Authorization' => "Bearer #{token}" }) - .to_return(status: 200, body: config_map.to_json) - - stub_request(:get, "https://#{k8s_svc_url}#{cw_configmap_path}") - .with(headers: { 'Authorization' => "Bearer #{token}" }) - .to_return(status: 200, body: cluster_map.to_json) - - attributes = SolarWindsAPM::ResourceDetector::EKS.detect - attribute_hash = attributes.instance_variable_get(:@attributes) - - _(attributes).must_be_instance_of(OpenTelemetry::SDK::Resources::Resource) - assert_equal(attribute_hash, { 'cloud.provider' => 'aws', 'cloud.platform' => 'aws_eks', 'k8s.cluster.name' => 'my-eks-cluster' }) - end -end diff --git a/test/support/resource_detector/aws/lambda_test.rb b/test/support/resource_detector/aws/lambda_test.rb deleted file mode 100644 index 245d0950..00000000 --- a/test/support/resource_detector/aws/lambda_test.rb +++ /dev/null @@ -1,51 +0,0 @@ -# frozen_string_literal: true - -# Copyright (c) 2016 SolarWinds, LLC. -# All rights reserved. - -require 'minitest_helper' -require './lib/solarwinds_apm/support/resource_detector/aws/lambda' - -describe 'AWS Lambda Resource Detector Test' do - puts "\n\033[1m=== TEST RUN Lambda TEST: #{RUBY_VERSION} #{File.basename(__FILE__)} #{Time.now.strftime('%Y-%m-%d %H:%M')} ===\033[0m\n" - - let(:expected_attributes) do - { 'cloud.provider' => 'aws', 'cloud.platform' => 'aws_lambda', 'cloud.region' => 'us-west-2', 'faas.name' => 'my_lambda_function', 'faas.version' => '1', 'faas.max_memory' => 134_217_728, 'aws.log.group.names' => ['/aws/lambda/my_lambda_function'], 'faas.instance' => ['2024/03/30/[$LATEST]abcdefgh1234567890'] } - end - before do - ENV['AWS_REGION'] = 'us-west-2' - ENV['AWS_LAMBDA_FUNCTION_NAME'] = 'my_lambda_function' - ENV['AWS_LAMBDA_FUNCTION_VERSION'] = '1' - ENV['AWS_LAMBDA_FUNCTION_MEMORY_SIZE'] = '128' - ENV['AWS_LAMBDA_LOG_GROUP_NAME'] = '/aws/lambda/my_lambda_function' - ENV['AWS_LAMBDA_LOG_STREAM_NAME'] = '2024/03/30/[$LATEST]abcdefgh1234567890' - end - - after do - ENV.delete('AWS_REGION') - ENV.delete('AWS_LAMBDA_FUNCTION_NAME') - ENV.delete('AWS_LAMBDA_FUNCTION_VERSION') - ENV.delete('AWS_LAMBDA_FUNCTION_MEMORY_SIZE') - ENV.delete('AWS_LAMBDA_LOG_GROUP_NAME') - ENV.delete('AWS_LAMBDA_LOG_STREAM_NAME') - ENV.delete('AWS_EXECUTION_ENV') - end - - it 'return empty attributes if not in lambda env' do - attributes = SolarWindsAPM::ResourceDetector::Lambda.detect - attribute_hash = attributes.instance_variable_get(:@attributes) - - _(attributes).must_be_instance_of(OpenTelemetry::SDK::Resources::Resource) - assert_equal(attribute_hash, {}) - end - - it 'get simple lambda resource attributes' do - ENV['AWS_EXECUTION_ENV'] = 'AWS_Lambda_abcd' - - attributes = SolarWindsAPM::ResourceDetector::Lambda.detect - attribute_hash = attributes.instance_variable_get(:@attributes) - - _(attributes).must_be_instance_of(OpenTelemetry::SDK::Resources::Resource) - assert_equal(attribute_hash, expected_attributes) - end -end diff --git a/test/support/resource_detector/aws/sample_result.rb b/test/support/resource_detector/aws/sample_result.rb deleted file mode 100644 index 4ad04ce3..00000000 --- a/test/support/resource_detector/aws/sample_result.rb +++ /dev/null @@ -1,190 +0,0 @@ -# frozen_string_literal: true - -# Copyright (c) 2025 SolarWinds, LLC. -# All rights reserved. - -# https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-metadata-endpoint-v4-fargate-examples.html -ECS_SAMPLE_JSON = { - 'DockerId' => 'cd189a933e5849daa93386466019ab50-2495160603', - 'Name' => 'curl', - 'DockerName' => 'curl', - 'Image' => '111122223333.dkr.ecr.us-west-2.amazonaws.com/curltest:latest', - 'ImageID' => 'sha256:25f3695bedfb454a50f12d127839a68ad3caf91e451c1da073db34c542c4d2cb', - 'Labels' => { - 'com.amazonaws.ecs.cluster' => 'arn:aws:ecs:us-west-2:111122223333:cluster/default', - 'com.amazonaws.ecs.container-name' => 'curl', - 'com.amazonaws.ecs.task-arn' => 'arn:aws:ecs:us-west-2:111122223333:task/default/cd189a933e5849daa93386466019ab50', - 'com.amazonaws.ecs.task-definition-family' => 'curltest', - 'com.amazonaws.ecs.task-definition-version' => '2' - }, - 'DesiredStatus' => 'RUNNING', - 'KnownStatus' => 'RUNNING', - 'Limits' => { - 'CPU' => 10, - 'Memory' => 128 - }, - 'CreatedAt' => '2020-10-08T20:09:11.44527186Z', - 'StartedAt' => '2020-10-08T20:09:11.44527186Z', - 'Type' => 'NORMAL', - 'Networks' => [ - { - 'NetworkMode' => 'awsvpc', - 'IPv4Addresses' => [ - '192.0.2.3' - ], - 'AttachmentIndex' => 0, - 'MACAddress' => '0a:de:f6:10:51:e5', - 'IPv4SubnetCIDRBlock' => '192.0.2.0/24', - 'DomainNameServers' => [ - '192.0.2.2' - ], - 'DomainNameSearchList' => [ - 'us-west-2.compute.internal' - ], - 'PrivateDNSName' => 'ip-10-0-0-222.us-west-2.compute.internal', - 'SubnetGatewayIpv4Address' => '192.0.2.0/24' - } - ], - 'ContainerARN' => 'arn:aws:ecs:us-west-2:111122223333:container/05966557-f16c-49cb-9352-24b3a0dcd0e1', - 'LogOptions' => { - 'awslogs-create-group' => 'true', - 'awslogs-group' => '/ecs/containerlogs', - 'awslogs-region' => 'us-west-2', - 'awslogs-stream' => 'ecs/curl/cd189a933e5849daa93386466019ab50' - }, - 'LogDriver' => 'awslogs', - 'Snapshotter' => 'overlayfs' -}.freeze - -ECS_SAMPLE_TASK = { - 'Cluster' => 'arn:aws:ecs:us-east-1:123456789012:cluster/MyEmptyCluster', - 'TaskARN' => 'arn:aws:ecs:us-east-1:123456789012:task/MyEmptyCluster/bfa2636268144d039771334145e490c5', - 'Family' => 'sample-fargate', - 'Revision' => '5', - 'DesiredStatus' => 'RUNNING', - 'KnownStatus' => 'RUNNING', - 'Limits' => { - 'CPU' => 0.25, - 'Memory' => 512 - }, - 'PullStartedAt' => '2023-07-21T15:45:33.532811081Z', - 'PullStoppedAt' => '2023-07-21T15:45:38.541068435Z', - 'AvailabilityZone' => 'us-east-1d', - 'Containers' => [ - { - 'DockerId' => 'bfa2636268144d039771334145e490c5-1117626119', - 'Name' => 'curl-image', - 'DockerName' => 'curl-image', - 'Image' => 'curlimages/curl', - 'ImageID' => 'sha256:daf3f46a2639c1613b25e85c9ee4193af8a1d538f92483d67f9a3d7f21721827', - 'Labels' => { - 'com.amazonaws.ecs.cluster' => 'arn:aws:ecs:us-east-1:123456789012:cluster/MyEmptyCluster', - 'com.amazonaws.ecs.container-name' => 'curl-image', - 'com.amazonaws.ecs.task-arn' => 'arn:aws:ecs:us-east-1:123456789012:task/MyEmptyCluster/bfa2636268144d039771334145e490c5', - 'com.amazonaws.ecs.task-definition-family' => 'sample-fargate', - 'com.amazonaws.ecs.task-definition-version' => '5' - }, - 'DesiredStatus' => 'RUNNING', - 'KnownStatus' => 'RUNNING', - 'Limits' => { 'CPU' => 128 }, - 'CreatedAt' => '2023-07-21T15:45:44.91368314Z', - 'StartedAt' => '2023-07-21T15:45:44.91368314Z', - 'Type' => 'NORMAL', - 'Networks' => [ - { - 'NetworkMode' => 'awsvpc', - 'IPv4Addresses' => ['172.31.42.189'], - 'AttachmentIndex' => 0, - 'MACAddress' => '0e:98:9f:33:76:d3', - 'IPv4SubnetCIDRBlock' => '172.31.32.0/20', - 'DomainNameServers' => ['172.31.0.2'], - 'DomainNameSearchList' => ['ec2.internal'], - 'PrivateDNSName' => 'ip-172-31-42-189.ec2.internal', - 'SubnetGatewayIpv4Address' => '172.31.32.1/20' - } - ], - 'ContainerARN' => 'arn:aws:ecs:us-east-1:123456789012:container/MyEmptyCluster/bfa2636268144d039771334145e490c5/da6cccf7-1178-400c-afdf-7536173ee209', - 'Snapshotter' => 'overlayfs' - }, - { - 'DockerId' => 'bfa2636268144d039771334145e490c5-3681984407', - 'Name' => 'fargate-app', - 'DockerName' => 'fargate-app', - 'Image' => 'public.ecr.aws/docker/library/httpd:latest', - 'ImageID' => 'sha256:8059bdd0058510c03ae4c808de8c4fd2c1f3c1b6d9ea75487f1e5caa5ececa02', - 'Labels' => { - 'com.amazonaws.ecs.cluster' => 'arn:aws:ecs:us-east-1:123456789012:cluster/MyEmptyCluster', - 'com.amazonaws.ecs.container-name' => 'fargate-app', - 'com.amazonaws.ecs.task-arn' => 'arn:aws:ecs:us-east-1:123456789012:task/MyEmptyCluster/bfa2636268144d039771334145e490c5', - 'com.amazonaws.ecs.task-definition-family' => 'sample-fargate', - 'com.amazonaws.ecs.task-definition-version' => '5' - }, - 'DesiredStatus' => 'RUNNING', - 'KnownStatus' => 'RUNNING', - 'Limits' => { 'CPU' => 2 }, - 'CreatedAt' => '2023-07-21T15:45:44.954460255Z', - 'StartedAt' => '2023-07-21T15:45:44.954460255Z', - 'Type' => 'NORMAL', - 'Networks' => [ - { - 'NetworkMode' => 'awsvpc', - 'IPv4Addresses' => ['172.31.42.189'], - 'AttachmentIndex' => 0, - 'MACAddress' => '0e:98:9f:33:76:d3', - 'IPv4SubnetCIDRBlock' => '172.31.32.0/20', - 'DomainNameServers' => ['172.31.0.2'], - 'DomainNameSearchList' => ['ec2.internal'], - 'PrivateDNSName' => 'ip-172-31-42-189.ec2.internal', - 'SubnetGatewayIpv4Address' => '172.31.32.1/20' - } - ], - 'ContainerARN' => 'arn:aws:ecs:us-east-1:123456789012:container/MyEmptyCluster/bfa2636268144d039771334145e490c5/f65b461d-aa09-4acb-a579-9785c0530cbc', - 'Snapshotter' => 'overlayfs' - } - ], - 'LaunchType' => 'FARGATE', - 'ClockDrift' => { - 'ClockErrorBound' => 0.446931, - 'ReferenceTimestamp' => '2023-07-21T16:09:17Z', - 'ClockSynchronizationStatus' => 'SYNCHRONIZED' - }, - 'EphemeralStorageMetrics' => { - 'Utilized' => 261, - 'Reserved' => 20_496 - } -}.freeze - -EKS_CLUSTER_MAP = { - 'kind' => 'ConfigMap', - 'apiVersion' => 'v1', - metadata: { - 'name' => 'cluster-info', - 'namespace' => 'amazon-cloudwatch', - 'uid' => 'abcdef12-3456-7890-abcd-ef1234567890', - 'resourceVersion' => '67890', - 'creationTimestamp' => '2024-03-30T12:34:56Z' - }, - 'data' => { - 'cluster.name' => 'my-eks-cluster', - 'cluster.endpoint' => 'https://ABCD123456.gr7.us-west-2.eks.amazonaws.com', - 'cluster.region' => 'us-west-2' - } -}.freeze - -EC2_IDENTITY_DOC = { - accountId: '123456789012', - architecture: 'x86_64', - availabilityZone: 'mock-west-2a', - billingProducts: nil, - devpayProductCodes: nil, - marketplaceProductCodes: nil, - imageId: 'ami-0957cee1854021123', - instanceId: 'i-1234ab56cd7e89f01', - instanceType: 't2.micro-mock', - kernelId: nil, - pendingTime: '2021-07-13T21:53:41Z', - privateIp: '172.12.34.567', - ramdiskId: nil, - region: 'mock-west-2', - version: '2017-09-30' -}.freeze