Skip to content

Instantly share code, notes, and snippets.

@DavidNepozitek
Last active July 29, 2024 16:46
Show Gist options
  • Save DavidNepozitek/46116c794a9cad4375756fb3a45d6401 to your computer and use it in GitHub Desktop.
Save DavidNepozitek/46116c794a9cad4375756fb3a45d6401 to your computer and use it in GitHub Desktop.
using System.Diagnostics;
using OpenTelemetry.Metrics;
namespace Exemplars;
public class TailSamplingHistogramAlignedReservoir(IReadOnlyList<double> explicitBounds)
: FixedSizeExemplarReservoir(explicitBounds.Count + 1)
{
private readonly bool[] _recorded = new bool[explicitBounds.Count + 1];
public override void Offer(in ExemplarMeasurement<long> measurement)
{
Debug.Fail("TailSamplingHistogramAlignedReservoir shouldn't be used with long values");
}
public override void Offer(in ExemplarMeasurement<double> measurement)
{
var index = GetBucketIndex(measurement.Value);
if (_recorded[index])
{
return;
}
UpdateExemplar(index, measurement);
Activity.Current?.AddTag("is_exemplar", true);
_recorded[index] = true;
}
protected override void OnCollected()
{
Array.Clear(_recorded);
}
private int GetBucketIndex(double value)
{
for (var i = 0; i < explicitBounds.Count; i++)
{
if (value <= explicitBounds[i])
{
return i;
}
}
return explicitBounds.Count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment