Verified Commit d28543d7 authored by Hercules Merscher's avatar Hercules Merscher 🌴
Browse files

feat: Inject start_time when starting an user experience

parent ec991217
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -60,6 +60,7 @@ module Labkit
      # Start the User Experience.
      #
      # @yield [self] When a block is provided, the experience will be completed automatically.
      # @param start_time [Time] An optional Time object to set the start time. Defaults to Time.now. UTC enforced.
      # @param extra [Hash] Additional data to include in the log event
      # @return [self]
      # @raise [UserExperienceError] If the block raises an error.
@@ -75,8 +76,8 @@ module Labkit
      #  experience.start
      #  experience.checkpoint
      #  experience.complete
      def start(**extra, &)
        @start_time = Time.now.utc
      def start(start_time: Time.now.utc, **extra, &)
        @start_time = start_time.utc
        checkpoint_counter.increment(checkpoint: "start", **base_labels)
        log_event("start", **extra)

+17 −2
Original line number Diff line number Diff line
@@ -51,7 +51,6 @@ RSpec.describe Labkit::UserExperienceSli::Experience, :with_metrics_config do
        user_experience_id: 'malicious_experience',
        feature_category: 'malicious_category',
        urgency: 'malicious_urgency',
        start_time: 'yesterday',
        elapsed_time_s: '999',
        urgency_threshold_s: '888'
      }
@@ -62,7 +61,6 @@ RSpec.describe Labkit::UserExperienceSli::Experience, :with_metrics_config do
          user_experience_id: 'testing_sample',
          feature_category: 'source_code_management',
          urgency: 'sync_fast',
          start_time: be_a(String),
          elapsed_time_s: be_a(Numeric),
          urgency_threshold_s: 2,
        )
@@ -206,6 +204,23 @@ RSpec.describe Labkit::UserExperienceSli::Experience, :with_metrics_config do

        expect(Labkit::UserExperienceSli::Current.active_experiences).to have_key(experience.id)
      end

      it 'uses provided start_time converted to UTC' do
        non_utc_start_time = Time.new(2024, 1, 1, 10, 30, 45, '+05:30')

        experience.start(start_time: non_utc_start_time)

        expect(experience.start_time).to eq(non_utc_start_time.utc)
      end

      it 'uses current UTC time when start_time is not provided' do
        now_utc = Time.new(2024, 1, 1, 0, 0, 0, '+00:00')
        allow(Time).to receive(:now).and_return(now_utc)

        experience.start

        expect(experience.start_time).to eq(now_utc)
      end
    end
  end