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'
START_INVOCATION_URI =
URI(EXTENSION_BASE_URL + START_INVOCATION_PATH).freeze
END_INVOCATION_URI =
URI(EXTENSION_BASE_URL + END_INVOCATION_PATH).freeze

Class Method Summary collapse

Class Method Details

.check_extension_runningObject



30
31
32
# File 'lib/datadog/lambda/utils/extension.rb', line 30

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

.extension_running?Boolean

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/datadog/lambda/utils/extension.rb', line 24

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

  @is_extension_running = check_extension_running
end

.loggerObject



16
17
18
# File 'lib/datadog/lambda/utils/logger.rb', line 16

def self.logger
  @logger ||= Logger.new(STDOUT)
end

.request_headersObject



63
64
65
66
67
68
69
# File 'lib/datadog/lambda/utils/extension.rb', line 63

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:) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/datadog/lambda/utils/extension.rb', line 47

def self.send_end_invocation_request(response:)
  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 = Datadog::Tracing.active_trace
  Tracing::Propagation::HTTP.inject!(trace, request)
  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



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/datadog/lambda/utils/extension.rb', line 34

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

  response = Net::HTTP.post(START_INVOCATION_URI, event.to_json, request_headers)

  trace_digest = Tracing::Propagation::HTTP.extract(response)
  # Only continue trace from a new one if it exist, or else,
  # it will create a new trace, which is not ideal here.
  Tracing.continue_trace!(trace_digest) if trace_digest
rescue StandardError => e
  Datadog::Utils.logger.debug "failed on start invocation request to extension: #{e}"
end

.update_log_levelObject



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

def self.update_log_level
  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