sprints and audit work
This commit is contained in:
@@ -28,6 +28,7 @@ public sealed class TtePercentileExporter : IDisposable
|
||||
private readonly Dictionary<string, LatencyWindow> _windows = new();
|
||||
private readonly int _windowSizeSeconds;
|
||||
private readonly int _maxSamplesPerWindow;
|
||||
private readonly Func<int, int> _randomIndexSource;
|
||||
|
||||
// Observable gauges for percentiles
|
||||
private readonly ObservableGauge<double> _p50Gauge;
|
||||
@@ -38,11 +39,14 @@ public sealed class TtePercentileExporter : IDisposable
|
||||
/// <summary>
|
||||
/// Initializes a new instance of <see cref="TtePercentileExporter"/>.
|
||||
/// </summary>
|
||||
public TtePercentileExporter(TtePercentileOptions? options = null)
|
||||
/// <param name="options">Configuration options.</param>
|
||||
/// <param name="randomIndexSource">Optional random index source for testability (receives max, returns 0..max-1).</param>
|
||||
public TtePercentileExporter(TtePercentileOptions? options = null, Func<int, int>? randomIndexSource = null)
|
||||
{
|
||||
var opts = options ?? new TtePercentileOptions();
|
||||
_windowSizeSeconds = opts.WindowSizeSeconds;
|
||||
_maxSamplesPerWindow = opts.MaxSamplesPerWindow;
|
||||
_randomIndexSource = randomIndexSource ?? Random.Shared.Next;
|
||||
|
||||
_meter = new Meter(MeterName, opts.Version);
|
||||
|
||||
@@ -82,7 +86,7 @@ public sealed class TtePercentileExporter : IDisposable
|
||||
{
|
||||
if (!_windows.TryGetValue(key, out var window))
|
||||
{
|
||||
window = new LatencyWindow(_windowSizeSeconds, _maxSamplesPerWindow);
|
||||
window = new LatencyWindow(_windowSizeSeconds, _maxSamplesPerWindow, _randomIndexSource);
|
||||
_windows[key] = window;
|
||||
}
|
||||
window.Add(latencySeconds, DateTimeOffset.UtcNow);
|
||||
@@ -158,12 +162,14 @@ public sealed class TtePercentileExporter : IDisposable
|
||||
{
|
||||
private readonly int _windowSizeSeconds;
|
||||
private readonly int _maxSamples;
|
||||
private readonly Func<int, int> _randomIndexSource;
|
||||
private readonly List<(double Latency, DateTimeOffset Timestamp)> _samples = new();
|
||||
|
||||
public LatencyWindow(int windowSizeSeconds, int maxSamples)
|
||||
public LatencyWindow(int windowSizeSeconds, int maxSamples, Func<int, int> randomIndexSource)
|
||||
{
|
||||
_windowSizeSeconds = windowSizeSeconds;
|
||||
_maxSamples = maxSamples;
|
||||
_randomIndexSource = randomIndexSource;
|
||||
}
|
||||
|
||||
public void Add(double latency, DateTimeOffset timestamp)
|
||||
@@ -180,7 +186,7 @@ public sealed class TtePercentileExporter : IDisposable
|
||||
else
|
||||
{
|
||||
// Reservoir sampling for large windows
|
||||
var index = Random.Shared.Next(_samples.Count + 1);
|
||||
var index = _randomIndexSource(_samples.Count + 1);
|
||||
if (index < _samples.Count)
|
||||
{
|
||||
_samples[index] = (latency, timestamp);
|
||||
|
||||
Reference in New Issue
Block a user