diff options
Diffstat (limited to 'lib/longboat')
| -rw-r--r-- | lib/longboat/config.rb | 16 | ||||
| -rw-r--r-- | lib/longboat/jobs.rb | 16 | ||||
| -rw-r--r-- | lib/longboat/server.rb | 12 | 
3 files changed, 38 insertions, 6 deletions
diff --git a/lib/longboat/config.rb b/lib/longboat/config.rb new file mode 100644 index 0000000..874a5be --- /dev/null +++ b/lib/longboat/config.rb @@ -0,0 +1,16 @@ +require 'optimist' + +module Longboat +  module Config +    def self.parse! +      Optimist::options do +        # Collection interval +        opt :collect_every, "Collection interval",      type: Integer, default: 60 + +        # Sinatra server +        opt :server_bind,   "Server listening address", type: String,  default: "127.0.0.1:8564" +        opt :server_path,   "Path to metrics",          type: String,  default: "/metrics" +      end +    end +  end +end diff --git a/lib/longboat/jobs.rb b/lib/longboat/jobs.rb index c6bbb17..86d3c64 100644 --- a/lib/longboat/jobs.rb +++ b/lib/longboat/jobs.rb @@ -1,10 +1,12 @@  module Longboat    class Jobs -    def initialize +    def initialize(collector, config) +      @collector = collector        @jobs = [] +      @config = config      end -    def load(collector) +    def load!        Dir.entries("./lib/jobs/").each do |file|          next if file =~ /^\./ @@ -12,7 +14,7 @@ module Longboat          cname = reqname.split('_').map(&:capitalize).join          require "jobs/#{reqname}" -        @jobs << Kernel.const_get(cname).new(collector) +        @jobs << Kernel.const_get(cname).new(@collector, job_config)        end      end @@ -20,7 +22,7 @@ module Longboat        @jobs.each(&:run)      end -    def collect_every(time = 60, async = true) +    def collect_every(time = @config[:collect_every], async = true)        if async          Thread.new do            loop do @@ -35,5 +37,11 @@ module Longboat          end        end      end + +    private + +    def job_config +      @config.slice(:collect_every) +    end    end  end diff --git a/lib/longboat/server.rb b/lib/longboat/server.rb index 474a7ed..eb4c56c 100644 --- a/lib/longboat/server.rb +++ b/lib/longboat/server.rb @@ -2,9 +2,17 @@ require 'sinatra/base'  module Longboat    module Server -    def self.serve!(collector) +    def self.serve!(collector, config) +      addr, port = config[:server_bind].split(":") +      addr = "127.0.0.1" if addr.nil? or addr == "" +      port = 8564 if port.nil? or port == "8564" +        Sinatra.new { -        get '/metrics' do +        set :bind, addr +        set :port, port.to_i +        set :environment, :production + +        get config[:server_path] do            collector.prometheus_metrics          end        }.run!  | 
