class SemanticLogger::Metric::Statsd

Attributes

url[RW]

Public Class Methods

new(url: "udp://localhost:8125") click to toggle source

Create Statsd metrics subscriber

Parameters:

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

Example:

SemanticLogger.add_appender(
  metric: :statsd,
  url:    'localhost:8125'
)
# File lib/semantic_logger/metric/statsd.rb, line 29
def initialize(url: "udp://localhost:8125")
  @url = url
end

Public Instance Methods

log(log) click to toggle source
# File lib/semantic_logger/metric/statsd.rb, line 42
def log(log)
  metric = log.metric
  if (duration = log.duration)
    @statsd.timing(metric, duration)
  else
    amount = (log.metric_amount || 1).round
    if amount.negative?
      amount.times { @statsd.decrement(metric) }
    else
      amount.times { @statsd.increment(metric) }
    end
  end
end
reopen() click to toggle source
# File lib/semantic_logger/metric/statsd.rb, line 33
def reopen
  uri = URI.parse(@url)
  raise('Statsd only supports udp. Example: "udp://localhost:8125"') if uri.scheme != "udp"

  @statsd           = ::Statsd.new(uri.host, uri.port)
  path              = uri.path.chomp("/")
  @statsd.namespace = path.sub("/", "") if path != ""
end
should_log?(log) click to toggle source

Only forward log entries that contain metrics.

# File lib/semantic_logger/metric/statsd.rb, line 57
def should_log?(log)
  # Does not support metrics with dimensions.
  log.metric && !log.dimensions && meets_log_level?(log) && !filtered?(log)
end