Class: DatadogMetrics

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog_metrics.rb,
lib/datadog_metrics/version.rb

Overview

The default endpoint is the local dogstatsd agent which we can talk to using the ruby client:

github.com/DataDog/dogstatsd-ruby

In development we will pass an object that just logs what it would have reported to DataDog to the local log file, and in test we pass an object we can test with.

Constant Summary collapse

VERSION =
"1.1.2"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint = Datadog::Statsd.new(ENV.fetch("STATS_HOST", "localhost"), 8125)) ⇒ DatadogMetrics

Returns a new instance of DatadogMetrics.



27
28
29
# File 'lib/datadog_metrics.rb', line 27

def initialize(endpoint = Datadog::Statsd.new(ENV.fetch("STATS_HOST", "localhost"), 8125))
  @endpoint = endpoint
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



25
26
27
# File 'lib/datadog_metrics.rb', line 25

def endpoint
  @endpoint
end

Class Method Details

.default_tagsObject



20
21
22
# File 'lib/datadog_metrics.rb', line 20

def default_tags
  @default_tags ||= {}
end

.default_tags=(tags) ⇒ Object



16
17
18
# File 'lib/datadog_metrics.rb', line 16

def default_tags=(tags)
  @default_tags = tags.transform_keys(&:to_s).freeze
end

Instance Method Details

#batchObject



84
85
86
# File 'lib/datadog_metrics.rb', line 84

def batch
  @endpoint.batch(&proc { yield(endpoint)})
end

#count(metric, count, tags: {}) ⇒ Object



51
52
53
# File 'lib/datadog_metrics.rb', line 51

def count(metric, count, tags: {})
  @endpoint.count(metric, count, tags: format_tags(tags))
end

#count_with_tags(metric, count, tags: {}) ⇒ Object



63
64
65
# File 'lib/datadog_metrics.rb', line 63

def count_with_tags(metric, count, tags: {})
  @endpoint.count(metric, count, tags: format_tags(tags))
end

#decrement(metric, tags: {}) ⇒ Object



47
48
49
# File 'lib/datadog_metrics.rb', line 47

def decrement(metric, tags: {})
  @endpoint.decrement(metric, tags: format_tags(tags))
end

#event(metric, value, tags: {}) ⇒ Object



80
81
82
# File 'lib/datadog_metrics.rb', line 80

def event(metric, value, tags: {})
  @endpoint.event(metric, value, tags: format_tags(tags))
end

#gauge(metric, value, tags = [], sample_rate = 1, **opts) ⇒ Object



67
68
69
70
# File 'lib/datadog_metrics.rb', line 67

def gauge(metric, value, tags=[], sample_rate=1, **opts)
  tags = opts[:tags] if opts.key?(:tags)
  @endpoint.gauge(metric, value, tags: format_tags(tags), sample_rate: sample_rate)
end

#histogram(metric, value, tags: {}) ⇒ Object



72
73
74
# File 'lib/datadog_metrics.rb', line 72

def histogram(metric, value, tags: {})
  @endpoint.histogram(metric, value, tags: format_tags(tags))
end

#histogram_with_tags(metric, value, tags) ⇒ Object



76
77
78
# File 'lib/datadog_metrics.rb', line 76

def histogram_with_tags(metric, value, tags)
  @endpoint.histogram(metric, value, tags: format_tags(tags))
end

#increment(metric, tags: {}) ⇒ Object



43
44
45
# File 'lib/datadog_metrics.rb', line 43

def increment(metric, tags: {})
  @endpoint.increment(metric, tags: format_tags(tags))
end

#increment_with_tags(metric, tags) ⇒ Object



59
60
61
# File 'lib/datadog_metrics.rb', line 59

def increment_with_tags(metric, tags)
  @endpoint.increment(metric, tags: format_tags(tags))
end

#time(stat, opt = {}, &block) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/datadog_metrics.rb', line 31

def time(stat, opt={}, &block)
  opt[:tags] = format_tags(opt[:tags]) if opt.key?(:tags)

  @endpoint.time(stat, opt) do
    yield
  end
end

#timing(metric, ms, tags: {}) ⇒ Object



39
40
41
# File 'lib/datadog_metrics.rb', line 39

def timing(metric, ms, tags: {})
  @endpoint.timing(metric, ms, tags: format_tags(tags))
end

#timing_with_tags(metric, ms, tags) ⇒ Object



55
56
57
# File 'lib/datadog_metrics.rb', line 55

def timing_with_tags(metric, ms, tags)
  @endpoint.timing(metric, ms, tags: format_tags(tags))
end