[Infra] Polyfill Stopwatch.GetElapsedTime#7193
Conversation
- Add a polyfill for `Stopwatch.GetElapsedTime()` for `netstandard2.0` and `net462` for use instead of allocating `Stopwatch`s. - Simplify timeout computations and duplicative code. - Rename `cur` to `current` for readability.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #7193 +/- ##
==========================================
+ Coverage 89.69% 89.75% +0.05%
==========================================
Files 272 272
Lines 13434 13381 -53
==========================================
- Hits 12050 12010 -40
+ Misses 1384 1371 -13
Flags with carried forward coverage won't be shown. Click here to find out more.
|
Not sure why this is appearing, but it doesn't apply to the stress tests anyway.
Remove need to suppress CA1515 by not using the polyfill.
Fix mistake from refactor that `Timeout.Infinite` was always used.
There was a problem hiding this comment.
Pull request overview
This PR introduces a shared Stopwatch.GetElapsedTime polyfill for older TFMs and refactors multiple timeout/elapsed-time computations to avoid allocating Stopwatch instances, plus some small readability cleanups.
Changes:
- Add
src/Shared/StopwatchExtensions.csto polyfillStopwatch.GetElapsedTime(long)on non-NETTFMs and add a sharedStopwatch.Remaining(...)helper. - Replace
Stopwatch.StartNew()allocations with timestamp-based elapsed/remaining computations across processors/readers/workers. - Rename loop variable
curtocurrentin composite structures.
Reviewed changes
Copilot reviewed 17 out of 17 changed files in this pull request and generated 10 comments.
Show a summary per file
| File | Description |
|---|---|
| test/OpenTelemetry.Tests/Metrics/MetricApiTests.cs | Uses timestamp + GetElapsedTime instead of allocating a Stopwatch for test timing output. |
| src/Shared/StopwatchExtensions.cs | Adds GetElapsedTime polyfill (non-NET) and a Remaining(...) helper. |
| src/OpenTelemetry/OpenTelemetry.csproj | Links the shared StopwatchExtensions.cs into the OpenTelemetry SDK project. |
| src/OpenTelemetry/Metrics/Reader/PeriodicExportingMetricReader.cs | Simplifies shutdown timeout computation using timestamp + remaining helper. |
| src/OpenTelemetry/Metrics/Reader/MetricReader.cs | Refactors collect timeout handling to timestamp + remaining helper and reduces duplicated logging branches. |
| src/OpenTelemetry/Metrics/Reader/CompositeMetricReaderExt.cs | Renames cur to current for readability. |
| src/OpenTelemetry/Metrics/Reader/CompositeMetricReader.cs | Refactors composite reader timeouts to timestamp-based logic and renames cur to current. |
| src/OpenTelemetry/Metrics/Reader/BaseExportingMetricReader.cs | Refactors shutdown timeout handling using timestamp + remaining helper. |
| src/OpenTelemetry/Internal/PeriodicExportingMetricReaderThreadWorker.cs | Refactors interval timeout computation to use GetElapsedTime instead of a Stopwatch instance. |
| src/OpenTelemetry/Internal/PeriodicExportingMetricReaderTaskWorker.cs | Same interval timeout refactor for task-based worker. |
| src/OpenTelemetry/Internal/BatchExportThreadWorker.cs | Refactors wait timeout logic to timestamp-based approach and simplifies Start/Shutdown. |
| src/OpenTelemetry/Internal/BatchExportTaskWorker.cs | Same wait timeout refactor for task-based worker; also simplifies Start/Shutdown and adjusts cancellation guards. |
| src/OpenTelemetry/CompositeProcessor.cs | Refactors force-flush/shutdown timeout logic and renames cur to current. |
| src/OpenTelemetry/BatchExportProcessor.cs | Simplifies force-flush expression body and refactors shutdown timeout computation. |
| src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Transmission/OtlpExporterTransmissionHandler.cs | Minor simplification in request submission and refactors shutdown timeout with timestamp-based helper. |
| src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Transmission/OtlpExporterPersistentStorageTransmissionHandler.cs | Refactors shutdown timeout computation to timestamp-based helper. |
| OpenTelemetry.slnx | Adds the new shared source file to the solution view. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Fix accidentally making the timeout smaller and smaller over time. - Avoid repeated frequency recalculation. - Fix potential overflow.
Fix warning related to `extension`.
Disable for the whole file.
|
Need to make StyleCop happy with |
Try another fix by moving field to inside a nested private class.
Fix-up merge with main.
Refactoring opportunity I spotted while doing other work that relates to open-telemetry/opentelemetry-dotnet-contrib#4300.
Changes
Stopwatch.GetElapsedTime()fornetstandard2.0andnet462for use instead of allocatingStopwatchs.curtocurrentfor readability.Merge requirement checklist
AppropriateCHANGELOG.mdfiles updated for non-trivial changesChanges in public API reviewed (if applicable)