#!/usr/bin/env ruby require 'optparse' require_relative "./../lib/prometheus_exporter" require_relative "./../lib/prometheus_exporter/server" def run port = PrometheusExporter::DEFAULT_PORT prefix = "ruby_" collector = nil verbose = false OptionParser.new do |opt| opt.on('-p', '--port INTEGER', Integer, "Port exporter should listen on (default: #{port})") do |o| port = o.to_i end opt.on('--prefix METRIC_PREFIX', String, "Prefix to apply to all metrics (default: #{prefix})") do |o| prefix = o.to_s end opt.on('-c', '--collector CUSTOM_COLLECTOR', String, "(optional) Custom collector to run") do |o| collector = o.to_s end opt.on('-v', '--verbose') do |o| verbose = true end end.parse! PrometheusExporter::Metric::Base.default_prefix = prefix if collector eval File.read(collector) ObjectSpace.each_object(Class) do |klass| if klass < PrometheusExporter::Server::CollectorBase collector = klass end end if !collector STDERR.puts "Can not find a class inheriting off PrometheusExporter::Server::Collector" usage exit 1 end end puts "#{Time.now} Starting prometheus exporter on port #{port}" server = PrometheusExporter::Server::WebServer.new port: port, collector: collector&.new, verbose: verbose server.start sleep end run