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: Options
Constant Summary
collapse
- DEFAULT_AGENT_HOST =
'127.0.0.1'.freeze
- DEFAULT_METRIC_AGENT_PORT =
'8125'.freeze
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(options = {}) ⇒ Metrics
Returns a new instance of Metrics.
16
17
18
19
|
# File 'lib/ddtrace/metrics.rb', line 16
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.
14
15
16
|
# File 'lib/ddtrace/metrics.rb', line 14
def statsd
@statsd
end
|
Instance Method Details
44
45
46
47
|
# File 'lib/ddtrace/metrics.rb', line 44
def configure(options = {})
@statsd = options[:statsd] if options.key?(:statsd)
@enabled = options[:enabled] if options.key?(:enabled)
end
|
#default_statsd_client ⇒ Object
34
35
36
37
38
39
40
41
42
|
# File 'lib/ddtrace/metrics.rb', line 34
def default_statsd_client
require 'datadog/statsd' unless defined?(::Datadog::Statsd)
Datadog::Statsd.new(
ENV.fetch('DD_AGENT_HOST', DEFAULT_AGENT_HOST),
ENV.fetch('DD_METRIC_AGENT_PORT', DEFAULT_METRIC_AGENT_PORT)
)
end
|
#distribution(stat, value, options = nil) ⇒ Object
53
54
55
56
57
58
|
# File 'lib/ddtrace/metrics.rb', line 53
def distribution(stat, value, options = nil)
return unless send_stats? && statsd.respond_to?(:distribution)
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, options = nil) ⇒ Object
67
68
69
70
71
72
|
# File 'lib/ddtrace/metrics.rb', line 67
def gauge(stat, value, options = nil)
return unless send_stats? && statsd.respond_to?(:gauge)
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
60
61
62
63
64
65
|
# File 'lib/ddtrace/metrics.rb', line 60
def increment(stat, options = nil)
return unless send_stats? && statsd.respond_to?(:increment)
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_stats? ⇒ Boolean
49
50
51
|
# File 'lib/ddtrace/metrics.rb', line 49
def send_stats?
enabled? && !statsd.nil?
end
|
#supported? ⇒ Boolean
21
22
23
24
|
# File 'lib/ddtrace/metrics.rb', line 21
def supported?
Gem.loaded_specs['dogstatsd-ruby'] \
&& Gem.loaded_specs['dogstatsd-ruby'].version >= Gem::Version.new('3.3.0')
end
|
#time(stat, options = nil) ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/ddtrace/metrics.rb', line 74
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
|