Class: Fluent::NewrelicMetricsInput

Inherits:
Input
  • Object
show all
Defined in:
lib/fluent/plugin/in_newrelic_metrics.rb

Constant Summary collapse

PREFIX =
'https://api.newrelic.com/v2'

Instance Method Summary collapse

Constructor Details

#initializeNewrelicMetricsInput

Returns a new instance of NewrelicMetricsInput.



11
12
13
14
# File 'lib/fluent/plugin/in_newrelic_metrics.rb', line 11

def initialize
  super
  require 'rest-client'
end

Instance Method Details

#configure(conf) ⇒ Object



25
26
27
28
# File 'lib/fluent/plugin/in_newrelic_metrics.rb', line 25

def configure(conf)
  super
  @state = {}
end

#emit_newrelic_metricsObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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

#runObject



38
39
40
41
42
43
# File 'lib/fluent/plugin/in_newrelic_metrics.rb', line 38

def run
  loop do
    Thread.new(&method(:emit_newrelic_metrics))
    sleep @interval
  end
end

#shutdownObject



34
35
36
# File 'lib/fluent/plugin/in_newrelic_metrics.rb', line 34

def shutdown
  Thread.kill(@thread)
end

#startObject



30
31
32
# File 'lib/fluent/plugin/in_newrelic_metrics.rb', line 30

def start
  @thread = Thread.new(&method(:run))
end