Class: Datadog::Core::Metrics::Client

Inherits:
Object
  • Object
show all
Extended by:
Helpers, Options
Includes:
Options
Defined in:
lib/datadog/core/metrics/client.rb

Overview

Acts as client for sending metrics (via Statsd) Wraps a Statsd client with default tags and additional configuration.

Constant Summary

Constants included from Options

Options::DEFAULT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Options

default_metric_options, metric_options

Constructor Details

#initialize(statsd: nil, enabled: true, **_) ⇒ Client

Returns a new instance of Client.



23
24
25
26
27
28
29
30
31
32
# File 'lib/datadog/core/metrics/client.rb', line 23

def initialize(statsd: nil, enabled: true, **_)
  @statsd =
    if supported?
      statsd || default_statsd_client
    else
      ignored_statsd_warning if statsd
      nil
    end
  @enabled = enabled
end

Instance Attribute Details

#statsdObject (readonly)

Returns the value of attribute statsd.



21
22
23
# File 'lib/datadog/core/metrics/client.rb', line 21

def statsd
  @statsd
end

Instance Method Details

#closeObject



164
165
166
# File 'lib/datadog/core/metrics/client.rb', line 164

def close
  @statsd.close if @statsd && @statsd.respond_to?(:close)
end

#configure(options = {}) ⇒ Object



81
82
83
84
# File 'lib/datadog/core/metrics/client.rb', line 81

def configure(options = {})
  @statsd = options[:statsd] if options.key?(:statsd)
  self.enabled = options[:enabled] if options.key?(:enabled)
end

#count(stat, value = nil, options = nil, &block) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/datadog/core/metrics/client.rb', line 90

def count(stat, value = nil, options = nil, &block)
  return unless send_stats? && statsd.respond_to?(:count)

  value, options = yield if block
  raise ArgumentError if value.nil?

  statsd.count(stat, value, metric_options(options))
rescue StandardError => e
  Datadog.logger.error(
    "Failed to send count stat. Cause: #{e.class.name} #{e.message} Source: #{Array(e.backtrace).first}"
  )
end

#default_hostnameObject



51
52
53
# File 'lib/datadog/core/metrics/client.rb', line 51

def default_hostname
  ENV.fetch(Configuration::Ext::Transport::ENV_DEFAULT_HOST, Ext::DEFAULT_HOST)
end

#default_portObject



55
56
57
# File 'lib/datadog/core/metrics/client.rb', line 55

def default_port
  ENV.fetch(Configuration::Ext::Metrics::ENV_DEFAULT_PORT, Ext::DEFAULT_PORT).to_i
end

#default_statsd_clientObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/datadog/core/metrics/client.rb', line 59

def default_statsd_client
  require 'datadog/statsd'

  # Create a StatsD client that points to the agent.
  #
  # We use `single_thread: true`, as dogstatsd-ruby >= 5.0 creates a background thread
  # by default, but does not handle forks correctly, causing resource leaks.
  #
  # Using dogstatsd-ruby >= 5.0 is still valuable, as it supports
  # transparent batch metric submission, which reduces submission
  # overhead.
  #
  # Versions < 5.0 are always single-threaded, but do not have the kwarg option.
  options = if dogstatsd_version >= Gem::Version.new('5.2')
              { single_thread: true }
            else
              {}
            end

  Datadog::Statsd.new(default_hostname, default_port, **options)
end

#distribution(stat, value = nil, options = nil, &block) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/datadog/core/metrics/client.rb', line 103

def distribution(stat, value = nil, options = nil, &block)
  return unless send_stats? && statsd.respond_to?(:distribution)

  value, options = yield if block
  raise ArgumentError if value.nil?

  statsd.distribution(stat, value, metric_options(options))
rescue StandardError => e
  Datadog.logger.error(
    "Failed to send distribution stat. Cause: #{e.class.name} #{e.message} Source: #{Array(e.backtrace).first}"
  )
end

#enabled=(enabled) ⇒ Object



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

def enabled=(enabled)
  @enabled = (enabled == true)
end

#enabled?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/datadog/core/metrics/client.rb', line 43

def enabled?
  @enabled
end

#gauge(stat, value = nil, options = nil, &block) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/datadog/core/metrics/client.rb', line 128

def gauge(stat, value = nil, options = nil, &block)
  return unless send_stats? && statsd.respond_to?(:gauge)

  value, options = yield if block
  raise ArgumentError if value.nil?

  statsd.gauge(stat, value, metric_options(options))
rescue StandardError => e
  Datadog.logger.error(
    "Failed to send gauge stat. Cause: #{e.class.name} #{e.message} Source: #{Array(e.backtrace).first}"
  )
end

#increment(stat, options = nil) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/datadog/core/metrics/client.rb', line 116

def increment(stat, options = nil)
  return unless send_stats? && statsd.respond_to?(:increment)

  options = yield if block_given?

  statsd.increment(stat, metric_options(options))
rescue StandardError => e
  Datadog.logger.error(
    "Failed to send increment stat. Cause: #{e.class.name} #{e.message} Source: #{Array(e.backtrace).first}"
  )
end

#send_metrics(metrics) ⇒ Object



160
161
162
# File 'lib/datadog/core/metrics/client.rb', line 160

def send_metrics(metrics)
  metrics.each { |m| send(m.type, *[m.name, m.value, m.options].compact) }
end

#send_stats?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/datadog/core/metrics/client.rb', line 86

def send_stats?
  enabled? && !statsd.nil?
end

#supported?Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
# File 'lib/datadog/core/metrics/client.rb', line 34

def supported?
  version = dogstatsd_version

  !version.nil? && version >= Gem::Version.new('3.3.0') &&
    # dogstatsd-ruby >= 5.0 & < 5.2.0 has known issues with process forks
    # and do not support the single thread mode we use to avoid this problem.
    !(version >= Gem::Version.new('5.0') && version < Gem::Version.new('5.3'))
end

#time(stat, options = nil) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/datadog/core/metrics/client.rb', line 141

def time(stat, options = nil)
  return yield unless send_stats?

  # Calculate time, send it as a distribution.
  start = Utils::Time.get_time
  yield
ensure
  begin
    if send_stats? && !start.nil?
      finished = Utils::Time.get_time
      distribution(stat, ((finished - start) * 1000), options)
    end
  rescue StandardError => e
    Datadog.logger.error(
      "Failed to send time stat. Cause: #{e.class.name} #{e.message} Source: #{Array(e.backtrace).first}"
    )
  end
end