Class: Librato::Metrics::Client
- Inherits:
-
Object
- Object
- Librato::Metrics::Client
- 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.
-
#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.
- #custom_user_agent ⇒ Object
-
#custom_user_agent=(agent) ⇒ Object
Overrride user agent for this client’s connections.
-
#delete(*metric_names) ⇒ Object
Completely delete metrics with the given names.
-
#fetch(metric, options = {}) ⇒ Object
Query metric data.
-
#flush_authentication ⇒ Object
Purge current credentials and connection.
-
#list(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, options = {}) ⇒ Object
Update metric with the given name.
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
5 6 7 |
# File 'lib/librato/metrics/client.rb', line 5 def api_key @api_key end |
#email ⇒ Object
Returns the value of attribute email.
5 6 7 |
# File 'lib/librato/metrics/client.rb', line 5 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
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/librato/metrics/client.rb', line 18 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 |
#api_endpoint ⇒ String
API endpoint to use for queries and direct persistence.
33 34 35 |
# File 'lib/librato/metrics/client.rb', line 33 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.
42 43 44 |
# File 'lib/librato/metrics/client.rb', line 42 def api_endpoint=(endpoint) @api_endpoint = endpoint end |
#authenticate(email, api_key) ⇒ Object
Authenticate for direct persistence
50 51 52 53 |
# File 'lib/librato/metrics/client.rb', line 50 def authenticate(email, api_key) flush_authentication self.email, self.api_key = email, api_key end |
#connection ⇒ Object
Current connection object
57 58 59 60 61 |
# File 'lib/librato/metrics/client.rb', line 57 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) end |
#custom_user_agent ⇒ Object
72 73 74 |
# File 'lib/librato/metrics/client.rb', line 72 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.
67 68 69 70 |
# File 'lib/librato/metrics/client.rb', line 67 def custom_user_agent=(agent) @user_agent = agent @connection = nil end |
#delete(*metric_names) ⇒ Object
Completely delete metrics with the given names. Be careful with this, this is instant and permanent.
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/librato/metrics/client.rb', line 84 def delete(*metric_names) raise(NoMetricsProvided, 'Metric name missing.') if metric_names.empty? metric_names.map!{|i| i.to_s} params = {:names => metric_names } connection.delete do |request| request.url connection.build_url("metrics") request.body = MultiJson.dump(params) end # expects 204, middleware will raise exception # otherwise. true end |
#fetch(metric, options = {}) ⇒ Object
Query metric data
A full list of query parameters can be found in the API documentation: http://dev.librato.com/v1/get/gauges/:name
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/librato/metrics/client.rb', line 126 def fetch(metric, ={}) 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/#{metric}", query) response = connection.get(url) parsed = MultiJson.load(response.body) # TODO: pagination support query.empty? ? parsed : parsed["measurements"] end |
#flush_authentication ⇒ Object
Purge current credentials and connection.
147 148 149 150 151 |
# File 'lib/librato/metrics/client.rb', line 147 def flush_authentication self.email = nil self.api_key = nil @connection = nil end |
#list(options = {}) ⇒ Object
List currently existing metrics
162 163 164 165 166 167 168 |
# File 'lib/librato/metrics/client.rb', line 162 def list(={}) 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.
173 174 175 176 |
# File 'lib/librato/metrics/client.rb', line 173 def new_queue(={}) [:client] = self Queue.new() end |
#persistence ⇒ Symbol
Persistence type to use when saving metrics. Default is :direct.
182 183 184 |
# File 'lib/librato/metrics/client.rb', line 182 def persistence @persistence ||= :direct end |
#persistence=(persist_method) ⇒ Object
Set persistence type to use when saving metrics.
189 190 191 |
# File 'lib/librato/metrics/client.rb', line 189 def persistence=(persist_method) @persistence = persist_method end |
#persister ⇒ Object
Current persister object.
194 195 196 |
# File 'lib/librato/metrics/client.rb', line 194 def persister @queue ? @queue.persister : nil end |
#submit(args) ⇒ Object
Submit all queued metrics.
200 201 202 203 204 205 206 |
# File 'lib/librato/metrics/client.rb', line 200 def submit(args) @queue ||= Queue.new(:client => self, :skip_measurement_times => true, :clear_failures => true) @queue.add args @queue.submit end |
#update(metric, options = {}) ⇒ Object
Update metric with the given name.
216 217 218 219 220 221 |
# File 'lib/librato/metrics/client.rb', line 216 def update(metric, = {}) connection.put do |request| request.url connection.build_url("metrics/#{metric}") request.body = MultiJson.dump() end end |