Class: OpenTelemetry::Instrumentation::AwsLambda::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/opentelemetry/instrumentation/aws_lambda/handler.rb

Overview

Handler class that creates a span around the _HANDLER

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHandler

anytime when the code in a Lambda function is updated or the functional configuration is changed, the next invocation results in a cold start; therefore these instance variables will be up-to-date



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

def initialize
  @flush_timeout    = ENV.fetch('OTEL_INSTRUMENTATION_AWS_LAMBDA_FLUSH_TIMEOUT', '30000').to_i
  @original_handler = ENV['ORIG_HANDLER'] || ENV['_HANDLER'] || ''
  @handler_class    = nil
  @handler_method   = nil
  @handler_file     = nil

  resolve_original_handler
end

Instance Attribute Details

#handler_classObject (readonly)

Returns the value of attribute handler_class.



14
15
16
# File 'lib/opentelemetry/instrumentation/aws_lambda/handler.rb', line 14

def handler_class
  @handler_class
end

#handler_methodObject (readonly)

Returns the value of attribute handler_method.



14
15
16
# File 'lib/opentelemetry/instrumentation/aws_lambda/handler.rb', line 14

def handler_method
  @handler_method
end

Instance Method Details

#call_wrapped(event:, context:) ⇒ 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
# File 'lib/opentelemetry/instrumentation/aws_lambda/handler.rb', line 30

def call_wrapped(event:, context:)
  parent_context = extract_parent_context(event)

  span_kind = nil
  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(@original_handler, attributes: otel_attributes(event, context), kind: span_kind) do |span|
      begin
        response = call_original_handler(event: event, context: context)
        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
      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)

  raise original_handler_error if original_handler_error

  original_response
end

#instrumentation_configObject



67
68
69
# File 'lib/opentelemetry/instrumentation/aws_lambda/handler.rb', line 67

def instrumentation_config
  AwsLambda::Instrumentation.instance.config
end

#tracerObject



71
72
73
# File 'lib/opentelemetry/instrumentation/aws_lambda/handler.rb', line 71

def tracer
  AwsLambda::Instrumentation.instance.tracer
end