Provides Prometheus metrics collectors and integrations for Sequel, Rack, S3, Roda and etc.
- Installation
- Rack Metrics
- Sequel Metrics
- AWS Client Metrics
- Ruby Method Metrics
- Endpoint Name Roda Plugin
- Management Server Puma plugin
- Ruby VM & GC Metrics
- System Process Metrics
Add this line to your application’s Gemfile:
gem 'bm-instrumentations'And then execute:
$ bundleOr install it yourself as:
$ gem install bm-instrumentationsBM::Instrumentations::Rack is a Rack middleware that collect metrics for HTTP request and
responses.
# config.ru
require 'bm/instrumentations'
use BM::Instrumentations::Rack, exclude_path: %w[/metrics /ping]The middleware has some optional parameters:
| Parameter | Type | Description |
|---|---|---|
exclude_path |
StringArray<String> |
a list of ignored path names, for that paths the metrics won’t be record |
registry |
Prometheus::Client::Registry |
override the default Prometheus registry |
| Metrics | Type | Labels | Description |
|---|---|---|---|
http_server_requests_total |
counter | methodpathstatusstatus_code |
the total number of HTTP requests handled by the Rack application |
http_server_exceptions_total |
counter | methodpathexception |
the total number of uncaught exceptions raised by the Rack application |
http_server_request_duration_seconds |
histogram | methodpathstatusstatus_code |
the HTTP response times in seconds of the Rack application |
methodis a HTTP method name such asGETorPOSTpathis an endpoint’s path that handled a request,noneif undefinedstatusis a cumulative value of a HTTP status code like2xxor5xxstatus_codeis a HTTP status code from response such as200or500exceptionis an uncaught exception class name such asRuntimeErrororErrno::ENOENT
Sequel::Extensions::PrometheusInstrumentation is a Sequel extension that instrument a database queries and write
metrics into Prometheus.
# Apply an extension
db = Sequel.connect(database_url)
db.extension(:prometheus_instrumentation)| Metrics | Type | Labels | Description |
|---|---|---|---|
sequel_queries_total |
counter | databasequerystatus |
how many Sequel queries processed, partitioned by status |
sequel_query_duration_seconds |
histogram | databasestatusstatus |
the duration in seconds that a Sequel queries spent |
databaseis a database name that a connection connectedqueryis a query statement name such asselect,updatestatusone ofsuccessorfailure
BM::Instrumentations::Aws.plugin is an AWS client plugin that instrument API calls and write metrics into
Prometheus.
require 'bm/instrumentations'
# Apply a plugin
Aws::S3::Client.add_plugin(BM::Instrumentations::Aws.plugin)
s3_client = Aws::S3::Client.new(options)| Metrics | Type | Labels | Description |
|---|---|---|---|
aws_sdk_client_requests_total |
counter | serviceapistatus |
the total number of successful or failed API calls to AWS services |
aws_sdk_client_request_duration_seconds |
histogram | serviceapistatus |
the total time in seconds for the AWS Client to make a call to AWS services |
aws_sdk_client_retries_total |
counter | serviceapi |
the total number retries of failed API calls from AWS client to AWS services |
aws_sdk_client_exceptions_total |
counter | serviceapiexception |
the total number of AWS API calls that fail |
serviceis an AWS service name such asS3apiis an AWS api method method such asListBucketsorGetObjectstatusis an HTTP status code returned by API call such as200or500exceptionis an exception class name such asSeahorse::Client::NetworkingError
BM::Instrumentations::Timings is an observer that watch on specified ruby method and write metrics about the method
invocations.
require 'bm/instrumentations'
class QueryUsers
include BM::Instrumentations::Timings[:user_queries] # (1)
def query_one(params)
# ... any ruby code to instrument ...
end
timings :query_one # (2)
def query_all(params)
# ... any ruby code to instrument ...
end
timings :query_all # (2)
end- Includes a module with the
user_queriesmetrics prefix, so each metric will have theuser_queries_prefix - Attach to methods, each time when any observed method invokes a corresponding counter and a histogram will be updated
| Metrics | Type | Labels | Description |
|---|---|---|---|
<metrics_prefix>_calls_total |
counter | classmethodstatus |
the total number of of successful or failed calls by ruby's method |
<metrics_prefix>_call_duration_seconds |
histogram | classmethodstatus |
the time in seconds which spent at ruby's method calls |
classis a ruby class where the module includedmethodis an observed ruby's method namestatusis one ofsuccessorfailure
The endpoint plugin adds an endpoint name to the Rack's request env.
Roda lacks of "controller and action" abstractions, so it cannot be obtain a some useful
information about who was handled a request. This plugin fixes the issue by exporting
an endpoint name (a function which handled a request) to the Rack's request env as a
x.rack.endpoint key.
This plugin is useful with BM::Instrumentations::Rack::Collector. When applied the rack
collector could be able to determine which function handled a request and correctly writes a
path label to metrics.
# Apply a plugin
class API < Roda
plugin(:endpoint) # (1)
endpoint def pong # (2)
'Pong'
end
route do |r|
r.get('ping') { pong }
end
end- Include a plugin, after included Roda has a class level method
endpoint - Use the
endpointto mark a specified method as a function that may handle a request. When a function will be invoked a keyx.rack.endpointwith a valuepongwill be exported into Rack env.
Requires: puma > 5.4.0
The management_server plugin provides monitoring and metrics on different HTTP port, it starts a separated
Puma::Server that serves requests.
The plugin exposes few endpoints
/ping- a liveness probe, always returnHTTP 200 OKwhen the server is running/metrics- metrics list from the current Prometheus registry/gc-status- print ruby GC statistics as JSON/threads- print running threads, names and backtraces as JSON
By default the server is running on 0.0.0.0:9990, the default configuration values could be override in puma
configuration file.
# config/puma.rb
plugin(:management_server)
# or override default configuration
plugin(:management_server)
management_server(host: '127.0.0.1', port: 9000, logger: Logger.new(IO::NULL))| Metrics | Type | Labels | Description |
|---|---|---|---|
puma_thread_pool_max_size |
gauge | - | The preconfigured maximum number of worker threads in the Puma server |
puma_thread_pool_size |
gauge | - | The number of spawned worker threads in the Puma server |
puma_thread_pool_active_size |
gauge | - | The number of worker threads that actively executing requests in the Puma server |
puma_thread_pool_queue_size |
gauge | - | The number of queued requests that waiting execution in the Puma server |
puma_server_socket_backlog_size |
gauge | listener |
Linux only The current size of the pending connection queue of the Puma listener |
puma_server_socket_backlog_max_size |
gauge | listener |
Linux only The preconfigured maximum size of the pending connections queue of the Puma listener |
BM::Instrumentation::RubyVM is a custom metrics collector that captures ruby's VM and GC stats. Due to the official
prometheus client for ruby isn't yet support that types of collectors, so the collectors only works
with BM::Instrumentations::Management::Server together.
require 'bm/instrumentations'
# It installs a custom collector to the default prometheus registry
BM::Instrumentations::RubyVM.install
# Or if you don't want to activate GC::Profiler
BM::Instrumentations::RubyVM.install(enable_gc_profiler: false)| Metrics | Type | Labels | Description |
|---|---|---|---|
ruby_version |
gauge | rubyversion |
The current ruby engine name and version |
ruby_gc_time_seconds |
summary | - | The total time that Ruby GC spends for garbage collection in seconds |
ruby_gc_heap_slots_size |
gauge | slots |
The size of available heap slots of Ruby GC partitioned by slots type (free or live) |
ruby_gc_allocated_objects_total |
gauge | - | The total number of allocated objects by Ruby GC |
ruby_gc_freed_objects_total |
gauge | - | The total number of freed objects by Ruby GC |
ruby_gc_counts_total |
gauge | counts |
The total number of Ruby GC counts partitioned by counts type (minor or major) |
ruby_vm_global_cache_state |
gauge | cache |
The Ruby VM global cache state (version) for methods and constants, partitioned by cache type |
ruby_threads_count |
gauge | - | The number of running threads |
BM::Instrumentations::Process is a custom metrics collector that captures process' RSS memory and the number of open
files. Due to the official prometheus client for ruby isn't yet support that types of collectors, so the collectors only
works with BM::Instrumentations::Management::Server together.
require 'bm/instrumentations'
# It installs a custom collector to the default prometheus registry
BM::Instrumentations::Process.install| Metrics | Type | Labels | Description |
|---|---|---|---|
process_rss_memory_bytes_count |
gauge | - | The number of bytes is allocated to this process |
process_open_fds_count |
gauge | - | The number of open files by this process |
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the Bm::Instrumentations project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.