Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue: Wrong duration for DB transaction event on ROR 7.1 #92 #96

Merged
merged 6 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/aws-xray-sdk/facets/rails/active_record.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'active_record'
require 'aws-xray-sdk/utils/math_utils'

module XRay
module Rails
Expand Down Expand Up @@ -27,9 +28,10 @@ def record(transaction)
subsegment = XRay.recorder.begin_subsegment name, namespace: 'remote'
# subsegment is nil in case of context missing
return if subsegment.nil?
subsegment.start_time = transaction.time.to_f

subsegment.start_time = convert_time_in_seconds(transaction.time.to_f)
subsegment.sql = sql
XRay.recorder.end_subsegment end_time: transaction.end.to_f
XRay.recorder.end_subsegment end_time: convert_time_in_seconds(transaction.end.to_f)
end

private
Expand Down
12 changes: 12 additions & 0 deletions lib/aws-xray-sdk/utils/math_utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def get_exponent(num)
return 0 if num == 0
exp = Math.log10(num.abs).floor
num.negative? ? exp - 1 : exp
end

# X-Ray SDK expects time in seconds with nanosecond precision.
def convert_time_in_seconds(time_float)
exponent = get_exponent(time_float)
division_factor = 10 ** (exponent - 9)
time_float / division_factor
jj22ee marked this conversation as resolved.
Show resolved Hide resolved
end
13 changes: 13 additions & 0 deletions test/aws-xray-sdk/tc_time_conversion.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'aws-xray-sdk/utils/math_utils'

class TestGetTimeInSeconds < Minitest::Test
def convert_time_from_milliseconds
time_in_seconds = convert_time_in_seconds 1.723627099460531E12
assert_equal time_in_seconds 172362709.9460531
ibnjunaid marked this conversation as resolved.
Show resolved Hide resolved
end

def convert_time_from_seconds
time_in_seconds = convert_time_in_seconds 1.7236270994659648E9
assert_equal time_in_seconds 1.7236270994659648E9
end
end