Class: NewRelicMetrics

Inherits:
Object
  • Object
show all
Defined in:
lib/newrelic_metrics.rb

Constant Summary collapse

BASE =
'https://api.newrelic.com/v2'

Instance Method Summary collapse

Constructor Details

#initialize(api_key, ids = {}) ⇒ NewRelicMetrics

Returns a new instance of NewRelicMetrics.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
# File 'lib/newrelic_metrics.rb', line 9

def initialize(api_key,ids={})
  @api_key = api_key
  raise ArgumentError, "Need to define either an application or server id" if ids.keys.length!=1
  raise ArgumentError, "#{ids.keys.first} must be `application` or `server`" if ids.keys.first != :server && ids.keys.first != :application
  @resource = "#{ids.keys.first}s"
  @resource_id = ids.values.first
end

Instance Method Details

#metrics(metrics = {}, options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/newrelic_metrics.rb', line 29

def metrics(metrics={},options={})
  query = ""
  conditions = []

  names = metrics.keys
  values = metrics.values.flatten

  names.each {|name| conditions << "names[]=#{URI.encode(name)}" }
  values.each {|val| conditions << "values[]=#{URI.encode(val)}" }

  if options[:from]
    from_time = Chronic.parse(options[:from], context: :past)
    to_time = Chronic.parse(options[:to]) if options[: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 options[:summarize]

  query = conditions.join('&')
  get("metrics/data",query)
end

#namesObject



17
18
19
# File 'lib/newrelic_metrics.rb', line 17

def names
  get("metrics")
end

#summarize(metrics = {}, range = {}) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/newrelic_metrics.rb', line 21

def summarize(metrics={},range={})
  throw ArgumentError, "Range must be an array with the from and to time" if range.length>2

  settings = {summarize:true}.merge(range)

  metrics(metrics,settings)
end