Move exemplarreservoir to outside of Histogram (#4250)

This commit is contained in:
Cijo Thomas 2023-03-01 16:16:29 -08:00 committed by GitHub
parent 869dccee15
commit 882006e753
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 31 deletions

View File

@ -32,8 +32,6 @@ namespace OpenTelemetry.Metrics
internal readonly long[] RunningBucketCounts;
internal readonly long[] SnapshotBucketCounts;
internal readonly ExemplarReservoir ExemplarReservoir;
internal double RunningSum;
internal double SnapshotSum;
@ -45,13 +43,11 @@ namespace OpenTelemetry.Metrics
internal int IsCriticalSectionOccupied = 0;
internal Exemplar[] Exemplars;
private readonly BucketLookupNode bucketLookupTreeRoot;
private readonly Func<double, int> findHistogramBucketIndex;
internal HistogramBuckets(double[] explicitBounds, bool enableExemplar = false)
internal HistogramBuckets(double[] explicitBounds)
{
this.ExplicitBounds = explicitBounds;
this.findHistogramBucketIndex = this.FindBucketIndexLinear;
@ -81,10 +77,6 @@ namespace OpenTelemetry.Metrics
this.RunningBucketCounts = explicitBounds != null ? new long[explicitBounds.Length + 1] : null;
this.SnapshotBucketCounts = explicitBounds != null ? new long[explicitBounds.Length + 1] : new long[0];
if (explicitBounds != null && enableExemplar)
{
this.ExemplarReservoir = new AlignedHistogramBucketExemplarReservoir(explicitBounds.Length);
}
}
public Enumerator GetEnumerator() => new(this);

View File

@ -58,7 +58,11 @@ namespace OpenTelemetry.Metrics
this.aggType == AggregationType.HistogramWithMinMaxBuckets)
{
this.mpComponents = new MetricPointOptionalComponents();
this.mpComponents.HistogramBuckets = new HistogramBuckets(histogramExplicitBounds, aggregatorStore.IsExemplarEnabled());
this.mpComponents.HistogramBuckets = new HistogramBuckets(histogramExplicitBounds);
if (aggregatorStore.IsExemplarEnabled())
{
this.mpComponents.ExemplarReservoir = new AlignedHistogramBucketExemplarReservoir(histogramExplicitBounds.Length);
}
}
else if (this.aggType == AggregationType.Histogram ||
this.aggType == AggregationType.HistogramWithMinMax)
@ -272,7 +276,7 @@ namespace OpenTelemetry.Metrics
public readonly Exemplar[] GetExemplars()
{
// TODO: Do not expose Exemplar data structure (array now)
return this.mpComponents.HistogramBuckets?.Exemplars ?? Array.Empty<Exemplar>();
return this.mpComponents.Exemplars ?? Array.Empty<Exemplar>();
}
internal readonly MetricPoint Copy()
@ -688,7 +692,7 @@ namespace OpenTelemetry.Metrics
}
}
histogramBuckets.Exemplars = histogramBuckets.ExemplarReservoir?.Collect(this.Tags, outputDelta);
this.mpComponents.Exemplars = this.mpComponents.ExemplarReservoir?.Collect(this.Tags, outputDelta);
this.MetricPointStatus = MetricPointStatus.NoCollectPending;
@ -765,7 +769,7 @@ namespace OpenTelemetry.Metrics
}
}
histogramBuckets.Exemplars = histogramBuckets.ExemplarReservoir?.Collect(this.Tags, outputDelta);
this.mpComponents.Exemplars = this.mpComponents.ExemplarReservoir?.Collect(this.Tags, outputDelta);
this.MetricPointStatus = MetricPointStatus.NoCollectPending;
// Release lock
@ -884,7 +888,7 @@ namespace OpenTelemetry.Metrics
histogramBuckets.RunningBucketCounts[i]++;
if (reportExemplar)
{
histogramBuckets.ExemplarReservoir.Offer(number, tags, i);
this.mpComponents.ExemplarReservoir.Offer(number, tags, i);
}
}
@ -915,7 +919,7 @@ namespace OpenTelemetry.Metrics
histogramBuckets.RunningBucketCounts[i]++;
if (reportExemplar)
{
histogramBuckets.ExemplarReservoir.Offer(number, tags, i);
this.mpComponents.ExemplarReservoir.Offer(number, tags, i);
}
histogramBuckets.RunningMin = Math.Min(histogramBuckets.RunningMin, number);

View File

@ -27,6 +27,10 @@ namespace OpenTelemetry.Metrics
{
public HistogramBuckets HistogramBuckets;
public ExemplarReservoir ExemplarReservoir;
public Exemplar[] Exemplars;
internal MetricPointOptionalComponents Copy()
{
MetricPointOptionalComponents copy = new MetricPointOptionalComponents();

View File

@ -25,7 +25,7 @@ public partial class Program
private const int ArraySize = 10;
// Note: Uncomment the below line if you want to run Histogram stress test
// private const int MaxHistogramMeasurement = 1000;
private const int MaxHistogramMeasurement = 1000;
private static readonly Meter TestMeter = new(Utils.GetCurrentMethodName());
private static readonly Counter<long> TestCounter = TestMeter.CreateCounter<long>("TestCounter");
@ -33,7 +33,7 @@ public partial class Program
private static readonly ThreadLocal<Random> ThreadLocalRandom = new(() => new Random());
// Note: Uncomment the below line if you want to run Histogram stress test
// private static readonly Histogram<long> TestHistogram = TestMeter.CreateHistogram<long>("TestHistogram");
private static readonly Histogram<long> TestHistogram = TestMeter.CreateHistogram<long>("TestHistogram");
public static void Main()
{
@ -44,6 +44,7 @@ public partial class Program
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddMeter(TestMeter.Name)
.SetExemplarFilter(new AlwaysOnExemplarFilter())
.AddPrometheusHttpListener(
options => options.UriPrefixes = new string[] { $"http://localhost:9185/" })
.Build();
@ -51,26 +52,27 @@ public partial class Program
Stress(prometheusPort: 9464);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected static void Run()
{
var random = ThreadLocalRandom.Value;
TestCounter.Add(
100,
new("DimName1", DimensionValues[random.Next(0, ArraySize)]),
new("DimName2", DimensionValues[random.Next(0, ArraySize)]),
new("DimName3", DimensionValues[random.Next(0, ArraySize)]));
}
// Note: Uncomment the below lines if you want to run Histogram stress test
// Note: Uncomment the below lines if you want to run Counter stress test
// [MethodImpl(MethodImplOptions.AggressiveInlining)]
// protected static void Run()
// {
// var random = ThreadLocalRandom.Value;
// TestHistogram.Record(
// random.Next(MaxHistogramMeasurement),
// TestCounter.Add(
// 100,
// new("DimName1", DimensionValues[random.Next(0, ArraySize)]),
// new("DimName2", DimensionValues[random.Next(0, ArraySize)]),
// new("DimName3", DimensionValues[random.Next(0, ArraySize)]));
// }
// Note: Uncomment the below lines if you want to run Histogram stress test
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected static void Run()
{
var random = ThreadLocalRandom.Value;
TestHistogram.Record(
random.Next(MaxHistogramMeasurement),
new("DimName1", DimensionValues[random.Next(0, ArraySize)]),
new("DimName2", DimensionValues[random.Next(0, ArraySize)]),
new("DimName3", DimensionValues[random.Next(0, ArraySize)]));
}
}