-
Notifications
You must be signed in to change notification settings - Fork 773
/
FixedSizeExemplarReservoir.cs
68 lines (52 loc) · 1.91 KB
/
FixedSizeExemplarReservoir.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using OpenTelemetry.Internal;
namespace OpenTelemetry.Metrics;
internal abstract class FixedSizeExemplarReservoir : ExemplarReservoir
{
private readonly Exemplar[] runningExemplars;
private readonly Exemplar[] snapshotExemplars;
protected FixedSizeExemplarReservoir(int capacity)
{
Guard.ThrowIfOutOfRange(capacity, min: 1);
this.runningExemplars = new Exemplar[capacity];
this.snapshotExemplars = new Exemplar[capacity];
this.Capacity = capacity;
}
internal int Capacity { get; }
/// <summary>
/// Collects all the exemplars accumulated by the Reservoir.
/// </summary>
/// <returns><see cref="ReadOnlyExemplarCollection"/>.</returns>
public sealed override ReadOnlyExemplarCollection Collect()
{
var runningExemplars = this.runningExemplars;
for (int i = 0; i < runningExemplars.Length; i++)
{
ref var running = ref runningExemplars[i];
running.Collect(
ref this.snapshotExemplars[i],
reset: this.ResetOnCollect);
}
this.OnCollected();
return new(this.snapshotExemplars);
}
internal sealed override void Initialize(AggregatorStore aggregatorStore)
{
var viewDefinedTagKeys = aggregatorStore.TagKeysInteresting;
for (int i = 0; i < this.runningExemplars.Length; i++)
{
this.runningExemplars[i].ViewDefinedTagKeys = viewDefinedTagKeys;
this.snapshotExemplars[i].ViewDefinedTagKeys = viewDefinedTagKeys;
}
base.Initialize(aggregatorStore);
}
protected virtual void OnCollected()
{
}
protected void UpdateExemplar<T>(int exemplarIndex, in ExemplarMeasurement<T> measurement)
where T : struct
{
this.runningExemplars[exemplarIndex].Update(in measurement);
}
}