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



22
23
24
25
# File 'lib/fluent/plugin/in_newrelic_metrics.rb', line 22

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

#emit_newrelic_metricsObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fluent/plugin/in_newrelic_metrics.rb', line 42

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|
        record['links']['alert_policy'] == @alert_policy_id
      }
    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, record
        @state["#{record['id']}"] = last_reported_at
      end
    end
  rescue => e
    log.error e
  end
end

#runObject



35
36
37
38
39
40
# File 'lib/fluent/plugin/in_newrelic_metrics.rb', line 35

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

#shutdownObject



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

def shutdown
  Thread.kill(@thread)
end

#startObject



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

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