Class: Librato::Metrics::Client
- Inherits:
-
Object
- Object
- Librato::Metrics::Client
- Extended by:
- Forwardable
- Defined in:
- lib/librato/metrics/client.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#email ⇒ Object
Returns the value of attribute email.
Instance Method Summary collapse
-
#agent_identifier(*args) ⇒ Object
Provide agent identifier for the developer program.
- #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(email, api_key) ⇒ Object
Authenticate for direct persistence.
-
#connection ⇒ Object
Current connection object.
-
#create_snapshot(options = {}) ⇒ Object
Create a snapshot of an instrument.
- #custom_user_agent ⇒ Object
-
#custom_user_agent=(agent) ⇒ Object
Overrride user agent for this client’s connections.
-
#delete(*metric_names) ⇒ Object
deprecated
Deprecated.
Use #delete_metrics instead
-
#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.
-
#fetch(metric, options = {}) ⇒ Object
deprecated
Deprecated.
Use #get_metric or #get_measurements instead.
-
#flush_authentication ⇒ Object
Purge current credentials and connection.
-
#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_snapshot(id) ⇒ Object
Retrive a snapshot, to check its progress or find its image_href.
-
#get_source(name) ⇒ Object
Retrieve a single source by name.
-
#list(options = {}) ⇒ Object
deprecated
Deprecated.
Use #metrics instead
-
#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.
-
#sources(filter = {}) ⇒ Object
List sources, optionally limited by a name.
-
#submit(args) ⇒ Object
Submit all queued metrics.
-
#update_metric(metric, options = {}) ⇒ Object
(also: #update)
Update a single metric with new attributes.
-
#update_metrics(metrics) ⇒ Object
Update multiple metrics.
-
#update_source(name, options = {}) ⇒ Object
Update a source by name.
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
9 10 11 |
# File 'lib/librato/metrics/client.rb', line 9 def api_key @api_key end |
#email ⇒ Object
Returns the value of attribute email.
9 10 11 |
# File 'lib/librato/metrics/client.rb', line 9 def email @email end |
Instance Method Details
#agent_identifier(*args) ⇒ Object
Provide agent identifier for the developer program. See: support.metrics.librato.com/knowledgebase/articles/53548-developer-program
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/librato/metrics/client.rb', line 22 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
33 34 35 |
# File 'lib/librato/metrics/client.rb', line 33 def annotator @annotator ||= Annotator.new(:client => self) end |
#api_endpoint ⇒ String
API endpoint to use for queries and direct persistence.
41 42 43 |
# File 'lib/librato/metrics/client.rb', line 41 def api_endpoint @api_endpoint ||= 'https://metrics-api.librato.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 Librato Metrics endpoint.
50 51 52 |
# File 'lib/librato/metrics/client.rb', line 50 def api_endpoint=(endpoint) @api_endpoint = endpoint end |
#authenticate(email, api_key) ⇒ Object
Authenticate for direct persistence
58 59 60 61 |
# File 'lib/librato/metrics/client.rb', line 58 def authenticate(email, api_key) flush_authentication self.email, self.api_key = email, api_key end |
#connection ⇒ Object
Current connection object
65 66 67 68 69 70 |
# File 'lib/librato/metrics/client.rb', line 65 def connection # prevent successful creation if no credentials set raise CredentialsMissing unless (self.email and self.api_key) @connection ||= Connection.new(:client => self, :api_endpoint => api_endpoint, :adapter => faraday_adapter) end |
#create_snapshot(options = {}) ⇒ Object
Create a snapshot of an instrument
401 402 403 404 405 406 407 408 |
# File 'lib/librato/metrics/client.rb', line 401 def create_snapshot( = {}) url = "snapshots" response = connection.post do |request| request.url connection.build_url(url) request.body = SmartJSON.write() end parsed = SmartJSON.read(response.body) end |
#custom_user_agent ⇒ Object
81 82 83 |
# File 'lib/librato/metrics/client.rb', line 81 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.
76 77 78 79 |
# File 'lib/librato/metrics/client.rb', line 76 def custom_user_agent=(agent) @user_agent = agent @connection = nil end |
#delete(*metric_names) ⇒ Object
Use #delete_metrics instead
Completely delete metrics with the given names. Be careful with this, this is instant and permanent.
116 |
# File 'lib/librato/metrics/client.rb', line 116 def delete(*metric_names); delete_metrics(*metric_names); end |
#delete_metrics(*metric_names) ⇒ Object
Completely delete metrics with the given names. Be careful with this, this is instant and permanent.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/librato/metrics/client.rb', line 97 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
121 122 123 |
# File 'lib/librato/metrics/client.rb', line 121 def faraday_adapter @faraday_adapter ||= default_faraday_adapter end |
#faraday_adapter=(adapter) ⇒ Object
Set faraday adapter this client will use
126 127 128 |
# File 'lib/librato/metrics/client.rb', line 126 def faraday_adapter=(adapter) @faraday_adapter = adapter end |
#fetch(metric, options = {}) ⇒ Object
Use #get_metric or #get_measurements instead.
Query metric data
A full list of query parameters can be found in the API documentation: http://dev.librato.com/v1/get/metrics/:name
161 162 163 164 |
# File 'lib/librato/metrics/client.rb', line 161 def fetch(metric, ={}) metric = get_metric(metric, ) .empty? ? metric : metric["measurements"] end |
#flush_authentication ⇒ Object
Purge current credentials and connection.
232 233 234 235 236 |
# File 'lib/librato/metrics/client.rb', line 232 def flush_authentication self.email = nil self.api_key = nil @connection = nil 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://dev.librato.com/v1/get/metrics/:name
225 226 227 228 |
# File 'lib/librato/metrics/client.rb', line 225 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://dev.librato.com/v1/get/metrics/:name
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/librato/metrics/client.rb', line 180 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_snapshot(id) ⇒ Object
Retrive a snapshot, to check its progress or find its image_href
416 417 418 419 420 |
# File 'lib/librato/metrics/client.rb', line 416 def get_snapshot(id) url = "snapshots/#{id}" response = connection.get(url) parsed = SmartJSON.read(response.body) end |
#get_source(name) ⇒ Object
Retrieve a single source by name. See dev.librato.com/v1/get/sources/:name
369 370 371 372 373 |
# File 'lib/librato/metrics/client.rb', line 369 def get_source(name) url = connection.build_url("sources/#{name}") response = connection.get(url) parsed = SmartJSON.read(response.body) end |
#list(options = {}) ⇒ Object
Use #metrics instead
List currently existing metrics
258 |
# File 'lib/librato/metrics/client.rb', line 258 def list(={}); metrics(); end |
#metrics(options = {}) ⇒ Object
List currently existing metrics
247 248 249 250 251 252 253 |
# File 'lib/librato/metrics/client.rb', line 247 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.
263 264 265 266 |
# File 'lib/librato/metrics/client.rb', line 263 def new_queue(={}) [:client] = self Queue.new() end |
#persistence ⇒ Symbol
Persistence type to use when saving metrics. Default is :direct.
272 273 274 |
# File 'lib/librato/metrics/client.rb', line 272 def persistence @persistence ||= :direct end |
#persistence=(persist_method) ⇒ Object
Set persistence type to use when saving metrics.
279 280 281 |
# File 'lib/librato/metrics/client.rb', line 279 def persistence=(persist_method) @persistence = persist_method end |
#persister ⇒ Object
Current persister object.
284 285 286 |
# File 'lib/librato/metrics/client.rb', line 284 def persister @queue ? @queue.persister : nil end |
#sources(filter = {}) ⇒ Object
List sources, optionally limited by a name. See dev.librato.com/v1/sources and dev.librato.com/v1/get/sources
357 358 359 360 361 |
# File 'lib/librato/metrics/client.rb', line 357 def sources(filter = {}) query = filter[:name] if filter.has_key?(:name) path = "sources" Collection.paginated_collection("sources", connection, path, query) end |
#submit(args) ⇒ Object
Submit all queued metrics.
290 291 292 293 294 295 296 |
# File 'lib/librato/metrics/client.rb', line 290 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 Also known as: update
Update a single metric with new attributes.
306 307 308 309 310 311 312 |
# File 'lib/librato/metrics/client.rb', line 306 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.
322 323 324 325 326 327 328 |
# File 'lib/librato/metrics/client.rb', line 322 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 |
#update_source(name, options = {}) ⇒ Object
Update a source by name. See dev.librato.com/v1/get/sources/:name
382 383 384 385 386 387 388 |
# File 'lib/librato/metrics/client.rb', line 382 def update_source(name, = {}) url = "sources/#{name}" connection.put do |request| request.url connection.build_url(url) request.body = SmartJSON.write() end end |