Commit d815abdc authored by Jacob Vosmaer's avatar Jacob Vosmaer Committed by Heinrich Lee Yu

Instrumentation: fix rounding errors on short durations

Rounding causes a loss of information. We should only round numbers
when we have to. This commit fixes instrumentation code that was
unnecessarily rounding intermediate results.
parent aafc36f0
...@@ -25,7 +25,7 @@ module Gitlab ...@@ -25,7 +25,7 @@ module Gitlab
if Gitlab::RuggedInstrumentation.active? if Gitlab::RuggedInstrumentation.active?
Gitlab::RuggedInstrumentation.increment_query_count Gitlab::RuggedInstrumentation.increment_query_count
Gitlab::RuggedInstrumentation.query_time += duration Gitlab::RuggedInstrumentation.add_query_time(duration)
Gitlab::RuggedInstrumentation.add_call_details( Gitlab::RuggedInstrumentation.add_call_details(
feature: method_name, feature: method_name,
......
...@@ -201,7 +201,8 @@ module Gitlab ...@@ -201,7 +201,8 @@ module Gitlab
request_hash = request.is_a?(Google::Protobuf::MessageExts) ? request.to_h : {} request_hash = request.is_a?(Google::Protobuf::MessageExts) ? request.to_h : {}
# Keep track, separately, for the performance bar # Keep track, separately, for the performance bar
self.query_time += duration self.add_query_time(duration)
if Gitlab::PerformanceBar.enabled_for_request? if Gitlab::PerformanceBar.enabled_for_request?
add_call_details(feature: "#{service}##{rpc}", duration: duration, request: request_hash, rpc: rpc, add_call_details(feature: "#{service}##{rpc}", duration: duration, request: request_hash, rpc: rpc,
backtrace: Gitlab::BacktraceCleaner.clean_backtrace(caller)) backtrace: Gitlab::BacktraceCleaner.clean_backtrace(caller))
...@@ -209,12 +210,15 @@ module Gitlab ...@@ -209,12 +210,15 @@ module Gitlab
end end
def self.query_time def self.query_time
query_time = SafeRequestStore[:gitaly_query_time] ||= 0 query_time = Gitlab::SafeRequestStore[:gitaly_query_time] || 0
query_time.round(Gitlab::InstrumentationHelper::DURATION_PRECISION) query_time.round(Gitlab::InstrumentationHelper::DURATION_PRECISION)
end end
def self.query_time=(duration) def self.add_query_time(duration)
SafeRequestStore[:gitaly_query_time] = duration return unless Gitlab::SafeRequestStore.active?
Gitlab::SafeRequestStore[:gitaly_query_time] ||= 0
Gitlab::SafeRequestStore[:gitaly_query_time] += duration
end end
def self.current_transaction_labels def self.current_transaction_labels
......
...@@ -43,8 +43,8 @@ module Gitlab ...@@ -43,8 +43,8 @@ module Gitlab
end end
def self.add_duration(duration) def self.add_duration(duration)
total_time = query_time + duration ::RequestStore[REDIS_CALL_DURATION] ||= 0
::RequestStore[REDIS_CALL_DURATION] = total_time ::RequestStore[REDIS_CALL_DURATION] += duration
end end
def self.add_call_details(duration, args) def self.add_call_details(duration, args)
......
...@@ -3,12 +3,13 @@ ...@@ -3,12 +3,13 @@
module Gitlab module Gitlab
module RuggedInstrumentation module RuggedInstrumentation
def self.query_time def self.query_time
query_time = SafeRequestStore[:rugged_query_time] ||= 0 query_time = SafeRequestStore[:rugged_query_time] || 0
query_time.round(Gitlab::InstrumentationHelper::DURATION_PRECISION) query_time.round(Gitlab::InstrumentationHelper::DURATION_PRECISION)
end end
def self.query_time=(duration) def self.add_query_time(duration)
SafeRequestStore[:rugged_query_time] = duration SafeRequestStore[:rugged_query_time] ||= 0
SafeRequestStore[:rugged_query_time] += duration
end end
def self.query_time_ms def self.query_time_ms
......
...@@ -40,12 +40,6 @@ module Peek ...@@ -40,12 +40,6 @@ module Peek
super.merge(request: pretty_request || {}) super.merge(request: pretty_request || {})
end end
def setup_subscribers
subscribe 'start_processing.action_controller' do
::Gitlab::GitalyClient.query_time = 0
end
end
end end
end end
end end
...@@ -21,10 +21,10 @@ describe Gitlab::GitalyClient do ...@@ -21,10 +21,10 @@ describe Gitlab::GitalyClient do
describe '.query_time', :request_store do describe '.query_time', :request_store do
it 'increments query times' do it 'increments query times' do
subject.query_time += 0.451 subject.add_query_time(0.4510004)
subject.query_time += 0.322 subject.add_query_time(0.3220004)
expect(subject.query_time).to eq(0.773) expect(subject.query_time).to eq(0.773001)
end end
end end
......
# frozen_string_literal: true
require 'spec_helper'
describe Gitlab::Instrumentation::Redis do
describe '.add_duration', :request_store do
it 'does not lose precision while adding' do
precision = 1.0 / (10**::Gitlab::InstrumentationHelper::DURATION_PRECISION)
2.times { described_class.add_duration(0.4 * precision) }
# 2 * 0.4 should be 0.8 and get rounded to 1
expect(described_class.query_time).to eq(1 * precision)
end
end
end
...@@ -7,10 +7,10 @@ describe Gitlab::RuggedInstrumentation, :request_store do ...@@ -7,10 +7,10 @@ describe Gitlab::RuggedInstrumentation, :request_store do
describe '.query_time' do describe '.query_time' do
it 'increments query times' do it 'increments query times' do
subject.query_time += 0.451 subject.add_query_time(0.4510004)
subject.query_time += 0.322 subject.add_query_time(0.3220004)
expect(subject.query_time).to be_within(0.001).of(0.773) expect(subject.query_time).to eq(0.773001)
expect(subject.query_time_ms).to eq(773.0) expect(subject.query_time_ms).to eq(773.0)
end end
end end
......
...@@ -12,7 +12,7 @@ describe Gitlab::SidekiqMiddleware do ...@@ -12,7 +12,7 @@ describe Gitlab::SidekiqMiddleware do
def perform(_arg) def perform(_arg)
Gitlab::SafeRequestStore['gitaly_call_actual'] = 1 Gitlab::SafeRequestStore['gitaly_call_actual'] = 1
Gitlab::GitalyClient.query_time = 5 Gitlab::SafeRequestStore[:gitaly_query_time] = 5
end end
end end
end end
......
...@@ -16,7 +16,7 @@ describe Peek::Views::Rugged, :request_store do ...@@ -16,7 +16,7 @@ describe Peek::Views::Rugged, :request_store do
end end
it 'returns aggregated results' do it 'returns aggregated results' do
::Gitlab::RuggedInstrumentation.query_time += 1.234 ::Gitlab::RuggedInstrumentation.add_query_time(1.234)
::Gitlab::RuggedInstrumentation.increment_query_count ::Gitlab::RuggedInstrumentation.increment_query_count
::Gitlab::RuggedInstrumentation.increment_query_count ::Gitlab::RuggedInstrumentation.increment_query_count
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment