GreekFire
Prometheus metrics for rails.
Usage
How to use my plugin.
Installation
Add this line to your application's Gemfile:
gem 'greek_fire'
And then execute:
$ bundle
Or install it yourself as:
$ gem install greek_fire
Mount the engine
config/routes.rb:
mount GreekFire::Engine => '/metrics'
Add yer metrics
config/initializers/greek_fire.rb:
GreekFire::Gauge.register "a_generic_metric" do
GenericModel.where('created_at > ?', Time.zone.now.advance(minutes: -1)).count
end
GreekFire::Gauge.register "another_generic_metric"
description: "describes the metric",
labels: {:a_label: [1, 2], b_label: "foo"} do | labels |
AnotherModel.where('created_at > ?', Time.zone.now.advance(minutes: -1)).count
end
GreekFire::Gauge.register "sidekiq_job_latency",
description: "How long are jobs enqueued",
labels: { :queue => Proc.new { Sidekiq::Queue.all.map(&:name) }} do | labels |
Sidekiq::Queue.new(labels[:queue]).latency
end
GreekFire::Gauge.register "sidekiq_job_count",
description: "How many jobs are enqueued",
labels: { :queue => Proc.new { Sidekiq::Queue.all.map(&:name) }} do | labels |
Sidekiq::Queue.new(labels[:queue]).size
end
Metrics API
The api for creating metrics.
Currently only gauge and counter types of metrics are supported, but other metrics are a PR away!
Gauges
module GreekFire
class Gauge
def self.register(name, description:nil, labels:nil, &block)
...
end
end
end
To register a Gauge (point in time value) call the above method.
Counters
module GreekFire
class Counter
def self.register(name, description:nil, labels:nil, &block)
...
end
end
end
To register a Counter (point in time value) call the above method.
Labels
Labels are a hash of label_name to label_value(s). A label_value may be a collection of values for the labels, or a Proc that returns a collection of label values.
The cross product of all possible label values are used to call the given block.
For example, a gauge with the following labels:
{a_label: [1, 2], b_label: Proc.new { ["some", "list"] }, c_label: "c" }
would have it's block called one for each of the following hashes:
{a_label: 1, b_label: "some", c_label: "c" }
{a_label: 1, b_label: "list", c_label: "c" }
{a_label: 2, b_label: "some", c_label: "c" }
{a_label: 2, b_label: "list", c_label: "c" }
If the label_value is a Proc, the Proc will be called each time metrics are requested. This should allow you to export metrics across label dimentions that change as your app runs. In the installation example you can see that the sidekiq_job_count
and sidekiq_job_latency
gauges will have a metrics produced for each queue the app is using.
Contributing
- Fork
- Write tests.
- Implement new feature.
- Create a PR.
License
The gem is available as open source under the terms of the MIT License.