Sha256: c936aa40303f4b6c947271d83088154424b7476ed960094e69aba75ee4fe77d4

Contents?: true

Size: 1.22 KB

Versions: 10

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

module PrometheusExporter::Instrumentation
  class PeriodicStats

    def self.start(*args, frequency:, client: nil, **kwargs)
      client ||= PrometheusExporter::Client.default

      if !(Numeric === frequency)
        raise ArgumentError.new("Expected frequency to be a number")
      end

      if frequency < 0
        raise ArgumentError.new("Expected frequency to be a positive number")
      end

      if !@worker_loop
        raise ArgumentError.new("Worker loop was not set")
      end

      klass = self

      stop

      @stop_thread = false

      @thread = Thread.new do
        while !@stop_thread
          begin
            @worker_loop.call
          rescue => e
            client.logger.error("#{klass} Prometheus Exporter Failed To Collect Stats #{e}")
          ensure
            sleep frequency
          end
        end
      end

    end

    def self.started?
      !!@thread&.alive?
    end

    def self.worker_loop(&blk)
      @worker_loop = blk
    end

    def self.stop
      # to avoid a warning
      @thread = nil if !defined?(@thread)

      if @thread&.alive?
        @stop_thread = true
        @thread.wakeup
        @thread.join
      end
      @thread = nil
    end

  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
prometheus_exporter-2.1.1 lib/prometheus_exporter/instrumentation/periodic_stats.rb
prometheus_exporter-2.1.0 lib/prometheus_exporter/instrumentation/periodic_stats.rb
prometheus_exporter-2.0.8 lib/prometheus_exporter/instrumentation/periodic_stats.rb
prometheus_exporter-2.0.7 lib/prometheus_exporter/instrumentation/periodic_stats.rb
prometheus_exporter-2.0.6 lib/prometheus_exporter/instrumentation/periodic_stats.rb
prometheus_exporter-2.0.5 lib/prometheus_exporter/instrumentation/periodic_stats.rb
prometheus_exporter-2.0.4 lib/prometheus_exporter/instrumentation/periodic_stats.rb
prometheus_exporter-2.0.3 lib/prometheus_exporter/instrumentation/periodic_stats.rb
prometheus_exporter-2.0.2 lib/prometheus_exporter/instrumentation/periodic_stats.rb
prometheus_exporter-2.0.1 lib/prometheus_exporter/instrumentation/periodic_stats.rb