Class: Conductor::Worker::Telemetry::MetricsServer

Inherits:
Object
  • Object
show all
Defined in:
lib/conductor/worker/telemetry/prometheus_backend.rb

Overview

MetricsServer - HTTP server for exposing Prometheus metrics Serves metrics at /metrics endpoint

Constant Summary collapse

DEFAULT_PORT =
9090

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port: DEFAULT_PORT, registry: nil) ⇒ MetricsServer

Returns a new instance of MetricsServer.

Parameters:

  • port (Integer) (defaults to: DEFAULT_PORT)

    Port to listen on (default: 9090)

  • registry (Prometheus::Client::Registry) (defaults to: nil)

    Prometheus registry



179
180
181
182
183
184
185
186
187
# File 'lib/conductor/worker/telemetry/prometheus_backend.rb', line 179

def initialize(port: DEFAULT_PORT, registry: nil)
  require 'prometheus/client'
  require 'prometheus/client/formats/text'
  require 'webrick'

  @port = port
  @registry = registry || Prometheus::Client.registry
  @server = nil
end

Instance Attribute Details

#portInteger (readonly)

Returns Server port.

Returns:

  • (Integer)

    Server port



220
221
222
# File 'lib/conductor/worker/telemetry/prometheus_backend.rb', line 220

def port
  @port
end

Instance Method Details

#startThread

Start the metrics server in a background thread

Returns:

  • (Thread)

    Server thread



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/conductor/worker/telemetry/prometheus_backend.rb', line 191

def start
  @server = WEBrick::HTTPServer.new(
    Port: @port,
    Logger: WEBrick::Log.new('/dev/null'),
    AccessLog: []
  )

  @server.mount_proc '/metrics' do |_req, res|
    res.content_type = 'text/plain; version=0.0.4'
    res.body = Prometheus::Client::Formats::Text.marshal(@registry)
  end

  @server.mount_proc '/health' do |_req, res|
    res.content_type = 'application/json'
    res.body = '{"status":"healthy"}'
  end

  @thread = Thread.new { @server.start }
  @thread.name = 'prometheus-metrics-server'
  @thread
end

#stopObject

Stop the metrics server



214
215
216
217
# File 'lib/conductor/worker/telemetry/prometheus_backend.rb', line 214

def stop
  @server&.shutdown
  @thread&.join(5)
end