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.
14
15
16
17
|
# File 'lib/ddtrace/metrics.rb', line 14
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.
12
13
14
|
# File 'lib/ddtrace/metrics.rb', line 12
def statsd
@statsd
end
|
Instance Method Details
49
50
51
52
|
# File 'lib/ddtrace/metrics.rb', line 49
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
58
59
60
61
62
63
64
65
66
|
# File 'lib/ddtrace/metrics.rb', line 58
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::Tracer.log.error("Failed to send count stat. Cause: #{e.message} Source: #{e.backtrace.first}")
end
|
#default_hostname ⇒ Object
#default_statsd_client ⇒ Object
42
43
44
45
46
47
|
# File 'lib/ddtrace/metrics.rb', line 42
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
68
69
70
71
72
73
74
75
76
|
# File 'lib/ddtrace/metrics.rb', line 68
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::Tracer.log.error("Failed to send distribution stat. Cause: #{e.message} Source: #{e.backtrace.first}")
end
|
#enabled=(enabled) ⇒ Object
30
31
32
|
# File 'lib/ddtrace/metrics.rb', line 30
def enabled=(enabled)
@enabled = (enabled == true)
end
|
#enabled? ⇒ Boolean
26
27
28
|
# File 'lib/ddtrace/metrics.rb', line 26
def enabled?
@enabled
end
|
#gauge(stat, value = nil, options = nil, &block) ⇒ Object
87
88
89
90
91
92
93
94
95
|
# File 'lib/ddtrace/metrics.rb', line 87
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::Tracer.log.error("Failed to send gauge stat. Cause: #{e.message} Source: #{e.backtrace.first}")
end
|
#increment(stat, options = nil) ⇒ Object
78
79
80
81
82
83
84
85
|
# File 'lib/ddtrace/metrics.rb', line 78
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::Tracer.log.error("Failed to send increment stat. Cause: #{e.message} Source: #{e.backtrace.first}")
end
|
#send_metrics(metrics) ⇒ Object
114
115
116
|
# File 'lib/ddtrace/metrics.rb', line 114
def send_metrics(metrics)
metrics.each { |m| send(m.type, *[m.name, m.value, m.options].compact) }
end
|
#send_stats? ⇒ Boolean
54
55
56
|
# File 'lib/ddtrace/metrics.rb', line 54
def send_stats?
enabled? && !statsd.nil?
end
|
#supported? ⇒ Boolean
19
20
21
22
23
24
|
# File 'lib/ddtrace/metrics.rb', line 19
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/ddtrace/metrics.rb', line 97
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::Tracer.log.error("Failed to send time stat. Cause: #{e.message} Source: #{e.backtrace.first}")
end
end
|