Sha256: 29ee62647b37ceef8cae7506cf913fb632a6e773e022155e704454150e6133c8

Contents?: true

Size: 1.37 KB

Versions: 4

Compression:

Stored size: 1.37 KB

Contents

require 'uri'
begin
  require 'statsd-ruby'
rescue LoadError
  raise 'Gem statsd-ruby is required for logging metrics. Please add the gem "statsd-ruby" to your Gemfile.'
end

module SemanticLogger
  module Metrics
    class Statsd
      # 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
      def initialize(options = {})
        options = options.dup
        @url    = options.delete(:url) || 'udp://localhost:8125'
        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

      def call(log)
        metric = log.metric
        if duration = log.duration
          @statsd.timing(metric, duration)
        else
          amount = (log.metric_amount || 1).round
          if amount < 0
            amount.times { @statsd.decrement(metric) }
          else
            amount.times { @statsd.increment(metric) }
          end
        end
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
semantic_logger-3.4.1 lib/semantic_logger/metrics/statsd.rb
semantic_logger-3.4.0 lib/semantic_logger/metrics/statsd.rb
semantic_logger-3.2.1 lib/semantic_logger/metrics/statsd.rb
semantic_logger-3.2.0 lib/semantic_logger/metrics/statsd.rb