Sha256: f93eb0fc2f34a0c3a801a0a6b4b2f897b5bcb51be01746e94fa321e840e9d752

Contents?: true

Size: 1.29 KB

Versions: 4

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module Riemann
  class MetricThread
    # A metric thread is simple: it wraps some metric object which responds to <<,
    # and every interval seconds, calls #flush which replaces the object and calls
    # a user specified function.

    INTERVAL = 10

    attr_accessor :interval, :metric

    # client = Riemann::Client.new
    # m = MetricThread.new Mtrc::Rate do |rate|
    #   client << rate
    # end
    #
    # loop do
    #   sleep rand
    #   m << rand
    # end
    def initialize(klass, *klass_args, &block)
      @klass = klass
      @klass_args = klass_args
      @block = block
      @interval = INTERVAL

      @metric = new_metric

      start
    end

    def <<(value)
      @metric.<<(value)
    end

    def new_metric
      @klass.new(*@klass_args)
    end

    def flush
      old = @metric
      @metric = new_metric
      @block[old]
    end

    def start
      raise 'already running' if @runner

      @running = true
      @runner = Thread.new do
        while @running
          sleep @interval
          begin
            flush
          rescue StandardError
            # ignore
          end
        end
        @runner = nil
      end
    end

    def stop
      stop!
      @runner.join
    end

    def stop!
      @running = false
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
riemann-client-1.2.1 lib/riemann/metric_thread.rb
riemann-client-1.2.0 lib/riemann/metric_thread.rb
riemann-client-1.1.0 lib/riemann/metric_thread.rb
riemann-client-1.0.1 lib/riemann/metric_thread.rb