Module: Datadog::Lambda

Defined in:
lib/datadog/lambda.rb,
lib/datadog/lambda/version.rb

Overview

Instruments AWS Lambda functions with Datadog distributed tracing and custom metrics

Defined Under Namespace

Modules: VERSION

Class Method Summary collapse

Class Method Details

.metric(name, value, time: nil, **tags) ⇒ Object

Send a custom distribution metric

Parameters:

  • name (String)

    name of the metric

  • value (Numeric)

    value of the metric

  • time (Time) (defaults to: nil)

    the time of the metric, should be in the past

  • tags (Hash)

    hash of tags, must be in “my.tag.name”:“value” format



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

def self.metric(name, value, time: nil, **tags)
  raise 'name must be a string' unless name.is_a?(String)
  raise 'value must be a number' unless value.is_a?(Numeric)

  time ||= Time.now
  tag_list = ['dd_lambda_layer:datadog-ruby25']
  tags.each do |tag|
    tag_list.push("#{tag[0]}:#{tag[1]}")
  end
  time_ms = time.to_f.to_i
  metric = { e: time_ms, m: name, t: tag_list, v: value }.to_json
  puts metric
end

.trace_contextObject

Gets the current tracing context



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

def self.trace_context
  Datadog::Trace.trace_context
end

.wrap(event, _context, &block) ⇒ Object

Wrap the body of a lambda invocation

Parameters:

  • event (Object)

    event sent to lambda

  • context (Object)

    lambda context

  • block (Proc)

    implementation of the handler function.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/datadog/lambda.rb', line 25

def self.wrap(event, _context, &block)
  Datadog::Utils.update_log_level

  @listener ||= Trace::Listener.new
  @listener.on_start(event: event)
  begin
    res = block.call
  ensure
    @listener.on_end
  end
  res
end