class Bigcommerce::Prometheus::Servers::Thin::Controllers::SendMetricsController

POST /send-metrics

Public Instance Methods

call() click to toggle source

Handle incoming metrics

# File lib/bigcommerce/prometheus/servers/thin/controllers/send_metrics_controller.rb, line 34
def call
  raise InvalidRequestError unless @request.post?

  @server_metrics.add_session
  process_metrics
  succeed!
rescue InvalidRequestError => _e
  fail!('Invalid request type. Only POST is supported.')
rescue BadMetricsError => e
  fail!(e.message)
end

Private Instance Methods

body() click to toggle source

@return [String]

# File lib/bigcommerce/prometheus/servers/thin/controllers/send_metrics_controller.rb, line 85
def body
  @request.body.read
end
fail!(message) click to toggle source

Fail the request

@param [String]

# File lib/bigcommerce/prometheus/servers/thin/controllers/send_metrics_controller.rb, line 63
def fail!(message)
  @response['Content-Type'] = 'application/json'
  @response.write([message].to_json)
  @response.status = 500
  @response
end
process_metrics() click to toggle source

Process the metrics

# File lib/bigcommerce/prometheus/servers/thin/controllers/send_metrics_controller.rb, line 73
def process_metrics
  @server_metrics.add_metric
  @collector.process(body)
rescue StandardError => e
  @logger.error "[bigcommerce-prometheus] Error collecting metrics: #{e.inspect} - #{e.backtrace[0..4].join("\n")}"
  @server_metrics.add_bad_metric
  raise BadMetricsError, e.message
end
succeed!() click to toggle source

Succeed the request

# File lib/bigcommerce/prometheus/servers/thin/controllers/send_metrics_controller.rb, line 51
def succeed!
  @response['Content-Type'] = 'text/plain'
  @response.write('OK')
  @response.status = 200
  @response
end