Skip to content

Commit f1a1835

Browse files
CodeBlanchutpilla
andauthored
[sdk-metrics] Remove some reflection used by tests (#5338)
Co-authored-by: Utkarsh Umesan Pillai <66651184+utpilla@users.noreply.github.com>
1 parent 8325786 commit f1a1835

File tree

2 files changed

+34
-43
lines changed

2 files changed

+34
-43
lines changed

src/OpenTelemetry/Metrics/AggregatorStore.cs

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ internal sealed class AggregatorStore
1515
internal readonly bool OutputDeltaWithUnusedMetricPointReclaimEnabled;
1616
internal readonly int CardinalityLimit;
1717
internal readonly bool EmitOverflowAttribute;
18+
internal readonly ConcurrentDictionary<Tags, LookupData>? TagsToMetricPointIndexDictionaryDelta;
1819
internal long DroppedMeasurements = 0;
1920

2021
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.";
@@ -32,8 +33,6 @@ internal sealed class AggregatorStore
3233
private readonly ConcurrentDictionary<Tags, int> tagsToMetricPointIndexDictionary =
3334
new();
3435

35-
private readonly ConcurrentDictionary<Tags, LookupData>? tagsToMetricPointIndexDictionaryDelta;
36-
3736
private readonly string name;
3837
private readonly string metricPointCapHitMessage;
3938
private readonly MetricPoint[] metricPoints;
@@ -110,7 +109,7 @@ internal AggregatorStore(
110109
// There is no overload which only takes capacity as the parameter
111110
// 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
112111
// We expect at the most (maxMetricPoints - reservedMetricPointsCount) * 2 entries- one for sorted and one for unsorted input
113-
this.tagsToMetricPointIndexDictionaryDelta =
112+
this.TagsToMetricPointIndexDictionaryDelta =
114113
new ConcurrentDictionary<Tags, LookupData>(concurrencyLevel: Environment.ProcessorCount, capacity: (cardinalityLimit - reservedMetricPointsCount) * 2);
115114

116115
// Add all the indices except for the reserved ones to the queue so that threads have
@@ -266,28 +265,28 @@ internal void SnapshotDeltaWithMetricPointReclaim()
266265
// Snapshot method can use this to skip trying to reclaim indices which have already been reclaimed and added to the queue.
267266
metricPoint.LookupData = null;
268267

269-
Debug.Assert(this.tagsToMetricPointIndexDictionaryDelta != null, "this.tagsToMetricPointIndexDictionaryDelta was null");
268+
Debug.Assert(this.TagsToMetricPointIndexDictionaryDelta != null, "this.tagsToMetricPointIndexDictionaryDelta was null");
270269

271-
lock (this.tagsToMetricPointIndexDictionaryDelta!)
270+
lock (this.TagsToMetricPointIndexDictionaryDelta!)
272271
{
273272
LookupData? dictionaryValue;
274273
if (lookupData.SortedTags != Tags.EmptyTags)
275274
{
276275
// Check if no other thread added a new entry for the same Tags.
277276
// If no, then remove the existing entries.
278-
if (this.tagsToMetricPointIndexDictionaryDelta.TryGetValue(lookupData.SortedTags, out dictionaryValue) &&
277+
if (this.TagsToMetricPointIndexDictionaryDelta.TryGetValue(lookupData.SortedTags, out dictionaryValue) &&
279278
dictionaryValue == lookupData)
280279
{
281-
this.tagsToMetricPointIndexDictionaryDelta.TryRemove(lookupData.SortedTags, out var _);
282-
this.tagsToMetricPointIndexDictionaryDelta.TryRemove(lookupData.GivenTags, out var _);
280+
this.TagsToMetricPointIndexDictionaryDelta.TryRemove(lookupData.SortedTags, out var _);
281+
this.TagsToMetricPointIndexDictionaryDelta.TryRemove(lookupData.GivenTags, out var _);
283282
}
284283
}
285284
else
286285
{
287-
if (this.tagsToMetricPointIndexDictionaryDelta.TryGetValue(lookupData.GivenTags, out dictionaryValue) &&
286+
if (this.TagsToMetricPointIndexDictionaryDelta.TryGetValue(lookupData.GivenTags, out dictionaryValue) &&
288287
dictionaryValue == lookupData)
289288
{
290-
this.tagsToMetricPointIndexDictionaryDelta.TryRemove(lookupData.GivenTags, out var _);
289+
this.TagsToMetricPointIndexDictionaryDelta.TryRemove(lookupData.GivenTags, out var _);
291290
}
292291
}
293292

@@ -550,11 +549,11 @@ private int LookupAggregatorStoreForDeltaWithReclaim(KeyValuePair<string, object
550549
int index;
551550
var givenTags = new Tags(tagKeysAndValues);
552551

553-
Debug.Assert(this.tagsToMetricPointIndexDictionaryDelta != null, "this.tagsToMetricPointIndexDictionaryDelta was null");
552+
Debug.Assert(this.TagsToMetricPointIndexDictionaryDelta != null, "this.tagsToMetricPointIndexDictionaryDelta was null");
554553

555554
bool newMetricPointCreated = false;
556555

557-
if (!this.tagsToMetricPointIndexDictionaryDelta!.TryGetValue(givenTags, out var lookupData))
556+
if (!this.TagsToMetricPointIndexDictionaryDelta!.TryGetValue(givenTags, out var lookupData))
558557
{
559558
if (length > 1)
560559
{
@@ -567,7 +566,7 @@ private int LookupAggregatorStoreForDeltaWithReclaim(KeyValuePair<string, object
567566

568567
var sortedTags = new Tags(tempSortedTagKeysAndValues);
569568

570-
if (!this.tagsToMetricPointIndexDictionaryDelta.TryGetValue(sortedTags, out lookupData))
569+
if (!this.TagsToMetricPointIndexDictionaryDelta.TryGetValue(sortedTags, out lookupData))
571570
{
572571
// Note: We are using storage from ThreadStatic (for up to MaxTagCacheSize tags) for both the input order of tags and the sorted order of tags,
573572
// so we need to make a deep copy for Dictionary storage.
@@ -585,10 +584,10 @@ private int LookupAggregatorStoreForDeltaWithReclaim(KeyValuePair<string, object
585584

586585
Debug.Assert(this.availableMetricPoints != null, "this.availableMetricPoints was null");
587586

588-
lock (this.tagsToMetricPointIndexDictionaryDelta)
587+
lock (this.TagsToMetricPointIndexDictionaryDelta)
589588
{
590589
// check again after acquiring lock.
591-
if (!this.tagsToMetricPointIndexDictionaryDelta.TryGetValue(sortedTags, out lookupData))
590+
if (!this.TagsToMetricPointIndexDictionaryDelta.TryGetValue(sortedTags, out lookupData))
592591
{
593592
// Check for an available MetricPoint
594593
if (this.availableMetricPoints!.Count > 0)
@@ -612,8 +611,8 @@ private int LookupAggregatorStoreForDeltaWithReclaim(KeyValuePair<string, object
612611
// MetricPoint, if dictionary entry found.
613612

614613
// Add the sorted order along with the given order of tags
615-
this.tagsToMetricPointIndexDictionaryDelta.TryAdd(sortedTags, lookupData);
616-
this.tagsToMetricPointIndexDictionaryDelta.TryAdd(givenTags, lookupData);
614+
this.TagsToMetricPointIndexDictionaryDelta.TryAdd(sortedTags, lookupData);
615+
this.TagsToMetricPointIndexDictionaryDelta.TryAdd(givenTags, lookupData);
617616
}
618617
}
619618
}
@@ -631,10 +630,10 @@ private int LookupAggregatorStoreForDeltaWithReclaim(KeyValuePair<string, object
631630

632631
Debug.Assert(this.availableMetricPoints != null, "this.availableMetricPoints was null");
633632

634-
lock (this.tagsToMetricPointIndexDictionaryDelta)
633+
lock (this.TagsToMetricPointIndexDictionaryDelta)
635634
{
636635
// check again after acquiring lock.
637-
if (!this.tagsToMetricPointIndexDictionaryDelta.TryGetValue(givenTags, out lookupData))
636+
if (!this.TagsToMetricPointIndexDictionaryDelta.TryGetValue(givenTags, out lookupData))
638637
{
639638
// Check for an available MetricPoint
640639
if (this.availableMetricPoints!.Count > 0)
@@ -658,7 +657,7 @@ private int LookupAggregatorStoreForDeltaWithReclaim(KeyValuePair<string, object
658657
// MetricPoint, if dictionary entry found.
659658

660659
// givenTags will always be sorted when tags length == 1
661-
this.tagsToMetricPointIndexDictionaryDelta.TryAdd(givenTags, lookupData);
660+
this.TagsToMetricPointIndexDictionaryDelta.TryAdd(givenTags, lookupData);
662661
}
663662
}
664663
}
@@ -735,7 +734,7 @@ private bool TryGetAvailableMetricPointRare(
735734
out LookupData? lookupData,
736735
out bool newMetricPointCreated)
737736
{
738-
Debug.Assert(this.tagsToMetricPointIndexDictionaryDelta != null, "this.tagsToMetricPointIndexDictionaryDelta was null");
737+
Debug.Assert(this.TagsToMetricPointIndexDictionaryDelta != null, "this.tagsToMetricPointIndexDictionaryDelta was null");
739738
Debug.Assert(this.availableMetricPoints != null, "this.availableMetricPoints was null");
740739

741740
int index;
@@ -744,8 +743,8 @@ private bool TryGetAvailableMetricPointRare(
744743
if (length > 1)
745744
{
746745
// check again after acquiring lock.
747-
if (!this.tagsToMetricPointIndexDictionaryDelta!.TryGetValue(givenTags, out lookupData) &&
748-
!this.tagsToMetricPointIndexDictionaryDelta.TryGetValue(sortedTags, out lookupData))
746+
if (!this.TagsToMetricPointIndexDictionaryDelta!.TryGetValue(givenTags, out lookupData) &&
747+
!this.TagsToMetricPointIndexDictionaryDelta.TryGetValue(sortedTags, out lookupData))
749748
{
750749
// Check for an available MetricPoint
751750
if (this.availableMetricPoints!.Count > 0)
@@ -769,14 +768,14 @@ private bool TryGetAvailableMetricPointRare(
769768
// MetricPoint, if dictionary entry found.
770769

771770
// Add the sorted order along with the given order of tags
772-
this.tagsToMetricPointIndexDictionaryDelta.TryAdd(sortedTags, lookupData);
773-
this.tagsToMetricPointIndexDictionaryDelta.TryAdd(givenTags, lookupData);
771+
this.TagsToMetricPointIndexDictionaryDelta.TryAdd(sortedTags, lookupData);
772+
this.TagsToMetricPointIndexDictionaryDelta.TryAdd(givenTags, lookupData);
774773
}
775774
}
776775
else
777776
{
778777
// check again after acquiring lock.
779-
if (!this.tagsToMetricPointIndexDictionaryDelta!.TryGetValue(givenTags, out lookupData))
778+
if (!this.TagsToMetricPointIndexDictionaryDelta!.TryGetValue(givenTags, out lookupData))
780779
{
781780
// Check for an available MetricPoint
782781
if (this.availableMetricPoints!.Count > 0)
@@ -800,7 +799,7 @@ private bool TryGetAvailableMetricPointRare(
800799
// MetricPoint, if dictionary entry found.
801800

802801
// givenTags will always be sorted when tags length == 1
803-
this.tagsToMetricPointIndexDictionaryDelta.TryAdd(givenTags, lookupData);
802+
this.TagsToMetricPointIndexDictionaryDelta.TryAdd(givenTags, lookupData);
804803
}
805804
}
806805

@@ -823,23 +822,23 @@ private int RemoveStaleEntriesAndGetAvailableMetricPointRare(LookupData lookupDa
823822
// If self-claimed, then add a fresh entry to the dictionary
824823
// If an available MetricPoint is found, then only increment the ReferenceCount
825824

826-
Debug.Assert(this.tagsToMetricPointIndexDictionaryDelta != null, "this.tagsToMetricPointIndexDictionaryDelta was null");
825+
Debug.Assert(this.TagsToMetricPointIndexDictionaryDelta != null, "this.tagsToMetricPointIndexDictionaryDelta was null");
827826

828827
// Delete the entry for these Tags and get another MetricPoint.
829-
lock (this.tagsToMetricPointIndexDictionaryDelta!)
828+
lock (this.TagsToMetricPointIndexDictionaryDelta!)
830829
{
831830
LookupData? dictionaryValue;
832831
if (lookupData.SortedTags != Tags.EmptyTags)
833832
{
834833
// Check if no other thread added a new entry for the same Tags in the meantime.
835834
// If no, then remove the existing entries.
836-
if (this.tagsToMetricPointIndexDictionaryDelta.TryGetValue(lookupData.SortedTags, out dictionaryValue))
835+
if (this.TagsToMetricPointIndexDictionaryDelta.TryGetValue(lookupData.SortedTags, out dictionaryValue))
837836
{
838837
if (dictionaryValue == lookupData)
839838
{
840839
// No other thread added a new entry for the same Tags.
841-
this.tagsToMetricPointIndexDictionaryDelta.TryRemove(lookupData.SortedTags, out _);
842-
this.tagsToMetricPointIndexDictionaryDelta.TryRemove(lookupData.GivenTags, out _);
840+
this.TagsToMetricPointIndexDictionaryDelta.TryRemove(lookupData.SortedTags, out _);
841+
this.TagsToMetricPointIndexDictionaryDelta.TryRemove(lookupData.GivenTags, out _);
843842
}
844843
else
845844
{
@@ -851,12 +850,12 @@ private int RemoveStaleEntriesAndGetAvailableMetricPointRare(LookupData lookupDa
851850
}
852851
else
853852
{
854-
if (this.tagsToMetricPointIndexDictionaryDelta.TryGetValue(lookupData.GivenTags, out dictionaryValue))
853+
if (this.TagsToMetricPointIndexDictionaryDelta.TryGetValue(lookupData.GivenTags, out dictionaryValue))
855854
{
856855
if (dictionaryValue == lookupData)
857856
{
858857
// No other thread added a new entry for the same Tags.
859-
this.tagsToMetricPointIndexDictionaryDelta.TryRemove(lookupData.GivenTags, out _);
858+
this.TagsToMetricPointIndexDictionaryDelta.TryRemove(lookupData.GivenTags, out _);
860859
}
861860
else
862861
{

test/OpenTelemetry.Tests/Metrics/MetricPointReclaimTestsBase.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
using System.Collections.Concurrent;
54
using System.Diagnostics.Metrics;
6-
using System.Reflection;
75
using Microsoft.Extensions.Configuration;
86
using Microsoft.Extensions.DependencyInjection;
97
using OpenTelemetry.Tests;
@@ -286,23 +284,17 @@ private sealed class CustomExporter : BaseExporter<Metric>
286284

287285
private readonly bool assertNoDroppedMeasurements;
288286

289-
private readonly FieldInfo metricPointLookupDictionaryFieldInfo;
290-
291287
public CustomExporter(bool assertNoDroppedMeasurements)
292288
{
293289
this.assertNoDroppedMeasurements = assertNoDroppedMeasurements;
294-
295-
var aggregatorStoreFields = typeof(AggregatorStore).GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
296-
this.metricPointLookupDictionaryFieldInfo = aggregatorStoreFields!.FirstOrDefault(field => field.Name == "tagsToMetricPointIndexDictionaryDelta");
297290
}
298291

299292
public override ExportResult Export(in Batch<Metric> batch)
300293
{
301294
foreach (var metric in batch)
302295
{
303296
var aggStore = metric.AggregatorStore;
304-
var metricPointLookupDictionary = this.metricPointLookupDictionaryFieldInfo.GetValue(aggStore) as ConcurrentDictionary<Tags, LookupData>;
305-
297+
var metricPointLookupDictionary = aggStore.TagsToMetricPointIndexDictionaryDelta;
306298
var droppedMeasurements = aggStore.DroppedMeasurements;
307299

308300
if (this.assertNoDroppedMeasurements)

0 commit comments

Comments
 (0)