Class: Fluent::MetricSenseOutput::Backends::DatadogBackend

Inherits:
Fluent::MetricSenseOutput::Backend show all
Defined in:
lib/fluent/plugin/backends/datadog_backend.rb

Constant Summary

Constants inherited from Fluent::MetricSenseOutput::Backend

Fluent::MetricSenseOutput::Backend::UpdateMode

Instance Attribute Summary

Attributes inherited from Fluent::MetricSenseOutput::Backend

#log

Instance Method Summary collapse

Methods inherited from Fluent::MetricSenseOutput::Backend

#shutdown, #start

Constructor Details

#initializeDatadogBackend

Returns a new instance of DatadogBackend.



29
30
31
32
# File 'lib/fluent/plugin/backends/datadog_backend.rb', line 29

def initialize()
  super
  require "dogapi"
end

Instance Method Details

#configure(conf) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fluent/plugin/backends/datadog_backend.rb', line 34

def configure(conf)
  super

  if @dd_api_key.nil?
    raise Fluent::ConfigError, "missing Datadog API key"
  end

  client_args = [@dd_api_key]
  client_args << @dd_app_key if @dd_app_key
  @dog = Dogapi::Client.new(*client_args)
end

#write(data) ⇒ Object



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
# File 'lib/fluent/plugin/backends/datadog_backend.rb', line 46

def write(data)
  data.each_slice(@batch_size) do |slice|
    metric_points = {}
    slice.each do |tag, time, value, seg_key, seg_val|
      if seg_key and seg_val
        # segmented values
        metric = "#{tag}_by_#{seg_key}"
        segment = "#{seg_key}:#{seg_val}"
      else
        # simple values
        metric = tag
        segment = ""
      end
      metric_points[metric] ||= {}
      metric_points[metric][segment] ||= []
      metric_points[metric][segment].push([Time.at(time), value])
    end

    metric_points.each do |metric, segment_points|
      segment_points.each do |segment, points|
        tags = @tags.dup
        if segment and not segment.empty?
          tags.push(segment)
        end

        options = {}
        options[:tags] = tags
        options[:host] = @host if @host
        options[:type] = "gauge"

        log.debug("datadog emit points: metric=#{metric}, points=#{points.inspect}, options=#{options.inspect}")
        code, response = @dog.emit_points(metric, points, options)
        if code.to_i / 100 != 2
          raise("datadog returns #{code}: #{response.inspect}")
        end
      end
    end
  end
end