prober.rb 1.02 KB
Newer Older
1
module GitLab
2
  module Exporter
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
    # A class to combine multiple probers into one
    class Prober
      def initialize(prober_opts, metrics: PrometheusMetrics.new)
        @prober_opts = prober_opts
        @metrics     = metrics

        resolve_prober_classes
      end

      def probe_all
        @prober_opts.each do |_probe_name, params|
          Utils.wrap_in_array(params[:opts]).each do |opts|
            prober = params[:class].new(opts, metrics: @metrics)
            params[:methods].each do |meth|
              prober.send(meth)
            end
          end
        end
      end

      def write_to(target)
        target.write(@metrics.to_s)
      end

      private

      def resolve_prober_classes
        @prober_opts.each do |probe_name, params|
          prober_class_name = params[:class_name] || Utils.camel_case_string("#{probe_name}_prober")

33
          klass = prober_class_name.split("::").reduce(GitLab::Exporter) { |a, e| a.const_get(e) }
34 35 36 37 38 39 40

          params[:class] = klass
        end
      end
    end
  end
end