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.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/new_relic/rack/metric_app.rb', line 5

def initialize(options)
  if options[:install]
    src = File.join(File.dirname(__FILE__), "newrelic.yml")
    require 'new_relic/command'
    begin
      NewRelic::Command::Install.new(:quiet => true, :src_file => src).run
      NewRelic::Agent.logger.info "A newrelic.yml template was copied to #{File.expand_path('.')}."
      NewRelic::Agent.logger.info "Please add a license key to the file and restart #{$0}"
      exit 0
    rescue NewRelic::Command::CommandFailure => e
      NewRelic::Agent.logger.error e.message
      exit 1
    end
  end
  options[:app_name] ||= 'EPM Monitor'
  options[:disable_samplers] = true
  NewRelic::Agent.manual_start options
  unless NewRelic::Control.instance.license_key
    raise "Please add a valid license key to newrelic.yml."
  end
end

Instance Method Details

#call(env) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/new_relic/rack/metric_app.rb', line 26

def call(env)
  request = ::Rack::Request.new env
  params = request.params
  if !(params['uri'] || params['metric'])
    [400, { 'Content-Type' => 'text/plain' },  "Missing 'uri' or 'metric' parameter: #{params.inspect}" ]
  elsif !params['value']
    [400, { 'Content-Type' => 'text/plain' },  "Missing 'value' parameter: #{params.inspect}" ]
  else
    NewRelic::Agent.record_transaction( params['value'].to_f, params )
    ::Rack::Response.new(params.collect { |k, v| "#{k}=#{v} " }.join).finish
  end
end