Class: Slimy::Context
- Inherits:
-
Object
- Object
- Slimy::Context
- Defined in:
- lib/slimy/context.rb
Overview
Slimy::Context
This is a group of metadata that exists for the duration of some request to be measured as part of an SLI.
Instance Attribute Summary collapse
-
#deadline ⇒ Object
Returns the value of attribute deadline.
-
#end_time ⇒ Object
readonly
Returns the value of attribute end_time.
-
#start_time ⇒ Object
readonly
Returns the value of attribute start_time.
-
#tags ⇒ Object
Returns the value of attribute tags.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#deadline_success? ⇒ Boolean
Did the request finish before the deadline (or was there no deadline).
-
#debug_format ⇒ Object
tool for debugging.
- #do_not_report! ⇒ Object
-
#duration ⇒ Object
duration in ms of the event.
-
#finish(end_time: nil) ⇒ Object
Set the end_time value for the context if not already set.
-
#finished? ⇒ Boolean
Whether or not an end time has been set.
-
#initialize(start_time: nil, type: "request", deadline: nil) ⇒ Context
constructor
Create a new context sets the ‘start_time` to now.
-
#reportable? ⇒ Boolean
Whether or not this context should be reported.
-
#result_error! ⇒ Object
mark request as having an error, or otherwise unacceptable for users.
-
#result_error? ⇒ Boolean
was the result of the request an error?.
-
#result_success! ⇒ Object
mark the result as being successful.
-
#result_success? ⇒ Boolean
Was the execution path of this request normal (not an error)? This does not include deadline failures.
-
#success? ⇒ Boolean
Returns true if there was no error and if the deadline was not reached.
Constructor Details
#initialize(start_time: nil, type: "request", deadline: nil) ⇒ Context
Create a new context sets the ‘start_time` to now
Parameters
start_time:
An overriden Time at which the event started. Defaults to now.
- deadline
-
ms that this context should be finished in to be considered acceptable
19 20 21 22 23 24 25 26 27 |
# File 'lib/slimy/context.rb', line 19 def initialize(start_time: nil, type: "request", deadline: nil) @start_time = start_time || Time.now @deadline = deadline @result_status = :success @end_time = nil @tags = {} @reportable = true @type = type end |
Instance Attribute Details
#deadline ⇒ Object
Returns the value of attribute deadline.
31 32 33 |
# File 'lib/slimy/context.rb', line 31 def deadline @deadline end |
#end_time ⇒ Object (readonly)
Returns the value of attribute end_time.
29 30 31 |
# File 'lib/slimy/context.rb', line 29 def end_time @end_time end |
#start_time ⇒ Object (readonly)
Returns the value of attribute start_time.
29 30 31 |
# File 'lib/slimy/context.rb', line 29 def start_time @start_time end |
#tags ⇒ Object
Returns the value of attribute tags.
31 32 33 |
# File 'lib/slimy/context.rb', line 31 def @tags end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
29 30 31 |
# File 'lib/slimy/context.rb', line 29 def type @type end |
Instance Method Details
#deadline_success? ⇒ Boolean
Did the request finish before the deadline (or was there no deadline)
83 84 85 86 87 |
# File 'lib/slimy/context.rb', line 83 def deadline_success? return true if @deadline.nil? || !finished? duration < @deadline end |
#debug_format ⇒ Object
tool for debugging
99 100 101 102 103 104 105 106 107 |
# File 'lib/slimy/context.rb', line 99 def debug_format "slimy_ctx:\n\tfinished: #{finished?}\n" \ + "\tduration: #{duration}\n" \ + "\tresult_success: #{result_success?}\n" \ + "\tduration_success: #{deadline_success?}\n" \ + "\tsuccess: #{success?}\n" \ + "\tdeadline: #{deadline}\n" \ + "\ttags: #{.inspect}\n" end |
#do_not_report! ⇒ Object
94 95 96 |
# File 'lib/slimy/context.rb', line 94 def do_not_report! @reportable = false end |
#duration ⇒ Object
duration in ms of the event
76 77 78 79 80 |
# File 'lib/slimy/context.rb', line 76 def duration return -1 unless finished? [@end_time - @start_time, 0].max * 1000.0 end |
#finish(end_time: nil) ⇒ Object
Set the end_time value for the context if not already set
Parameters
- end_time
-
An overriden Time at which the event ended. Defaults to now.
39 40 41 |
# File 'lib/slimy/context.rb', line 39 def finish(end_time: nil) @end_time = end_time || Time.now unless finished? end |
#finished? ⇒ Boolean
Whether or not an end time has been set
44 45 46 |
# File 'lib/slimy/context.rb', line 44 def finished? !@end_time.nil? end |
#reportable? ⇒ Boolean
Whether or not this context should be reported
90 91 92 |
# File 'lib/slimy/context.rb', line 90 def reportable? @reportable end |
#result_error! ⇒ Object
mark request as having an error, or otherwise unacceptable for users
49 50 51 |
# File 'lib/slimy/context.rb', line 49 def result_error! @result_status = :error end |
#result_error? ⇒ Boolean
was the result of the request an error?
54 55 56 |
# File 'lib/slimy/context.rb', line 54 def result_error? @result_status == :error end |
#result_success! ⇒ Object
mark the result as being successful. This will not override deadline failure
60 61 62 |
# File 'lib/slimy/context.rb', line 60 def result_success! @result_status = :success end |
#result_success? ⇒ Boolean
Was the execution path of this request normal (not an error)? This does not include deadline failures
66 67 68 |
# File 'lib/slimy/context.rb', line 66 def result_success? @result_status == :success end |
#success? ⇒ Boolean
Returns true if there was no error and if the deadline was not reached.
71 72 73 |
# File 'lib/slimy/context.rb', line 71 def success? result_success? && deadline_success? end |