-
Notifications
You must be signed in to change notification settings - Fork 765
/
AggregatorStore.cs
1131 lines (953 loc) · 51.1 KB
/
AggregatorStore.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using System.Collections.Concurrent;
#if NET
using System.Collections.Frozen;
#endif
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using OpenTelemetry.Internal;
namespace OpenTelemetry.Metrics;
internal sealed class AggregatorStore
{
#if NET
internal readonly FrozenSet<string>? TagKeysInteresting;
#else
internal readonly HashSet<string>? TagKeysInteresting;
#endif
internal readonly bool OutputDelta;
internal readonly bool OutputDeltaWithUnusedMetricPointReclaimEnabled;
internal readonly int NumberOfMetricPoints;
internal readonly bool EmitOverflowAttribute;
internal readonly ConcurrentDictionary<Tags, LookupData>? TagsToMetricPointIndexDictionaryDelta;
internal readonly Func<ExemplarReservoir?>? ExemplarReservoirFactory;
internal long DroppedMeasurements = 0;
private const ExemplarFilterType DefaultExemplarFilter = ExemplarFilterType.AlwaysOff;
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.";
private static readonly Comparison<KeyValuePair<string, object?>> DimensionComparisonDelegate = (x, y) => x.Key.CompareTo(y.Key);
private readonly object lockZeroTags = new();
private readonly object lockOverflowTag = new();
private readonly int tagsKeysInterestingCount;
// This holds the reclaimed MetricPoints that are available for reuse.
private readonly Queue<int>? availableMetricPoints;
private readonly ConcurrentDictionary<Tags, int> tagsToMetricPointIndexDictionary =
new();
private readonly string name;
private readonly string metricPointCapHitMessage;
private readonly MetricPoint[] metricPoints;
private readonly int[] currentMetricPointBatch;
private readonly AggregationType aggType;
private readonly double[] histogramBounds;
private readonly int exponentialHistogramMaxSize;
private readonly int exponentialHistogramMaxScale;
private readonly UpdateLongDelegate updateLongCallback;
private readonly UpdateDoubleDelegate updateDoubleCallback;
private readonly ExemplarFilterType exemplarFilter;
private readonly Func<KeyValuePair<string, object?>[], int, int> lookupAggregatorStore;
private int metricPointIndex = 0;
private int batchSize = 0;
private int metricCapHitMessageLogged;
private bool zeroTagMetricPointInitialized;
private bool overflowTagMetricPointInitialized;
internal AggregatorStore(
MetricStreamIdentity metricStreamIdentity,
AggregationType aggType,
AggregationTemporality temporality,
int cardinalityLimit,
bool emitOverflowAttribute,
bool shouldReclaimUnusedMetricPoints,
ExemplarFilterType? exemplarFilter = null,
Func<ExemplarReservoir?>? exemplarReservoirFactory = null)
{
this.name = metricStreamIdentity.InstrumentName;
// 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.NumberOfMetricPoints = cardinalityLimit + 2;
this.metricPointCapHitMessage = $"Maximum MetricPoints limit reached for this Metric stream. Configured limit: {cardinalityLimit}";
this.metricPoints = new MetricPoint[this.NumberOfMetricPoints];
this.currentMetricPointBatch = new int[this.NumberOfMetricPoints];
this.aggType = aggType;
this.OutputDelta = temporality == AggregationTemporality.Delta;
this.histogramBounds = metricStreamIdentity.HistogramBucketBounds ?? FindDefaultHistogramBounds(in metricStreamIdentity);
this.exponentialHistogramMaxSize = metricStreamIdentity.ExponentialHistogramMaxSize;
this.exponentialHistogramMaxScale = metricStreamIdentity.ExponentialHistogramMaxScale;
this.StartTimeExclusive = DateTimeOffset.UtcNow;
this.ExemplarReservoirFactory = exemplarReservoirFactory;
if (metricStreamIdentity.TagKeys == null)
{
this.updateLongCallback = this.UpdateLong;
this.updateDoubleCallback = this.UpdateDouble;
}
else
{
this.updateLongCallback = this.UpdateLongCustomTags;
this.updateDoubleCallback = this.UpdateDoubleCustomTags;
#if NET
var hs = FrozenSet.ToFrozenSet(metricStreamIdentity.TagKeys, StringComparer.Ordinal);
#else
var hs = new HashSet<string>(metricStreamIdentity.TagKeys, StringComparer.Ordinal);
#endif
this.TagKeysInteresting = hs;
this.tagsKeysInterestingCount = hs.Count;
}
this.EmitOverflowAttribute = emitOverflowAttribute;
this.exemplarFilter = exemplarFilter ?? DefaultExemplarFilter;
Debug.Assert(
this.exemplarFilter == ExemplarFilterType.AlwaysOff
|| this.exemplarFilter == ExemplarFilterType.AlwaysOn
|| this.exemplarFilter == ExemplarFilterType.TraceBased,
"this.exemplarFilter had an unexpected value");
// Setting metricPointIndex to 1 as we would reserve the metricPoints[1] for overflow attribute.
// Newer attributes should be added starting at the index: 2
this.metricPointIndex = 1;
this.OutputDeltaWithUnusedMetricPointReclaimEnabled = shouldReclaimUnusedMetricPoints && this.OutputDelta;
if (this.OutputDeltaWithUnusedMetricPointReclaimEnabled)
{
this.availableMetricPoints = new Queue<int>(cardinalityLimit);
// 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 (user provided cardinality limit) * 2 entries- one for sorted and one for unsorted input
this.TagsToMetricPointIndexDictionaryDelta =
new ConcurrentDictionary<Tags, LookupData>(concurrencyLevel: Environment.ProcessorCount, capacity: cardinalityLimit * 2);
// 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.
// Index 0 and 1 are reserved for no tags and overflow
for (int i = 2; i < this.NumberOfMetricPoints; i++)
{
this.availableMetricPoints.Enqueue(i);
}
this.lookupAggregatorStore = this.LookupAggregatorStoreForDeltaWithReclaim;
}
else
{
this.lookupAggregatorStore = this.LookupAggregatorStore;
}
}
private delegate void UpdateLongDelegate(long value, ReadOnlySpan<KeyValuePair<string, object?>> tags);
private delegate void UpdateDoubleDelegate(double value, ReadOnlySpan<KeyValuePair<string, object?>> tags);
internal DateTimeOffset StartTimeExclusive { get; private set; }
internal DateTimeOffset EndTimeInclusive { get; private set; }
internal double[] HistogramBounds => this.histogramBounds;
internal bool IsExemplarEnabled()
{
// Using this filter to indicate On/Off
// instead of another separate flag.
return this.exemplarFilter != ExemplarFilterType.AlwaysOff;
}
internal void Update(long value, ReadOnlySpan<KeyValuePair<string, object?>> tags)
{
try
{
this.updateLongCallback(value, tags);
}
catch (Exception)
{
Interlocked.Increment(ref this.DroppedMeasurements);
OpenTelemetrySdkEventSource.Log.MeasurementDropped(this.name, "SDK internal error occurred.", "Contact SDK owners.");
}
}
internal void Update(double value, ReadOnlySpan<KeyValuePair<string, object?>> tags)
{
try
{
this.updateDoubleCallback(value, tags);
}
catch (Exception)
{
Interlocked.Increment(ref this.DroppedMeasurements);
OpenTelemetrySdkEventSource.Log.MeasurementDropped(this.name, "SDK internal error occurred.", "Contact SDK owners.");
}
}
internal int Snapshot()
{
this.batchSize = 0;
if (this.OutputDeltaWithUnusedMetricPointReclaimEnabled)
{
this.SnapshotDeltaWithMetricPointReclaim();
}
else if (this.OutputDelta)
{
var indexSnapshot = Math.Min(this.metricPointIndex, this.NumberOfMetricPoints - 1);
this.SnapshotDelta(indexSnapshot);
}
else
{
var indexSnapshot = Math.Min(this.metricPointIndex, this.NumberOfMetricPoints - 1);
this.SnapshotCumulative(indexSnapshot);
}
this.EndTimeInclusive = DateTimeOffset.UtcNow;
return this.batchSize;
}
internal void SnapshotDelta(int indexSnapshot)
{
for (int i = 0; i <= indexSnapshot; i++)
{
ref var metricPoint = ref this.metricPoints[i];
if (metricPoint.MetricPointStatus == MetricPointStatus.NoCollectPending)
{
continue;
}
this.TakeMetricPointSnapshot(ref metricPoint, outputDelta: true);
this.currentMetricPointBatch[this.batchSize] = i;
this.batchSize++;
}
if (this.EndTimeInclusive != default)
{
this.StartTimeExclusive = this.EndTimeInclusive;
}
}
internal void SnapshotDeltaWithMetricPointReclaim()
{
// Index = 0 is reserved for the case where no dimensions are provided.
ref var metricPointWithNoTags = ref this.metricPoints[0];
if (metricPointWithNoTags.MetricPointStatus != MetricPointStatus.NoCollectPending)
{
this.TakeMetricPointSnapshot(ref metricPointWithNoTags, outputDelta: true);
this.currentMetricPointBatch[this.batchSize] = 0;
this.batchSize++;
}
if (this.EmitOverflowAttribute)
{
// TakeSnapshot for the MetricPoint for overflow
ref var metricPointForOverflow = ref this.metricPoints[1];
if (metricPointForOverflow.MetricPointStatus != MetricPointStatus.NoCollectPending)
{
this.TakeMetricPointSnapshot(ref metricPointForOverflow, outputDelta: true);
this.currentMetricPointBatch[this.batchSize] = 1;
this.batchSize++;
}
}
// Index 0 and 1 are reserved for no tags and overflow
for (int i = 2; i < this.NumberOfMetricPoints; i++)
{
ref var metricPoint = ref this.metricPoints[i];
if (metricPoint.MetricPointStatus == MetricPointStatus.NoCollectPending)
{
// Reclaim the MetricPoint if it was marked for it in the previous collect cycle
if (metricPoint.LookupData != null && metricPoint.LookupData.DeferredReclaim == true)
{
this.ReclaimMetricPoint(ref metricPoint, i);
continue;
}
// Check if the MetricPoint could be reclaimed in the current Collect cycle.
// If metricPoint.LookupData is `null` then the MetricPoint is already reclaimed and in the queue.
// If the Collect thread is successfully able to compare and swap the reference count from zero to int.MinValue, it means that
// the MetricPoint can be reused for other tags.
if (metricPoint.LookupData != null && Interlocked.CompareExchange(ref metricPoint.ReferenceCount, int.MinValue, 0) == 0)
{
// This is similar to double-checked locking. For some rare case, the Collect thread might read the status as `NoCollectPending`,
// and then get switched out before it could set the ReferenceCount to `int.MinValue`. In the meantime, an Update thread could come in
// and update the MetricPoint, thereby, setting its status to `CollectPending`. Note that the ReferenceCount would be 0 after the update.
// If the Collect thread now wakes up, it would be able to set the ReferenceCount to `int.MinValue`, thereby, marking the MetricPoint
// invalid for newer updates. In such cases, the MetricPoint, should not be reclaimed before taking its Snapshot.
if (metricPoint.MetricPointStatus == MetricPointStatus.NoCollectPending)
{
this.ReclaimMetricPoint(ref metricPoint, i);
}
else
{
// MetricPoint's ReferenceCount is `int.MinValue` but it still has a collect pending. Take the MetricPoint's Snapshot
// and mark it to be reclaimed in the next Collect cycle.
metricPoint.LookupData.DeferredReclaim = true;
this.TakeMetricPointSnapshot(ref metricPoint, outputDelta: true);
this.currentMetricPointBatch[this.batchSize] = i;
this.batchSize++;
}
}
continue;
}
this.TakeMetricPointSnapshot(ref metricPoint, outputDelta: true);
this.currentMetricPointBatch[this.batchSize] = i;
this.batchSize++;
}
if (this.EndTimeInclusive != default)
{
this.StartTimeExclusive = this.EndTimeInclusive;
}
}
internal void SnapshotCumulative(int indexSnapshot)
{
for (int i = 0; i <= indexSnapshot; i++)
{
ref var metricPoint = ref this.metricPoints[i];
if (!metricPoint.IsInitialized)
{
continue;
}
this.TakeMetricPointSnapshot(ref metricPoint, outputDelta: false);
this.currentMetricPointBatch[this.batchSize] = i;
this.batchSize++;
}
}
internal MetricPointsAccessor GetMetricPoints()
=> new(this.metricPoints, this.currentMetricPointBatch, this.batchSize);
private static double[] FindDefaultHistogramBounds(in MetricStreamIdentity metricStreamIdentity)
{
if (metricStreamIdentity.Unit == "s")
{
if (Metric.DefaultHistogramBoundShortMappings
.Contains((metricStreamIdentity.MeterName, metricStreamIdentity.InstrumentName)))
{
return Metric.DefaultHistogramBoundsShortSeconds;
}
if (Metric.DefaultHistogramBoundLongMappings
.Contains((metricStreamIdentity.MeterName, metricStreamIdentity.InstrumentName)))
{
return Metric.DefaultHistogramBoundsLongSeconds;
}
}
return Metric.DefaultHistogramBounds;
}
private void TakeMetricPointSnapshot(ref MetricPoint metricPoint, bool outputDelta)
{
if (this.IsExemplarEnabled())
{
metricPoint.TakeSnapshotWithExemplar(outputDelta);
}
else
{
metricPoint.TakeSnapshot(outputDelta);
}
}
private void ReclaimMetricPoint(ref MetricPoint metricPoint, int metricPointIndex)
{
/*
This method does three things:
1. Set `metricPoint.LookupData` and `metricPoint.mpComponents` to `null` to have them collected faster by GC.
2. Tries to remove the entry for this MetricPoint from the lookup dictionary. An update thread which retrieves this
MetricPoint would realize that the MetricPoint is not valid for use since its reference count would have been set to a negative number.
When that happens, the update thread would also try to remove the entry for this MetricPoint from the lookup dictionary.
We only care about the entry getting removed from the lookup dictionary and not about which thread removes it.
3. Put the array index of this MetricPoint to the queue of available metric points. This makes it available for update threads
to use this MetricPoint to track newer dimension combinations.
*/
var lookupData = metricPoint.LookupData;
// This method is only called after checking that `metricPoint.LookupData` is not `null`.
Debug.Assert(lookupData != null, "LookupData for the provided MetricPoint was null");
metricPoint.NullifyMetricPointState();
Debug.Assert(this.TagsToMetricPointIndexDictionaryDelta != null, "this.tagsToMetricPointIndexDictionaryDelta was null");
lock (this.TagsToMetricPointIndexDictionaryDelta!)
{
LookupData? dictionaryValue;
if (lookupData!.SortedTags != Tags.EmptyTags)
{
// Check if no other thread added a new entry for the same Tags.
// If no, then remove the existing entries.
if (this.TagsToMetricPointIndexDictionaryDelta.TryGetValue(lookupData.SortedTags, out dictionaryValue) &&
dictionaryValue == lookupData)
{
this.TagsToMetricPointIndexDictionaryDelta.TryRemove(lookupData.SortedTags, out var _);
this.TagsToMetricPointIndexDictionaryDelta.TryRemove(lookupData.GivenTags, out var _);
}
}
else
{
if (this.TagsToMetricPointIndexDictionaryDelta.TryGetValue(lookupData.GivenTags, out dictionaryValue) &&
dictionaryValue == lookupData)
{
this.TagsToMetricPointIndexDictionaryDelta.TryRemove(lookupData.GivenTags, out var _);
}
}
Debug.Assert(this.availableMetricPoints != null, "this.availableMetricPoints was null");
this.availableMetricPoints!.Enqueue(metricPointIndex);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void InitializeZeroTagPointIfNotInitialized()
{
if (!this.zeroTagMetricPointInitialized)
{
lock (this.lockZeroTags)
{
if (!this.zeroTagMetricPointInitialized)
{
if (this.OutputDelta)
{
var lookupData = new LookupData(0, Tags.EmptyTags, Tags.EmptyTags);
this.metricPoints[0] = new MetricPoint(this, this.aggType, null, this.histogramBounds, this.exponentialHistogramMaxSize, this.exponentialHistogramMaxScale, lookupData);
}
else
{
this.metricPoints[0] = new MetricPoint(this, this.aggType, null, this.histogramBounds, this.exponentialHistogramMaxSize, this.exponentialHistogramMaxScale);
}
this.zeroTagMetricPointInitialized = true;
}
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void InitializeOverflowTagPointIfNotInitialized()
{
if (!this.overflowTagMetricPointInitialized)
{
lock (this.lockOverflowTag)
{
if (!this.overflowTagMetricPointInitialized)
{
var keyValuePairs = new KeyValuePair<string, object?>[] { new("otel.metric.overflow", true) };
var tags = new Tags(keyValuePairs);
if (this.OutputDelta)
{
var lookupData = new LookupData(1, tags, tags);
this.metricPoints[1] = new MetricPoint(this, this.aggType, keyValuePairs, this.histogramBounds, this.exponentialHistogramMaxSize, this.exponentialHistogramMaxScale, lookupData);
}
else
{
this.metricPoints[1] = new MetricPoint(this, this.aggType, keyValuePairs, this.histogramBounds, this.exponentialHistogramMaxSize, this.exponentialHistogramMaxScale);
}
this.overflowTagMetricPointInitialized = true;
}
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private int LookupAggregatorStore(KeyValuePair<string, object?>[] tagKeysAndValues, int length)
{
var givenTags = new Tags(tagKeysAndValues);
if (!this.tagsToMetricPointIndexDictionary.TryGetValue(givenTags, out var aggregatorIndex))
{
if (length > 1)
{
// Note: We are using storage from ThreadStatic, so need to make a deep copy for Dictionary storage.
// Create or obtain new arrays to temporarily hold the sorted tag Keys and Values
var storage = ThreadStaticStorage.GetStorage();
storage.CloneKeysAndValues(tagKeysAndValues, length, out var tempSortedTagKeysAndValues);
Array.Sort(tempSortedTagKeysAndValues, DimensionComparisonDelegate);
var sortedTags = new Tags(tempSortedTagKeysAndValues);
if (!this.tagsToMetricPointIndexDictionary.TryGetValue(sortedTags, out aggregatorIndex))
{
aggregatorIndex = this.metricPointIndex;
if (aggregatorIndex >= this.NumberOfMetricPoints)
{
// sorry! out of data points.
// TODO: Once we support cleanup of
// unused points (typically with delta)
// we can re-claim them here.
return -1;
}
// Note: We are using storage from ThreadStatic (for upto MaxTagCacheSize tags) for both the input order of tags and the sorted order of tags,
// so we need to make a deep copy for Dictionary storage.
if (length <= ThreadStaticStorage.MaxTagCacheSize)
{
var givenTagKeysAndValues = new KeyValuePair<string, object?>[length];
tagKeysAndValues.CopyTo(givenTagKeysAndValues.AsSpan());
var sortedTagKeysAndValues = new KeyValuePair<string, object?>[length];
tempSortedTagKeysAndValues.CopyTo(sortedTagKeysAndValues.AsSpan());
givenTags = new Tags(givenTagKeysAndValues);
sortedTags = new Tags(sortedTagKeysAndValues);
}
lock (this.tagsToMetricPointIndexDictionary)
{
// check again after acquiring lock.
if (!this.tagsToMetricPointIndexDictionary.TryGetValue(sortedTags, out aggregatorIndex))
{
aggregatorIndex = ++this.metricPointIndex;
if (aggregatorIndex >= this.NumberOfMetricPoints)
{
// sorry! out of data points.
// TODO: Once we support cleanup of
// unused points (typically with delta)
// we can re-claim them here.
return -1;
}
ref var metricPoint = ref this.metricPoints[aggregatorIndex];
metricPoint = new MetricPoint(this, this.aggType, sortedTags.KeyValuePairs, this.histogramBounds, this.exponentialHistogramMaxSize, this.exponentialHistogramMaxScale);
// Add to dictionary *after* initializing MetricPoint
// as other threads can start writing to the
// MetricPoint, if dictionary entry found.
// Add the sorted order along with the given order of tags
this.tagsToMetricPointIndexDictionary.TryAdd(sortedTags, aggregatorIndex);
this.tagsToMetricPointIndexDictionary.TryAdd(givenTags, aggregatorIndex);
}
}
}
}
else
{
// This else block is for tag length = 1
aggregatorIndex = this.metricPointIndex;
if (aggregatorIndex >= this.NumberOfMetricPoints)
{
// sorry! out of data points.
// TODO: Once we support cleanup of
// unused points (typically with delta)
// we can re-claim them here.
return -1;
}
// Note: We are using storage from ThreadStatic, so need to make a deep copy for Dictionary storage.
var givenTagKeysAndValues = new KeyValuePair<string, object?>[length];
tagKeysAndValues.CopyTo(givenTagKeysAndValues.AsSpan());
givenTags = new Tags(givenTagKeysAndValues);
lock (this.tagsToMetricPointIndexDictionary)
{
// check again after acquiring lock.
if (!this.tagsToMetricPointIndexDictionary.TryGetValue(givenTags, out aggregatorIndex))
{
aggregatorIndex = ++this.metricPointIndex;
if (aggregatorIndex >= this.NumberOfMetricPoints)
{
// sorry! out of data points.
// TODO: Once we support cleanup of
// unused points (typically with delta)
// we can re-claim them here.
return -1;
}
ref var metricPoint = ref this.metricPoints[aggregatorIndex];
metricPoint = new MetricPoint(this, this.aggType, givenTags.KeyValuePairs, this.histogramBounds, this.exponentialHistogramMaxSize, this.exponentialHistogramMaxScale);
// Add to dictionary *after* initializing MetricPoint
// as other threads can start writing to the
// MetricPoint, if dictionary entry found.
// givenTags will always be sorted when tags length == 1
this.tagsToMetricPointIndexDictionary.TryAdd(givenTags, aggregatorIndex);
}
}
}
}
return aggregatorIndex;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private int LookupAggregatorStoreForDeltaWithReclaim(KeyValuePair<string, object?>[] tagKeysAndValues, int length)
{
int index;
var givenTags = new Tags(tagKeysAndValues);
Debug.Assert(this.TagsToMetricPointIndexDictionaryDelta != null, "this.tagsToMetricPointIndexDictionaryDelta was null");
bool newMetricPointCreated = false;
if (!this.TagsToMetricPointIndexDictionaryDelta!.TryGetValue(givenTags, out var lookupData))
{
if (length > 1)
{
// Note: We are using storage from ThreadStatic, so need to make a deep copy for Dictionary storage.
// Create or obtain new arrays to temporarily hold the sorted tag Keys and Values
var storage = ThreadStaticStorage.GetStorage();
storage.CloneKeysAndValues(tagKeysAndValues, length, out var tempSortedTagKeysAndValues);
Array.Sort(tempSortedTagKeysAndValues, DimensionComparisonDelegate);
var sortedTags = new Tags(tempSortedTagKeysAndValues);
if (!this.TagsToMetricPointIndexDictionaryDelta.TryGetValue(sortedTags, out lookupData))
{
// 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,
// so we need to make a deep copy for Dictionary storage.
if (length <= ThreadStaticStorage.MaxTagCacheSize)
{
var givenTagKeysAndValues = new KeyValuePair<string, object?>[length];
tagKeysAndValues.CopyTo(givenTagKeysAndValues.AsSpan());
var sortedTagKeysAndValues = new KeyValuePair<string, object?>[length];
tempSortedTagKeysAndValues.CopyTo(sortedTagKeysAndValues.AsSpan());
givenTags = new Tags(givenTagKeysAndValues);
sortedTags = new Tags(sortedTagKeysAndValues);
}
Debug.Assert(this.availableMetricPoints != null, "this.availableMetricPoints was null");
lock (this.TagsToMetricPointIndexDictionaryDelta)
{
// check again after acquiring lock.
if (!this.TagsToMetricPointIndexDictionaryDelta.TryGetValue(sortedTags, out lookupData))
{
// Check for an available MetricPoint
if (this.availableMetricPoints!.Count > 0)
{
index = this.availableMetricPoints.Dequeue();
}
else
{
// No MetricPoint is available for reuse
return -1;
}
lookupData = new LookupData(index, sortedTags, givenTags);
ref var metricPoint = ref this.metricPoints[index];
metricPoint = new MetricPoint(this, this.aggType, sortedTags.KeyValuePairs, this.histogramBounds, this.exponentialHistogramMaxSize, this.exponentialHistogramMaxScale, lookupData);
newMetricPointCreated = true;
// Add to dictionary *after* initializing MetricPoint
// as other threads can start writing to the
// MetricPoint, if dictionary entry found.
// Add the sorted order along with the given order of tags
this.TagsToMetricPointIndexDictionaryDelta.TryAdd(sortedTags, lookupData);
this.TagsToMetricPointIndexDictionaryDelta.TryAdd(givenTags, lookupData);
}
}
}
}
else
{
// This else block is for tag length = 1
// Note: We are using storage from ThreadStatic, so need to make a deep copy for Dictionary storage.
var givenTagKeysAndValues = new KeyValuePair<string, object?>[length];
tagKeysAndValues.CopyTo(givenTagKeysAndValues.AsSpan());
givenTags = new Tags(givenTagKeysAndValues);
Debug.Assert(this.availableMetricPoints != null, "this.availableMetricPoints was null");
lock (this.TagsToMetricPointIndexDictionaryDelta)
{
// check again after acquiring lock.
if (!this.TagsToMetricPointIndexDictionaryDelta.TryGetValue(givenTags, out lookupData))
{
// Check for an available MetricPoint
if (this.availableMetricPoints!.Count > 0)
{
index = this.availableMetricPoints.Dequeue();
}
else
{
// No MetricPoint is available for reuse
return -1;
}
lookupData = new LookupData(index, Tags.EmptyTags, givenTags);
ref var metricPoint = ref this.metricPoints[index];
metricPoint = new MetricPoint(this, this.aggType, givenTags.KeyValuePairs, this.histogramBounds, this.exponentialHistogramMaxSize, this.exponentialHistogramMaxScale, lookupData);
newMetricPointCreated = true;
// Add to dictionary *after* initializing MetricPoint
// as other threads can start writing to the
// MetricPoint, if dictionary entry found.
// givenTags will always be sorted when tags length == 1
this.TagsToMetricPointIndexDictionaryDelta.TryAdd(givenTags, lookupData);
}
}
}
}
// Found the MetricPoint
index = lookupData.Index;
// If the running thread created a new MetricPoint, then the Snapshot method cannot reclaim that MetricPoint because MetricPoint is initialized with a ReferenceCount of 1.
// It can simply return the index.
if (!newMetricPointCreated)
{
// If the running thread did not create the MetricPoint, it could be working on an index that has been reclaimed by Snapshot method.
// This could happen if the thread get switched out by CPU after it retrieves the index but the Snapshot method reclaims it before the thread wakes up again.
ref var metricPointAtIndex = ref this.metricPoints[index];
var referenceCount = Interlocked.Increment(ref metricPointAtIndex.ReferenceCount);
if (referenceCount < 0)
{
// Rare case: Snapshot method had already marked the MetricPoint available for reuse as it has not been updated in last collect cycle.
// Example scenario:
// Thread T1 wants to record a measurement for (k1,v1).
// Thread T1 creates a new MetricPoint at index 100 and adds an entry for (k1,v1) in the dictionary with the relevant LookupData value; ReferenceCount of the MetricPoint is 1 at this point.
// Thread T1 completes the update and decrements the ReferenceCount to 0.
// Later, another update thread (could be T1 as well) wants to record a measurement for (k1,v1)
// It looks up the dictionary and retrieves the index as 100. ReferenceCount for the MetricPoint is 0 at this point.
// This update thread gets switched out by the CPU.
// With the reclaim behavior, Snapshot method reclaims the index 100 as the MetricPoint for the index has NoCollectPending and has a ReferenceCount of 0.
// Snapshot thread sets the ReferenceCount to int.MinValue.
// The update thread wakes up and increments the ReferenceCount but finds the value to be negative.
// Retry attempt to get a MetricPoint.
index = this.RemoveStaleEntriesAndGetAvailableMetricPointRare(lookupData, length);
}
else if (metricPointAtIndex.LookupData != lookupData)
{
// Rare case: Another thread with different input tags could have reclaimed this MetricPoint if it was freed up by Snapshot method.
// Example scenario:
// Thread T1 wants to record a measurement for (k1,v1).
// Thread T1 creates a new MetricPoint at index 100 and adds an entry for (k1,v1) in the dictionary with the relevant LookupData value; ReferenceCount of the MetricPoint is 1 at this point.
// Thread T1 completes the update and decrements the ReferenceCount to 0.
// Later, another update thread T2 (could be T1 as well) wants to record a measurement for (k1,v1)
// It looks up the dictionary and retrieves the index as 100. ReferenceCount for the MetricPoint is 0 at this point.
// This update thread T2 gets switched out by the CPU.
// With the reclaim behavior, Snapshot method reclaims the index 100 as the MetricPoint for the index has NoCollectPending and has a ReferenceCount of 0.
// Snapshot thread sets the ReferenceCount to int.MinValue.
// An update thread T3 wants to record a measurement for (k2,v2).
// Thread T3 looks for an available index from the queue and finds index 100.
// Thread T3 creates a new MetricPoint at index 100 and adds an entry for (k2,v2) in the dictionary with the LookupData value for (k2,v2). ReferenceCount of the MetricPoint is 1 at this point.
// The update thread T2 wakes up and increments the ReferenceCount and finds the value to be positive but the LookupData value does not match the one for (k1,v1).
// Remove reference since its not the right MetricPoint.
Interlocked.Decrement(ref metricPointAtIndex.ReferenceCount);
// Retry attempt to get a MetricPoint.
index = this.RemoveStaleEntriesAndGetAvailableMetricPointRare(lookupData, length);
}
}
return index;
}
// This method is always called under `lock(this.tagsToMetricPointIndexDictionaryDelta)` so it's safe with other code that adds or removes
// entries from `this.tagsToMetricPointIndexDictionaryDelta`
private bool TryGetAvailableMetricPointRare(
Tags givenTags,
Tags sortedTags,
int length,
[NotNullWhen(true)]
out LookupData? lookupData,
out bool newMetricPointCreated)
{
Debug.Assert(this.TagsToMetricPointIndexDictionaryDelta != null, "this.tagsToMetricPointIndexDictionaryDelta was null");
Debug.Assert(this.availableMetricPoints != null, "this.availableMetricPoints was null");
int index;
newMetricPointCreated = false;
if (length > 1)
{
// check again after acquiring lock.
if (!this.TagsToMetricPointIndexDictionaryDelta!.TryGetValue(givenTags, out lookupData) &&
!this.TagsToMetricPointIndexDictionaryDelta.TryGetValue(sortedTags, out lookupData))
{
// Check for an available MetricPoint
if (this.availableMetricPoints!.Count > 0)
{
index = this.availableMetricPoints.Dequeue();
}
else
{
// No MetricPoint is available for reuse
return false;
}
lookupData = new LookupData(index, sortedTags, givenTags);
ref var metricPoint = ref this.metricPoints[index];
metricPoint = new MetricPoint(this, this.aggType, sortedTags.KeyValuePairs, this.histogramBounds, this.exponentialHistogramMaxSize, this.exponentialHistogramMaxScale, lookupData);
newMetricPointCreated = true;
// Add to dictionary *after* initializing MetricPoint
// as other threads can start writing to the
// MetricPoint, if dictionary entry found.
// Add the sorted order along with the given order of tags
this.TagsToMetricPointIndexDictionaryDelta.TryAdd(sortedTags, lookupData);
this.TagsToMetricPointIndexDictionaryDelta.TryAdd(givenTags, lookupData);
}
}
else
{
// check again after acquiring lock.
if (!this.TagsToMetricPointIndexDictionaryDelta!.TryGetValue(givenTags, out lookupData))
{
// Check for an available MetricPoint
if (this.availableMetricPoints!.Count > 0)
{
index = this.availableMetricPoints.Dequeue();
}
else
{
// No MetricPoint is available for reuse
return false;
}
lookupData = new LookupData(index, Tags.EmptyTags, givenTags);
ref var metricPoint = ref this.metricPoints[index];
metricPoint = new MetricPoint(this, this.aggType, givenTags.KeyValuePairs, this.histogramBounds, this.exponentialHistogramMaxSize, this.exponentialHistogramMaxScale, lookupData);
newMetricPointCreated = true;
// Add to dictionary *after* initializing MetricPoint
// as other threads can start writing to the
// MetricPoint, if dictionary entry found.
// givenTags will always be sorted when tags length == 1
this.TagsToMetricPointIndexDictionaryDelta.TryAdd(givenTags, lookupData);
}
}
return true;
}
// This method is essentially a retry attempt for when `LookupAggregatorStoreForDeltaWithReclaim` cannot find a MetricPoint.
// If we still fail to get a MetricPoint in this method, we don't retry any further and simply drop the measurement.
// This method acquires `lock (this.tagsToMetricPointIndexDictionaryDelta)`
private int RemoveStaleEntriesAndGetAvailableMetricPointRare(LookupData lookupData, int length)
{
bool foundMetricPoint = false;
bool newMetricPointCreated = false;
var sortedTags = lookupData.SortedTags;
var inputTags = lookupData.GivenTags;
// Acquire lock
// Try to remove stale entries from dictionary
// Get the index for a new MetricPoint (it could be self-claimed or from another thread that added a fresh entry)
// If self-claimed, then add a fresh entry to the dictionary
// If an available MetricPoint is found, then only increment the ReferenceCount
Debug.Assert(this.TagsToMetricPointIndexDictionaryDelta != null, "this.tagsToMetricPointIndexDictionaryDelta was null");
// Delete the entry for these Tags and get another MetricPoint.
lock (this.TagsToMetricPointIndexDictionaryDelta!)
{
LookupData? dictionaryValue;
if (lookupData.SortedTags != Tags.EmptyTags)
{
// Check if no other thread added a new entry for the same Tags in the meantime.
// If no, then remove the existing entries.
if (this.TagsToMetricPointIndexDictionaryDelta.TryGetValue(lookupData.SortedTags, out dictionaryValue))
{
if (dictionaryValue == lookupData)
{
// No other thread added a new entry for the same Tags.
this.TagsToMetricPointIndexDictionaryDelta.TryRemove(lookupData.SortedTags, out _);
this.TagsToMetricPointIndexDictionaryDelta.TryRemove(lookupData.GivenTags, out _);
}
else
{
// Some other thread added a new entry for these Tags. Use the new MetricPoint
lookupData = dictionaryValue;
foundMetricPoint = true;
}
}
}
else
{
if (this.TagsToMetricPointIndexDictionaryDelta.TryGetValue(lookupData.GivenTags, out dictionaryValue))
{
if (dictionaryValue == lookupData)
{
// No other thread added a new entry for the same Tags.
this.TagsToMetricPointIndexDictionaryDelta.TryRemove(lookupData.GivenTags, out _);
}
else
{
// Some other thread added a new entry for these Tags. Use the new MetricPoint
lookupData = dictionaryValue;
foundMetricPoint = true;
}
}
}
if (!foundMetricPoint
&& this.TryGetAvailableMetricPointRare(inputTags, sortedTags, length, out var tempLookupData, out newMetricPointCreated))
{
foundMetricPoint = true;
lookupData = tempLookupData;
}
}
if (foundMetricPoint)
{
var index = lookupData.Index;
// If the running thread created a new MetricPoint, then the Snapshot method cannot reclaim that MetricPoint because MetricPoint is initialized with a ReferenceCount of 1.
// It can simply return the index.
if (!newMetricPointCreated)
{
// If the running thread did not create the MetricPoint, it could be working on an index that has been reclaimed by Snapshot method.
// This could happen if the thread get switched out by CPU after it retrieves the index but the Snapshot method reclaims it before the thread wakes up again.
ref var metricPointAtIndex = ref this.metricPoints[index];
var referenceCount = Interlocked.Increment(ref metricPointAtIndex.ReferenceCount);
if (referenceCount < 0)
{
// Super rare case: Snapshot method had already marked the MetricPoint available for reuse as it has not been updated in last collect cycle even in the retry attempt.
// Example scenario mentioned in `LookupAggregatorStoreForDeltaWithReclaim` method.
// Don't retry again and drop the measurement.
return -1;
}
else if (metricPointAtIndex.LookupData != lookupData)
{
// Rare case: Another thread with different input tags could have reclaimed this MetricPoint if it was freed up by Snapshot method even in the retry attempt.
// Example scenario mentioned in `LookupAggregatorStoreForDeltaWithReclaim` method.
// Remove reference since its not the right MetricPoint.
Interlocked.Decrement(ref metricPointAtIndex.ReferenceCount);
// Don't retry again and drop the measurement.
return -1;
}
}
return index;
}
else
{
// No MetricPoint is available for reuse
return -1;
}
}
private void UpdateLong(long value, ReadOnlySpan<KeyValuePair<string, object?>> tags)
{
var index = this.FindMetricAggregatorsDefault(tags);
this.UpdateLongMetricPoint(index, value, tags);
}
private void UpdateLongCustomTags(long value, ReadOnlySpan<KeyValuePair<string, object?>> tags)
{
var index = this.FindMetricAggregatorsCustomTag(tags);
this.UpdateLongMetricPoint(index, value, tags);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void UpdateLongMetricPoint(int metricPointIndex, long value, ReadOnlySpan<KeyValuePair<string, object?>> tags)
{
if (metricPointIndex < 0)
{
Interlocked.Increment(ref this.DroppedMeasurements);
if (this.EmitOverflowAttribute)
{
this.InitializeOverflowTagPointIfNotInitialized();