Last active
July 29, 2024 16:46
-
-
Save DavidNepozitek/46116c794a9cad4375756fb3a45d6401 to your computer and use it in GitHub Desktop.
This file contains 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
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