-
Notifications
You must be signed in to change notification settings - Fork 858
Fix exponential histogram bucket snapshot #4313
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -21,14 +21,15 @@ namespace OpenTelemetry.Metrics; | |||||||||
| public sealed class ExponentialHistogramBuckets | ||||||||||
| { | ||||||||||
| private long[] buckets = Array.Empty<long>(); | ||||||||||
| private int size = 0; | ||||||||||
|
|
||||||||||
| internal ExponentialHistogramBuckets() | ||||||||||
| { | ||||||||||
| } | ||||||||||
|
|
||||||||||
| public int Offset { get; private set; } | ||||||||||
|
|
||||||||||
| public Enumerator GetEnumerator() => new(this.buckets); | ||||||||||
| public Enumerator GetEnumerator() => new(this.buckets, this.size); | ||||||||||
|
|
||||||||||
| internal void SnapshotBuckets(CircularBufferBuckets buckets) | ||||||||||
| { | ||||||||||
|
|
@@ -37,13 +38,15 @@ internal void SnapshotBuckets(CircularBufferBuckets buckets) | |||||||||
| this.buckets = new long[buckets.Capacity]; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| this.size = buckets.Size; | ||||||||||
| this.Offset = buckets.Offset; | ||||||||||
| buckets.Copy(this.buckets); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| internal ExponentialHistogramBuckets Copy() | ||||||||||
| { | ||||||||||
| var copy = new ExponentialHistogramBuckets(); | ||||||||||
| copy.size = this.size; | ||||||||||
| copy.Offset = this.Offset; | ||||||||||
| copy.buckets = new long[this.buckets.Length]; | ||||||||||
| Array.Copy(this.buckets, copy.buckets, this.buckets.Length); | ||||||||||
|
|
@@ -58,10 +61,12 @@ public struct Enumerator | |||||||||
| { | ||||||||||
| private readonly long[] buckets; | ||||||||||
| private int index; | ||||||||||
| private int size; | ||||||||||
alanwest marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
|
|
||||||||||
| internal Enumerator(long[] buckets) | ||||||||||
| internal Enumerator(long[] buckets, int size) | ||||||||||
| { | ||||||||||
| this.index = 0; | ||||||||||
alanwest marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
| this.size = size; | ||||||||||
| this.buckets = buckets; | ||||||||||
| this.Current = default; | ||||||||||
| } | ||||||||||
|
|
@@ -81,7 +86,7 @@ internal Enumerator(long[] buckets) | |||||||||
| /// collection.</returns> | ||||||||||
| public bool MoveNext() | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the unit tests should also cover a scale-down scenario when the CircularBufferBuckets offset is updated.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not very obvious, but the existing tests actually do cover this scenario. Even this simple test that only records two values requires a scale down. When the first value is recorded (10) the initial scale of 20 is fine, but when the second value is recorded (5) a scale down is necessary to fit this range of values within the default 160 buckets. opentelemetry-dotnet/test/OpenTelemetry.Tests/Metrics/MetricSnapshotTests.cs Lines 238 to 241 in d924663
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it. Thanks!
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm going to merge this PR, but I will be coming back to the tests. Clearly there's improvements to make as the bug this PR fixes is not really covered by any tests yet. |
||||||||||
| { | ||||||||||
| if (this.index < this.buckets.Length) | ||||||||||
| if (this.index < this.size) | ||||||||||
| { | ||||||||||
| this.Current = this.buckets[this.index++]; | ||||||||||
| return true; | ||||||||||
|
|
||||||||||
Uh oh!
There was an error while loading. Please reload this page.