class Bigcommerce::Prometheus::Instrumentors::Web

Instrumentors for web process

Public Class Methods

new(app:) click to toggle source
# File lib/bigcommerce/prometheus/instrumentors/web.rb, line 27
def initialize(app:)
  @app = app
  @enabled = Bigcommerce::Prometheus.enabled
  @server_port = Bigcommerce::Prometheus.server_port
  @server_timeout = Bigcommerce::Prometheus.server_timeout
  @server_prefix = Bigcommerce::Prometheus.server_prefix
  @process_name = Bigcommerce::Prometheus.process_name
  @collectors = Bigcommerce::Prometheus.web_collectors || []
  @type_collectors = Bigcommerce::Prometheus.web_type_collectors || []
end

Public Instance Methods

start() click to toggle source

Start the web instrumentor

# File lib/bigcommerce/prometheus/instrumentors/web.rb, line 41
def start
  unless @enabled
    logger.debug "[bigcommerce-prometheus][#{@process_name}] Prometheus disabled, skipping web start..."
    return
  end

  setup_before_fork
  setup_after_fork
  setup_middleware
rescue StandardError => e
  logger.error "[bigcommerce-prometheus][#{@process_name}] Failed to start web instrumentation - #{e.message} - #{e.backtrace[0..4].join("\n")}"
end

Private Instance Methods

server() click to toggle source
# File lib/bigcommerce/prometheus/instrumentors/web.rb, line 56
def server
  @server ||= ::Bigcommerce::Prometheus::Server.new(
    port: @server_port,
    timeout: @server_timeout,
    prefix: @server_prefix
  )
end
setup_after_fork() click to toggle source
# File lib/bigcommerce/prometheus/instrumentors/web.rb, line 77
def setup_after_fork
  @app.config.after_fork_callbacks = [] unless @app.config.after_fork_callbacks
  @app.config.after_fork_callbacks << lambda do
    ::Bigcommerce::Prometheus::Integrations::Puma.start(client: Bigcommerce::Prometheus.client)
    @collectors.each(&:start)
  end
end
setup_before_fork() click to toggle source
# File lib/bigcommerce/prometheus/instrumentors/web.rb, line 64
def setup_before_fork
  @app.config.before_fork_callbacks = [] unless @app.config.before_fork_callbacks
  @app.config.before_fork_callbacks << lambda do
    server.add_type_collector(PrometheusExporter::Server::ActiveRecordCollector.new)
    server.add_type_collector(PrometheusExporter::Server::WebCollector.new)
    server.add_type_collector(PrometheusExporter::Server::PumaCollector.new)
    @type_collectors.each do |tc|
      server.add_type_collector(tc)
    end
    server.start
  end
end
setup_middleware() click to toggle source
# File lib/bigcommerce/prometheus/instrumentors/web.rb, line 85
def setup_middleware
  @app.middleware.unshift(PrometheusExporter::Middleware, client: Bigcommerce::Prometheus.client)
rescue StandardError => e
  logger.warn "[bc-prometheus-ruby] Failed to attach app middleware in web instrumentor: #{e.message}"
end