Class: Datadog::Metrics
Overview
Acts as client for sending metrics (via Statsd) Wraps a Statsd client with default tags and additional configuration.
Defined Under Namespace
Modules: Helpers, Logging, Options
Classes: Metric
Constant Summary
Constants included
from Options
Options::DEFAULT
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#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(options = {}) ⇒ Metrics
constructor
A new instance of Metrics.
-
#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(options = {}) ⇒ Metrics
Returns a new instance of Metrics.
15
16
17
18
|
# File 'lib/ddtrace/metrics.rb', line 15
def initialize(options = {})
@statsd = options.fetch(:statsd) { default_statsd_client if supported? }
@enabled = options.fetch(:enabled, true)
end
|
Instance Attribute Details
#statsd ⇒ Object
Returns the value of attribute statsd.
13
14
15
|
# File 'lib/ddtrace/metrics.rb', line 13
def statsd
@statsd
end
|
Instance Method Details
50
51
52
53
|
# File 'lib/ddtrace/metrics.rb', line 50
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
59
60
61
62
63
64
65
66
67
|
# File 'lib/ddtrace/metrics.rb', line 59
def count(stat, value = nil, options = nil, &block)
return unless send_stats? && statsd.respond_to?(:count)
value, options = yield if block_given?
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.message} Source: #{e.backtrace.first}")
end
|
#default_hostname ⇒ Object
35
36
37
|
# File 'lib/ddtrace/metrics.rb', line 35
def default_hostname
ENV.fetch(Datadog::Ext::Metrics::ENV_DEFAULT_HOST, Datadog::Ext::Metrics::DEFAULT_HOST)
end
|
#default_port ⇒ Object
39
40
41
|
# File 'lib/ddtrace/metrics.rb', line 39
def default_port
ENV.fetch(Datadog::Ext::Metrics::ENV_DEFAULT_PORT, Datadog::Ext::Metrics::DEFAULT_PORT).to_i
end
|
#default_statsd_client ⇒ Object
43
44
45
46
47
48
|
# File 'lib/ddtrace/metrics.rb', line 43
def default_statsd_client
require 'datadog/statsd' unless defined?(::Datadog::Statsd)
Datadog::Statsd.new(default_hostname, default_port)
end
|
#distribution(stat, value = nil, options = nil, &block) ⇒ Object
69
70
71
72
73
74
75
76
77
|
# File 'lib/ddtrace/metrics.rb', line 69
def distribution(stat, value = nil, options = nil, &block)
return unless send_stats? && statsd.respond_to?(:distribution)
value, options = yield if block_given?
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.message} Source: #{e.backtrace.first}")
end
|
#enabled=(enabled) ⇒ Object
31
32
33
|
# File 'lib/ddtrace/metrics.rb', line 31
def enabled=(enabled)
@enabled = (enabled == true)
end
|
#enabled? ⇒ Boolean
27
28
29
|
# File 'lib/ddtrace/metrics.rb', line 27
def enabled?
@enabled
end
|
#gauge(stat, value = nil, options = nil, &block) ⇒ Object
88
89
90
91
92
93
94
95
96
|
# File 'lib/ddtrace/metrics.rb', line 88
def gauge(stat, value = nil, options = nil, &block)
return unless send_stats? && statsd.respond_to?(:gauge)
value, options = yield if block_given?
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.message} Source: #{e.backtrace.first}")
end
|
#increment(stat, options = nil) ⇒ Object
79
80
81
82
83
84
85
86
|
# File 'lib/ddtrace/metrics.rb', line 79
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.message} Source: #{e.backtrace.first}")
end
|
#send_metrics(metrics) ⇒ Object
115
116
117
|
# File 'lib/ddtrace/metrics.rb', line 115
def send_metrics(metrics)
metrics.each { |m| send(m.type, *[m.name, m.value, m.options].compact) }
end
|
#send_stats? ⇒ Boolean
55
56
57
|
# File 'lib/ddtrace/metrics.rb', line 55
def send_stats?
enabled? && !statsd.nil?
end
|
#supported? ⇒ Boolean
20
21
22
23
24
25
|
# File 'lib/ddtrace/metrics.rb', line 20
def supported?
version = Gem.loaded_specs['dogstatsd-ruby'] \
&& Gem.loaded_specs['dogstatsd-ruby'].version
!version.nil? && (version >= Gem::Version.new('3.3.0'))
end
|
#time(stat, options = nil) ⇒ Object
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/ddtrace/metrics.rb', line 98
def time(stat, options = nil)
return yield unless send_stats?
start = Utils::Time.get_time
return 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.message} Source: #{e.backtrace.first}")
end
end
|