Project

mettric

0.0
No commit activity in last 3 years
No release in over 3 years
Mittwoch ist Metttag
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.12
>= 0
~> 10.0
~> 3.0

Runtime

 Project Readme

Mettric

Hector Salamanca

Tracks events, meter readings and timings to riemann.io in a background thread using riemann-ruby-client. Fire and forget.

Mettric will also prepend your app's name to the services you track with riemann and set the reporting host and tags automatically.

Getting started

gem install mettric

Configuration

You can configure Mettric like this:

Mettric.config = {
  host: 'my-riemann-server',
  port: 5555,

  reporting_host: 'override', # Defaults to system hostname
  app: 'my_app',              # Defaults to rails app (if in rails)
  poll_intervall: 100,        # Defaults to 50 (ms)
  sidekiq_middleware: false   # Defaults to true
}

Upon configuration, Mettric will install a sidekiq middleware that automatically tracks the success and error rates of your workers as well as their duration (see below for details)

Track events

# Tracks payload with metric=1 and tags it 'event'
🛎 service: 'users.created'

# Will send the following payload via riemann-ruby-client:
#
# {
#    host: 'override',
#    service: 'my_app.users.created',
#    metric: 1,
#    tags: ['event']
# }

Track meter readings

🌡 service: 'Rails req/s', metric: 400, state: 'normal'

# Will send the following payload via riemann-ruby-client:
#
# {
#    host: 'override',
#    service: 'my_app.Rails req/s',
#    metric: 400,
#    state: 'normal'
# }

Time things

 service: 'test.sleep', tags: [:tired] do
  sleep 1
end

Above snippet will send the following payloads via riemann-ruby-client:

[
  {
     "host": "override",
     "service": "my_app.test.slept.duration",
     "metric": 1000,
     "description": "(ms)",
     "tags": ["timing", "tired"]
  },
  {
     "host": "override",
     "service": "my_app.test.success",
     "metric": 1,
     "description": "(ms)",
     "tags": ["event", "tired"]
  }
]

Exceptions in your code are also handled:

 service: 'test.sleep', tags: [:tired] do
  sleep 1
  raise "My ambition is handicapped by my laziness"
end

Above snippet will send the following payloads (and then raise your exception):

[
  {
     "host": "override",
     "service": "my_app.test.sleep.duration",
     "metric": 1000,
     "description": "(ms)",
     "tags": ["timing", "tired"]
  },
  {
     "host": "override",
     "service": "my_app.test.sleep.failure",
     "metric": 1,
     "description": "My ambition is handicapped by my laziness",
     "tags": ["event", "tired"]
  }

For grown ups

Just in case you don't want to use the silly methods names, please know that the emoji variants of above methods just call Mettric.event(payload), Mettric.meter(payload and Mettric.time(payload) { ... } respectively. Be aware, though, that the emoji methods ignore a misconfigured or unavailable riemann server by rescuing Mettric::CouldNotStartWorkerThread exceptions. The non-emoji methods will throw that exception. Just have a look at lib/mettric/scnr.rb and you'll see what I mean.

Sidekiq

Let's assume you have sidekiq worker class called MyWorker that uses the my_queue queue. Each time it runs, Mettric will automatically track how long the worker took by sending the following payload to Riemann (assuming the worker took 80ms):

{
  host: 'override',
  service: 'my_app.sidekiq.my_queue.my_worker.duration',
  metric: 80,
  tags: ['sidekiq']
}

Also, it will track the success

{
  host: 'override',
  service: 'my_app.sidekiq.my_queue.my_worker.success',
  metric: 1,
  tags: ['event']
}

or error

{
  host: 'override',
  service: 'my_app.sidekiq.my_queue.my_worker.error',
  metric: 1,
  tags: ['event']
}

of the worker's execution. If you want a worker not to be tracked by mettric, you would write:

class MyWorker
  include Sidekiq::Worker
  sidekiq_options mettric: false

  def perform
    
  end
end