Class: Labkit::UserExperienceSli::Experience

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/labkit/user_experience_sli/experience.rb

Overview

The Experience class represents a single User Experience event to be measured and reported.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition) ⇒ Experience

Returns a new instance of Experience.



40
41
42
# File 'lib/labkit/user_experience_sli/experience.rb', line 40

def initialize(definition)
  @definition = definition
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



38
39
40
# File 'lib/labkit/user_experience_sli/experience.rb', line 38

def error
  @error
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



38
39
40
# File 'lib/labkit/user_experience_sli/experience.rb', line 38

def start_time
  @start_time
end

Instance Method Details

#checkpoint(**extra) ⇒ self

Checkpoint the User Experience.

Parameters:

  • extra (Hash)

    Additional data to include in the log event

Returns:

  • (self)


94
95
96
97
98
99
100
101
102
# File 'lib/labkit/user_experience_sli/experience.rb', line 94

def checkpoint(**extra)
  return self unless ensure_started!

  @checkpoint_time = Time.now.utc
  checkpoint_counter.increment(checkpoint: "intermediate", **base_labels)
  log_event("intermediate", **extra)

  self
end

#complete(**extra) ⇒ self

Complete the User Experience.

Parameters:

  • extra (Hash)

    Additional data to include in the log event

Returns:

  • (self)


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/labkit/user_experience_sli/experience.rb', line 122

def complete(**extra)
  return self unless ensure_started! && ensure_incomplete!

  begin
    @end_time = Time.now.utc
  ensure
    checkpoint_counter.increment(checkpoint: "end", **base_labels)
    total_counter.increment(error: has_error?, **base_labels)
    apdex_counter.increment(success: apdex_success?, **base_labels) unless has_error?
    log_event("end", **extra)
    Labkit::UserExperienceSli::Current.active_experiences.delete(id)
  end

  self
end

#error!(error) ⇒ self

Marks the experience as failed with an error

Parameters:

  • error (StandardError, String)

    The error that caused the experience to fail.

Returns:

  • (self)


142
143
144
145
# File 'lib/labkit/user_experience_sli/experience.rb', line 142

def error!(error)
  @error = error
  self
end

#has_error?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/labkit/user_experience_sli/experience.rb', line 147

def has_error?
  !!@error
end

#idObject



44
45
46
# File 'lib/labkit/user_experience_sli/experience.rb', line 44

def id
  @definition.user_experience_id
end

#rehydrate(data = {}) ⇒ Experience

Rehydrate an Experience instance from serialized data.

Parameters:

  • data (Hash) (defaults to: {})

    A hash of serialized data.

Returns:



52
53
54
55
56
57
58
# File 'lib/labkit/user_experience_sli/experience.rb', line 52

def rehydrate(data = {})
  @start_time = Time.iso8601(data["start_time"]) if data&.has_key?("start_time") && data["start_time"]
  self
rescue ArgumentError
  warn("Invalid #{id}, start_time: #{data['start_time']}")
  self
end

#resume(**extra) {|self| ... } ⇒ Object

Resume the User Experience.

Parameters:

  • extra (Hash)

    Additional data to include in the log

Yields:

  • (self)

    When a block is provided, the experience will be completed automatically.



108
109
110
111
112
113
114
115
116
# File 'lib/labkit/user_experience_sli/experience.rb', line 108

def resume(**extra, &)
  return self unless ensure_started!

  checkpoint(checkpoint_action: 'resume', **extra)

  return self unless block_given?

  completable(**extra, &)
end

#start(**extra) {|self| ... } ⇒ self

Start the User Experience.

Usage:

UserExperience.new(definition).start do |experience|
  experience.checkpoint
  experience.checkpoint
end

experience = UserExperience.new(definition)
experience.start
experience.checkpoint
experience.complete

Parameters:

  • extra (Hash)

    Additional data to include in the log event

Yields:

  • (self)

    When a block is provided, the experience will be completed automatically.

Returns:

  • (self)

Raises:



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/labkit/user_experience_sli/experience.rb', line 78

def start(**extra, &)
  @start_time = Time.now.utc
  checkpoint_counter.increment(checkpoint: "start", **base_labels)
  log_event("start", **extra)

  Labkit::UserExperienceSli::Current.active_experiences[id] = self

  return self unless block_given?

  completable(**extra, &)
end

#to_hObject



151
152
153
154
155
# File 'lib/labkit/user_experience_sli/experience.rb', line 151

def to_h
  return {} unless ensure_started!

  { id => { "start_time" => @start_time&.iso8601(3) } }
end