Class: Librato::Metrics::Client

Inherits:
Object
  • Object
show all
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.



5
6
7
# File 'lib/librato/metrics/client.rb', line 5

def api_key
  @api_key
end

#emailObject

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

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 ''


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_endpointString

API endpoint to use for queries and direct persistence.

Returns:

  • (String)

    api_endpoint



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

Parameters:

  • email (String)
  • api_key (String)


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

#connectionObject

Current connection object

Raises:



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

#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



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

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 = MultiJson.decode(response.body)
  # TODO: pagination support
  query.empty? ? parsed : parsed["measurements"]
end

#flush_authenticationObject

Purge current credentials and connection.



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

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: {})


128
129
130
131
132
133
134
# File 'lib/librato/metrics/client.rb', line 128

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

#new_queueQueue

Create a new queue which uses this client.

Returns:



139
140
141
# File 'lib/librato/metrics/client.rb', line 139

def new_queue
  Queue.new(:client => self)
end

#persistenceSymbol

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

Returns:

  • (Symbol)


147
148
149
# File 'lib/librato/metrics/client.rb', line 147

def persistence
  @persistence ||= :direct
end

#persistence=(persist_method) ⇒ Object

Set persistence type to use when saving metrics.

Parameters:

  • persistence_type (Symbol)


154
155
156
# File 'lib/librato/metrics/client.rb', line 154

def persistence=(persist_method)
  @persistence = persist_method
end

#persisterObject

Current persister object.



159
160
161
# File 'lib/librato/metrics/client.rb', line 159

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

#submit(args) ⇒ Object

Submit all queued metrics.



165
166
167
168
169
# File 'lib/librato/metrics/client.rb', line 165

def submit(args)
  @queue ||= Queue.new(:client => self, :skip_measurement_times => true)
  @queue.add args
  @queue.submit
end