Module: OpenTelemetry::Resource::Detector::AWS::Lambda

Extended by:
Lambda
Included in:
Lambda
Defined in:
lib/opentelemetry/resource/detector/aws/lambda.rb

Overview

Lambda contains detect class method for determining Lambda resource attributes

Constant Summary collapse

RESOURCE =

Create a constant for resource semantic conventions

OpenTelemetry::SemanticConventions::Resource
'/tmp/.otel-aws-account-id'

Instance Method Summary collapse

Instance Method Details

#detectObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/opentelemetry/resource/detector/aws/lambda.rb', line 23

def detect
  # Return empty resource if not running on Lambda
  return OpenTelemetry::SDK::Resources::Resource.create({}) unless lambda_environment?

  resource_attributes = {}

  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)

    # 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']

    # Read cloud.account.id from symlink created by the OTel Lambda extension
    begin
       = File.readlink(ACCOUNT_ID_SYMLINK_PATH)
      resource_attributes[RESOURCE::CLOUD_ACCOUNT_ID] = 
    rescue Errno::ENOENT, Errno::EINVAL
      OpenTelemetry.logger.debug('Lambda: cloud.account.id not available via symlink')
    end
  rescue StandardError => e
    OpenTelemetry.handle_error(exception: e, message: 'Lambda resource detection failed')
    return OpenTelemetry::SDK::Resources::Resource.create({})
  end

  # 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

  OpenTelemetry::SDK::Resources::Resource.create(resource_attributes)
end