Skip to content

Conversation

xuan-cao-swi
Copy link
Contributor

@xuan-cao-swi xuan-cao-swi commented Aug 15, 2025

Closes #1890

Description

Found a couple of issues while developing the test case:

  1. The current callback timeout applies to the entire set of callbacks of the instrument instead of each callback individually. This PR fixes that.
  2. A faulty callback that raises an error can crash the entire application. This update adds a safeguard to log the error while allowing the application to continue running. Also we shouldn't update to instrument and merge attributes if callback return non-numeric data or nil. (Reference)
  3. Replace Timeout.timeout with thread for executing the callback.
  4. Currently, each instrument supports only one view. Assigning multiple views to the same instrument can lead to inconsistent data_point values, as each view may modify the underlying data. This PR does not address the issue, but it will be resolved in the future.

Each metric_stream should be bound to its own instrument. While most functionality is tested through instruments, here we focus on fundamental correctness, which will be important when views and exemplars are updated.

@xuan-cao-swi xuan-cao-swi changed the title tests: add test case for metric_store and metric_view test: add test case for metric_store and metric_view Aug 26, 2025
Comment on lines 74 to 76
def now_in_nano
(Time.now.to_r * 1_000_000_000).to_i
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a helper for this now in the common gem:

# Converts the provided timestamp to nanosecond integer
#
# @param timestamp [Time] the timestamp to convert, defaults to Time.now
# @return [Integer]
def time_in_nanoseconds(timestamp = Time.now)
(timestamp.to_r * 1_000_000_000).to_i
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.


metric_data = nil
threads = Array.new(5) do
# Thread.new { thread_stream.invoke_callback(timeout, attributes) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

Comment on lines 226 to 241
describe '#now_in_nano' do
it 'returns current time in nanoseconds with increasing values' do
nano_time = async_metric_stream.now_in_nano
_(nano_time).must_be_instance_of(Integer)
_(nano_time).must_be :>, 0

# Should be a reasonable timestamp (not too old, not in future)
current_time_nano = (Time.now.to_r * 1_000_000_000).to_i
_(nano_time).must_be_close_to(current_time_nano, 1_000_000_000) # Within 1 second

# Test successive calls return increasing values
sleep(0.001) # Small delay
time2 = async_metric_stream.now_in_nano
_(time2).must_be :>, nano_time
end
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you swap to using the utils method, can take these out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

Comment on lines 136 to 137
# this test case is unstable as it involve thread in minitest
skip if snapshot.size != 10
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How often do you think this is skipped?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed these unstable thread-related test for now.

@xuan-cao-swi xuan-cao-swi changed the title test: add test case for metric_store and metric_view fix: add test case for metric_store and metric_view Oct 8, 2025
Copy link
Contributor

@kaylareopelle kaylareopelle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for these tests and the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: In progress

Development

Successfully merging this pull request may close these issues.

Add missing unit test for metric_stream and metric_store

2 participants