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

rubocop:disable CyclomaticComplexity, PerceivedComplexity



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

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



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

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

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



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

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

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



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

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

#dog_optionsObject



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

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

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



143
144
145
# File 'lib/sapience/metrics/datadog.rb', line 143

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

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

Create Event

Examples:

Create an Event

Sapience.metrics.event('article-published', "article #123")

Create a namespaced Event with default namespacing

Sapience.metrics.event('article-published', "article #123", {namespaced_keys: [:title, :aggregation_key]})

Create a namespaced Event with custom namespacing

Sapience.metrics.event(
  'article-published',
  "article #123",
  {namespace_prefix: 'custom_namespace',  namespaced_keys: [:title, :aggregation_key]}
)

Create an Event with a custom aggregation_key

Sapience.metrics.event('article-published', "article #123", {aggregation_key: 'custom_aggregation_key')

Parameters:

  • title (String)

    the title of the event

  • text (String) (defaults to: "")

    the description

  • opts (Hash) (defaults to: {})

    event options

Options Hash (opts):

  • :namespaced_keys (Array)

    the keys we want to be namespaced. Valid: :title or :aggregation_key

  • :namespace_prefix (String)

    custom namespace (to override default from Sapience ‘app_name.environment`)

  • :aggregation_key (String)

    custom aggregation_key (to override default based on ‘title`, only applies when :aggregation_key includes in namespaced_keys option)



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

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



147
148
149
# File 'lib/sapience/metrics/datadog.rb', line 147

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

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



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

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

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



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

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

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



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

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

#namespaceObject



151
152
153
154
155
# File 'lib/sapience/metrics/datadog.rb', line 151

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

#providerObject



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

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

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



139
140
141
# File 'lib/sapience/metrics/datadog.rb', line 139

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

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



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

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

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



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

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)


45
46
47
# File 'lib/sapience/metrics/datadog.rb', line 45

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