Class: Sapience::Metrics::Datadog

Inherits:
Sapience::Metrics show all
Defined in:
lib/sapience/metrics/datadog.rb

Constant Summary collapse

VALIDATION_MESSAGE =
"Statsd only supports udp. Example: '#{Sapience::DEFAULT_STATSD_URL}'"

Instance Method Summary collapse

Methods included from Descendants

#descendants

Constructor Details

#initialize(opts = {}) ⇒ Datadog

Create Appender

Parameters:

level: :trace
url: [String]
  Valid URL to postdogstatsd-ruby 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


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

def initialize(opts = {})
  options = opts.dup
  fail("Options should be a Hash") unless options.is_a?(Hash)
  url   = options.delete(:url) || Sapience::DEFAULT_STATSD_URL
  @tags = options.delete(:tags)
  @uri  = URI.parse(url)
end

Instance Method Details

#batch(&block) ⇒ Object



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

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

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



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

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

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



65
66
67
68
# File 'lib/sapience/metrics/datadog.rb', line 65

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

#dog_optionsObject



159
160
161
162
163
164
# File 'lib/sapience/metrics/datadog.rb', line 159

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

#error(module_name, action, opts = {}) ⇒ Object



145
146
147
# File 'lib/sapience/metrics/datadog.rb', line 145

def error(module_name, action, opts = {})
  increment("error", add_tags(module_name, action, opts))
end

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

rubocop:disable CyclomaticComplexity, PerceivedComplexity



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/sapience/metrics/datadog.rb', line 123

def event(title, text = "", opts = {})
  return false unless valid?
  fail ArgumentError "Title must be provided" unless title
  opts ||= {}

  namespaced_keys = opts.delete(:namespaced_keys) || []
  namespace_prefix = opts.delete(:namespace_prefix) || namespace

  if namespaced_keys.include?(:aggregation_key)
    aggregation_key = opts[:aggregation_key] || title
    opts[:aggregation_key] = "#{namespace_prefix}.#{aggregation_key}"
  end

  title = "#{namespace_prefix}.#{title}" if namespaced_keys.include?(:title)
  provider.event(title, text, opts)
end

#exception(module_name, action, opts = {}) ⇒ Object



149
150
151
# File 'lib/sapience/metrics/datadog.rb', line 149

def exception(module_name, action, opts = {})
  increment("exception", add_tags(module_name, action, opts))
end

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



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

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

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



70
71
72
73
# File 'lib/sapience/metrics/datadog.rb', line 70

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

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



60
61
62
63
# File 'lib/sapience/metrics/datadog.rb', line 60

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

#namespaceObject



153
154
155
156
157
# File 'lib/sapience/metrics/datadog.rb', line 153

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

#providerObject



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

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

#success(module_name, action, opts = {}) ⇒ Object

rubocop:enable CyclomaticComplexity, PerceivedComplexity



141
142
143
# File 'lib/sapience/metrics/datadog.rb', line 141

def success(module_name, action, opts = {})
  increment("success", add_tags(module_name, action, opts))
end

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



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

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

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



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sapience/metrics/datadog.rb', line 48

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/metrics/datadog.rb', line 44

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