Class: NewRelicMetrics::Client

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(config = nil) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


30
31
32
33
34
# File 'lib/newrelic-metrics.rb', line 30

def initialize(config=nil)
  @config = config || NewRelicMetrics.configuration

  raise ArgumentError, "No API Key is configured" unless @config && @config.api_key
end

Instance Method Details

#metrics(application: nil, server: nil, metrics:, range: {}, summarize: false) ⇒ Object

Raises:

  • (ArgumentError)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/newrelic-metrics.rb', line 44

def metrics(application:nil, server:nil, metrics:, range:{}, summarize: false)
  if range && range!={}
    raise ArgumentError, "Range must only contain a :to and :from time" unless range.keys.all?{|k| k==:to || k==:from }
    raise ArgumentError, "Range must contain a :from time" unless range.keys.include?(:from)
  end

  raise ArgumentError, "Need to define either an application or server id" unless application || server
  raise ArgumentError, "Need to define either an application or server id, but not both" if application && server

  resource = application ? 'applications' : 'servers'
  resource_id = application || server

  conditions = []

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

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

#names(application: nil, server: nil) ⇒ Object

Raises:

  • (ArgumentError)


36
37
38
39
40
41
42
# File 'lib/newrelic-metrics.rb', line 36

def names(application:nil, server:nil)
  raise ArgumentError, "Need to define either an application or server id" unless application || server
  raise ArgumentError, "Need to define either an application or server id, but not both" if application && server
  resource = application ? 'applications' : 'servers'
  resource_id = application || server
  get(resource, resource_id, "metrics")
end