Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust Cardinality Limit to Accommodate Internal Reserves #5382

Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions src/OpenTelemetry/Metrics/AggregatorStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal sealed class AggregatorStore
internal readonly HashSet<string>? TagKeysInteresting;
internal readonly bool OutputDelta;
internal readonly bool OutputDeltaWithUnusedMetricPointReclaimEnabled;
internal readonly int CardinalityLimit;
internal readonly int NumberOfReservedMetricPoints;
internal readonly bool EmitOverflowAttribute;
internal readonly ConcurrentDictionary<Tags, LookupData>? TagsToMetricPointIndexDictionaryDelta;
internal readonly Func<ExemplarReservoir?>? ExemplarReservoirFactory;
Expand Down Expand Up @@ -64,11 +64,15 @@ internal AggregatorStore(
Func<ExemplarReservoir?>? exemplarReservoirFactory = null)
{
this.name = metricStreamIdentity.InstrumentName;
this.CardinalityLimit = cardinalityLimit;

this.metricPointCapHitMessage = $"Maximum MetricPoints limit reached for this Metric stream. Configured limit: {this.CardinalityLimit}";
this.metricPoints = new MetricPoint[cardinalityLimit];
this.currentMetricPointBatch = new int[cardinalityLimit];
// Increase the CardinalityLimit by 2 to reserve additional space.
// This adjustment accounts for overflow attribute and a case where zero tags are provided.
// Previously, these were included within the original cardinalityLimit, but now they are explicitly added to enhance clarity.
this.NumberOfReservedMetricPoints = cardinalityLimit + 2;

this.metricPointCapHitMessage = $"Maximum MetricPoints limit reached for this Metric stream. Configured limit: {this.NumberOfReservedMetricPoints}";
rajkumar-rangaraj marked this conversation as resolved.
Show resolved Hide resolved
this.metricPoints = new MetricPoint[this.NumberOfReservedMetricPoints];
this.currentMetricPointBatch = new int[this.NumberOfReservedMetricPoints];
this.aggType = aggType;
this.OutputDelta = temporality == AggregationTemporality.Delta;
this.histogramBounds = metricStreamIdentity.HistogramBucketBounds ?? FindDefaultHistogramBounds(in metricStreamIdentity);
Expand Down Expand Up @@ -107,17 +111,17 @@ internal AggregatorStore(

if (this.OutputDeltaWithUnusedMetricPointReclaimEnabled)
rajkumar-rangaraj marked this conversation as resolved.
Show resolved Hide resolved
{
this.availableMetricPoints = new Queue<int>(cardinalityLimit - reservedMetricPointsCount);
this.availableMetricPoints = new Queue<int>(this.NumberOfReservedMetricPoints - reservedMetricPointsCount);
rajkumar-rangaraj marked this conversation as resolved.
Show resolved Hide resolved

// There is no overload which only takes capacity as the parameter
// Using the DefaultConcurrencyLevel defined in the ConcurrentDictionary class: https://github.com/dotnet/runtime/blob/v7.0.5/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentDictionary.cs#L2020
// We expect at the most (maxMetricPoints - reservedMetricPointsCount) * 2 entries- one for sorted and one for unsorted input
this.TagsToMetricPointIndexDictionaryDelta =
new ConcurrentDictionary<Tags, LookupData>(concurrencyLevel: Environment.ProcessorCount, capacity: (cardinalityLimit - reservedMetricPointsCount) * 2);
new ConcurrentDictionary<Tags, LookupData>(concurrencyLevel: Environment.ProcessorCount, capacity: (this.NumberOfReservedMetricPoints - reservedMetricPointsCount) * 2);
rajkumar-rangaraj marked this conversation as resolved.
Show resolved Hide resolved

// Add all the indices except for the reserved ones to the queue so that threads have
// readily available access to these MetricPoints for their use.
for (int i = reservedMetricPointsCount; i < this.CardinalityLimit; i++)
for (int i = reservedMetricPointsCount; i < this.NumberOfReservedMetricPoints; i++)
{
this.availableMetricPoints.Enqueue(i);
}
Expand Down Expand Up @@ -166,12 +170,12 @@ internal int Snapshot()
}
else if (this.OutputDelta)
{
var indexSnapshot = Math.Min(this.metricPointIndex, this.CardinalityLimit - 1);
var indexSnapshot = Math.Min(this.metricPointIndex, this.NumberOfReservedMetricPoints - 1);
this.SnapshotDelta(indexSnapshot);
}
else
{
var indexSnapshot = Math.Min(this.metricPointIndex, this.CardinalityLimit - 1);
var indexSnapshot = Math.Min(this.metricPointIndex, this.NumberOfReservedMetricPoints - 1);
this.SnapshotCumulative(indexSnapshot);
}

Expand Down Expand Up @@ -251,7 +255,7 @@ internal void SnapshotDeltaWithMetricPointReclaim()
}
}

