newrelic_data 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/Gemfile +11 -0
  2. data/HISTORY.md +15 -0
  3. data/README.md +56 -0
  4. data/lib/newrelic_data.rb +207 -0
  5. metadata +119 -0
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source :rubygems
2
+
3
+ gem 'curb'
4
+ gem 'xml-simple'
5
+
6
+ group :development do
7
+ gem 'rspec'
8
+ gem 'simplecov'
9
+ gem 'yard'
10
+ gem 'redcarpet'
11
+ end
data/HISTORY.md ADDED
@@ -0,0 +1,15 @@
1
+ NewRelicData History
2
+ ====================
3
+
4
+ ### 0.0.2
5
+
6
+ * Made work.
7
+ * Added support for "app_name"
8
+ * Added functional tests.
9
+ * `rspec ./spec/newrelic_data_functional.rb`
10
+
11
+ ### 0.0.1
12
+
13
+ * Initial Release
14
+
15
+
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ NewRelicData
2
+ ============
3
+
4
+ This is a simple interface for accessing data through [NewRelic's RESTful API](https://newrelic.com/docs/docs/getting-started-with-the-new-relic-rest-api).
5
+
6
+ * [Gem on Rubygems.com](https://rubygems.org/gems/newrelic_data)
7
+ * [Documentation](http://jmervine.github.com/newrelic_data/doc/)
8
+ * [Coverage](http://jmervine.github.com/newrelic_data/coverage/)
9
+ * [History](https://github.com/jmervine/newrelic_data/blob/master/HISTORY.md)
10
+
11
+ ### Installation
12
+
13
+ gem install newrelic_data
14
+
15
+ Via Bundler
16
+
17
+ sources :rubygems
18
+ gem 'newrelic_data'
19
+
20
+ ### Usage
21
+
22
+ require 'newrelic_data'
23
+ @newrelic = NewRelicData.new
24
+
25
+ @newrelic.format = "json" # default
26
+
27
+ @newrelic.configure({
28
+ api_key: "your_newrelic_api_key",
29
+ account: 4321, # NewRelic Account ID
30
+ application: 654321, # NewRelic Application ID
31
+ # Also supported:
32
+ # application: "app name", # NewRelic Application name.
33
+ query_params: {
34
+ metrics: [ "Apdex", "Apdex/home/index" ],
35
+ field: "score",
36
+ begin: @newrelic.today,
37
+ end: 7.days_ago
38
+ }
39
+ })
40
+
41
+ pp @newrelic.fetch
42
+
43
+ ### Disclaimer
44
+
45
+ NewRelic (tm) is a trademark of [New Relic, Inc.](http://www.newrelic.com/). This was written without their consent and is in no way affiliated with them.
46
+
47
+ #### MIT License
48
+
49
+ Copyright (C) 2012 Joshua P. Mervine
50
+
51
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
52
+
53
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
54
+
55
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
56
+
@@ -0,0 +1,207 @@
1
+ require 'date'
2
+ require 'curb'
3
+ require 'json'
4
+ require 'xmlsimple'
5
+ require 'csv'
6
+ require 'uri'
7
+
8
+ # A simple utility for getting data from NewRelic based
9
+ # loosly off of NewRelic's API.
10
+ class NewRelicData
11
+
12
+ # Version for gem.
13
+ VERSION = "0.0.3"
14
+
15
+ # @return [String] NewRelic API key.
16
+ attr_accessor :api_key
17
+
18
+ # @return [Boolean] Use 'http' or 'https', defaults to true.
19
+ attr_accessor :ssl
20
+
21
+ # @return [String] NewRelic API host, defaults to 'rpm.newrelic.com'.
22
+ attr_accessor :host
23
+
24
+ # @return [Fixnum] NewRelic API port, defaults to '80'.
25
+ attr_accessor :port
26
+
27
+ # @return [Fixnum] NewRelic Account ID.
28
+ attr_accessor :account
29
+
30
+ # @return [Fixnum] NewRelic Application ID.
31
+ attr_accessor :application
32
+
33
+ # @return [String] Format to return NewRelic data; valid options:
34
+ # * json
35
+ # * xml
36
+ # * csv
37
+ attr_accessor :format
38
+
39
+ # @return [Hash] Parameters to be passed to NewRelic API.
40
+ attr_accessor :query_params
41
+
42
+ # == Parameters:
43
+ # See {#configure}.
44
+ def initialize opts={}
45
+ self.ssl = true
46
+ configure(opts)
47
+ end
48
+
49
+ # == Parameters:
50
+ # api_key::
51
+ # See {#api_key}
52
+ # ssl::
53
+ # See {#ssl}
54
+ # host::
55
+ # See {#host}
56
+ # port::
57
+ # See {#port}
58
+ # application::
59
+ # See {#application}
60
+ # account::
61
+ # See {#account}
62
+ # format::
63
+ # See {#format}
64
+ # query_params::
65
+ # See {#query_params}
66
+ #
67
+ # == Example:
68
+ #
69
+ # nr.configure( metrics: [ "Apdex" ], field: "score" )
70
+ def configure opts
71
+ opts.each do |key,val|
72
+ self.send("#{key}=".to_sym, val)
73
+ end
74
+ end
75
+
76
+ # @return [Hash] or [Array] parsed data from NewRelic.
77
+ def fetch
78
+ case self.format
79
+ when "json"
80
+ return JSON.parse(fetch_raw)
81
+ when "xml"
82
+ return XmlSimple.xml_in(fetch_raw)
83
+ when "csv"
84
+ return CSV.parse(fetch_raw)
85
+ else
86
+ raise "unsupport format '#{self.format}', try #fetch_raw"
87
+ end
88
+ end
89
+
90
+ # @return [String] raw data from NewRelic.
91
+ def fetch_raw
92
+ Curl::Easy.perform(fetch_path) do |curl|
93
+ curl.headers['x-api-key'] = self.api_key
94
+ end.body_str
95
+ end
96
+
97
+ # @return [String] URI to be used to fetch content
98
+ def fetch_path
99
+ port = ":#{self.port}" unless self.port.nil? or self.port == 80
100
+ "#{self.ssl ? 'https' : 'http' }://#{self.host}#{port}/api/v1/#{self.account}/metrics/data.#{self.format}?#{self.query_string}"
101
+ end
102
+
103
+ # @return [String] NewRelic API key.
104
+ def api_key
105
+ raise "api_key required" unless @api_key
106
+ @api_key
107
+ end
108
+
109
+ # @return [String] NewRelic API host, defaults to 'api.newrelic.com'.
110
+ def host
111
+ @host||"api.newrelic.com"
112
+ end
113
+
114
+ # @return [Fixnum] NewRelic API port, defaults to '80'.
115
+ def port
116
+ @port||80
117
+ end
118
+
119
+ # @return [Fixnum] NewRelic Application ID.
120
+ def application
121
+ raise "application required" unless @application
122
+ @application
123
+ end
124
+
125
+ def application=(a)
126
+ @application = a
127
+ if @application.kind_of? String and @application.to_i == 0
128
+ self.query_params = self.query_params.merge({ "app_name" => @application })
129
+ self.query_params.delete("app_id") if self.query_params.has_key?("app_id")
130
+ else
131
+ self.query_params = self.query_params.merge({ "app_id" => @application })
132
+ self.query_params.delete("app_name") if self.query_params.has_key?("app_name")
133
+ end
134
+ @application
135
+ end
136
+
137
+ # @return [Fixnum] NewRelic Account ID.
138
+ def account
139
+ raise "account required" unless @account
140
+ "accounts/#{@account}"
141
+ end
142
+
143
+ # @return [String] Format to return NewRelic data; valid options:
144
+ # * json
145
+ # * xml
146
+ # * csv
147
+ def format
148
+ @format||"json"
149
+ end
150
+
151
+ # @return [Hash] Parameters to be passed to NewRelic API.
152
+ #
153
+ # == Parameters:
154
+ # * "metrics" -- [Array] (see NewRelic API)
155
+ # * "field" -- [String] (see NewRelic API)
156
+ # * "begin" -- [String] (format: YYYY-MM-DDTHH:MM:SSZ)
157
+ # * "end" -- [String] (format: YYYY-MM-DDTHH:MM:SSZ)
158
+ #
159
+ # == Note:
160
+ # 'begin' and 'end' can be set using X.days_ago or helpers {#today} or {#yesterday}
161
+ def query_params
162
+ @query_params||{ "metrics" => [ "Apdex" ], "field" => "score", "begin" => 1.day_ago, "end" => self.today, "summary" => 1 }
163
+ end
164
+
165
+ def query_params=(q)
166
+ @query_params = q
167
+ @query_params["app_id"] = @application if @application and !@query_params.has_key?("app_id")
168
+ end
169
+
170
+ # @return [String] {#query_params} as a URI query string.
171
+ def query_string
172
+ q = []
173
+ self.query_params.each do |key,val|
174
+ if val.kind_of? Array
175
+ val.each do |v|
176
+ q.push "#{key}[]=#{v}"
177
+ end
178
+ else
179
+ q.push "#{key}=#{val}"
180
+ end
181
+ end
182
+ URI.escape(q.join("&"))
183
+ end
184
+
185
+ # @return [String] Today formated for 'begin' or 'end' {#query_params}.
186
+ def today
187
+ 0.days_ago
188
+ end
189
+
190
+ # @return [String] Yesterday formated for 'begin' or 'end' {#query_params}.
191
+ def yesterday
192
+ 1.day_ago
193
+ end
194
+
195
+ end
196
+
197
+ # Monkey patch of the [Integer] object for quickly generating
198
+ # {NewRelicData#query_params} options 'begin' and 'end'.
199
+ class Integer
200
+ # @return [String] formated for {NewRelicData#query_params} 'begin' and 'end'.
201
+ # * N.days_ago at '00:00:00'
202
+ def days_ago
203
+ (DateTime.now-self).strftime("%Y-%m-%dT00:00:00Z")
204
+ end
205
+ alias :day_ago :days_ago
206
+ end
207
+
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: newrelic_data
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Joshua Mervine
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-06 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &16055980 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *16055980
25
+ - !ruby/object:Gem::Dependency
26
+ name: simplecov
27
+ requirement: &16054500 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *16054500
36
+ - !ruby/object:Gem::Dependency
37
+ name: yard
38
+ requirement: &16053580 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *16053580
47
+ - !ruby/object:Gem::Dependency
48
+ name: redcarpet
49
+ requirement: &16052620 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *16052620
58
+ - !ruby/object:Gem::Dependency
59
+ name: curb
60
+ requirement: &16051560 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :runtime
67
+ prerelease: false
68
+ version_requirements: *16051560
69
+ - !ruby/object:Gem::Dependency
70
+ name: xml-simple
71
+ requirement: &16050340 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: *16050340
80
+ description: Simple utility for access NewRelic data through their RESTful API.
81
+ email:
82
+ - joshua@mervine.net
83
+ executables: []
84
+ extensions: []
85
+ extra_rdoc_files: []
86
+ files:
87
+ - lib/newrelic_data.rb
88
+ - README.md
89
+ - HISTORY.md
90
+ - Gemfile
91
+ homepage: http://github.com/jmervine/newrelic_data
92
+ licenses: []
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ segments:
104
+ - 0
105
+ hash: -3303414572505684251
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: 1.3.6
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 1.7.2
115
+ signing_key:
116
+ specification_version: 3
117
+ summary: NewRelic Data access API
118
+ test_files: []
119
+ has_rdoc: