Skip to content

Commit 4416c86

Browse files
authored
fix: Track latencies greater than 1 second (#133)
1 parent 756b427 commit 4416c86

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

pkgs/sdk/server/src/Migrations/MigrationOpTracker.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,9 @@ private bool CheckEventConsistency()
279279
{
280280
migrationOpEvent.Latency = new EventProcessorTypes.MigrationOpEvent.LatencyMeasurement
281281
{
282-
Old = _oldLatency?.Milliseconds,
283-
New = _newLatency?.Milliseconds
282+
// Fractional milliseconds are trucated as LatencyMeasurement does not support them.
283+
Old = (long?)_oldLatency?.TotalMilliseconds,
284+
New = (long?)_newLatency?.TotalMilliseconds
284285
};
285286
}
286287

pkgs/sdk/server/test/Migrations/MigrationOpTrackerTest.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,55 @@ public void ItHandlesAllCombinationsOfLatency(bool oldLatency, bool newLatency)
213213
Assert.Empty(LogCapture.GetMessages());
214214
}
215215

216+
[Fact]
217+
public void ItRecordsLatencyInMillisecondsGreaterThanASecond()
218+
{
219+
var tracker = BasicTracker();
220+
tracker.Op(MigrationOperation.Read);
221+
tracker.Invoked(MigrationOrigin.New);
222+
tracker.Invoked(MigrationOrigin.Old);
223+
224+
// 2.5 seconds = 2500 milliseconds
225+
var oldLatency = TimeSpan.FromMilliseconds(2500);
226+
var newLatency = TimeSpan.FromMilliseconds(1500);
227+
228+
tracker.Latency(MigrationOrigin.Old, oldLatency);
229+
tracker.Latency(MigrationOrigin.New, newLatency);
230+
231+
var optEvent = tracker.CreateEvent();
232+
Assert.True(optEvent.HasValue);
233+
var migrationOpEvent = optEvent.Value;
234+
235+
Assert.True(migrationOpEvent.Latency.HasValue);
236+
Assert.Equal(2500, migrationOpEvent.Latency?.Old);
237+
Assert.Equal(1500, migrationOpEvent.Latency?.New);
238+
Assert.Empty(LogCapture.GetMessages());
239+
}
240+
241+
[Fact]
242+
public void ItHandlesExtremelyLargeLatencyWithoutException()
243+
{
244+
var tracker = BasicTracker();
245+
tracker.Op(MigrationOperation.Read);
246+
tracker.Invoked(MigrationOrigin.New);
247+
tracker.Invoked(MigrationOrigin.Old);
248+
249+
var hugeLatency = TimeSpan.MaxValue;
250+
251+
Exception ex = Record.Exception(() =>
252+
{
253+
tracker.Latency(MigrationOrigin.Old, hugeLatency);
254+
tracker.Latency(MigrationOrigin.New, hugeLatency);
255+
var optEvent = tracker.CreateEvent();
256+
var migrationOpEvent = optEvent.Value;
257+
Assert.Equal((long?)TimeSpan.MaxValue.TotalMilliseconds, migrationOpEvent.Latency?.Old);
258+
Assert.Equal((long?)TimeSpan.MaxValue.TotalMilliseconds, migrationOpEvent.Latency?.New);
259+
Assert.Empty(LogCapture.GetMessages());
260+
});
261+
262+
Assert.Null(ex);
263+
}
264+
216265
[Theory]
217266
[InlineData(true)]
218267
[InlineData(false)]

0 commit comments

Comments
 (0)