class Ruby::Reporters::Datadog

Attributes

default_http_tags[R]
response_time_metric_name[R]
statsd[R]

Public Class Methods

new(opts={}) click to toggle source
# File lib/ruby/reporters/datadog.rb, line 8
def initialize(opts={})
  statsd_host                 = opts[:statsd_host] || "localhost"
  statsd_port                 = opts[:statsd_port] || 8125
  namespace                   = opts[:statsd_prefix] || nil
  
  @default_http_tags          = ['type:http']
  @response_time_metric_name  = 'response_time_ms'
  
  @statsd = opts[:statsd] || ::Datadog::Statsd.new(statsd_host, statsd_port, namespace: namespace)
end

Public Instance Methods

gauge(metric_name, value, tags) click to toggle source
# File lib/ruby/reporters/datadog.rb, line 29
def gauge(metric_name, value, tags)
  statsd.gauge(
    metric_name,
    value,
    tags: tags
  )
end
histogram(metric_name, value, tags) click to toggle source
# File lib/ruby/reporters/datadog.rb, line 44
def histogram(metric_name, value, tags)
  statsd.histogram(
    metric_name,
    value,
    tags:tags
  )
end
increment(metric_name, tags) click to toggle source
# File lib/ruby/reporters/datadog.rb, line 37
def increment(metric_name, tags)
  statsd.increment(
    metric_name,
    tags: tags
  )
end
response_time_ms(path, method, status, value) click to toggle source
# File lib/ruby/reporters/datadog.rb, line 19
def response_time_ms(path, method, status, value)
  tags = [
    "route:#{method.upcase} #{path}",
    "status:#{status}",
    "error:#{from_status_to_error(status)}"
  ]

  histogram(response_time_metric_name, value, default_http_tags | tags)
end
timing(metric_name, value, tags) click to toggle source
# File lib/ruby/reporters/datadog.rb, line 52
def timing(metric_name, value, tags)
  statsd.timing(
    metric_name,
    value,
    tags:tags
  )
end

Private Instance Methods

from_status_to_error(status) click to toggle source
# File lib/ruby/reporters/datadog.rb, line 61
def from_status_to_error(status)
  status.to_i < 400 ? "false" : "true"
end