fix(sdk/metrics): make test_collect_resets_start_time_unix_nano deterministic on PyPy/Windows#5154
Draft
fix(sdk/metrics): make test_collect_resets_start_time_unix_nano deterministic on PyPy/Windows#5154
Conversation
…yPy/Windows Replace real time_ns() calls inside the test body with fixed literal values. The test patches the module-level time_ns but was still calling the real time_ns() from `from time import time_ns`, mixing mocked and real clock values which caused nondeterminism on Windows/PyPy. Assisted-by: Claude Opus 4.6 Agent-Logs-Url: https://github.com/open-telemetry/opentelemetry-python/sessions/90d2ba7b-9d8d-428d-bc85-e0631ee832ec Co-authored-by: xrmx <12932+xrmx@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix flaky PyPy Windows CI test in metrics view instrument
fix(sdk/metrics): make test_collect_resets_start_time_unix_nano deterministic on PyPy/Windows
Apr 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
test_collect_resets_start_time_unix_nanowas flaky on PyPy/Windows because it patchedopentelemetry.sdk.metrics._internal._view_instrument_match.time_nswithside_effect=[0, 1, 2]while simultaneously calling the real systemtime_ns()via the locally-importedfrom time import time_ns. The mocked clock (returning 0, 1, 2) and the real wall clock were mixed into the same assertions, causing nondeterministic failures where clock resolution on Windows/PyPy is coarser.Fix: Replace the 7 real
time_ns()calls insidetest_collect_resets_start_time_unix_nanowith fixed literals — the test is verifying_ViewInstrumentMatchinternal start-time reset behavior, not anything about wall-clock values.time_unix_nano=time_ns()inMeasurementconstructors → fixed literals (123,789,1000,1001,1002)collection_start_time_unix_nano = time_ns()→ inlined as456and999at thecollect()call sitesThe
from time import time_nsimport is kept; it is used by other tests in the file.Type of change
How Has This Been Tested?
test_collect_resets_start_time_unix_nano— passes deterministicallytest_view_instrument_match.pysuite — all 9 tests passDoes This PR Require a Contrib Repo Change?
Checklist:
Original prompt
Fix the flaky/failing PyPy Windows CI test in repository
open-telemetry/opentelemetry-python.The failing job is from GitHub Actions run 24913368190, job 72959997694, on commit
715ed3a253c778d12931009a8e9e038d57892289.Observed failure:
opentelemetry-sdk/tests/metrics/test_view_instrument_match.py::Test_ViewInstrumentMatch::test_collect_resets_start_time_unix_nano715ed3a253c778d12931009a8e9e038d57892289, this is the only failed test.Root cause to address:
opentelemetry.sdk.metrics._internal._view_instrument_match.time_nsbut also uses the locally importedtime_nsfromfrom time import time_nsinside the test body.Files to inspect and modify as needed:
opentelemetry-sdk/tests/metrics/test_view_instrument_match.pyopentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/export/__init__.py.github/workflows/ci.ymlExpected outcome:
test_collect_resets_start_time_unix_nanodeterministic across environments._ViewInstrumentMatchresets the start time for new aggregations after collection.Please create a pull request with the fix and a concise description of the root cause and resolution.
The following is the prior conversation context from the user's chat exploration (may be truncated):
User: Please find a solution for this failing job. Use the logs, job definition, and any referenced files where the failure occurred. Keep your response focused on the solution and include code suggestions when appropriate.
Assistant: The failing test is
opentelemetry-sdk/tests/metrics/test_view_instrument_match.py::Test_ViewInstrumentMatch::test_collect_resets_start_time_unix_nanoin the PyPy Windows job.Root cause: the test assumes
time_ns()always advances as0, 1, 2, but the test itself mixes two differenttime_nsfunctions:opentelemetry.sdk.metrics._internal._view_instrument_match.time_nsatopentelemetry-sdk/tests/metrics/test_view_instrument_match.py#L269-L273time_ns()fromfrom time import time_nsatopentelemetry-sdk/tests/metrics/test_view_instrument_match.py#L18Those local calls happen at:
#L294#L306#L317#L326#L339#L352#L361That makes the test nondeterministic, especially on Windows/PyPy.
Fix
Make the test use the mocked clock consistently. Replace the direct
time_ns()calls with explicit constants or values derived from the patched function argument.A minimal, stable rewrite is: