Class: AppOptics::Metrics::Client
- Inherits:
-
Object
- Object
- AppOptics::Metrics::Client
- Extended by:
- Forwardable
- Defined in:
- lib/appoptics/metrics/client.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#proxy ⇒ Object
Returns the value of attribute proxy.
Instance Method Summary collapse
- #agent_identifier(*args) ⇒ Object
- #annotator ⇒ Object
-
#api_endpoint ⇒ String
API endpoint to use for queries and direct persistence.
-
#api_endpoint=(endpoint) ⇒ Object
Set API endpoint for use with queries and direct persistence.
-
#authenticate(api_key) ⇒ Object
Authenticate for direct persistence.
-
#connection ⇒ Object
Current connection object.
- #custom_user_agent ⇒ Object
-
#custom_user_agent=(agent) ⇒ Object
Overrride user agent for this client’s connections.
-
#delete_metrics(*metric_names) ⇒ Object
Completely delete metrics with the given names.
-
#faraday_adapter ⇒ Object
Return current adapter this client will use.
-
#faraday_adapter=(adapter) ⇒ Object
Set faraday adapter this client will use.
-
#flush_authentication ⇒ Object
Purge current credentials and connection.
-
#get_composite(definition, options = {}) ⇒ Object
Retrieve measurements for a given composite metric definition.
-
#get_measurements(metric_name, options = {}) ⇒ Object
Retrieve data points for a specific metric.
-
#get_metric(name, options = {}) ⇒ Object
Retrieve a specific metric by name, optionally including data points.
-
#get_series(metric_name, options = {}) ⇒ Object
Retrieve series of measurements for a given metric.
-
#get_snapshot(id) ⇒ Object
Retrive a snapshot, to check its progress or find its image_href.
-
#metrics(options = {}) ⇒ Object
List currently existing metrics.
-
#new_queue(options = {}) ⇒ Queue
Create a new queue which uses this client.
-
#persistence ⇒ Symbol
Persistence type to use when saving metrics.
-
#persistence=(persist_method) ⇒ Object
Set persistence type to use when saving metrics.
-
#persister ⇒ Object
Current persister object.
-
#submit(args) ⇒ Object
Submit all queued metrics.
-
#update_metric(metric, options = {}) ⇒ Object
Update a single metric with new attributes.
-
#update_metrics(metrics) ⇒ Object
Update multiple metrics.
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
9 10 11 |
# File 'lib/appoptics/metrics/client.rb', line 9 def api_key @api_key end |
#proxy ⇒ Object
Returns the value of attribute proxy.
9 10 11 |
# File 'lib/appoptics/metrics/client.rb', line 9 def proxy @proxy end |
Instance Method Details
#agent_identifier(*args) ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/appoptics/metrics/client.rb', line 19 def agent_identifier(*args) if args.length == 1 @agent_identifier = args.first elsif args.length == 3 @agent_identifier = "#{args[0]}/#{args[1]} (dev_id:#{args[2]})" elsif ![0,1,3].include?(args.length) raise ArgumentError, 'invalid arguments, see method documentation' end @agent_identifier ||= '' end |
#annotator ⇒ Object
30 31 32 |
# File 'lib/appoptics/metrics/client.rb', line 30 def annotator @annotator ||= Annotator.new(client: self) end |
#api_endpoint ⇒ String
API endpoint to use for queries and direct persistence.
38 39 40 |
# File 'lib/appoptics/metrics/client.rb', line 38 def api_endpoint @api_endpoint ||= 'https://api.appoptics.com' end |
#api_endpoint=(endpoint) ⇒ Object
Set API endpoint for use with queries and direct persistence. Generally you should not need to set this as it will default to the current Appoptics endpoint.
46 47 48 |
# File 'lib/appoptics/metrics/client.rb', line 46 def api_endpoint=(endpoint) @api_endpoint = endpoint end |
#authenticate(api_key) ⇒ Object
Authenticate for direct persistence
54 55 56 57 |
# File 'lib/appoptics/metrics/client.rb', line 54 def authenticate(api_key) flush_authentication self.api_key = api_key end |
#connection ⇒ Object
Current connection object
61 62 63 64 65 66 |
# File 'lib/appoptics/metrics/client.rb', line 61 def connection # prevent successful creation if no credentials set raise CredentialsMissing unless (self.api_key) @connection ||= Connection.new(client: self, api_endpoint: api_endpoint, adapter: faraday_adapter, proxy: self.proxy) end |
#custom_user_agent ⇒ Object
77 78 79 |
# File 'lib/appoptics/metrics/client.rb', line 77 def custom_user_agent @user_agent end |
#custom_user_agent=(agent) ⇒ Object
Overrride user agent for this client’s connections. If you are trying to specify an agent identifier for developer program, see #agent_identifier.
72 73 74 75 |
# File 'lib/appoptics/metrics/client.rb', line 72 def custom_user_agent=(agent) @user_agent = agent @connection = nil end |
#delete_metrics(*metric_names) ⇒ Object
Completely delete metrics with the given names. Be careful with this, this is instant and permanent.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/appoptics/metrics/client.rb', line 93 def delete_metrics(*metric_names) raise(NoMetricsProvided, 'Metric name missing.') if metric_names.empty? if metric_names[0].respond_to?(:keys) # hash form params = metric_names[0] else params = { names: metric_names.map(&:to_s) } end connection.delete do |request| request.url connection.build_url("metrics") request.body = SmartJSON.write(params) end # expects 204, middleware will raise exception otherwise. true end |
#faraday_adapter ⇒ Object
Return current adapter this client will use. Defaults to Metrics.faraday_adapter if set, otherwise Faraday.default_adapter
111 112 113 |
# File 'lib/appoptics/metrics/client.rb', line 111 def faraday_adapter @faraday_adapter ||= default_faraday_adapter end |
#faraday_adapter=(adapter) ⇒ Object
Set faraday adapter this client will use
116 117 118 |
# File 'lib/appoptics/metrics/client.rb', line 116 def faraday_adapter=(adapter) @faraday_adapter = adapter end |
#flush_authentication ⇒ Object
Purge current credentials and connection.
240 241 242 243 |
# File 'lib/appoptics/metrics/client.rb', line 240 def flush_authentication self.api_key = nil @connection = nil end |
#get_composite(definition, options = {}) ⇒ Object
Retrieve measurements for a given composite metric definition. :start_time and :resolution are required options, :end_time is optional.
131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/appoptics/metrics/client.rb', line 131 def get_composite(definition, ={}) unless [:start_time] && [:resolution] raise "You must provide a :start_time and :resolution" end query = .dup query[:compose] = definition url = connection.build_url("metrics", query) response = connection.get(url) parsed = SmartJSON.read(response.body) # TODO: pagination support parsed end |
#get_measurements(metric_name, options = {}) ⇒ Object
Retrieve data points for a specific metric
A full list of query parameters can be found in the API documentation: http://docs.appoptics.com/api/#retrieve-a-metric
233 234 235 236 |
# File 'lib/appoptics/metrics/client.rb', line 233 def get_measurements(metric_name, = {}) raise ArgumentError, "you must provide at least a :start_time or :count" if .empty? get_metric(metric_name, )["measurements"] end |
#get_metric(name, options = {}) ⇒ Object
Retrieve a specific metric by name, optionally including data points
A full list of query parameters can be found in the API documentation: http://docs.appoptics.com/api/#retrieve-a-metric
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/appoptics/metrics/client.rb', line 158 def get_metric(name, = {}) query = .dup if query[:start_time].respond_to?(:year) query[:start_time] = query[:start_time].to_i end if query[:end_time].respond_to?(:year) query[:end_time] = query[:end_time].to_i end unless query.empty? query[:resolution] ||= 1 end # expects 200 url = connection.build_url("metrics/#{name}", query) response = connection.get(url) parsed = SmartJSON.read(response.body) # TODO: pagination support parsed end |
#get_series(metric_name, options = {}) ⇒ Object
Retrieve series of measurements for a given metric
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/appoptics/metrics/client.rb', line 192 def get_series(metric_name, ={}) raise ArgumentError, ":resolution and :duration or :start_time must be set" if .empty? query = .dup if query[:start_time].respond_to?(:year) query[:start_time] = query[:start_time].to_i end if query[:end_time].respond_to?(:year) query[:end_time] = query[:end_time].to_i end query[:resolution] ||= 1 unless query[:start_time] || query[:end_time] query[:duration] ||= 3600 end url = connection.build_url("measurements/#{metric_name}", query) response = connection.get(url) parsed = SmartJSON.read(response.body) parsed["series"] end |
#get_snapshot(id) ⇒ Object
Retrive a snapshot, to check its progress or find its image_href
338 339 340 341 342 |
# File 'lib/appoptics/metrics/client.rb', line 338 def get_snapshot(id) url = "snapshots/#{id}" response = connection.get(url) parsed = SmartJSON.read(response.body) end |
#metrics(options = {}) ⇒ Object
List currently existing metrics
254 255 256 257 258 259 260 |
# File 'lib/appoptics/metrics/client.rb', line 254 def metrics(={}) query = {} query[:name] = [:name] if [:name] offset = 0 path = "metrics" Collection.paginated_metrics(connection, path, query) end |
#new_queue(options = {}) ⇒ Queue
Create a new queue which uses this client.
265 266 267 268 |
# File 'lib/appoptics/metrics/client.rb', line 265 def new_queue(={}) [:client] = self Queue.new() end |
#persistence ⇒ Symbol
Persistence type to use when saving metrics. Default is :direct.
274 275 276 |
# File 'lib/appoptics/metrics/client.rb', line 274 def persistence @persistence ||= :direct end |
#persistence=(persist_method) ⇒ Object
Set persistence type to use when saving metrics.
281 282 283 |
# File 'lib/appoptics/metrics/client.rb', line 281 def persistence=(persist_method) @persistence = persist_method end |
#persister ⇒ Object
Current persister object.
286 287 288 |
# File 'lib/appoptics/metrics/client.rb', line 286 def persister @queue ? @queue.persister : nil end |
#submit(args) ⇒ Object
Submit all queued metrics.
292 293 294 295 296 297 298 |
# File 'lib/appoptics/metrics/client.rb', line 292 def submit(args) @queue ||= Queue.new(client: self, skip_measurement_times: true, clear_failures: true) @queue.add args @queue.submit end |
#update_metric(metric, options = {}) ⇒ Object
Update a single metric with new attributes.
308 309 310 311 312 313 314 |
# File 'lib/appoptics/metrics/client.rb', line 308 def update_metric(metric, = {}) url = "metrics/#{metric}" connection.put do |request| request.url connection.build_url(url) request.body = SmartJSON.write() end end |
#update_metrics(metrics) ⇒ Object
Update multiple metrics.
324 325 326 327 328 329 330 |
# File 'lib/appoptics/metrics/client.rb', line 324 def update_metrics(metrics) url = "metrics" # update multiple metrics connection.put do |request| request.url connection.build_url(url) request.body = SmartJSON.write(metrics) end end |