From 8d232b1172b08dec4accefeac5b495c4573bff77 Mon Sep 17 00:00:00 2001 From: ibnjunaid Date: Sat, 24 Aug 2024 00:45:38 +0530 Subject: [PATCH] Conditionally handle for ruby 7.1 --- lib/aws-xray-sdk/facets/rails/active_record.rb | 7 ++++--- lib/aws-xray-sdk/utils/math_utils.rb | 12 ------------ test/aws-xray-sdk/tc_time_conversion.rb | 13 ------------- 3 files changed, 4 insertions(+), 28 deletions(-) delete mode 100644 lib/aws-xray-sdk/utils/math_utils.rb delete mode 100644 test/aws-xray-sdk/tc_time_conversion.rb diff --git a/lib/aws-xray-sdk/facets/rails/active_record.rb b/lib/aws-xray-sdk/facets/rails/active_record.rb index 9f7f2e3..cc6a23a 100644 --- a/lib/aws-xray-sdk/facets/rails/active_record.rb +++ b/lib/aws-xray-sdk/facets/rails/active_record.rb @@ -1,5 +1,4 @@ require 'active_record' -require 'aws-xray-sdk/utils/math_utils' module XRay module Rails @@ -28,9 +27,11 @@ 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 = (::Rails::VERSION::MAJOR == 7 and ::Rails::VERSION::MINOR == 1) ? convert_time_in_seconds(transaction.time.to_f) : transaction.time.to_f + # Rails 7.1 introduced time measurement in milliseconds instead seconds of causing xray-sdk to report wrong duration for transaction calls. + # This is being handled in rails 7.2 and later. https://github.com/rails/rails/pull/50779 + subsegment.start_time = (::Rails::VERSION::MAJOR == 7 and ::Rails::VERSION::MINOR == 1) ? transaction.time.to_f/1000 : transaction.time.to_f subsegment.sql = sql - XRay.recorder.end_subsegment end_time: (::Rails::VERSION::MAJOR == 7 and ::Rails::VERSION::MINOR == 1) ? convert_time_in_seconds(transaction.end.to_f) : transaction.end.to_f + XRay.recorder.end_subsegment end_time: (::Rails::VERSION::MAJOR == 7 and ::Rails::VERSION::MINOR == 1) ? transaction.end.to_f/1000 : transaction.end.to_f end private diff --git a/lib/aws-xray-sdk/utils/math_utils.rb b/lib/aws-xray-sdk/utils/math_utils.rb deleted file mode 100644 index f9dc2eb..0000000 --- a/lib/aws-xray-sdk/utils/math_utils.rb +++ /dev/null @@ -1,12 +0,0 @@ -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 -end \ No newline at end of file diff --git a/test/aws-xray-sdk/tc_time_conversion.rb b/test/aws-xray-sdk/tc_time_conversion.rb deleted file mode 100644 index 839c556..0000000 --- a/test/aws-xray-sdk/tc_time_conversion.rb +++ /dev/null @@ -1,13 +0,0 @@ -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 \ No newline at end of file