Class: Deimos::Metrics::Datadog

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

Overview

A Metrics wrapper class for Datadog.

Instance Method Summary collapse

Constructor Details

#initialize(config, logger) ⇒ Datadog

Returns a new instance of Datadog.

Parameters:

  • config (Hash)

    a hash of both client and Karakfa MetricsListener configs.

  • logger (Logger)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/deimos/metrics/datadog.rb', line 14

def initialize(config, logger)
  raise 'Metrics config must specify host_ip' if config[:host_ip].nil?
  raise 'Metrics config must specify host_port' if config[:host_port].nil?
  raise 'Metrics config must specify namespace' if config[:namespace].nil?

  logger.info("DatadogMetricsProvider configured with: #{config}")

  @client = ::Datadog::Statsd.new(
    config[:host_ip],
    config[:host_port],
    tags: config[:tags],
    namespace: config[:namespace]
  )
  setup_karafka(config)
  setup_waterdrop(config)
end

Instance Method Details

#gauge(metric_name, count, options = {}) ⇒ Object

:nodoc:



70
71
72
# File 'lib/deimos/metrics/datadog.rb', line 70

def gauge(metric_name, count, options={})
  @client.gauge(metric_name, count, options)
end

#histogram(metric_name, count, options = {}) ⇒ Object

:nodoc:



75
76
77
# File 'lib/deimos/metrics/datadog.rb', line 75

def histogram(metric_name, count, options={})
  @client.histogram(metric_name, count, options)
end

#increment(metric_name, options = {}) ⇒ Object

:nodoc:



65
66
67
# File 'lib/deimos/metrics/datadog.rb', line 65

def increment(metric_name, options={})
  @client.increment(metric_name, options)
end

#setup_karafka(config = {}) ⇒ Object



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

def setup_karafka(config={})
  karafka_listener = ::Karafka::Instrumentation::Vendors::Datadog::MetricsListener.new do |karafka_config|
    karafka_config.client = @client
    if config[:karafka_namespace]
      karafka_config.namespace = config[:karafka_namespace]
    end
    if config[:karafka_distribution_mode]
      karafka_config.distribution_mode = config[:karafka_distribution_mode]
    end
    if config[:rd_kafka_metrics]
      karafka_config.rd_kafka_metrics = config[:rd_kafka_metrics]
    end
  end
  Karafka.monitor.subscribe(karafka_listener)
end

#setup_waterdrop(config) ⇒ Object



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

def setup_waterdrop(config)
  waterdrop_listener = ::WaterDrop::Instrumentation::Vendors::Datadog::MetricsListener.new do |waterdrop_config|
    waterdrop_config.client = @client
    if config[:karafka_namespace]
      waterdrop_config.namespace = config[:karafka_namespace]
    end
    if config[:karafka_distribution_mode]
      waterdrop_config.distribution_mode = config[:karafka_distribution_mode]
    end
    if config[:rd_kafka_metrics]
      karafka_config.rd_kafka_metrics = [] # handled in Karafka
    end
  end
  Karafka::Setup::Config.setup if Karafka.producer.nil?
  Karafka.producer.monitor.subscribe(waterdrop_listener)
end

#time(metric_name, options = {}) ⇒ Object

:nodoc:



80
81
82
83
84
# File 'lib/deimos/metrics/datadog.rb', line 80

def time(metric_name, options={})
  @client.time(metric_name, options) do
    yield
  end
end