{"id":19312125,"url":"https://github.com/bigcommerce/bc-prometheus-ruby","last_synced_at":"2025-04-22T15:31:57.478Z","repository":{"id":35935979,"uuid":"186044111","full_name":"bigcommerce/bc-prometheus-ruby","owner":"bigcommerce","description":"Drop-in support for prometheus metrics for Ruby apps","archived":false,"fork":false,"pushed_at":"2024-04-19T15:30:13.000Z","size":126,"stargazers_count":1,"open_issues_count":1,"forks_count":9,"subscribers_count":32,"default_branch":"main","last_synced_at":"2024-10-31T13:56:16.081Z","etag":null,"topics":["gem","library","prometheus","ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bigcommerce.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2019-05-10T19:32:59.000Z","updated_at":"2023-04-13T01:57:00.000Z","dependencies_parsed_at":"2023-02-19T13:01:27.694Z","dependency_job_id":"b1eda047-96a3-4096-9d71-da4621c2d6a4","html_url":"https://github.com/bigcommerce/bc-prometheus-ruby","commit_stats":{"total_commits":76,"total_committers":6,"mean_commits":"12.666666666666666","dds":"0.10526315789473684","last_synced_commit":"799091dfebe8f74c5856b9d3c6ba7a2cd8afd092"},"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bigcommerce%2Fbc-prometheus-ruby","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bigcommerce%2Fbc-prometheus-ruby/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bigcommerce%2Fbc-prometheus-ruby/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bigcommerce%2Fbc-prometheus-ruby/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bigcommerce","download_url":"https://codeload.github.com/bigcommerce/bc-prometheus-ruby/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223900372,"owners_count":17222028,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gem","library","prometheus","ruby"],"created_at":"2024-11-10T00:32:56.659Z","updated_at":"2024-11-10T00:32:56.799Z","avatar_url":"https://github.com/bigcommerce.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# bc-prometheus-ruby - Drop-in Prometheus metrics\n\n[![CircleCI](https://circleci.com/gh/bigcommerce/bc-prometheus-ruby.svg?style=svg\u0026circle-token=fc3e2c4405a1f53a31e298f0ef981c2d0dfdee90)](https://circleci.com/gh/bigcommerce/bc-prometheus-ruby) [![Gem Version](https://badge.fury.io/rb/bc-prometheus-ruby.svg)](https://badge.fury.io/rb/bc-prometheus-ruby) [![Documentation](https://inch-ci.org/github/bigcommerce/bc-prometheus-ruby.svg?branch=main)](https://inch-ci.org/github/bigcommerce/bc-prometheus-ruby?branch=main) [![Maintainability](https://api.codeclimate.com/v1/badges/4a06277c738245e8bac1/maintainability)](https://codeclimate.com/github/bigcommerce/bc-prometheus-ruby/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/4a06277c738245e8bac1/test_coverage)](https://codeclimate.com/github/bigcommerce/bc-prometheus-ruby/test_coverage)\n\n## Installation\n\n```ruby\ngem 'bc-prometheus-ruby'\n```\n\nThen in your `application.rb`, prior to extending `Rails::Application` or any initializers:\n\n```ruby\nrequire 'bigcommerce/prometheus'\n```\n\nThen in your web server config file (e.g. `puma.rb`)\n\n```ruby\nbefore_fork do\n  Rails.application.config.before_fork_callbacks.each(\u0026:call)\nend\n```\n\nYou can then view your metrics at: http://0.0.0.0:9394/metrics\n\n## Puma\n\nFor extra Puma metrics, add this to `config/puma.rb`:\n\n```ruby\nafter_worker_fork do\n  Rails.application.config.after_fork_callbacks.each(\u0026:call)\nend\n```\n\n## Resque\n\nIn your `task 'resque:setup'` rake task, do: \n\n```ruby\nrequire 'bigcommerce/prometheus'\nBigcommerce::Prometheus::Instrumentors::Resque.new(app: Rails.application).start\n```\n\n## Configuration\n\nAfter requiring the main file, you can further configure with:\n\n| Option | Description | Default | Environment Variable |\n| ------ | ----------- | ------- | -------------------- |\n| client_custom_labels | A hash of custom labels to send with each client request | `{}` | None |\n| client_max_queue_size | The max amount of metrics to send before flushing | `10000` | `ENV['PROMETHEUS_CLIENT_MAX_QUEUE_SIZE']` |\n| client_thread_sleep | How often to sleep the worker thread that manages the client buffer (seconds) | `0.5` | `ENV['PROMETHEUS_CLIENT_THREAD_SLEEP']` |\n| puma_collection_frequency | How often to poll puma collection metrics (seconds) | `30` | `ENV['PROMETHEUS_PUMA_COLLECTION_FREQUENCY']` |\n| server_host | The host to run the exporter on | `\"0.0.0.0\"` | `ENV['PROMETHEUS_SERVER_HOST']` |\n| server_port | The port to run the exporter on | `9394` | `ENV['PROMETHEUS_SERVER_PORT']` |\n| server_thread_pool_size | The number of threads used for the exporter server | `3` | `ENV['PROMETHEUS_SERVER_THREAD_POOL_SIZE']` |\n| process_name | What the current process name is (used in logging) | `\"unknown\"` | `ENV['PROCESS']` |\n| railtie_disabled | Opt out flag for Railtie; use `Bigcommerce::Prometheus::Instrumentors::Web.new(app: Rails.application).start` in your app's code to start it up yourself  | `0` | `ENV['PROMETHEUS_DISABLE_RAILTIE']` |\n\n## Custom Collectors\n\nTo create custom metrics and collectors, simply create two files: a collector (the class that runs and collects metrics),\nand the type collector, which runs on the threaded prometheus server and \n\n### Type Collector\n\nFirst, create a type collector. Note that the \"type\" of this will be the full name of the class, with `TypeCollector`\nstripped. This is important later. Our example here will have a \"type\" of \"app\".\n\n```ruby\nclass AppTypeCollector \u003c ::Bigcommerce::Prometheus::TypeCollectors::Base\n  def build_metrics\n    {\n      honks: PrometheusExporter::Metric::Counter.new('honks', 'Running counter of honks'),\n      points: PrometheusExporter::Metric::Gauge.new('points', 'Current amount of points')\n    }\n  end\n\n  def collect_metrics(data:, labels: {})\n    metric(:points).observe(data.fetch('points', 0))\n    metric(:honks).observe(1, labels) if data.fetch('honks', 0).to_i.positive?\n  end\nend\n```\n\nThere are two important methods here: `build_metrics`, which registers the different metrics you want to measure, and\n`collect_metrics`, which actually takes in the metrics and prepares them to be rendered so that Prometheus can scrape\nthem.\n\nNote also in the example the different ways of observing Gauges vs Counters. \n\n### Collector\n\nNext, create a collector. Your \"type\" of the Collector must match the type collector above, so that bc-prometheus-ruby\nknows how to map the metrics to the right TypeCollector. This is inferred from the class name. Here, it is \"app\":\n\n```ruby\nclass AppCollector \u003c ::Bigcommerce::Prometheus::Collectors::Base\n  def honk!\n    push(\n      honks: 1,\n      custom_labels: {\n        volume: 'loud'\n      }\n    )\n  end\n\n  def collect(metrics)\n    metrics[:points] = rand(1..100)\n    metrics\n  end\nend\n```\n\nThere are two types of metrics here: on-demand, and polled. Let's look at the first:\n\n#### On-Demand Metrics\n\nTo issue an on-demand metric (usually a counter) that then automatically updates, in your application code, you would\nthen run:\n\n```ruby\napp_collector = AppCollector.new\napp_collector.honk!\n```\n\nThis will \"push\" the metrics to our `AppTypeCollector` instance, which will render them as:\n\n```\n# HELP ruby_honks Running counter of honks\n# TYPE ruby_honks counter\nruby_honks{volume=\"loud\"} 2\n```\n\nAs you can see this will respect any custom labels we push in as well.\n\n### Polling Metrics\n\nUsing our same AppCollector, if you note the `collect` method: this method will run on a 15 second polled basis\n(the frequency of which is configurable in the initializer of the AppCollector). Here we're just spitting out random\npoints, so it'll look something like this:\n\n```\n# HELP ruby_points Current amount of points\n# TYPE ruby_points gauge\nruby_points 42\n```\n\n### Registering Our Collectors\n\nEach different type of integration will need to have the collectors passed into them, where appropriate. For example,\nif we want these collectors to run on our web, resque, and hutch processes, we'll need to:\n\n```ruby\n::Bigcommerce::Prometheus.configure do |c|\n  c.web_collectors = [AppCollector]\n  c.web_type_collectors = [AppTypeCollector.new]\n  c.resque_collectors = [AppCollector]\n  c.resque_type_collectors = [AppTypeCollector.new]\n  c.hutch_collectors = [AppCollector]\n  c.hutch_type_collectors = [AppTypeCollector.new]\nend\n```\n\n#### Custom Server Integrations\n\nFor custom integrations that initialize their own server, you'll need to pass your TypeCollector instance via the \n`.add_type_collector` method on the prometheus server instance before starting it:\n\n```ruby\nserver = ::Bigcommerce::Prometheus::Server.new\nBigcommerce::Prometheus.web_type_collectors.each do |tc|\n  server.add_type_collector(tc)\nend\n\n# and for polling:\n\nAppCollector.start\n```\n\n## License\n\nCopyright (c) 2019-present, BigCommerce Pty. Ltd. All rights reserved \n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated \ndocumentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the \nrights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit \npersons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the \nSoftware.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE \nWARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR \nCOPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR \nOTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbigcommerce%2Fbc-prometheus-ruby","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbigcommerce%2Fbc-prometheus-ruby","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbigcommerce%2Fbc-prometheus-ruby/lists"}