Class: Sapience::Appender::Datadog

Inherits:
Subscriber show all
Defined in:
lib/sapience/appender/datadog.rb

Instance Attribute Summary

Attributes inherited from Subscriber

#application, #formatter, #host

Attributes inherited from Base

#filter, #name

Instance Method Summary collapse

Methods inherited from Subscriber

#close, #default_formatter, #flush, #level

Methods inherited from Base

#fast_tag, #level, #level=, #measure, #payload, #pop_tags, #push_tags, #silence, #tagged, #tags, #with_payload

Constructor Details

#initialize(options = {}, &block) ⇒ Datadog

Create Appender

Parameters:

level: :trace
url: [String]
  Valid URL to post to.
  Example:
    udp://localhost:8125
  Example, send all metrics to a particular namespace:
    udp://localhost:8125/namespace
  Default: udp://localhost:8125
tags: [String]
  Example:
    tag1:true

rubocop:disable AbcSize, CyclomaticComplexity, PerceivedComplexity



30
31
32
33
34
35
36
37
38
# File 'lib/sapience/appender/datadog.rb', line 30

def initialize(options = {}, &block)
  fail("Options should be a Hash") unless options.is_a?(Hash)
  url   = options.delete(:url) || "udp://localhost:8125"
  @tags = options.delete(:tags)
  @uri  = URI.parse(url)
  fail('Statsd only supports udp. Example: "udp://localhost:8125"') if @uri.scheme != "udp"

  super(options, &block)
end

Instance Method Details

#batch {|provider| ... } ⇒ Object

Yields:



105
106
107
# File 'lib/sapience/appender/datadog.rb', line 105

def batch
  yield provider
end

#count(metric, amount, hash = {}) ⇒ Object



97
98
99
# File 'lib/sapience/appender/datadog.rb', line 97

def count(metric, amount, hash = {})
  provider.count(metric, amount, hash)
end

#decrement(metric, amount = 1) ⇒ Object



83
84
85
86
87
# File 'lib/sapience/appender/datadog.rb', line 83

def decrement(metric, amount = 1)
  provider.batch do
    amount.times { provider.decrement(metric) }
  end
end

#event(title, text, options = {}) ⇒ Object



109
110
111
# File 'lib/sapience/appender/datadog.rb', line 109

def event(title, text, options = {})
  provider.event(title, text, options)
end

#gauge(metric, amount, hash = {}) ⇒ Object



93
94
95
# File 'lib/sapience/appender/datadog.rb', line 93

def gauge(metric, amount, hash = {})
  provider.gauge(metric, amount, hash)
end

#histogram(metric, amount) ⇒ Object



89
90
91
# File 'lib/sapience/appender/datadog.rb', line 89

def histogram(metric, amount)
  provider.histogram(metric, amount)
end

#increment(metric, amount = 1) ⇒ Object



77
78
79
80
81
# File 'lib/sapience/appender/datadog.rb', line 77

def increment(metric, amount = 1)
  provider.batch do
    amount.times { provider.increment(metric) }
  end
end

#log(log) ⇒ Object

Send an error notification to sentry



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sapience/appender/datadog.rb', line 50

def log(log)
  metric = log.metric
  return false unless metric

  if log.duration
    timing(metric, log.duration)
  else
    amount = (log.metric_amount || 1).round
    if amount < 0
      decrement(metric, amount.abs)
    else
      increment(metric, amount)
    end
  end
  true
end

#providerObject



40
41
42
43
44
45
46
47
# File 'lib/sapience/appender/datadog.rb', line 40

def provider
  @_provider ||= begin
    statsd           = ::Statsd.new(@uri.host, @uri.port, tags: @tags)
    path             = @uri.path.chomp("/")
    statsd.namespace = path.sub("/", "") if path != ""
    statsd
  end
end

#time(metric, &block) ⇒ Object



101
102
103
# File 'lib/sapience/appender/datadog.rb', line 101

def time(metric, &block)
  provider.time(metric, &block)
end

#timing(metric, duration = 0) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/sapience/appender/datadog.rb', line 67

def timing(metric, duration = 0)
  if block_given?
    start = Time.now
    yield
    provider.timing(metric, ((Time.now - start) * 1000).floor)
  else
    provider.timing(metric, duration)
  end
end