No commit activity in last 3 years
No release in over 3 years
A ruby library for collecting prometheus metrics within forking servers
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 1.15
>= 12.3.3
~> 3.9

Runtime

 Project Readme

prom_multi_proc_rb

Build Status Gem Version

Ruby client library for collecting prometheus metrics. Designed for use in applications running under forking servers (unicorn, puma). Writes metrics in json format to unix socket being listened to by prom_multi_proc.

Installation

Add this line to your application's Gemfile:

gem 'prom_multi_proc_rb', require: 'prom_multi_proc'

And then execute:

$ bundle

Or install it yourself as:

$ gem install prom_multi_proc_rb

General Usage

Define metrics

Create a json file to define the prometheus metrics that your application will track, for example:

[
    {
        "type": "counter",
        "name": "app_test_counter_total",
        "help": "A test counter",
        "labels": [
            "label1",
        ]
    },
    {
        "type": "gauge",
        "name": "app_test_gauge_total",
        "help": "A test gauge",
        "labels": [
            "label2",
        ]
    },
    {
        "type": "histogram",
        "name": "app_test_histogram_seconds",
        "help": "A test histogram",
        "labels": [
            "label3",
        ]
    },
    {
        "type": "summary",
        "name": "app_test_summary_seconds",
        "help": "A test summary",
        "labels": [
            "label4",
        ]
    }
]

This file is intened to be shared by both the aggregator process and this ruby library.

Install and start the aggregator process

Download, install, and start the prom_multi_proc aggregator application using the metrics json definition file created earlier. Make note of the socket location.

Note that in development, the ruby client will funtion normally if there is no aggregator process listening on the socket.

Collect metrics in ruby app

Create a PromMultiProc::Base object for collecting metrics and begin collecting metrics:

metrics = PromMultiProc::Base.new(
  prefix: "app_",
  socket: "path/to/aggregator/socket.sock",
  metrics: "path/to/metrics/definition.json",
  batch_size: 10,
  validate: true
)

metrics.test_counter_total.inc(label1: "my-label-value")
metrics.test_histogram_seconds.observe(2.3, label3: "my-other-label-value")

Rails Usage

This helper class can be used to simplify a rails initializer, for example, in config/initializers/prom_multi_proc.rb:

$prom_multi_proc = PromMultiProc::Rails.init

$prom_multi_proc.test_counter_total.inc(label1: "my-label-value")
$prom_multi_proc.test_histogram_seconds.observe(2.3, label3: "my-other-label-value")

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/atongen/prom_multi_proc_rb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the prom_multi_proc_rb project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.