for (int i = startIndexForReclaimableMetricPoints; i < this.CardinalityLimit; i++)
for (int i = startIndexForReclaimableMetricPoints; i < this.NumberOfReservedMetricPoints; i++)
rajkumar-rangaraj marked this conversation as resolved.
Show resolved Hide resolved
{
ref var metricPoint = ref this.metricPoints[i];

Expand Down Expand Up @@ -442,7 +446,7 @@ private int LookupAggregatorStore(KeyValuePair<string, object?>[] tagKeysAndValu
if (!this.tagsToMetricPointIndexDictionary.TryGetValue(sortedTags, out aggregatorIndex))
{
aggregatorIndex = this.metricPointIndex;
if (aggregatorIndex >= this.CardinalityLimit)
if (aggregatorIndex >= this.NumberOfReservedMetricPoints)
{
// sorry! out of data points.
// TODO: Once we support cleanup of
Expand Down Expand Up @@ -471,7 +475,7 @@ private int LookupAggregatorStore(KeyValuePair<string, object?>[] tagKeysAndValu
if (!this.tagsToMetricPointIndexDictionary.TryGetValue(sortedTags, out aggregatorIndex))
{
aggregatorIndex = ++this.metricPointIndex;
if (aggregatorIndex >= this.CardinalityLimit)
if (aggregatorIndex >= this.NumberOfReservedMetricPoints)
{
// sorry! out of data points.
// TODO: Once we support cleanup of
Expand All @@ -498,7 +502,7 @@ private int LookupAggregatorStore(KeyValuePair<string, object?>[] tagKeysAndValu
{
// This else block is for tag length = 1
aggregatorIndex = this.metricPointIndex;
if (aggregatorIndex >= this.CardinalityLimit)
if (aggregatorIndex >= this.NumberOfReservedMetricPoints)
{
// sorry! out of data points.
// TODO: Once we support cleanup of
Expand All @@ -520,7 +524,7 @@ private int LookupAggregatorStore(KeyValuePair<string, object?>[] tagKeysAndValu
if (!this.tagsToMetricPointIndexDictionary.TryGetValue(givenTags, out aggregatorIndex))
{
aggregatorIndex = ++this.metricPointIndex;
if (aggregatorIndex >= this.CardinalityLimit)
if (aggregatorIndex >= this.NumberOfReservedMetricPoints)
{
// sorry! out of data points.
// TODO: Once we support cleanup of
Expand Down
8 changes: 6 additions & 2 deletions src/OpenTelemetry/Metrics/MetricStreamConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,12 @@ public string[]? TagKeys
/// <para>Spec reference: <see
/// href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#cardinality-limits">Cardinality
/// limits</see>.</para>
/// Note: If not set the default MeterProvider cardinality limit of 2000
/// will apply.
/// Note: The cardinality limit determines the maximum number of unique
/// dimension combinations for metrics.
/// Metrics with zero dimensions and overflow metrics are treated specially
/// and do not count against this limit.
/// If not set the default
/// MeterProvider cardinality limit of 2000 will apply.
/// </remarks>
#if NET8_0_OR_GREATER
[Experimental(DiagnosticDefinitions.CardinalityLimitExperimentalApi, UrlFormat = DiagnosticDefinitions.ExperimentalApiUrlFormat)]
Expand Down
8 changes: 5 additions & 3 deletions test/OpenTelemetry.Tests/Metrics/MetricApiTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1391,6 +1391,8 @@ public void TestInstrumentDisposal(MetricReaderTemporalityPreference temporality
[InlineData(MetricReaderTemporalityPreference.Delta)]
public void TestMetricPointCap(MetricReaderTemporalityPreference temporality)
{
// Constant to account for additional space for overflow attribute and a case with zero Tags.
var additionalReserve = 2;
var exportedItems = new List<Metric>();

int MetricPointCount()
Expand Down Expand Up @@ -1429,7 +1431,7 @@ int MetricPointCount()
}

meterProvider.ForceFlush(MaxTimeToAllowForFlush);
Assert.Equal(MeterProviderBuilderSdk.DefaultCardinalityLimit, MetricPointCount());
Assert.Equal(MeterProviderBuilderSdk.DefaultCardinalityLimit + additionalReserve, MetricPointCount());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test should verify that we don't allow the user to exceed the cardinality cap. The right thing to test here now would be to check that count of metric points exported (excluding zero tags and overflow attribute) is equal to MeterProviderBuilderSdk.DefaultCardinalityLimit. We probably need to update MetricPointCount() method to return the count of unreserved MetricPoints instead of all the MetricPoints that were exported in that particular Collect cycle.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The overflow is guarded with an experimental flag. In cases where the overflow experimental attribute is not set, the value will be the cardinality limit + 1.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you have now updated MetricPointCount() method definition to account for that.


exportedItems.Clear();
counterLong.Add(10);
Expand All @@ -1439,7 +1441,7 @@ int MetricPointCount()
}

meterProvider.ForceFlush(MaxTimeToAllowForFlush);
Assert.Equal(MeterProviderBuilderSdk.DefaultCardinalityLimit, MetricPointCount());
Assert.Equal(MeterProviderBuilderSdk.DefaultCardinalityLimit + additionalReserve, MetricPointCount());

counterLong.Add(10);
for (int i = 0; i < MeterProviderBuilderSdk.DefaultCardinalityLimit + 1; i++)
Expand All @@ -1453,7 +1455,7 @@ int MetricPointCount()
counterLong.Add(10, new KeyValuePair<string, object>("key", "valueC"));
exportedItems.Clear();
meterProvider.ForceFlush(MaxTimeToAllowForFlush);
Assert.Equal(MeterProviderBuilderSdk.DefaultCardinalityLimit, MetricPointCount());
Assert.Equal(MeterProviderBuilderSdk.DefaultCardinalityLimit + additionalReserve, MetricPointCount());
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public void MetricOverflowAttributeIsRecordedCorrectlyForCounter(MetricReaderTem
counter.Add(10); // Record measurement for zero tags

// Max number for MetricPoints available for use when emitted with tags
int maxMetricPointsForUse = MeterProviderBuilderSdk.DefaultCardinalityLimit - 2;
int maxMetricPointsForUse = MeterProviderBuilderSdk.DefaultCardinalityLimit;

for (int i = 0; i < maxMetricPointsForUse; i++)
{
Expand Down Expand Up @@ -186,7 +186,7 @@ public void MetricOverflowAttributeIsRecordedCorrectlyForCounter(MetricReaderTem
exportedItems.Clear();
metricPoints.Clear();

counter.Add(5, new KeyValuePair<string, object>("Key", 1998)); // Emit a metric to exceed the max MetricPoint limit
counter.Add(5, new KeyValuePair<string, object>("Key", 2000)); // Emit a metric to exceed the max MetricPoint limit

meterProvider.ForceFlush();
metric = exportedItems[0];
Expand Down Expand Up @@ -215,7 +215,7 @@ public void MetricOverflowAttributeIsRecordedCorrectlyForCounter(MetricReaderTem
counter.Add(15); // Record another measurement for zero tags

// Emit 2500 more newer MetricPoints with distinct dimension combinations
for (int i = 2000; i < 4500; i++)
for (int i = 2002; i < 4502; i++)
rajkumar-rangaraj marked this conversation as resolved.
Show resolved Hide resolved
{
counter.Add(5, new KeyValuePair<string, object>("Key", i));
}
Expand All @@ -236,11 +236,11 @@ public void MetricOverflowAttributeIsRecordedCorrectlyForCounter(MetricReaderTem

int expectedSum;

// Number of metric points that were available before the 2500 measurements were made = 2000 (max MetricPoints) - 2 (reserved for zero tags and overflow) = 1998
// Number of metric points that were available before the 2500 measurements were made = 2000 (max MetricPoints)
if (this.shouldReclaimUnusedMetricPoints)
{
// If unused metric points are reclaimed, then number of metric points dropped = 2500 - 1998 = 502
expectedSum = 2510; // 502 * 5
// If unused metric points are reclaimed, then number of metric points dropped = 2500 - 2000 = 500
expectedSum = 2500; // 500 * 5
}
else
{
Expand Down Expand Up @@ -309,7 +309,7 @@ public void MetricOverflowAttributeIsRecordedCorrectlyForHistogram(MetricReaderT
histogram.Record(10); // Record measurement for zero tags

// Max number for MetricPoints available for use when emitted with tags
int maxMetricPointsForUse = MeterProviderBuilderSdk.DefaultCardinalityLimit - 2;
int maxMetricPointsForUse = MeterProviderBuilderSdk.DefaultCardinalityLimit;

for (int i = 0; i < maxMetricPointsForUse; i++)
{
Expand Down Expand Up @@ -337,7 +337,7 @@ public void MetricOverflowAttributeIsRecordedCorrectlyForHistogram(MetricReaderT
exportedItems.Clear();
metricPoints.Clear();

histogram.Record(5, new KeyValuePair<string, object>("Key", 1998)); // Emit a metric to exceed the max MetricPoint limit
histogram.Record(5, new KeyValuePair<string, object>("Key", 2000)); // Emit a metric to exceed the max MetricPoint limit

meterProvider.ForceFlush();
metric = exportedItems[0];
Expand Down Expand Up @@ -366,7 +366,7 @@ public void MetricOverflowAttributeIsRecordedCorrectlyForHistogram(MetricReaderT
histogram.Record(15); // Record another measurement for zero tags

// Emit 2500 more newer MetricPoints with distinct dimension combinations
for (int i = 2000; i < 4500; i++)
for (int i = 2002; i < 4502; i++)
rajkumar-rangaraj marked this conversation as resolved.
Show resolved Hide resolved
{
histogram.Record(5, new KeyValuePair<string, object>("Key", i));
}
Expand All @@ -388,12 +388,12 @@ public void MetricOverflowAttributeIsRecordedCorrectlyForHistogram(MetricReaderT
int expectedCount;
int expectedSum;

// Number of metric points that were available before the 2500 measurements were made = 2000 (max MetricPoints) - 2 (reserved for zero tags and overflow) = 1998
// Number of metric points that were available before the 2500 measurements were made = 2000 (max MetricPoints)
if (this.shouldReclaimUnusedMetricPoints)
{
// If unused metric points are reclaimed, then number of metric points dropped = 2500 - 1998 = 502
expectedCount = 502;
expectedSum = 2510; // 502 * 5
// If unused metric points are reclaimed, then number of metric points dropped = 2500 - 2000 = 500
expectedCount = 500;
expectedSum = 2500; // 500 * 5
}
else
{
Expand All @@ -407,7 +407,6 @@ public void MetricOverflowAttributeIsRecordedCorrectlyForHistogram(MetricReaderT
else
{
Assert.Equal(25, zeroTagsMetricPoint.GetHistogramSum());

Assert.Equal(2501, overflowMetricPoint.GetHistogramCount());
Assert.Equal(12505, overflowMetricPoint.GetHistogramSum()); // 5 + (2500 * 5)
}
Expand Down
16 changes: 8 additions & 8 deletions test/OpenTelemetry.Tests/Metrics/MetricViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -963,16 +963,16 @@ public void CardinalityLimitofMatchingViewTakesPrecedenceOverMeterProvider(bool

Assert.Equal(3, exportedItems.Count);

Assert.Equal(10000, exportedItems[1].AggregatorStore.CardinalityLimit);
Assert.Equal(10002, exportedItems[1].AggregatorStore.NumberOfReservedMetricPoints);
if (setDefault)
{
Assert.Equal(3, exportedItems[0].AggregatorStore.CardinalityLimit);
Assert.Equal(3, exportedItems[2].AggregatorStore.CardinalityLimit);
Assert.Equal(5, exportedItems[0].AggregatorStore.NumberOfReservedMetricPoints);
Assert.Equal(5, exportedItems[2].AggregatorStore.NumberOfReservedMetricPoints);
}
else
{
Assert.Equal(2000, exportedItems[0].AggregatorStore.CardinalityLimit);
Assert.Equal(2000, exportedItems[2].AggregatorStore.CardinalityLimit);
Assert.Equal(2002, exportedItems[0].AggregatorStore.NumberOfReservedMetricPoints);
Assert.Equal(2002, exportedItems[2].AggregatorStore.NumberOfReservedMetricPoints);
}
}

Expand Down Expand Up @@ -1015,15 +1015,15 @@ public void ViewConflict_TwoDistinctInstruments_ThreeStreams()
var metricB = exportedItems[1];
var metricC = exportedItems[2];

Assert.Equal(256, metricA.AggregatorStore.CardinalityLimit);
Assert.Equal(258, metricA.AggregatorStore.NumberOfReservedMetricPoints);
Assert.Equal("MetricStreamA", metricA.Name);
Assert.Equal(20, GetAggregatedValue(metricA));

Assert.Equal(3, metricB.AggregatorStore.CardinalityLimit);
Assert.Equal(5, metricB.AggregatorStore.NumberOfReservedMetricPoints);
Assert.Equal("MetricStreamB", metricB.Name);
Assert.Equal(10, GetAggregatedValue(metricB));

Assert.Equal(200000, metricC.AggregatorStore.CardinalityLimit);
Assert.Equal(200002, metricC.AggregatorStore.NumberOfReservedMetricPoints);
Assert.Equal("MetricStreamC", metricC.Name);
Assert.Equal(10, GetAggregatedValue(metricC));

Expand Down