Skip to content

Commit

Permalink
Exponential Bucket Histogram - part 7 (#3499)
Browse files Browse the repository at this point in the history
* hammer out the upper/lower bound of scale

* comment
  • Loading branch information
reyang authored Jul 27, 2022
1 parent 6789efa commit 25cfa17
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 68 deletions.
20 changes: 18 additions & 2 deletions src/OpenTelemetry/Metrics/ExponentialBucketHistogram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,18 @@ internal class ExponentialBucketHistogram
private int scale;
private double scalingFactor; // 2 ^ scale / log(2)

public ExponentialBucketHistogram(int scale, int maxBuckets = 160)
/// <summary>
/// Initializes a new instance of the <see cref="ExponentialBucketHistogram"/> class.
/// </summary>
/// <param name="maxBuckets">
/// The maximum number of buckets in each of the positive and negative ranges, not counting the special zero bucket. The default value is 160.
/// </param>
public ExponentialBucketHistogram(int maxBuckets = 160)
: this(maxBuckets, 20)
{
}

internal ExponentialBucketHistogram(int maxBuckets, int scale)
{
/*
The following table is calculated based on [ MapToIndex(double.Epsilon), MapToIndex(double.MaxValue) ]:
Expand Down Expand Up @@ -80,7 +91,12 @@ The following table is calculated based on [ MapToIndex(double.Epsilon), MapToIn
*/
Guard.ThrowIfOutOfRange(scale, min: -11, max: 20);

Guard.ThrowIfOutOfRange(maxBuckets, min: 1);
/*
Regardless of the scale, MapToIndex(1) will always be -1, so we need two buckets at minimum:
bucket[-1] = (1/base, 1]
bucket[0] = (1, base]
*/
Guard.ThrowIfOutOfRange(maxBuckets, min: 2);

this.Scale = scale;
this.PositiveBuckets = new CircularBufferBuckets(maxBuckets);
Expand Down
Loading

0 comments on commit 25cfa17

Please sign in to comment.