Class: Librato::Metrics::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/librato/metrics/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



9
10
11
# File 'lib/librato/metrics/client.rb', line 9

def api_key
  @api_key
end

#emailObject

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

Examples:

Have the gem build your identifier string

Librato::Metrics.agent_identifier 'flintstone', '0.5', 'fred'

Provide your own identifier string

Librato::Metrics.agent_identifier 'flintstone/0.5 (dev_id:fred)'

Remove identifier string

Librato::Metrics.agent_identifier ''


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

#annotatorObject



33
34
35
# File 'lib/librato/metrics/client.rb', line 33

def annotator
  @annotator ||= Annotator.new(:client => self)
end

#api_endpointString

API endpoint to use for queries and direct persistence.

Returns:

  • (String)

    api_endpoint



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

Parameters:

  • email (String)
  • api_key (String)


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

#connectionObject

Current connection object

Raises:



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

#custom_user_agentObject



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

Completely delete metrics with the given names. Be careful with this, this is instant and permanent.

Examples:

Delete metric ‘temperature’

Librato::Metrics.delete :temperature

Delete metrics ‘foo’ and ‘bar’

Librato::Metrics.delete :foo, :bar

Raises:



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/librato/metrics/client.rb', line 93

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 = SmartJSON.write(params)
  end
  # expects 204, middleware will raise exception
  # otherwise.
  true
end

#faraday_adapterObject

Return current adapter this client will use. Defaults to Metrics.faraday_adapter if set, otherwise Faraday.default_adapter



109
110
111
# File 'lib/librato/metrics/client.rb', line 109

def faraday_adapter
  @faraday_adapter ||= default_faraday_adapter
end

#faraday_adapter=(adapter) ⇒ Object

Set faraday adapter this client will use



114
115
116
# File 'lib/librato/metrics/client.rb', line 114

def faraday_adapter=(adapter)
  @faraday_adapter = adapter
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

Examples:

Get attributes for a metric

attrs = Librato::Metrics.fetch :temperature

Get 20 most recent data points for metric

data = Librato::Metrics.fetch :temperature, :count => 20

Get 20 most recent data points for a specific source

data = Librato::Metrics.fetch :temperature, :count => 20,
                              :source => 'app1'

Get the 20 most recent 15 minute data point rollups

data = Librato::Metrics.fetch :temperature, :count => 20,
                              :resolution => 900

Get data points for the last hour

data = Librato::Metrics.fetch :start_time => Time.now-3600

Get 15 min data points from two hours to an hour ago

data = Librato::Metrics.fetch :start_time => Time.now-7200,
                              :end_time => Time.now-3600,
                              :resolution => 900

Parameters:

  • metric (Symbol|String)

    Metric name

  • options (Hash) (defaults to: {})

    Query options



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/librato/metrics/client.rb', line 147

def fetch(metric, options={})
  query = options.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 = SmartJSON.read(response.body)
  # TODO: pagination support
  query.empty? ? parsed : parsed["measurements"]
end

#flush_authenticationObject

Purge current credentials and connection.



168
169
170
171
172
# File 'lib/librato/metrics/client.rb', line 168

def flush_authentication
  self.email = nil
  self.api_key = nil
  @connection = nil
end

#list(options = {}) ⇒ Object

List currently existing metrics

Examples:

List all metrics

Librato::Metrics.list

List metrics with ‘foo’ in the name

Librato::Metrics.list :name => 'foo'

Parameters:

  • options (Hash) (defaults to: {})


183
184
185
186
187
188
189
# File 'lib/librato/metrics/client.rb', line 183

def list(options={})
  query = {}
  query[:name] = options[:name] if options[:name]
  offset = 0
  path = "metrics"
  Collection.paginated_metrics(connection, path, query)
end

#new_queue(options = {}) ⇒ Queue

Create a new queue which uses this client.

Returns:



194
195
196
197
# File 'lib/librato/metrics/client.rb', line 194

def new_queue(options={})
  options[:client] = self
  Queue.new(options)
end

#persistenceSymbol

Persistence type to use when saving metrics. Default is :direct.

Returns:

  • (Symbol)


203
204
205
# File 'lib/librato/metrics/client.rb', line 203

def persistence
  @persistence ||= :direct
end

#persistence=(persist_method) ⇒ Object

Set persistence type to use when saving metrics.

Parameters:

  • persist_method (Symbol)


210
211
212
# File 'lib/librato/metrics/client.rb', line 210

def persistence=(persist_method)
  @persistence = persist_method
end

#persisterObject

Current persister object.



215
216
217
# File 'lib/librato/metrics/client.rb', line 215

def persister
  @queue ? @queue.persister : nil
end

#submit(args) ⇒ Object

Submit all queued metrics.



221
222
223
224
225
226
227
# File 'lib/librato/metrics/client.rb', line 221

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.

Examples:

Update metric ‘temperature’

Librato::Metrics.update :temperature, :period => 15, :attributes => { :color => 'F00' }

Update metric ‘humidity’, creating it if it doesn’t exist

Librato::Metrics.update 'humidity', :type => :gauge, :period => 60, :display_name => 'Humidity'


237
238
239
240
241
242
# File 'lib/librato/metrics/client.rb', line 237

def update(metric, options = {})
  connection.put do |request|
    request.url connection.build_url("metrics/#{metric}")
    request.body = SmartJSON.write(options)
  end
end