Class: Slimy::Context

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#deadlineObject

Returns the value of attribute deadline.



31
32
33
# File 'lib/slimy/context.rb', line 31

def deadline
  @deadline
end

#end_timeObject (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_timeObject (readonly)

Returns the value of attribute start_time.



29
30
31
# File 'lib/slimy/context.rb', line 29

def start_time
  @start_time
end

#tagsObject

Returns the value of attribute tags.



31
32
33
# File 'lib/slimy/context.rb', line 31

def tags
  @tags
end

#typeObject (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)

Returns:

  • (Boolean)


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_formatObject

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: #{tags.inspect}\n"
end

#do_not_report!Object



94
95
96
# File 'lib/slimy/context.rb', line 94

def do_not_report!
  @reportable = false
end

#durationObject

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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


71
72
73
# File 'lib/slimy/context.rb', line 71

def success?
  result_success? && deadline_success?
end