Class: Datadog::Metrics::Client

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

Overview

Client is a singleton class that instantiates a Datadog::Statsd client to send metrics to the Datadog Extension if present.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.instanceObject



25
26
27
# File 'lib/datadog/lambda/metrics.rb', line 25

def self.instance
  @instance ||= new
end

Instance Method Details

#closeObject



47
48
49
# File 'lib/datadog/lambda/metrics.rb', line 47

def close
  @statsd&.close
end

#distribution(name, value, time: nil, **tags) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/datadog/lambda/metrics.rb', line 29

def distribution(name, value, time: nil, **tags)
  tag_list = get_tags(**tags)

  if Datadog::Utils.extension_running?
    begin
      @statsd.distribution(name, value, tags: tag_list)
    rescue StandardError => e
      Datadog::Utils.logger.warning "error sending metric to the extension: #{e}"
    end
  else
    time ||= Time.now
    time_ms = time.to_i

    metric = { e: time_ms, m: name, t: tag_list, v: value }
    puts metric.to_json
  end
end

#get_tags(**tags) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/datadog/lambda/metrics.rb', line 51

def get_tags(**tags)
  tag_list = ["dd_lambda_layer:datadog-ruby#{Datadog::Lambda.dd_lambda_layer_tag}"]
  tags.each do |tag|
    tag_list << "#{tag[0]}:#{tag[1]}"
  end

  tag_list
end