class Fluent::NewrelicMetricsInput

Constants

PREFIX

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/fluent/plugin/in_newrelic_metrics.rb, line 11
def initialize
  super
  require 'rest-client'
end

Public Instance Methods

configure(conf) click to toggle source
Calls superclass method
# File lib/fluent/plugin/in_newrelic_metrics.rb, line 25
def configure(conf)
  super
  @state = {}
end
emit_newrelic_metrics() click to toggle source
# File lib/fluent/plugin/in_newrelic_metrics.rb, line 45
def emit_newrelic_metrics
  begin
    results = RestClient.get PREFIX + "/#{@metrics}.json", 'X-Api-Key' => @api_key
    records = JSON.parse(results)[@metrics]
    if @alert_policy_id
      records = records.select{ |record|
        @alert_policy_id.include?(record['links']['alert_policy'])
      }
    end
    records.each do |record|
      before_last_reported_at = @state["#{record['id']}"]
      last_reported_at = Time.parse(record['last_reported_at'])
      if before_last_reported_at.nil? || (before_last_reported_at < last_reported_at)
        router.emit @tag, last_reported_at.to_i, record
        @state["#{record['id']}"] = last_reported_at
      end
    end
  rescue => e
    log.error e
  end
end
run() click to toggle source
# File lib/fluent/plugin/in_newrelic_metrics.rb, line 38
def run
  loop do
    Thread.new(&method(:emit_newrelic_metrics))
    sleep @interval
  end
end
shutdown() click to toggle source
# File lib/fluent/plugin/in_newrelic_metrics.rb, line 34
def shutdown
  Thread.kill(@thread)
end
start() click to toggle source
# File lib/fluent/plugin/in_newrelic_metrics.rb, line 30
def start
  @thread = Thread.new(&method(:run))
end