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
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



36
37
38
# File 'lib/datadog/lambda/utils/extension.rb', line 36

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

.extension_running?Boolean

Returns:

  • (Boolean)


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

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



69
70
71
72
73
74
75
# File 'lib/datadog/lambda/utils/extension.rb', line 69

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/datadog/lambda/utils/extension.rb', line 52

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_digest = Datadog::Tracing.active_trace&.to_digest

  PROPAGATOR.inject!(trace_digest, 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



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/datadog/lambda/utils/extension.rb', line 40

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

.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