class Fluent::DogstatsdOutput

Attributes

statsd[RW]

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_dogstatsd.rb, line 22
def initialize
  super

  require 'statsd' # dogstatsd-ruby
end

Public Instance Methods

format(tag, time, record) click to toggle source
# File lib/fluent/plugin/out_dogstatsd.rb, line 37
def format(tag, time, record)
  [tag, time, record].to_msgpack
end
start() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_dogstatsd.rb, line 28
def start
  super

  host = @host || Statsd::DEFAULT_HOST
  port = @port || Statsd::DEFAULT_PORT

  @statsd ||= Statsd.new(host, port)
end
write(chunk) click to toggle source
# File lib/fluent/plugin/out_dogstatsd.rb, line 41
def write(chunk)
  @statsd.batch do |s|
    chunk.msgpack_each do |tag, time, record|
      key = if @use_tag_as_key
              tag
            else
              record.delete('key')
            end

      if !key && @use_tag_as_key_if_missing
        key = tag
      end

      unless key
        log.warn "'key' is not specified. skip this record:", tag: tag
        next
      end

      value = record.delete(@value_key || 'value')

      options = {}
      text  = record.delete(@text_key || 'text')
      type  = @metric_type || record.delete('type')
      sample_rate = @sample_rate || record.delete('sample_rate')

      if sample_rate
        options[:sample_rate] = sample_rate
      end

      tags = if @flat_tags || @flat_tag
               record
             else
               record['tags']
             end
      if tags
        options[:tags] = tags.map do |k, v|
          "#{k}:#{v}"
        end
      end

      case type
      when 'increment'
        s.increment(key, options)
      when 'decrement'
        s.decrement(key, options)
      when 'count'
        s.count(key, value, options)
      when 'gauge'
        s.gauge(key, value, options)
      when 'histogram'
        s.histogram(key, value, options)
      when 'timing'
        s.timing(key, value, options)
      when 'set'
        s.set(key, value, options)
      when 'event'
        alert_type = ['error', 'warning', 'info', 'success']
        options[:alert_type] = if !record['alert_type'].nil? and alert_type.include?(record['alert_type'].downcase)
                                 record['alert_type'].downcase
                               else
                                 'info'
                               end
        s.event(key, text, options)
      when nil
        log.warn "type is not provided (You can provide type via `metric_type` in config or `type` field in a record."
      else
        log.warn "Type '#{type}' is unknown."
      end
    end
  end
end