Skip to content

Commit

Permalink
[metrics] Fix perf regression introduced by metric point reclaim feat…
Browse files Browse the repository at this point in the history
…ure (#5307)

Co-authored-by: Alan West <3676547+alanwest@users.noreply.github.com>
  • Loading branch information
CodeBlanch and alanwest authored Feb 2, 2024
1 parent 71f9a42 commit ef942a1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 22 deletions.
23 changes: 10 additions & 13 deletions src/OpenTelemetry/Metrics/AggregatorStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace OpenTelemetry.Metrics;
internal sealed class AggregatorStore
{
internal readonly bool OutputDelta;
internal readonly bool ShouldReclaimUnusedMetricPoints;
internal readonly bool OutputDeltaWithUnusedMetricPointReclaimEnabled;
internal long DroppedMeasurements = 0;

private static readonly string MetricPointCapHitFixMessage = "Consider opting in for the experimental SDK feature to emit all the throttled metrics under the overflow attribute by setting env variable OTEL_DOTNET_EXPERIMENTAL_METRICS_EMIT_OVERFLOW_ATTRIBUTE = true. You could also modify instrumentation to reduce the number of unique key/value pair combinations. Or use Views to drop unwanted tags. Or use MeterProviderBuilder.SetMaxMetricPointsPerMetricStream to set higher limit.";
Expand Down Expand Up @@ -101,9 +101,9 @@ internal AggregatorStore(
reservedMetricPointsCount++;
}

this.ShouldReclaimUnusedMetricPoints = shouldReclaimUnusedMetricPoints;
this.OutputDeltaWithUnusedMetricPointReclaimEnabled = shouldReclaimUnusedMetricPoints && this.OutputDelta;

if (this.OutputDelta && shouldReclaimUnusedMetricPoints)
if (this.OutputDeltaWithUnusedMetricPointReclaimEnabled)
{
this.availableMetricPoints = new Queue<int>(maxMetricPoints - reservedMetricPointsCount);

Expand Down Expand Up @@ -158,17 +158,14 @@ internal void Update(double value, ReadOnlySpan<KeyValuePair<string, object?>> t
internal int Snapshot()
{
this.batchSize = 0;
if (this.OutputDelta)
if (this.OutputDeltaWithUnusedMetricPointReclaimEnabled)
{
if (this.ShouldReclaimUnusedMetricPoints)
{
this.SnapshotDeltaWithMetricPointReclaim();
}
else
{
var indexSnapshot = Math.Min(this.metricPointIndex, this.maxMetricPoints - 1);
this.SnapshotDelta(indexSnapshot);
}
this.SnapshotDeltaWithMetricPointReclaim();
}
else if (this.OutputDelta)
{
var indexSnapshot = Math.Min(this.metricPointIndex, this.maxMetricPoints - 1);
this.SnapshotDelta(indexSnapshot);
}
else
{
Expand Down
14 changes: 5 additions & 9 deletions src/OpenTelemetry/Metrics/MetricPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,7 @@ internal MetricPoint(
{
Debug.Assert(aggregatorStore != null, "AggregatorStore was null.");
Debug.Assert(histogramExplicitBounds != null, "Histogram explicit Bounds was null.");

if (aggregatorStore!.OutputDelta && aggregatorStore.ShouldReclaimUnusedMetricPoints)
{
Debug.Assert(lookupData != null, "LookupData was null.");
}
Debug.Assert(!aggregatorStore!.OutputDeltaWithUnusedMetricPointReclaimEnabled || lookupData != null, "LookupData was null.");

this.aggType = aggType;
this.Tags = new ReadOnlyTagCollection(tagKeysAndValues);
Expand Down Expand Up @@ -445,7 +441,7 @@ internal void Update(long number)
// by ignoring Zero points
this.MetricPointStatus = MetricPointStatus.CollectPending;

if (this.aggregatorStore.OutputDelta)
if (this.aggregatorStore.OutputDeltaWithUnusedMetricPointReclaimEnabled)
{
Interlocked.Decrement(ref this.ReferenceCount);
}
Expand Down Expand Up @@ -570,7 +566,7 @@ internal void UpdateWithExemplar(long number, ReadOnlySpan<KeyValuePair<string,
// by ignoring Zero points
this.MetricPointStatus = MetricPointStatus.CollectPending;

if (this.aggregatorStore.OutputDelta)
if (this.aggregatorStore.OutputDeltaWithUnusedMetricPointReclaimEnabled)
{
Interlocked.Decrement(ref this.ReferenceCount);
}
Expand Down Expand Up @@ -666,7 +662,7 @@ internal void Update(double number)
// by ignoring Zero points
this.MetricPointStatus = MetricPointStatus.CollectPending;

if (this.aggregatorStore.OutputDelta)
if (this.aggregatorStore.OutputDeltaWithUnusedMetricPointReclaimEnabled)
{
Interlocked.Decrement(ref this.ReferenceCount);
}
Expand Down Expand Up @@ -797,7 +793,7 @@ internal void UpdateWithExemplar(double number, ReadOnlySpan<KeyValuePair<string
// by ignoring Zero points
this.MetricPointStatus = MetricPointStatus.CollectPending;

if (this.aggregatorStore.OutputDelta)
if (this.aggregatorStore.OutputDeltaWithUnusedMetricPointReclaimEnabled)
{
Interlocked.Decrement(ref this.ReferenceCount);
}
Expand Down

0 comments on commit ef942a1

Please sign in to comment.