Class: LogStash::Outputs::DatadogMetrics

Inherits:
Base
  • Object
show all
Includes:
Stud::Buffer
Defined in:
lib/logstash/outputs/datadog_metrics.rb

Overview

Default queue_size and timeframe are low in order to provide near realtime alerting. If you do not use Datadog for alerting, consider raising these thresholds.

Constant Summary

Constants included from Config::Mixin

Config::Mixin::CONFIGSORT

Instance Attribute Summary

Attributes included from Config::Mixin

#config, #original_params

Attributes inherited from Plugin

#logger, #params

Instance Method Summary collapse

Methods inherited from Base

#handle, #handle_worker, #initialize, #worker_setup, #workers_not_supported

Methods included from Config::Mixin

#config_init, included

Methods inherited from Plugin

#eql?, #finished, #finished?, #hash, #initialize, #inspect, lookup, #reload, #running?, #shutdown, #teardown, #terminating?, #to_s

Constructor Details

This class inherits a constructor from LogStash::Outputs::Base

Instance Method Details

#flush(events, final = false) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/logstash/outputs/datadog_metrics.rb', line 92

def flush(events, final=false)
  dd_series = Hash.new
  dd_series['series'] = []

  events.each do |event|
    begin
      dd_series['series'] << event
    rescue
      @logger.warn("Error adding event to series!", :exception => e)
      next
    end
  end

  request = Net::HTTP::Post.new("#{@uri.path}?api_key=#{@api_key}")

  begin
    request.body = dd_series.to_json
    request.add_field("Content-Type", 'application/json')
    response = @client.request(request)
    @logger.info("DD convo", :request => request.inspect, :response => response.inspect)
    raise unless response.code == '202'
  rescue Exception => e
    @logger.warn("Unhandled exception", :request => request.inspect, :response => response.inspect, :exception => e.inspect)
  end
end

#receive(event) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/logstash/outputs/datadog_metrics.rb', line 68

def receive(event)
  return unless output?(event)
  return unless @metric_name && @metric_value && @metric_type
  return unless ["gauge", "counter"].include? event.sprintf(@metric_type)

  dd_metrics = Hash.new
  dd_metrics['metric'] = event.sprintf(@metric_name)
  dd_metrics['points'] = [[to_epoch(event.timestamp), event.sprintf(@metric_value).to_f]]
  dd_metrics['type'] = event.sprintf(@metric_type)
  dd_metrics['host'] = event.sprintf(@host)
  dd_metrics['device'] = event.sprintf(@device)

  if @dd_tags
    tagz = @dd_tags.collect {|x| event.sprintf(x) }
  else
    tagz = event["tags"]
  end
  dd_metrics['tags'] = tagz if tagz

  @logger.info("Queueing event", :event => dd_metrics)
  buffer_receive(dd_metrics)
end

#registerObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/logstash/outputs/datadog_metrics.rb', line 49

def register
  require 'time'
  require "net/https"
  require "uri"

  @url = "https://app.datadoghq.com/api/v1/series"
  @uri = URI.parse(@url)
  @client = Net::HTTP.new(@uri.host, @uri.port)
  @client.use_ssl = true
  @client.verify_mode = OpenSSL::SSL::VERIFY_NONE
  @logger.debug("Client", :client => @client.inspect)
  buffer_initialize(
    :max_items => @queue_size,
    :max_interval => @timeframe,
    :logger => @logger
  )
end