Skip to content

Commit cf3b119

Browse files
authored
Metrics: MetricPointValueStorage explicit layout struct part2 (#2718)
1 parent 4bf8ed4 commit cf3b119

File tree

6 files changed

+211
-110
lines changed

6 files changed

+211
-110
lines changed

src/OpenTelemetry/Metrics/HistogramBucket.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
namespace OpenTelemetry.Metrics
1818
{
19+
/// <summary>
20+
/// Represents a bucket in the histogram metric type.
21+
/// </summary>
1922
public readonly struct HistogramBucket
2023
{
2124
internal HistogramBucket(double explicitBound, long bucketCount)
@@ -24,8 +27,15 @@ internal HistogramBucket(double explicitBound, long bucketCount)
2427
this.BucketCount = bucketCount;
2528
}
2629

30+
/// <summary>
31+
/// Gets the configured bounds for the bucket or <see
32+
/// cref="double.PositiveInfinity"/> for the catch-all bucket.
33+
/// </summary>
2734
public double ExplicitBound { get; }
2835

36+
/// <summary>
37+
/// Gets the count of items in the bucket.
38+
/// </summary>
2939
public long BucketCount { get; }
3040
}
3141
}

src/OpenTelemetry/Metrics/HistogramBuckets.cs

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,37 @@
1616

1717
namespace OpenTelemetry.Metrics
1818
{
19+
/// <summary>
20+
/// A collection of <see cref="HistogramBucket"/>s associated with a histogram metric type.
21+
/// </summary>
22+
// Note: Does not implement IEnumerable<> to prevent accidental boxing.
1923
public class HistogramBuckets
2024
{
21-
internal readonly long[] BucketCounts;
25+
internal readonly double[] ExplicitBounds;
2226

23-
internal readonly long[] AggregatedBucketCounts;
27+
internal readonly long[] RunningBucketCounts;
2428

25-
internal readonly double[] ExplicitBounds;
29+
internal readonly long[] SnapshotBucketCounts;
2630

27-
internal readonly object LockObject;
31+
internal double RunningSum;
2832

29-
internal HistogramBuckets(double[] histogramBounds)
33+
internal double SnapshotSum;
34+
35+
internal HistogramBuckets(double[] explicitBounds)
3036
{
31-
this.ExplicitBounds = histogramBounds;
32-
this.BucketCounts = histogramBounds != null ? new long[histogramBounds.Length + 1] : null;
33-
this.AggregatedBucketCounts = histogramBounds != null ? new long[histogramBounds.Length + 1] : null;
34-
this.LockObject = new object();
37+
this.ExplicitBounds = explicitBounds;
38+
this.RunningBucketCounts = explicitBounds != null ? new long[explicitBounds.Length + 1] : null;
39+
this.SnapshotBucketCounts = explicitBounds != null ? new long[explicitBounds.Length + 1] : new long[0];
3540
}
3641

42+
internal object LockObject => this.SnapshotBucketCounts;
43+
3744
public Enumerator GetEnumerator() => new(this);
3845

46+
/// <summary>
47+
/// Enumerates the elements of a <see cref="HistogramBuckets"/>.
48+
/// </summary>
49+
// Note: Does not implement IEnumerator<> to prevent accidental boxing.
3950
public struct Enumerator
4051
{
4152
private readonly int numberOfBuckets;
@@ -47,19 +58,30 @@ internal Enumerator(HistogramBuckets histogramMeasurements)
4758
this.histogramMeasurements = histogramMeasurements;
4859
this.index = 0;
4960
this.Current = default;
50-
this.numberOfBuckets = histogramMeasurements.AggregatedBucketCounts == null ? 0 : histogramMeasurements.AggregatedBucketCounts.Length;
61+
this.numberOfBuckets = histogramMeasurements.SnapshotBucketCounts.Length;
5162
}
5263

64+
/// <summary>
65+
/// Gets the <see cref="HistogramBucket"/> at the current position of the enumerator.
66+
/// </summary>
5367
public HistogramBucket Current { get; private set; }
5468

69+
/// <summary>
70+
/// Advances the enumerator to the next element of the <see
71+
/// cref="HistogramBuckets"/>.
72+
/// </summary>
73+
/// <returns><see langword="true"/> if the enumerator was
74+
/// successfully advanced to the next element; <see
75+
/// langword="false"/> if the enumerator has passed the end of the
76+
/// collection.</returns>
5577
public bool MoveNext()
5678
{
5779
if (this.index < this.numberOfBuckets)
5880
{
5981
double explicitBound = this.index < this.numberOfBuckets - 1
6082
? this.histogramMeasurements.ExplicitBounds[this.index]
6183
: double.PositiveInfinity;
62-
long bucketCount = this.histogramMeasurements.AggregatedBucketCounts[this.index];
84+
long bucketCount = this.histogramMeasurements.SnapshotBucketCounts[this.index];
6385
this.Current = new HistogramBucket(explicitBound, bucketCount);
6486
this.index++;
6587
return true;

src/OpenTelemetry/Metrics/Metric.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ namespace OpenTelemetry.Metrics
2323
public sealed class Metric
2424
{
2525
internal static readonly double[] DefaultHistogramBounds = new double[] { 0, 5, 10, 25, 50, 75, 100, 250, 500, 1000 };
26-
private AggregatorStore aggStore;
26+
27+
private readonly AggregatorStore aggStore;
2728

2829
internal Metric(
2930
Instrument instrument,
@@ -38,7 +39,8 @@ internal Metric(
3839
this.Description = metricDescription ?? string.Empty;
3940
this.Unit = instrument.Unit ?? string.Empty;
4041
this.Meter = instrument.Meter;
41-
AggregationType aggType = default;
42+
43+
AggregationType aggType;
4244
if (instrument.GetType() == typeof(ObservableCounter<long>)
4345
|| instrument.GetType() == typeof(ObservableCounter<int>)
4446
|| instrument.GetType() == typeof(ObservableCounter<short>)

0 commit comments

Comments
 (0)