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



31
32
33
34
# File 'lib/fluent/plugin/backends/datadog_backend.rb', line 31

def initialize()
  super
  require "dogapi"
end

Instance Method Details

#configure(conf) ⇒ Object



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

def configure(conf)
  super

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

  silent = false # raise exceptions on dogapi errors
  @dog = Dogapi::Client.new(@dd_api_key, @dd_app_key, @host, @device, silent, @timeout)
end

#write(data) ⇒ Object



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

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

    begin
      log.debug("sending #{metric_points.length} metric(s) to datadog...")
      @dog.batch_metrics do
        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"
            @dog.emit_points(metric, points, options)
          end
        end
      end
    rescue Exception => error
       # dogapi may raise an Exception.
       # fluentd expects StandardError as retriable error, though.
       raise("datadog error: #{error.class}: #{error.message}")
    end
  end
end