Class: Datadog::Trace::Listener

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/lambda/trace/listener.rb

Overview

TraceListener tracks tracing context information

Instance Method Summary collapse

Constructor Details

#initialize(handler_name:, function_name:, patch_http:, merge_xray_traces:) ⇒ Listener

Returns a new instance of Listener.



20
21
22
23
24
25
26
27
# File 'lib/datadog/lambda/trace/listener.rb', line 20

def initialize(handler_name:, function_name:, patch_http:,
               merge_xray_traces:)
  @handler_name = handler_name
  @function_name = function_name
  @merge_xray_traces = merge_xray_traces

  Datadog::Trace.patch_http if patch_http
end

Instance Method Details

#on_end(response:) ⇒ Object

rubocop:enable Metrics/AbcSize



55
56
57
58
# File 'lib/datadog/lambda/trace/listener.rb', line 55

def on_end(response:)
  Datadog::Utils.send_end_invocation_request(response:)
  @trace&.finish
end

#on_start(event:, request_context:, cold_start:) ⇒ Object

rubocop:disable Metrics/AbcSize



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/datadog/lambda/trace/listener.rb', line 30

def on_start(event:, request_context:, cold_start:)
  trace_context = Datadog::Trace.extract_trace_context(event, @merge_xray_traces)
  Datadog::Trace.trace_context = trace_context
  Datadog::Utils.logger.debug "extracted trace context #{trace_context}"
  options = get_option_tags(
    request_context:,
    cold_start:
  )
  context = Datadog::Trace.trace_context
  source = context[:source] if context
  options[:tags]['_dd.parent_source'] = source if source && source != 'ddtrace'
  options[:resource] = @function_name
  options[:service] = 'aws.lambda'
  options[:span_type] = 'serverless'
  Datadog::Trace.apply_datadog_trace_context(Datadog::Trace.trace_context)

  trace_digest = Datadog::Utils.send_start_invocation_request(event:)
  # Only continue trace from a new one if it exist, or else,
  # it will create a new trace, which is not ideal here.
  options[:continue_from] = trace_digest if trace_digest

  @trace = Datadog::Tracing.trace('aws.lambda', **options)
end