Class: Pgbus::Metrics::PrometheusExporter

Inherits:
Object
  • Object
show all
Defined in:
lib/pgbus/metrics/prometheus_exporter.rb

Overview

Rack app that renders the in-process Prometheus registry as text exposition format. Mount it in the host Rails app (or any Rack server) so Prometheus can scrape it — see the README. Mirrors the "mountable Rack app" shape used by Pgbus::Web::StreamApp.

# config/routes.rb
mount Pgbus::Metrics::PrometheusExporter.new => "/metrics"

With no injected backend it reads Pgbus.configuration.metrics_backend, so a single config.metrics_backend = :prometheus wires both the subscriber and the exporter to the same registry.

Constant Summary collapse

CONTENT_TYPE =
"text/plain; version=0.0.4"

Instance Method Summary collapse

Constructor Details

#initialize(backend: nil) ⇒ PrometheusExporter

Returns a new instance of PrometheusExporter.



19
20
21
# File 'lib/pgbus/metrics/prometheus_exporter.rb', line 19

def initialize(backend: nil)
  @backend = backend
end

Instance Method Details

#call(_env) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/pgbus/metrics/prometheus_exporter.rb', line 23

def call(_env)
  backend = @backend || Pgbus.configuration.metrics_backend
  unless backend.is_a?(Backends::Prometheus)
    return [503, { "content-type" => "text/plain" },
            ["metrics_backend is not a Prometheus backend\n"]]
  end

  [200, { "content-type" => CONTENT_TYPE }, [backend.render]]
end