class NewRelicMetrics::Client
Constants
- BASE
Public Class Methods
new(config=nil)
click to toggle source
# File lib/newrelic-metrics.rb, line 30 def initialize(config=nil) @config = config || NewRelicMetrics.configuration raise ArgumentError.new("No API Key is configured") unless @config && @config.api_key end
Public Instance Methods
metrics(options={})
click to toggle source
# File lib/newrelic-metrics.rb, line 45 def metrics(options={}) resource_id = options[:application] || options[:server] resource = options[:application] ? 'applications' : 'servers' metrics = options[:metrics] range = options[:range] || {} summarize = options[:summarize] || false conditions = [] raise ArgumentError.new("Need to define either an application or server id") unless resource_id raise ArgumentError.new("Need to define either an application or server id, but not both") if options[:application] && options[:server] raise ArgumentError.new("Metrics must be set") unless metrics raise ArgumentError.new("Metrics must be an hash") unless metrics.is_a?(Hash) raise ArgumentError.new("Metric keys must be string") unless metrics.keys.all?{|k| k.is_a?(String)} raise ArgumentError.new("Metric values must be arrays") unless metrics.values.all?{|k| k.is_a?(Array)} raise ArgumentError.new("Metric values must be an array of strings") unless metrics.values.all?{|k| k.all?{|v| v.is_a?(String)} } if range && range!={} raise ArgumentError.new("Range must only contain a :to and :from time") unless range.keys.all?{|k| k==:to || k==:from } raise ArgumentError.new("Range must contain a :from time") unless range.keys.include?(:from) end metrics.keys.each {|name| conditions << "names[]=#{URI.encode(name)}" } metrics.values.flatten.each {|val| conditions << "values[]=#{URI.encode(val)}" } if range[:from] from_time = range[:from].is_a?(String) ? Chronic.parse(range[:from], context: :past) : range[:from] to_time = range[:to].is_a?(String) ? Chronic.parse(range[:to]) : range[:to] if range[:to] to_time ||= Time.now if from_time conditions << "from=#{from_time.getlocal('+00:00').iso8601}" conditions << "to=#{to_time.getlocal('+00:00').iso8601}" end end conditions << "summarize=true" if summarize query = conditions.join('&') get(resource, resource_id, "metrics/data", query)['metric_data'] end
names(options={})
click to toggle source
# File lib/newrelic-metrics.rb, line 36 def names(options={}) resource_id = options[:application] || options[:server] resource = options[:application] ? 'applications' : 'servers' raise ArgumentError.new("Need to define either an application or server id") unless resource_id raise ArgumentError.new("Need to define either an application or server id, but not both") if options[:application] && options[:server] get(resource, resource_id, "metrics")['metrics'] end
Private Instance Methods
get(resource, resource_id, path, query=nil)
click to toggle source
# File lib/newrelic-metrics.rb, line 87 def get(resource, resource_id, path, query=nil) uri = URI.parse('https://api.newrelic.com/') uri.path = "/v2/#{resource}/#{resource_id}/#{path}.json" uri.query = query if query && query != "" begin response = RestClient.get(uri.to_s,'X-Api-Key'=>@config.api_key) rescue => ex message = ex.response ? JSON.parse(ex.response).values.first['title'] : ex.message raise RequestFailed, message end begin content = JSON.parse(response) rescue => ex raise RequestFailed, ex.message end content end