class Bigcommerce::Prometheus::Servers::Thin::ServerMetrics

Server metrics for the collector

Public Class Methods

new(logger: nil) click to toggle source

@param [::Logger] logger

# File lib/bigcommerce/prometheus/servers/thin/server_metrics.rb, line 33
def initialize(logger: nil)
  @logger = logger || ::Bigcommerce::Prometheus.logger
  @metrics_total = ::PrometheusExporter::Metric::Counter.new('collector_metrics_total', 'Total metrics processed by exporter.')
  @sessions_total = ::PrometheusExporter::Metric::Counter.new('collector_sessions_total', 'Total send_metric sessions processed by exporter.')
  @bad_metrics_total = ::PrometheusExporter::Metric::Counter.new('collector_bad_metrics_total', 'Total mis-handled metrics by collector.')
  @collector_working_gauge = ::PrometheusExporter::Metric::Gauge.new('collector_working', 'Is the main process collector able to collect metrics')
  @collector_rss_gauge = ::PrometheusExporter::Metric::Gauge.new('collector_rss', 'total memory used by collector process')
end

Public Instance Methods

add_bad_metric() click to toggle source
# File lib/bigcommerce/prometheus/servers/thin/server_metrics.rb, line 50
def add_bad_metric
  @bad_metrics_total.observe
end
add_metric() click to toggle source
# File lib/bigcommerce/prometheus/servers/thin/server_metrics.rb, line 46
def add_metric
  @metrics_total.observe
end
add_session() click to toggle source
# File lib/bigcommerce/prometheus/servers/thin/server_metrics.rb, line 42
def add_session
  @sessions_total.observe
end
to_prometheus_text(working: true) click to toggle source

@param [Boolean] working @return [String]

# File lib/bigcommerce/prometheus/servers/thin/server_metrics.rb, line 58
def to_prometheus_text(working: true)
  collect(working: working).map(&:to_prometheus_text).join("\n\n")
end

Private Instance Methods

collect(working: true) click to toggle source

Collect server metrics

@param [Boolean] working

# File lib/bigcommerce/prometheus/servers/thin/server_metrics.rb, line 69
def collect(working: true)
  @collector_working_gauge.observe(working ? 1 : 0)
  @collector_rss_gauge.observe(rss)

  [
    @metrics_total,
    @sessions_total,
    @bad_metrics_total,
    @collector_working_gauge,
    @collector_rss_gauge
  ]
end
rss() click to toggle source

Get RSS size of the current process

@return [Integer]

# File lib/bigcommerce/prometheus/servers/thin/server_metrics.rb, line 87
def rss
  _pid, size = `ps ax -o pid,rss | grep -E "^[[:space:]]*#{::Process.pid}"`.strip.split.map(&:to_i)
  size
rescue StandardError => e
  @logger.error "Failed to get RSS size: #{e.message}"
  0
end