#!/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_filename = nil verbose = false type_collectors = [] collector_class = PrometheusExporter::Server::Collector 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 FILE', String, "(optional) Custom collector to run") do |o| collector_filename = o.to_s end opt.on('-a', '--type-collector FILE', String, "(optional) Custom type collectors to run in main collector") do |o| type_collectors << o end opt.on('-v', '--verbose') do |o| verbose = true end end.parse! PrometheusExporter::Metric::Base.default_prefix = prefix if collector_filename eval File.read(collector_filename), nil, File.expand_path(collector_filename) found = false ObjectSpace.each_object(Class) do |klass| if klass < PrometheusExporter::Server::CollectorBase collector_class = klass found = true end end if !found STDERR.puts "Can not find a class inheriting off PrometheusExporter::Server::CollectorBase" usage exit 1 end end collector = collector_class.new if type_collectors.length > 0 type_collectors.each do |t| eval File.read(t), nil, File.expand_path(t) end ObjectSpace.each_object(Class) do |klass| if klass < PrometheusExporter::Server::TypeCollector collector.register_collector klass.new STDERR.puts "Registered TypeCollector: #{klass}" if verbose end end end puts "#{Time.now} Starting prometheus exporter on port #{port}" server = PrometheusExporter::Server::WebServer.new port: port, collector: collector, verbose: verbose server.start sleep end run