require 'fileutils' module NewRelic module Rack class MetricApp def initialize(options) if options[:install] FileUtils.copy File.join(File.dirname(__FILE__), "newrelic.yml"), "." NewRelic::Agent.logger.info "===============================" 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 end options[:app_name] ||= 'EPM Monitor' options[:disable_samplers] = true NewRelic::Agent.manual_start options unless NewRelic::Control.instance.license_key NewRelic::Agent.logger.error "Please add a valid license key to newrelic.yml." exit 1 end end 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 end class Status def call(env) request = ::Rack::Request.new env data_url = "http://#{env['HTTP_HOST']}/metrics/path?value=nnn" body = StringIO.new body.puts "" body.puts "

New Relic Actively Monitoring #{NewRelic::Control.instance.app_names.join(' and ')}

" body.puts "

To submit a metric value, use #{data_url}

" body.puts "

Request Details

" body.puts "
" body.puts "
ip
#{request.ip}" body.puts "
host
#{request.host}" body.puts "
path
#{request.url}" body.puts "
query
#{request.query_string}" body.puts "
params
#{request.params.inspect}" body.puts "
" body.puts "

Complete ENV

" body.puts "" response = ::Rack::Response.new body.string response.finish end end end end