Created
April 7, 2011 17:24
-
-
Save cardinalblue/908251 to your computer and use it in GitHub Desktop.
an example of using rest_graph to query New Relic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Where would you define rest_graph variable?