diff options
Diffstat (limited to 'lib/gitlab/instrumentation_helper.rb')
-rw-r--r-- | lib/gitlab/instrumentation_helper.rb | 35 |
1 files changed, 30 insertions, 5 deletions
diff --git a/lib/gitlab/instrumentation_helper.rb b/lib/gitlab/instrumentation_helper.rb index 23acf1e8e86..26e44d7822e 100644 --- a/lib/gitlab/instrumentation_helper.rb +++ b/lib/gitlab/instrumentation_helper.rb @@ -131,18 +131,43 @@ module Gitlab enqueued_at_time = convert_to_time(enqueued_at) return unless enqueued_at_time - # Its possible that if theres clock-skew between two nodes - # this value may be less than zero. In that event, we record the value + round_elapsed_time(enqueued_at_time) + end + + # Returns the time it took for a scheduled job to be enqueued in seconds, as a float, + # if the `scheduled_at` and `enqueued_at` fields are available. + # + # * If the job doesn't contain sufficient information, returns nil + # * If the job has a start time in the future, returns 0 + # * If the job contains an invalid start time value, returns nil + # @param [Hash] job a Sidekiq job, represented as a hash + def self.enqueue_latency_for_scheduled_job(job) + scheduled_at = job['scheduled_at'] + enqueued_at = job['enqueued_at'] + + return unless scheduled_at && enqueued_at + + scheduled_at_time = convert_to_time(scheduled_at) + enqueued_at_time = convert_to_time(enqueued_at) + + return unless scheduled_at_time && enqueued_at_time + + round_elapsed_time(scheduled_at_time, enqueued_at_time) + end + + def self.round_elapsed_time(start, end_time = Time.now) + # It's possible that if there is clock-skew between two nodes this + # value may be less than zero. In that event, we record the value # as zero. - [elapsed_by_absolute_time(enqueued_at_time), 0].max.round(DURATION_PRECISION) + [elapsed_by_absolute_time(start, end_time), 0].max.round(DURATION_PRECISION) end # Calculates the time in seconds, as a float, from # the provided start time until now # # @param [Time] start - def self.elapsed_by_absolute_time(start) - (Time.now - start).to_f.round(DURATION_PRECISION) + def self.elapsed_by_absolute_time(start, end_time) + (end_time - start).to_f.round(DURATION_PRECISION) end private_class_method :elapsed_by_absolute_time |