Skip to content

Commit

Permalink
Metrics: MetricPointValueStorage explicit layout struct part2 (#2718)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch authored Jan 12, 2022
1 parent 4bf8ed4 commit cf3b119
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 110 deletions.
10 changes: 10 additions & 0 deletions src/OpenTelemetry/Metrics/HistogramBucket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

namespace OpenTelemetry.Metrics
{
/// <summary>
/// Represents a bucket in the histogram metric type.
/// </summary>
public readonly struct HistogramBucket
{
internal HistogramBucket(double explicitBound, long bucketCount)
Expand All @@ -24,8 +27,15 @@ internal HistogramBucket(double explicitBound, long bucketCount)
this.BucketCount = bucketCount;
}

/// <summary>
/// Gets the configured bounds for the bucket or <see
/// cref="double.PositiveInfinity"/> for the catch-all bucket.
/// </summary>
public double ExplicitBound { get; }

/// <summary>
/// Gets the count of items in the bucket.
/// </summary>
public long BucketCount { get; }
}
}
44 changes: 33 additions & 11 deletions src/OpenTelemetry/Metrics/HistogramBuckets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,37 @@

namespace OpenTelemetry.Metrics
{
/// <summary>
/// A collection of <see cref="HistogramBucket"/>s associated with a histogram metric type.
/// </summary>
// Note: Does not implement IEnumerable<> to prevent accidental boxing.
public class HistogramBuckets
{
internal readonly long[] BucketCounts;
internal readonly double[] ExplicitBounds;

internal readonly long[] AggregatedBucketCounts;
internal readonly long[] RunningBucketCounts;

internal readonly double[] ExplicitBounds;
internal readonly long[] SnapshotBucketCounts;

internal readonly object LockObject;
internal double RunningSum;

internal HistogramBuckets(double[] histogramBounds)
internal double SnapshotSum;

internal HistogramBuckets(double[] explicitBounds)
{
this.ExplicitBounds = histogramBounds;
this.BucketCounts = histogramBounds != null ? new long[histogramBounds.Length + 1] : null;
this.AggregatedBucketCounts = histogramBounds != null ? new long[histogramBounds.Length + 1] : null;
this.LockObject = new object();
this.ExplicitBounds = explicitBounds;
this.RunningBucketCounts = explicitBounds != null ? new long[explicitBounds.Length + 1] : null;
this.SnapshotBucketCounts = explicitBounds != null ? new long[explicitBounds.Length + 1] : new long[0];
}

internal object LockObject => this.SnapshotBucketCounts;

public Enumerator GetEnumerator() => new(this);

/// <summary>
/// Enumerates the elements of a <see cref="HistogramBuckets"/>.
/// </summary>
// Note: Does not implement IEnumerator<> to prevent accidental boxing.
public struct Enumerator
{
private readonly int numberOfBuckets;
Expand All @@ -47,19 +58,30 @@ internal Enumerator(HistogramBuckets histogramMeasurements)
this.histogramMeasurements = histogramMeasurements;
this.index = 0;
this.Current = default;
this.numberOfBuckets = histogramMeasurements.AggregatedBucketCounts == null ? 0 : histogramMeasurements.AggregatedBucketCounts.Length;
this.numberOfBuckets = histogramMeasurements.SnapshotBucketCounts.Length;
}

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

/// <summary>
/// Advances the enumerator to the next element of the <see
/// cref="HistogramBuckets"/>.
/// </summary>
/// <returns><see langword="true"/> if the enumerator was
/// successfully advanced to the next element; <see
/// langword="false"/> if the enumerator has passed the end of the
/// collection.</returns>
public bool MoveNext()
{
if (this.index < this.numberOfBuckets)
{
double explicitBound = this.index < this.numberOfBuckets - 1
? this.histogramMeasurements.ExplicitBounds[this.index]
: double.PositiveInfinity;
long bucketCount = this.histogramMeasurements.AggregatedBucketCounts[this.index];
long bucketCount = this.histogramMeasurements.SnapshotBucketCounts[this.index];
this.Current = new HistogramBucket(explicitBound, bucketCount);
this.index++;
return true;
Expand Down
6 changes: 4 additions & 2 deletions src/OpenTelemetry/Metrics/Metric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ namespace OpenTelemetry.Metrics
public sealed class Metric
{
internal static readonly double[] DefaultHistogramBounds = new double[] { 0, 5, 10, 25, 50, 75, 100, 250, 500, 1000 };
private AggregatorStore aggStore;

private readonly AggregatorStore aggStore;

internal Metric(
Instrument instrument,
Expand All @@ -38,7 +39,8 @@ internal Metric(
this.Description = metricDescription ?? string.Empty;
this.Unit = instrument.Unit ?? string.Empty;
this.Meter = instrument.Meter;
AggregationType aggType = default;

AggregationType aggType;
if (instrument.GetType() == typeof(ObservableCounter<long>)
|| instrument.GetType() == typeof(ObservableCounter<int>)
|| instrument.GetType() == typeof(ObservableCounter<short>)
Expand Down
Loading

0 comments on commit cf3b119

Please sign in to comment.