Module: Datadog::Utils

Defined in:
lib/datadog/lambda/utils/logger.rb,
lib/datadog/lambda/utils/extension.rb

Overview

Utils contains utility functions shared between modules

Constant Summary collapse

EXTENSION_PATH =
'/opt/extensions/datadog-agent'
EXTENSION_BASE_URL =
'http://127.0.0.1:8124'
START_INVOCATION_PATH =
'/lambda/start-invocation'
END_INVOCATION_PATH =
'/lambda/end-invocation'
DD_SPAN_ID_HEADER =
'x-datadog-span-id'
DD_PARENT_ID_HEADER =
Datadog::Tracing::Distributed::Datadog::PARENT_ID_KEY
START_INVOCATION_URI =
URI(EXTENSION_BASE_URL + START_INVOCATION_PATH).freeze
END_INVOCATION_URI =
URI(EXTENSION_BASE_URL + END_INVOCATION_PATH).freeze
PROPAGATOR =

Internal communications use Datadog tracing headers

Tracing::Distributed::Datadog.new(
  fetcher: Tracing::Contrib::HTTP::Distributed::Fetcher
)

Class Method Summary collapse

Class Method Details

.check_extension_runningObject



39
40
41
# File 'lib/datadog/lambda/utils/extension.rb', line 39

def self.check_extension_running
  File.exist?(EXTENSION_PATH)
end

.extension_running?Boolean

Returns:

  • (Boolean)


33
34
35
36
37
# File 'lib/datadog/lambda/utils/extension.rb', line 33

def self.extension_running?
  return @is_extension_running unless @is_extension_running.nil?

  @is_extension_running = check_extension_running
end

.loggerObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/datadog/lambda/utils/logger.rb', line 16

def self.logger
  @logger ||= Logger.new($stderr).tap do |logger|
    log_level = (ENV['DD_LOG_LEVEL'] || 'error').downcase
    logger.level = case log_level
                   when 'debug'
                     Logger::DEBUG
                   when 'info'
                     Logger::INFO
                   when 'warn'
                     Logger::WARN
                   else
                     Logger::ERROR
                   end
  end
end

.request_headersObject

rubocop:enable Metrics/AbcSize



81
82
83
84
85
86
87
# File 'lib/datadog/lambda/utils/extension.rb', line 81

def self.request_headers
  {
    # Header used to avoid tracing requests that are internal to
    # Datadog products.
    Datadog::Core::Transport::Ext::HTTP::HEADER_DD_INTERNAL_UNTRACED_REQUEST.to_sym => 'true'
  }
end

.send_end_invocation_request(response:, span_id:) ⇒ Object

rubocop:disable Metrics/AbcSize



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/datadog/lambda/utils/extension.rb', line 56

def self.send_end_invocation_request(response:, span_id:)
  return unless extension_running?

  request = Net::HTTP::Post.new(END_INVOCATION_URI)
  request.body = response.to_json
  request[Datadog::Core::Transport::Ext::HTTP::HEADER_DD_INTERNAL_UNTRACED_REQUEST] = 1

  trace_digest = Datadog::Tracing.active_trace&.to_digest

  PROPAGATOR.inject!(trace_digest, request)
  # Propagator doesn't inject span_id, so we do it manually
  # It is needed for the extension to take this span id
  request[DD_SPAN_ID_HEADER] = span_id.to_s
  # Remove Parent ID if it is the same as the Span ID
  request.delete(DD_PARENT_ID_HEADER) if request[DD_PARENT_ID_HEADER] == span_id.to_s
  Datadog::Utils.logger.debug "End invocation request headers: #{request.to_hash}"

  Net::HTTP.start(END_INVOCATION_URI.host, END_INVOCATION_URI.port) do |http|
    http.request(request)
  end
rescue StandardError => e
  Datadog::Utils.logger.debug "failed on end invocation request to extension: #{e}"
end

.send_start_invocation_request(event:) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/datadog/lambda/utils/extension.rb', line 43

def self.send_start_invocation_request(event:)
  return unless extension_running?

  response = Net::HTTP.post(START_INVOCATION_URI, event.to_json, request_headers)
  # Add origin, since tracer expects it for extraction
  response[Datadog::Trace::DD_ORIGIN] = 'lambda'

  PROPAGATOR.extract(response)
rescue StandardError => e
  Datadog::Utils.logger.debug "failed on start invocation request to extension: #{e}"
end