Class: NewRelic::Rack::MetricApp

Inherits:
Object
  • Object
show all
Defined in:
lib/new_relic/rack/metric_app.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ MetricApp

Returns a new instance of MetricApp.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/new_relic/rack/metric_app.rb', line 4

def initialize(options)
  if options[:install]
    FileUtils.copy File.join(File.dirname(__FILE__), "newrelic.yml"), "."
    NewRelic::Control.instance.log.info "==============================="
    NewRelic::Control.instance.log.info "A newrelic.yml template was copied to #{File.expand_path('.')}."
    NewRelic::Control.instance.log.info "Please add a license key to the file and restart #{$0}"
    exit 0
  end
  options[:app_name] ||= 'EPM Monitor'
  options[:disable_samplers] = true
  NewRelic::Agent.manual_start options
  unless NewRelic::Control.instance.license_key
    NewRelic::Control.instance.log.error "Please add a valid license key to newrelic.yml."
    exit 1
  end
end

Instance Method Details

#call(env) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/new_relic/rack/metric_app.rb', line 20

def call(env)
  request = ::Rack::Request.new env
  segments = request.url.gsub(/^.*?\/metrics\//, '').split("?")[0].split("/")
  metric = "Custom/" + segments.join("/")
  raise "Expected value parameter!" unless request['value']
  data = request['value'].to_f
  stats = NewRelic::Agent.get_stats(metric, false)
  stats.record_data_point data
  response = ::Rack::Response.new "#{metric}=#{data}" 
  response.finish
end