-
Notifications
You must be signed in to change notification settings - Fork 859
[OpenTelemetry] Move BucketLookup tree to AggregatorStore #6715
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
Open
iliar-turdushev
wants to merge
6
commits into
open-telemetry:main
Choose a base branch
from
iliar-turdushev:histogrambuckets_memory
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a0ddbc6
Fixes ##5976
iliar-turdushev 9c9c5d5
Merge branch 'main' into histogrambuckets_memory
iliar-turdushev 85c972e
Move BucketLookup tree to AggregateStore
iliar-turdushev 7934ab0
Update the changelog
iliar-turdushev 358a59d
Merge branch 'main' into histogrambuckets_memory
iliar-turdushev 697d90b
Update src/OpenTelemetry/CHANGELOG.md
iliar-turdushev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using System.Diagnostics; | ||
| using System.Runtime.CompilerServices; | ||
|
|
||
| namespace OpenTelemetry.Metrics; | ||
|
|
||
| internal sealed class HistogramExplicitBounds | ||
| { | ||
| internal const int DefaultBoundaryCountForBinarySearch = 50; | ||
|
|
||
| private readonly BucketLookupNode? bucketLookupTreeRoot; | ||
| private readonly Func<double, int> findHistogramBucketIndex; | ||
|
|
||
| public HistogramExplicitBounds(double[] bounds) | ||
| { | ||
| this.Bounds = CleanUpInfinitiesFromExplicitBounds(bounds); | ||
| this.findHistogramBucketIndex = this.FindBucketIndexLinear; | ||
|
|
||
| if (this.Bounds.Length >= DefaultBoundaryCountForBinarySearch) | ||
| { | ||
| this.bucketLookupTreeRoot = ConstructBalancedBST(this.Bounds, 0, this.Bounds.Length); | ||
| this.findHistogramBucketIndex = this.FindBucketIndexBinary; | ||
|
|
||
| static BucketLookupNode? ConstructBalancedBST(double[] values, int min, int max) | ||
| { | ||
| if (min == max) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| int median = min + ((max - min) / 2); | ||
| return new BucketLookupNode | ||
| { | ||
| Index = median, | ||
| UpperBoundInclusive = values[median], | ||
| LowerBoundExclusive = median > 0 ? values[median - 1] : double.NegativeInfinity, | ||
| Left = ConstructBalancedBST(values, min, median), | ||
| Right = ConstructBalancedBST(values, median + 1, max), | ||
| }; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public double[] Bounds { get; private set; } | ||
|
|
||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public int FindBucketIndex(double value) | ||
| { | ||
| return this.findHistogramBucketIndex(value); | ||
| } | ||
|
|
||
| private static double[] CleanUpInfinitiesFromExplicitBounds(double[] bounds) | ||
| { | ||
| for (var i = 0; i < bounds.Length; i++) | ||
| { | ||
| if (double.IsNegativeInfinity(bounds[i]) || double.IsPositiveInfinity(bounds[i])) | ||
| { | ||
| return bounds | ||
| .Where(b => !double.IsNegativeInfinity(b) && !double.IsPositiveInfinity(b)) | ||
| .ToArray(); | ||
| } | ||
| } | ||
|
|
||
| return bounds; | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| private int FindBucketIndexBinary(double value) | ||
| { | ||
| BucketLookupNode? current = this.bucketLookupTreeRoot; | ||
|
|
||
| Debug.Assert(current != null, "Bucket root was null."); | ||
|
|
||
| do | ||
| { | ||
| if (value <= current!.LowerBoundExclusive) | ||
| { | ||
| current = current.Left; | ||
| } | ||
| else if (value > current.UpperBoundInclusive) | ||
| { | ||
| current = current.Right; | ||
| } | ||
| else | ||
| { | ||
| return current.Index; | ||
| } | ||
| } | ||
| while (current != null); | ||
|
|
||
| Debug.Assert(this.Bounds != null, "ExplicitBounds was null."); | ||
|
|
||
| return this.Bounds!.Length; | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| private int FindBucketIndexLinear(double value) | ||
| { | ||
| Debug.Assert(this.Bounds != null, "ExplicitBounds was null."); | ||
|
|
||
| int i; | ||
| for (i = 0; i < this.Bounds!.Length; i++) | ||
| { | ||
| // Upper bound is inclusive | ||
| if (value <= this.Bounds[i]) | ||
| { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return i; | ||
| } | ||
|
|
||
| private sealed class BucketLookupNode | ||
| { | ||
| public double UpperBoundInclusive { get; set; } | ||
|
|
||
| public double LowerBoundExclusive { get; set; } | ||
|
|
||
| public int Index { get; set; } | ||
|
|
||
| public BucketLookupNode? Left { get; set; } | ||
|
|
||
| public BucketLookupNode? Right { get; set; } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.