class Sapience::Metrics::Datadog

Constants

VALIDATION_MESSAGE

Public Class Methods

new(opts = {}) click to toggle source

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
# 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

Public Instance Methods

batch(&block) click to toggle source
# File lib/sapience/metrics/datadog.rb, line 90
def batch(&block)
  provider.batch(&block)
end
count(metric, amount, options = {}) click to toggle source
# 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 = {}) click to toggle source
# File lib/sapience/metrics/datadog.rb, line 65
def decrement(metric, options = {})
  return false unless valid?
  provider.decrement(metric, options)
end
dog_options() click to toggle source
# File lib/sapience/metrics/datadog.rb, line 159
def dog_options
  {
    namespace: namespace,
    tags: @tags,
  }
end
error(module_name, action, opts = {}) click to toggle source
# 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 = {}) click to toggle source

rubocop:disable CyclomaticComplexity, PerceivedComplexity

# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# File lib/sapience/metrics/datadog.rb, line 60
def increment(metric, options = {})
  return false unless valid?
  provider.increment(metric, options)
end
namespace() click to toggle source
# 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
provider() click to toggle source
# 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 = {}) click to toggle source

rubocop:enable CyclomaticComplexity, PerceivedComplexity

# 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) click to toggle source
# 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 = {}) { || ... } click to toggle source
# 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?() click to toggle source
# File lib/sapience/metrics/datadog.rb, line 44
def valid?
  @uri.scheme == "udp"
end

Private Instance Methods

add_tags(module_name, action, opts) click to toggle source
# File lib/sapience/metrics/datadog.rb, line 168
def add_tags(module_name, action, opts)
  tags = opts.fetch(:tags, [])
  clean_up_tags(tags, :module, module_name)
  clean_up_tags(tags, :action, action)
  tags << "module:#{module_name}" << "action:#{action}"
  opts[:tags] = tags
  opts
end
clean_up_tags(tags, key, value) click to toggle source
# File lib/sapience/metrics/datadog.rb, line 177
def clean_up_tags(tags, key, value)
  old_tags = tags.dup
  tags.delete_if { |a| a =~ /^#{key}:/ }
  ::Sapience.logger.warn("WARNING: tag '#{key}' already exist, overwritten with #{value}") if tags != old_tags
end