class Gruf::Prometheus::Hook

Hook for implementing prometheus stats before a gruf server starts

Public Instance Methods

after_server_stop(*) click to toggle source

Handle proper shutdown of the prometheus server

# File lib/gruf/prometheus/hook.rb, line 48
def after_server_stop(*)
  stop_collectors
  prometheus_server.stop
rescue StandardError => e
  logger.error "[gruf-prometheus][#{::Gruf::Prometheus.process_name}] Failed to stop gruf instrumentation - #{e.message} - #{e.backtrace[0..4].join("\n")}"
end
before_server_start(server:) click to toggle source

Startup the instrumentors for collection of gRPC and process metrics prior to server start

@param [Gruf::Server] server

# File lib/gruf/prometheus/hook.rb, line 29
def before_server_start(server:)
  logger.info "[gruf-prometheus][#{::Gruf::Prometheus.process_name}] Starting #{server.class}"
  prometheus_server.add_type_collector(::Gruf::Prometheus::TypeCollector.new)
  prometheus_server.add_type_collector(::Gruf::Prometheus::Server::TypeCollector.new)
  prometheus_server.add_type_collector(::Gruf::Prometheus::Client::TypeCollector.new)
  prometheus_server.add_type_collector(::PrometheusExporter::Server::ActiveRecordCollector.new)
  custom_type_collectors.each do |tc|
    prometheus_server.add_type_collector(tc)
  end
  prometheus_server.start
  sleep 2 unless ENV['RACK_ENV'] == 'test' # wait for server to come online
  start_collectors(server: server)
rescue StandardError => e
  logger.error "[gruf-prometheus][#{::Gruf::Prometheus.process_name}] Failed to start gruf instrumentation - #{e.message} - #{e.backtrace[0..4].join("\n")}"
end

Private Instance Methods

custom_collectors() click to toggle source

@return [Array<Bigcommerce::Prometheus::Collectors::Base>]

# File lib/gruf/prometheus/hook.rb, line 110
def custom_collectors
  @options.fetch(:collectors, []) || []
end
custom_type_collectors() click to toggle source

@return [Array<Bigcommerce::Prometheus::TypeCollectors::Base>]

# File lib/gruf/prometheus/hook.rb, line 103
def custom_type_collectors
  @options.fetch(:type_collectors, []) || []
end
prometheus_server() click to toggle source

@return [Bigcommerce::Prometheus::Server]

# File lib/gruf/prometheus/hook.rb, line 90
def prometheus_server
  @prometheus_server ||= ::Bigcommerce::Prometheus::Server.new(
    host: Bigcommerce::Prometheus.server_host,
    port: Bigcommerce::Prometheus.server_port,
    timeout: Bigcommerce::Prometheus.server_timeout,
    prefix: Bigcommerce::Prometheus.server_prefix,
    logger: logger
  )
end
start_collectors(server:) click to toggle source

Start collectors for the gRPC process

# File lib/gruf/prometheus/hook.rb, line 60
def start_collectors(server:)
  ::PrometheusExporter::Instrumentation::Process.start(
    type: ::Gruf::Prometheus.process_label,
    client: ::Bigcommerce::Prometheus.client,
    frequency: ::Gruf::Prometheus.collection_frequency
  )
  ::Gruf::Prometheus::Collector.start(
    options: {
      server: server
    },
    type: 'grpc',
    client: ::Bigcommerce::Prometheus.client,
    frequency: ::Gruf::Prometheus.collection_frequency
  )
  custom_collectors.each do |collector, arguments|
    collector.start(arguments)
  end
end
stop_collectors() click to toggle source

Stop collectors for the gRPC process

# File lib/gruf/prometheus/hook.rb, line 82
def stop_collectors
  ::PrometheusExporter::Instrumentation::Process.stop
  ::Gruf::Prometheus::Collector.stop
end