Skip to content

Instantly share code, notes, and snippets.

@cardinalblue
Created April 7, 2011 17:24
Show Gist options
  • Save cardinalblue/908251 to your computer and use it in GitHub Desktop.
Save cardinalblue/908251 to your computer and use it in GitHub Desktop.
an example of using rest_graph to query New Relic
# returns like this:
# {"Apdex"=>"0.85 [0.5]", "Error Rate"=>"0.33%",
# "Throughput"=>"202 rpm", "CPU"=>"11%", "Response Time"=>"330 ms",
# "Errors"=>"0.67 epm", "Memory"=>"135 MB", "DB"=>"4.3%"}
def newrelic_data
key = Variant.current.newrelic_api_key
url = Variant.current.newrelic_data_url
return {} unless url # not using newrelic
xml = rest_graph.request({:headers => {'x-api-key' => key},
:expires_in => 1.minutes,
:auto_decode => false},
[:get, url])
doc = Nokogiri::XML.parse(xml)
return {} unless doc.errors.empty? # parse error
return {} unless doc.root # empty document (wrong format)
data = doc.root.xpath('threshold_value')
return {} if data.empty? # no threshold_value (wrong format)
return data.inject({}){ |r, i|
r[i['name']] = i['formatted_metric_value']; r }
rescue RestClient::Exception, SystemCallError # network error
{}
end
@nightfall708
Copy link

Where would you define rest_graph variable?

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