Class: Sapience::Appender::Datadog

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

Constant Summary collapse

VALIDATION_MESSAGE =
"Statsd only supports udp. Example: 'udp://localhost:8125'".freeze

Instance Attribute Summary

Attributes inherited from Subscriber

#app_name, #formatter, #host

Attributes inherited from Base

#filter, #name

Instance Method Summary collapse

Methods inherited from Subscriber

#close, #default_formatter, #flush, #level, #name, #to_s

Methods included from Descendants

#descendants

Methods inherited from Base

#app_name, #default_formatter, #fast_tag, #host, #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



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

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)
  super(options, &block)
end

Instance Method Details

#batch(&block) ⇒ Object



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

def batch(&block)
  provider.batch(&block)
end

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



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

def count(metric, amount, options = {})
  return false unless valid?
  provider.count(metric, amount, options)
end

#decrement(metric, options = {}) ⇒ Object



80
81
82
83
# File 'lib/sapience/appender/datadog.rb', line 80

def decrement(metric, options = {})
  return false unless valid?
  provider.decrement(metric, options)
end

#dog_optionsObject



120
121
122
123
124
125
# File 'lib/sapience/appender/datadog.rb', line 120

def dog_options
  {
    namespace: namespace,
    tags: @tags,
  }
end

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



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

def event(title, text, options = {})
  return false unless valid?
  provider.event(title, text, options)
end

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



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

def gauge(metric, amount, options = {})
  return false unless valid?
  provider.gauge(metric, amount, options)
end

#histogram(metric, amount, options = {}) ⇒ Object



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

def histogram(metric, amount, options = {})
  return false unless valid?
  provider.histogram(metric, amount, options)
end

#increment(metric, options = {}) ⇒ Object



75
76
77
78
# File 'lib/sapience/appender/datadog.rb', line 75

def increment(metric, options = {})
  return false unless valid?
  provider.increment(metric, options)
end

#log(log) ⇒ Object

Send an error notification to sentry



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

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

  if log.duration
    timing(metric, log.duration, tags: log.tags)
  else
    amount = (log.metric_amount || 1).round
    count(metric, amount, tags: log.tags)
  end
  true
end

#namespaceObject



114
115
116
117
118
# File 'lib/sapience/appender/datadog.rb', line 114

def namespace
  ns = Sapience.namify(app_name)
  ns << ".#{Sapience.namify(Sapience.environment)}" if Sapience.environment
  ns
end

#providerObject



40
41
42
# File 'lib/sapience/appender/datadog.rb', line 40

def provider
  @_provider ||= ::Datadog::Statsd.new(@uri.host, @uri.port, dog_options)
end

#time(metric, options = {}, &block) ⇒ Object



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

def time(metric, options = {}, &block)
  return false unless valid?
  provider.time(metric, options, &block)
end

#timing(metric, duration = 0, options = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/sapience/appender/datadog.rb', line 63

def timing(metric, duration = 0, options = {})
  if block_given?
    start = Time.now
    yield
    return false unless valid?
    provider.timing(metric, ((Time.now - start) * 1000).floor, options)
  else
    return false unless valid?
    provider.timing(metric, duration, options)
  end
end

#valid?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/sapience/appender/datadog.rb', line 44

def valid?
  @uri.scheme == "udp"
end