Class: Gitlab::Checks::TimedLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/checks/timed_logger.rb

Constant Summary collapse

TimeoutError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_time: Time.now, log: [], header: "", timeout:) ⇒ TimedLogger

Returns a new instance of TimedLogger.



10
11
12
13
14
15
# File 'lib/gitlab/checks/timed_logger.rb', line 10

def initialize(start_time: Time.now, log: [], header: "", timeout:)
  @start_time = start_time
  @timeout = timeout
  @header = header
  @log = log
end

Instance Attribute Details

#headerObject (readonly)

Returns the value of attribute header.



8
9
10
# File 'lib/gitlab/checks/timed_logger.rb', line 8

def header
  @header
end

#logObject (readonly)

Returns the value of attribute log.



8
9
10
# File 'lib/gitlab/checks/timed_logger.rb', line 8

def log
  @log
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



8
9
10
# File 'lib/gitlab/checks/timed_logger.rb', line 8

def start_time
  @start_time
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



8
9
10
# File 'lib/gitlab/checks/timed_logger.rb', line 8

def timeout
  @timeout
end

Instance Method Details

#append_message(message) ⇒ Object

We always want to append in-place on the log



54
55
56
# File 'lib/gitlab/checks/timed_logger.rb', line 54

def append_message(message)
  log << message
end

#check_timeout_reachedObject

Raises:



39
40
41
42
43
# File 'lib/gitlab/checks/timed_logger.rb', line 39

def check_timeout_reached
  return unless time_expired?

  raise TimeoutError
end

#full_messageObject



49
50
51
# File 'lib/gitlab/checks/timed_logger.rb', line 49

def full_message
  header + log.join("\n")
end

#log_timed(log_message, start = Time.now) ⇒ Object

Adds trace of method being tracked with the correspondent time it took to run it. We make use of the start default argument on unit tests related to this method



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/gitlab/checks/timed_logger.rb', line 22

def log_timed(log_message, start = Time.now)
  check_timeout_reached

  timed = true

  yield

  append_message(log_message + time_suffix_message(start: start))
rescue GRPC::DeadlineExceeded, TimeoutError
  args = { cancelled: true }
  args[:start] = start if timed

  append_message(log_message + time_suffix_message(**args))

  raise TimeoutError
end

#time_leftObject



45
46
47
# File 'lib/gitlab/checks/timed_logger.rb', line 45

def time_left
  (start_time + timeout.seconds) - Time.now
end