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 4 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
2 changes: 1 addition & 1 deletion .github/workflows/continuous-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
matrix:
os:
- macos-latest
- macos-13
jj22ee marked this conversation as resolved.
Show resolved Hide resolved
- ubuntu-latest
ruby:
- 2.4
Expand Down
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 1723627099.460531
end

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