Module: OpenTelemetry::Instrumentation::AwsLambda::Wrap

Included in:
Handler
Defined in:
lib/opentelemetry/instrumentation/aws_lambda/wrap.rb

Overview

Helper module that can be used to wrap a lambda handler method

Constant Summary collapse

AWS_TRIGGERS =

rubocop:disable Metrics/ModuleLength

['aws:sqs', 'aws:s3', 'aws:sns', 'aws:dynamodb'].freeze
DEFAULT_FLUSH_TIMEOUT =
ENV.fetch('OTEL_INSTRUMENTATION_AWS_LAMBDA_FLUSH_TIMEOUT', '30000').to_i

Instance Method Summary collapse

Instance Method Details

#instrument_handler(method, flush_timeout: DEFAULT_FLUSH_TIMEOUT) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/opentelemetry/instrumentation/aws_lambda/wrap.rb', line 15

def instrument_handler(method, flush_timeout: DEFAULT_FLUSH_TIMEOUT)
  raise ArgumentError, "#{method} is not a method of #{name}" unless respond_to?(method)

  uninstrumented_method = "#{method}_without_instrumentation"
  singleton_class.alias_method uninstrumented_method, method

  handler = "#{name}.#{method}"

  define_singleton_method(method) do |event:, context:|
    wrap_lambda(event: event, context: context, handler: handler, flush_timeout: flush_timeout) { public_send(uninstrumented_method, event: event, context: context) }
  end
end

#instrumentation_configObject



70
71
72
# File 'lib/opentelemetry/instrumentation/aws_lambda/wrap.rb', line 70

def instrumentation_config
  AwsLambda::Instrumentation.instance.config
end

#tracerObject



74
75
76
# File 'lib/opentelemetry/instrumentation/aws_lambda/wrap.rb', line 74

def tracer
  AwsLambda::Instrumentation.instance.tracer
end

#wrap_lambda(event:, context:, handler:, flush_timeout: DEFAULT_FLUSH_TIMEOUT) ⇒ Object

Try to record and re-raise any exception from the wrapped function handler Instrumentation should never raise its own exception



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
61
62
63
64
65
66
67
68
# File 'lib/opentelemetry/instrumentation/aws_lambda/wrap.rb', line 30

def wrap_lambda(event:, context:, handler:, flush_timeout: DEFAULT_FLUSH_TIMEOUT)
  parent_context = extract_parent_context(event)

  span_kind = if event['Records'] && AWS_TRIGGERS.include?(event['Records'].dig(0, 'eventSource'))
                :consumer
              else
                :server
              end

  original_handler_error = nil
  original_response = nil
  OpenTelemetry::Context.with_current(parent_context) do
    tracer.in_span(handler, attributes: otel_attributes(event, context), kind: span_kind) do |span|
      begin
        response = yield

        unless span.attributes.key?(OpenTelemetry::SemanticConventions::Trace::HTTP_STATUS_CODE)
          status_code = response['statusCode'] || response[:statusCode] if response.is_a?(Hash)
          span.set_attribute(OpenTelemetry::SemanticConventions::Trace::HTTP_STATUS_CODE, status_code) if status_code
        end
      rescue StandardError => e
        original_handler_error = e
      ensure
        original_response = response
      end
      if original_handler_error
        span.record_exception(original_handler_error)
        span.status = OpenTelemetry::Trace::Status.error(original_handler_error.message)
      end
    end
  end

  OpenTelemetry.tracer_provider.force_flush(timeout: flush_timeout)
  OpenTelemetry.meter_provider.force_flush(timeout: flush_timeout) if OpenTelemetry.respond_to?(:meter_provider)

  raise original_handler_error if original_handler_error

  original_response
end