Sha256: 8a4530a7a9a9b6936624806a38cc696d54dd7a8de1084db5669018291456f889

Contents?: true

Size: 1.29 KB

Versions: 11

Compression:

Stored size: 1.29 KB

Contents

module Katello
  module Logging
    def self.time(message, data: {}, logger: Rails.logger, level: :info)
      start = Time.now

      yield

      data[:duration] = ((Time.now - start) * 1000).truncate(2)
      data_string = data.map { |k, v| "#{k}=#{v}" }.join(' ')

      logger.send(level, "#{message} #{data_string}")
    end

    class Timer
      def initialize(key = "default")
        @key = key
        @start_time = Time.now
        Thread.current[:timers] ||= {}
        Thread.current[:timers][key] = self
      end

      def start
        Rails.logger.info "Timer #{@key} already started; resetting start time" if @start_time
        Rails.logger.info "Timer #{@key} starting at #{Time.now}"
        @start_time = Time.now
        self
      end

      def stop
        fail ::StandardError, "Timer #{@key} is not started" unless @start_time
        duration = (Time.now - @start_time).truncate(2)
        @start_time = nil
        Rails.logger.info "Timer #{@key} stopping at #{Time.now}: #{duration} sec"
      end

      def self.find_by_key(key)
        if Thread.current&.[](:timers)&.[](key)
          Thread.current[:timers][key]
        else
          Rails.logger.warn "Timer #{key} not found on current thread; creating a new timer"
          self.new(key)
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
katello-4.0.3 app/lib/katello/logging.rb
katello-4.0.2.1 app/lib/katello/logging.rb
katello-4.0.2 app/lib/katello/logging.rb
katello-4.0.1.2 app/lib/katello/logging.rb
katello-4.0.1.1 app/lib/katello/logging.rb
katello-4.0.1 app/lib/katello/logging.rb
katello-4.0.0 app/lib/katello/logging.rb
katello-4.0.0.rc3.1 app/lib/katello/logging.rb
katello-4.0.0.rc3 app/lib/katello/logging.rb
katello-4.0.0.rc2 app/lib/katello/logging.rb
katello-4.0.0.rc1 app/lib/katello/logging.rb