-
Notifications
You must be signed in to change notification settings - Fork 3k
deltatocumulative: exponential histograms #32030
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
Merged
jpkrohling
merged 27 commits into
open-telemetry:main
from
tombrk:deltatocumulative-exphist
May 14, 2024
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
e9c29d0
deltatocumulative: exponential histograms
tombrk 1f9743d
exphist: properly handle min,max,sum,count
tombrk 7311667
data: expo.Merge
tombrk 918c168
data: expo.WidenZero, adjust zero-count
tombrk 85fec7e
data: expo.Downscale, adjust scale if neccessary
tombrk 943359e
*: addlicense, lint, sort imports
tombrk 41a2301
data: use expotest
tombrk aae577d
expo: type aliases
tombrk 885e39a
expo: TestAbsolute
tombrk 402da46
*: forward to main
tombrk 4babe1d
processor: aggregate exponential histograms
tombrk 8fc7def
*: addlicense, goporto
tombrk 7e6576d
expotest: reflect getter comparison
tombrk 50c6f6a
*: make linter happy
tombrk 154a7d7
*: changelog
tombrk eb7b637
*: forward to main
tombrk 7c14cf4
deltatocumulative: scope panics to component
tombrk d3a9c57
*: review feedback
tombrk b11184b
changelog: reword
tombrk 55f3bbc
*: remove fatal recovery
tombrk 3c295cf
*: unit test panics
tombrk 47f418c
*: make linter happy
tombrk 54661a2
expotest: remove unneccesary test case
tombrk 6be320a
expo.WidenZero: properly constraint slice range
tombrk 866c685
*: forward to main
tombrk 97cdf13
expo: Merge only if both are non-zero
tombrk c80582b
expo: more comments for Collapse
tombrk 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: "enhancement" | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: deltatocumulativeprocessor | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: exponential histogram accumulation | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [31340] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: | ||
accumulates exponential histogram datapoints by adding respective bucket counts. | ||
also handles downscaling, changing zero-counts, offset adaptions and optional fields | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [user] |
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
52 changes: 52 additions & 0 deletions
52
processor/deltatocumulativeprocessor/internal/data/expo/expo.go
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,52 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Package expo implements various operations on exponential histograms and their bucket counts | ||
package expo // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/deltatocumulativeprocessor/internal/data/expo" | ||
|
||
import "go.opentelemetry.io/collector/pdata/pmetric" | ||
|
||
type ( | ||
DataPoint = pmetric.ExponentialHistogramDataPoint | ||
Buckets = pmetric.ExponentialHistogramDataPointBuckets | ||
) | ||
|
||
// Abs returns a view into the buckets using an absolute scale | ||
func Abs(bs Buckets) Absolute { | ||
return Absolute{buckets: bs} | ||
} | ||
|
||
type buckets = Buckets | ||
|
||
// Absolute addresses bucket counts using an absolute scale, such that it is | ||
// interoperable with [Scale]. | ||
// | ||
// It spans from [[Absolute.Lower]:[Absolute.Upper]] | ||
// | ||
// NOTE: The zero-value is unusable, use [Abs] to construct | ||
type Absolute struct { | ||
buckets | ||
} | ||
|
||
// Abs returns the value at absolute index 'at' | ||
func (a Absolute) Abs(at int) uint64 { | ||
jpkrohling marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if i, ok := a.idx(at); ok { | ||
return a.BucketCounts().At(i) | ||
} | ||
return 0 | ||
jpkrohling marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// Upper returns the minimal index outside the set, such that every i < Upper | ||
func (a Absolute) Upper() int { | ||
return a.BucketCounts().Len() + int(a.Offset()) | ||
} | ||
|
||
// Lower returns the minimal index inside the set, such that every i >= Lower | ||
func (a Absolute) Lower() int { | ||
return int(a.Offset()) | ||
} | ||
|
||
func (a Absolute) idx(at int) (int, bool) { | ||
idx := at - a.Lower() | ||
return idx, idx >= 0 && idx < a.BucketCounts().Len() | ||
} |
63 changes: 63 additions & 0 deletions
63
processor/deltatocumulativeprocessor/internal/data/expo/expo_test.go
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,63 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package expo_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/deltatocumulativeprocessor/internal/data/expo" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/deltatocumulativeprocessor/internal/data/expo/expotest" | ||
) | ||
|
||
func TestAbsolute(t *testing.T) { | ||
is := expotest.Is(t) | ||
|
||
bs := expotest.Bins{ø, 1, 2, 3, 4, 5, ø, ø}.Into() | ||
abs := expo.Abs(bs) | ||
|
||
lo, up := abs.Lower(), abs.Upper() | ||
is.Equalf(-2, lo, "lower-bound") | ||
is.Equalf(3, up, "upper-bound") | ||
|
||
for i := lo; i < up; i++ { | ||
got := abs.Abs(i) | ||
is.Equal(bs.BucketCounts().At(i+2), got) | ||
} | ||
} | ||
|
||
func ExampleAbsolute() { | ||
nums := []float64{0.4, 2.3, 2.4, 4.5} | ||
|
||
bs := expotest.Observe0(nums...) | ||
abs := expo.Abs(bs) | ||
|
||
s := expo.Scale(0) | ||
for _, n := range nums { | ||
fmt.Printf("%.1f belongs to bucket %+d\n", n, s.Idx(n)) | ||
} | ||
|
||
fmt.Printf("\n index:") | ||
for i := 0; i < bs.BucketCounts().Len(); i++ { | ||
fmt.Printf(" %d", i) | ||
} | ||
fmt.Printf("\n abs:") | ||
for i := abs.Lower(); i < abs.Upper(); i++ { | ||
fmt.Printf(" %+d", i) | ||
} | ||
fmt.Printf("\ncounts:") | ||
for i := abs.Lower(); i < abs.Upper(); i++ { | ||
fmt.Printf(" %d", abs.Abs(i)) | ||
} | ||
|
||
// Output: | ||
tombrk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// 0.4 belongs to bucket -2 | ||
// 2.3 belongs to bucket +1 | ||
// 2.4 belongs to bucket +1 | ||
// 4.5 belongs to bucket +2 | ||
// | ||
// index: 0 1 2 3 4 | ||
// abs: -2 -1 +0 +1 +2 | ||
// counts: 1 0 0 2 1 | ||
} |
81 changes: 81 additions & 0 deletions
81
processor/deltatocumulativeprocessor/internal/data/expo/expotest/bins.go
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,81 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package expotest // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/deltatocumulativeprocessor/internal/data/expo/expotest" | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
|
||
"go.opentelemetry.io/collector/pdata/pmetric" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/deltatocumulativeprocessor/internal/data/expo" | ||
) | ||
|
||
const ( | ||
Empty = math.MaxUint64 | ||
ø = Empty | ||
) | ||
|
||
// index: 0 1 2 3 4 5 6 7 | ||
// bucket: -3 -2 -1 0 1 2 3 4 | ||
// bounds: (0.125,0.25], (0.25,0.5], (0.5,1], (1,2], (2,4], (4,8], (8,16], (16,32] | ||
type Bins [8]uint64 | ||
|
||
func (bins Bins) Into() expo.Buckets { | ||
start := 0 | ||
for i := 0; i < len(bins); i++ { | ||
if bins[i] != ø { | ||
start = i | ||
break | ||
} | ||
} | ||
|
||
end := len(bins) | ||
for i := start; i < len(bins); i++ { | ||
if bins[i] == ø { | ||
end = i | ||
break | ||
} | ||
} | ||
|
||
counts := bins[start:end] | ||
|
||
buckets := pmetric.NewExponentialHistogramDataPointBuckets() | ||
buckets.SetOffset(int32(start - 3)) | ||
buckets.BucketCounts().FromRaw(counts) | ||
return buckets | ||
} | ||
|
||
func ObserveInto(bs expo.Buckets, scale expo.Scale, pts ...float64) { | ||
counts := bs.BucketCounts() | ||
|
||
for _, pt := range pts { | ||
pt = math.Abs(pt) | ||
if pt <= 0.125 || pt > 32 { | ||
panic(fmt.Sprintf("out of bounds: 0.125 < %f <= 32", pt)) | ||
tombrk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
idx := scale.Idx(pt) - int(bs.Offset()) | ||
switch { | ||
case idx < 0: | ||
bs.SetOffset(bs.Offset() + int32(idx)) | ||
counts.FromRaw(append(make([]uint64, -idx), counts.AsRaw()...)) | ||
idx = 0 | ||
case idx >= counts.Len(): | ||
counts.Append(make([]uint64, idx-counts.Len()+1)...) | ||
} | ||
|
||
counts.SetAt(idx, counts.At(idx)+1) | ||
} | ||
} | ||
|
||
func Observe(scale expo.Scale, pts ...float64) expo.Buckets { | ||
bs := pmetric.NewExponentialHistogramDataPointBuckets() | ||
ObserveInto(bs, scale, pts...) | ||
return bs | ||
} | ||
|
||
func Observe0(pts ...float64) expo.Buckets { | ||
return Observe(0, pts...) | ||
} |
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.