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
# 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
        segment = "#{seg_key}:#{seg_val}"
      else
        # simple values
        segment = "simple"
      end
      metric = tag # use fluentd's tag as metric name on datadog
      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|
        seg_key, seg_val = segment.split(":", 2)

        tags = @tags.dup
        tags.push(segment)
        if seg_key and seg_val
          # add seg_key as a tag to allow calculating metrics over the segment name
          tags.push(seg_key)
        end

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

        @dog.emit_points(metric, points, options)
      end
    end
  end
end