Skip to content

Instantly share code, notes, and snippets.

@mjc
Forked from erikwennerberg/README.md
Last active February 10, 2017 17:53
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjc/7836009 to your computer and use it in GitHub Desktop.
Save mjc/7836009 to your computer and use it in GitHub Desktop.

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. Specify your app IDs (found in the New Relic URL of an application), and define the corresponding metrics in the .erb.

<li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
<div data-id="YOURAPPID1_rpm_throughput" data-view="Meter" data-title="RPM" data-min="0" data-max="100"></div>
</li>
<li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
<div data-id="YOURAPPID2_rpm_throughput" data-view="Meter" data-title="RPM" data-min="0" data-max="100"></div>
</li>
require 'newrelic_api'
# Newrelic API key
key = nil
# Array of monitored application ids
app_ids = nil
# Emitted metrics:
# - rpm_apdex
# - rpm_error_rate
# - rpm_throughput
# - rpm_errors
# - rpm_response_time
# - rpm_db
# - rpm_cpu
# - rpm_memory
NewRelicApi.api_key = key
newrelic_account = NewRelicApi::Account.find(:first)
newrelic_apps = newrelic_account.applications.select do |app|
app_ids.include? app.id.to_s if app_ids
end
SCHEDULER.every '10s', first_in: 0 do |job|
newrelic_apps.each do |newrelicapp|
newrelicapp.threshold_values.each do |v|
underscored_name = v.name.downcase.gsub(' ', '_')
event_name = sprintf('%s_rpm_%s', newrelicapp.id, underscored_name)
send_event(event_name, value: v.metric_value)
end
end
end if app_ids
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment