Class: Fluent::NewRelicSummaryInput

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

Constant Summary collapse

NEW_RELIC_API_DOMAIN =
'api.newrelic.com'
SUMMARIES =
%w(applications servers key_transactions)

Instance Method Summary collapse

Constructor Details

#initializeNewRelicSummaryInput

Returns a new instance of NewRelicSummaryInput.



19
20
21
# File 'lib/fluent/plugin/in_newrelic_summary.rb', line 19

def initialize
  super
end

Instance Method Details

#configure(conf) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fluent/plugin/in_newrelic_summary.rb', line 23

def configure(conf)
  log.trace "in_newrelic_summary: configure"
  super

  if @tag.nil?
    raise Fluent::ConfigError, "newrelic_summary: 'tag' parameter is required"
  end
  unless SUMMARIES.include?(@summary)
    raise Fluent::ConfigError, "newrelic_summary: no such 'summary' parameter. summary should be applications, servers or key_transactions"
  end
end

#emit_data(data) ⇒ Object

The end of run method



70
71
72
73
# File 'lib/fluent/plugin/in_newrelic_summary.rb', line 70

def emit_data(data)
  log.trace "in_newrelic_summary: emit_data"
  Engine.emit("#{tag}", Engine.now, data)
end

#runObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fluent/plugin/in_newrelic_summary.rb', line 49

def run
  log.trace "in_newrelic_summary: run"

  loop do
    http = Net::HTTP.new(NEW_RELIC_API_DOMAIN, 443)
    http.use_ssl = true
    uri = "/v2/#{@summary}.json"
    req = Net::HTTP::Get.new(uri)
    req["NewRelic-Api-Key"] = @api_key
    res = http.request(req)

    case res
    when Net::HTTPSuccess
      emit_data(JSON.parse(res.body))
    else
      log.error "in_newrelic_summary: error"
    end
    sleep @interval
  end
end

#shutdownObject



42
43
44
45
46
47
# File 'lib/fluent/plugin/in_newrelic_summary.rb', line 42

def shutdown
  log.trace "in_newrelic_summary: shutdown"
  super

  Thread.kill(@thread)
end

#startObject



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

def start
  log.trace "in_newrelic_summary: start"
  super

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