Skip to content

Instantly share code, notes, and snippets.

@assimovt
Last active June 8, 2018 22:12
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save assimovt/5181244 to your computer and use it in GitHub Desktop.
Save assimovt/5181244 to your computer and use it in GitHub Desktop.
Adds Newrelic RPMs metrics to your dashboard.

Setup

Add the following gems to your Gemfile:

gem 'activeresource'
gem 'newrelic_api'

Update your bundle:

bundle

Put the newrelic_rpm.rb to jobs/newrelic_rpm.rb and configure your API key and Application name.

Finally, choose a metric (see "Emitted metrics" jobs/newrelic_rpm.rb) you want to show on the dashboard and add corresponding HTML code. For example, the code below shows Newrelic Throughput (requests/minute) on the scale of 100. Adjust the scale based on your application usage.

<li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
<div data-id="rpm_throughput" data-view="Meter" data-title="RPM" data-min="0" data-max="100"></div>
</li>
require 'newrelic_api'
# Newrelic API key
key = 'ENTER YOUR KEY'
# Monitored application
app_name = 'ENTER YOUR APPLICATION NAME'
# Emitted metrics:
# - rpm_apdex
# - rpm_error_rate
# - rpm_throughput
# - rpm_errors
# - rpm_response_time
# - rpm_db
# - rpm_cpu
# - rpm_memory
NewRelicApi.api_key = key
SCHEDULER.every '10s', :first_in => 0 do |job|
app = NewRelicApi::Account.find(:first).applications(:params =>
{:conditions => {:name => app_name}}
).first
app.threshold_values.each do |v|
send_event("rpm_" + v.name.downcase.gsub(/ /, '_'), { value: v.metric_value })
end
end
@hannesfostie
Copy link

I've been looking into this in order to modify it to accept multiple accounts/applications/api keys and I ran into a problem where line 22..24 in your job file didn't actually return the correct application.

Given 2 applications on one account, with very distinct names, running your 'query' with the conditions it always returns both applications, so the first is always the same (given an alphabetical order). Where did you find this code? It doesn't appear to be mentioned on the NewRelicApi github page.

@morphizer
Copy link

I couldn't get the gem to return the application correctly, was quicker to parse the xml with nokogiri.

xml = Nokogiri::XML(open(
  'https://api.newrelic.com/api/v1/accounts/' + account_id + '/applications/' + app_id + '/threshold_values.xml',
  'x-api-key' => key
))

send_event('rpm_throughput', { value: xml.at_css('threshold_value[name="Throughput"]')['metric_value'] })

@erikwennerberg
Copy link

I wrote a gist that enables several New Relic applications in Dashing:
https://gist.github.com/erikwennerberg/6875943

@bayleedev
Copy link

@morphizer here is the updated version

NewRelicApi.api_key = key

SCHEDULER.every '10s', :first_in => 0 do |job|
  app = NewRelicApi::Account.first.applications.select { |el|
    el.name == app_name
  }.first

  app.threshold_values.each do |v|
    send_event("rpm_" + v.name.downcase.gsub(/ /, '_'), { value: v.metric_value })
  end
end

@munish1
Copy link

munish1 commented Mar 23, 2018

I know very old, wanted to add a data point. New Relic has deprecated the gem and is no longer work with new API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment