Class: Fluent::DogstatsdOutput

Inherits:
BufferedOutput
  • Object
show all
Defined in:
lib/fluent/plugin/out_dogstatsd.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDogstatsdOutput

Returns a new instance of DogstatsdOutput.



21
22
23
24
25
# File 'lib/fluent/plugin/out_dogstatsd.rb', line 21

def initialize
  super

  require 'statsd' # dogstatsd-ruby
end

Instance Attribute Details

#statsdObject

Returns the value of attribute statsd.



19
20
21
# File 'lib/fluent/plugin/out_dogstatsd.rb', line 19

def statsd
  @statsd
end

Instance Method Details

#format(tag, time, record) ⇒ Object



36
37
38
# File 'lib/fluent/plugin/out_dogstatsd.rb', line 36

def format(tag, time, record)
  [tag, time, record].to_msgpack
end

#startObject



27
28
29
30
31
32
33
34
# File 'lib/fluent/plugin/out_dogstatsd.rb', line 27

def start
  super

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

  @statsd ||= Statsd.new(host, port)
end

#write(chunk) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/fluent/plugin/out_dogstatsd.rb', line 40

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 = {}
      title = record.delete('title')
      text  = record.delete('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'
        options[:alert_type] = record['alert_type']
        s.event(title, 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