Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exponential Bucket Histogram - part 9 #3553

Merged
merged 9 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions src/OpenTelemetry/Metrics/ExponentialBucketHistogram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
// limitations under the License.
// </copyright>

#if NET6_0_OR_GREATER

using System;
using System.Diagnostics;
using OpenTelemetry.Internal;
Expand All @@ -30,8 +28,6 @@ namespace OpenTelemetry.Metrics;
/// </summary>
internal class ExponentialBucketHistogram
{
private static readonly double Log2E = Math.Log2(Math.E); // 1 / Math.Log(2)

private int scale;
private double scalingFactor; // 2 ^ scale / log(2)

Expand Down Expand Up @@ -107,13 +103,17 @@ internal int Scale
{
get => this.scale;

private set
set
{
this.scale = value;
this.scalingFactor = Math.ScaleB(Log2E, value);

// A subset of Math.ScaleB(Math.Log2(Math.E), value)
this.scalingFactor = BitConverter.Int64BitsToDouble(0x71547652B82FEL | ((0x3FFL + value) << 52 /* fraction width */));
}
}

internal double ScalingFactor => this.scalingFactor;

internal CircularBufferBuckets PositiveBuckets { get; }

internal long ZeroCount { get; private set; }
Expand All @@ -135,7 +135,7 @@ public int MapToIndex(double value)
{
Debug.Assert(MathHelper.IsFinite(value), "IEEE-754 +Inf, -Inf and NaN should be filtered out before calling this method.");
Debug.Assert(value != 0, "IEEE-754 zero values should be handled by ZeroCount.");
Debug.Assert(!double.IsNegative(value), "IEEE-754 negative values should be normalized before calling this method.");
Debug.Assert(value > 0, "IEEE-754 negative values should be normalized before calling this method.");

var bits = BitConverter.DoubleToInt64Bits(value);
var fraction = bits & 0xFFFFFFFFFFFFFL /* fraction mask */;
Expand Down Expand Up @@ -202,5 +202,3 @@ public void Record(double value)
Debug.Assert(n == 0, "Increment should always succeed after scale down.");
}
}

#endif
Loading