Class: Datadog::Core::Metrics::Client
- Inherits:
-
Object
- Object
- Datadog::Core::Metrics::Client
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
-
#close ⇒ Object
-
#configure(options = {}) ⇒ Object
-
#count(stat, value = nil, options = nil, &block) ⇒ Object
-
#default_hostname ⇒ Object
-
#default_port ⇒ Object
-
#default_statsd_client ⇒ Object
-
#distribution(stat, value = nil, options = nil, &block) ⇒ Object
-
#enabled=(enabled) ⇒ Object
-
#enabled? ⇒ Boolean
-
#gauge(stat, value = nil, options = nil, &block) ⇒ Object
-
#increment(stat, options = nil) ⇒ Object
-
#initialize(statsd: nil, enabled: true, **_) ⇒ Client
constructor
A new instance of Client.
-
#send_metrics(metrics) ⇒ Object
-
#send_stats? ⇒ Boolean
-
#supported? ⇒ Boolean
-
#time(stat, options = nil) ⇒ Object
Methods included from Options
default_metric_options, metric_options
Constructor Details
#initialize(statsd: nil, enabled: true, **_) ⇒ Client
Returns a new instance of Client.
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/datadog/core/metrics/client.rb', line 26
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
#statsd ⇒ Object
Returns the value of attribute statsd.
24
25
26
|
# File 'lib/datadog/core/metrics/client.rb', line 24
def statsd
@statsd
end
|
Instance Method Details
#close ⇒ Object
173
174
175
|
# File 'lib/datadog/core/metrics/client.rb', line 173
def close
@statsd.close if @statsd && @statsd.respond_to?(:close)
end
|
84
85
86
87
|
# File 'lib/datadog/core/metrics/client.rb', line 84
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
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/datadog/core/metrics/client.rb', line 93
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}"
)
Telemetry::Logger.report(e, description: 'Failed to send count stat')
end
|
#default_hostname ⇒ Object
#default_statsd_client ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/datadog/core/metrics/client.rb', line 62
def default_statsd_client
require 'datadog/statsd'
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
107
108
109
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/datadog/core/metrics/client.rb', line 107
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}"
)
Telemetry::Logger.report(e, description: 'Failed to send distribution stat')
end
|
#enabled=(enabled) ⇒ Object
50
51
52
|
# File 'lib/datadog/core/metrics/client.rb', line 50
def enabled=(enabled)
@enabled = (enabled == true)
end
|
#enabled? ⇒ Boolean
46
47
48
|
# File 'lib/datadog/core/metrics/client.rb', line 46
def enabled?
@enabled
end
|
#gauge(stat, value = nil, options = nil, &block) ⇒ Object
134
135
136
137
138
139
140
141
142
143
144
145
146
|
# File 'lib/datadog/core/metrics/client.rb', line 134
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}"
)
Telemetry::Logger.report(e, description: 'Failed to send gauge stat')
end
|
#increment(stat, options = nil) ⇒ Object
121
122
123
124
125
126
127
128
129
130
131
132
|
# File 'lib/datadog/core/metrics/client.rb', line 121
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}"
)
Telemetry::Logger.report(e, description: 'Failed to send increment stat')
end
|
#send_metrics(metrics) ⇒ Object
169
170
171
|
# File 'lib/datadog/core/metrics/client.rb', line 169
def send_metrics(metrics)
metrics.each { |m| send(m.type, *[m.name, m.value, m.options].compact) }
end
|
#send_stats? ⇒ Boolean
89
90
91
|
# File 'lib/datadog/core/metrics/client.rb', line 89
def send_stats?
enabled? && !statsd.nil?
end
|
#supported? ⇒ Boolean
37
38
39
40
41
42
43
44
|
# File 'lib/datadog/core/metrics/client.rb', line 37
def supported?
version = dogstatsd_version
!version.nil? && version >= Gem::Version.new('3.3.0') &&
!(version >= Gem::Version.new('5.0') && version < Gem::Version.new('5.3'))
end
|
#time(stat, options = nil) ⇒ Object
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
# File 'lib/datadog/core/metrics/client.rb', line 148
def time(stat, options = nil)
return yield unless send_stats?
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}"
)
Telemetry::Logger.report(e, description: 'Failed to send time stat')
end
end
|