Class: Store::Newrelic

Inherits:
Store
  • Object
show all
Defined in:
lib/store/newrelic.rb

Instance Method Summary collapse

Constructor Details

#initialize(origin, settings) ⇒ Newrelic

Returns a new instance of Newrelic


10
11
12
13
14
15
16
17
# File 'lib/store/newrelic.rb', line 10

def initialize origin, settings
		super
		@api_key = mandatory_param :api_key, "store_settings"
		@application_id = mandatory_param :application_id, "store_settings"

		@base_url = "https://api.newrelic.com"
		@newrelic_url = "#{@base_url}/v2/applications"
end

Instance Method Details

#get_headed_json(uri, headers) ⇒ Object


102
103
104
105
# File 'lib/store/newrelic.rb', line 102

def get_headed_json uri, headers
	a = JSON.parse(open(uri, "X-Api-Key" => headers[:api_key]).read, :symbolize_names => true)
	return a
end

#get_metric(m, start = nil, stop = nil, step = nil, args = {}) ⇒ Object


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/store/newrelic.rb', line 74

def get_metric m, start=nil, stop=nil, step=nil, args={}
		metric, sub_type = parse_newrelic m
		uri = get_metric_url m, start, stop, step		

		begin
			data = get_headed_json uri, {api_key: @api_key}
			
			stream = []

			data[:metric_data][:metrics].first[:timeslices].each {|a| 
				x = DateTime.parse(a[:from]).strftime("%s").to_i
				y = a[:values].select{|k,v| "#{k}" == sub_type}[sub_type.to_sym]
				stream << {x: x, y: y}
			}
		
			return stream
			
		rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, SocketError => e
			raise Store::Error, "Error retreiving New Relic metric #{m}: #{e}"
		end

		return data
end

#get_metric_url(m, start = nil, stop = nil, step = nil) ⇒ Object


67
68
69
70
71
72
# File 'lib/store/newrelic.rb', line 67

def get_metric_url m, start=nil, stop=nil, step=nil
	metric, sub_type = parse_newrelic m
	metric = metric.tr("_","/").tr(".",":")
	uri = "#{@newrelic_url}/#{@application_id}/metrics/data.json?names=#{metric}"
	return uri
end

#get_metrics_listObject


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/store/newrelic.rb', line 46

def get_metrics_list
		begin
		
			json_m = get_headed_json "#{@newrelic_url}/#{@application_id}/metrics.json", {api_key: @api_key}
			metrics = []; 

			json_m[:metrics].each{|a|
				parent = a[:name]
				a[:values].each { |b|
					metrics << "#{parent}-#{b}"
				}
			}

			metrics.map{|a| a.tr("/","_").tr(":",".")}
  .select{|a| !a.include? "{" }

		rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, SocketError => e
			raise Store::Error, "Error retreiving New Relic metrics list: #{e}"
		end
end

#hidden_keysObject


23
24
25
# File 'lib/store/newrelic.rb', line 23

def hidden_keys
	super.append "api_key"
end

#is_up?Boolean

Returns:

  • (Boolean)

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/store/newrelic.rb', line 27

def is_up?
	return true
	begin
		uri = URI.parse(@base_url)

		http = Net::HTTP.new(uri.host, uri.port)
		http.use_ssl = true
		http.verify_mode = OpenSSL::SSL::VERIFY_NONE
		request = Net::HTTP::Get.new(uri.request_uri)

		response = http.request(request)	
		return true

	rescue Errno::EHOSTUNREACH, Errno::ECONNREFUSED, SocketError => e
		return false
	end

end

#keysObject


19
20
21
# File 'lib/store/newrelic.rb', line 19

def keys
	@@NEW_RELIC_KEYS 
end

#parse_newrelic(m) ⇒ Object


98
99
100
# File 'lib/store/newrelic.rb', line 98

def parse_newrelic m
	m.id.split(SEP).last.split("-")
end