forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[processor/deltatocumulative]: Sums (open-telemetry#30707)
**Description:** Implements major component functionality: - [x] metrics identification (`metrics.Ident`) and stream identification (`streams.Ident`) - [x] abstract data layer `data.Point` that keeps processing code data type (sum, histogram, exp histogram) agnostic - [x] `delta.Accumulator` stream processor for accumulating any `data.Point` **Link to tracking Issue:** open-telemetry#30705 **Testing:** Done **Documentation:** <kbd>TODO</kbd>
- Loading branch information
1 parent
1b5b72a
commit 2a3e0f0
Showing
21 changed files
with
952 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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,27 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: new_component | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: deltatocumulative | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: adds processor to convert sums (initially) from delta to cumulative temporality | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [30705] | ||
|
||
# (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: | ||
|
||
# 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] |
Validating CODEOWNERS rules …
This file contains 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 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package data // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/deltatocumulativeprocessor/internal/data" | ||
|
||
import "go.opentelemetry.io/collector/pdata/pmetric" | ||
|
||
func (dp Number) Add(in Number) Number { | ||
switch in.ValueType() { | ||
case pmetric.NumberDataPointValueTypeDouble: | ||
v := dp.DoubleValue() + in.DoubleValue() | ||
dp.SetDoubleValue(v) | ||
case pmetric.NumberDataPointValueTypeInt: | ||
v := dp.IntValue() + in.IntValue() | ||
dp.SetIntValue(v) | ||
} | ||
dp.SetTimestamp(in.Timestamp()) | ||
return dp | ||
} | ||
|
||
// nolint | ||
func (dp Histogram) Add(in Histogram) Histogram { | ||
panic("todo") | ||
} | ||
|
||
// nolint | ||
func (dp ExpHistogram) Add(in ExpHistogram) ExpHistogram { | ||
panic("todo") | ||
} |
76 changes: 76 additions & 0 deletions
76
processor/deltatocumulativeprocessor/internal/data/data.go
This file contains 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,76 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package data // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/deltatocumulativeprocessor/internal/data" | ||
|
||
import ( | ||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
"go.opentelemetry.io/collector/pdata/pmetric" | ||
) | ||
|
||
type Point[Self any] interface { | ||
StartTimestamp() pcommon.Timestamp | ||
Timestamp() pcommon.Timestamp | ||
Attributes() pcommon.Map | ||
|
||
Clone() Self | ||
CopyTo(Self) | ||
|
||
Add(Self) Self | ||
} | ||
|
||
type Number struct { | ||
pmetric.NumberDataPoint | ||
} | ||
|
||
func (dp Number) Clone() Number { | ||
clone := Number{NumberDataPoint: pmetric.NewNumberDataPoint()} | ||
if dp.NumberDataPoint != (pmetric.NumberDataPoint{}) { | ||
dp.CopyTo(clone) | ||
} | ||
return clone | ||
} | ||
|
||
func (dp Number) CopyTo(dst Number) { | ||
dp.NumberDataPoint.CopyTo(dst.NumberDataPoint) | ||
} | ||
|
||
type Histogram struct { | ||
pmetric.HistogramDataPoint | ||
} | ||
|
||
func (dp Histogram) Clone() Histogram { | ||
clone := Histogram{HistogramDataPoint: pmetric.NewHistogramDataPoint()} | ||
if dp.HistogramDataPoint != (pmetric.HistogramDataPoint{}) { | ||
dp.CopyTo(clone) | ||
} | ||
return clone | ||
} | ||
|
||
func (dp Histogram) CopyTo(dst Histogram) { | ||
dp.HistogramDataPoint.CopyTo(dst.HistogramDataPoint) | ||
} | ||
|
||
type ExpHistogram struct { | ||
pmetric.ExponentialHistogramDataPoint | ||
} | ||
|
||
func (dp ExpHistogram) Clone() ExpHistogram { | ||
clone := ExpHistogram{ExponentialHistogramDataPoint: pmetric.NewExponentialHistogramDataPoint()} | ||
if dp.ExponentialHistogramDataPoint != (pmetric.ExponentialHistogramDataPoint{}) { | ||
dp.CopyTo(clone) | ||
} | ||
return clone | ||
} | ||
|
||
func (dp ExpHistogram) CopyTo(dst ExpHistogram) { | ||
dp.ExponentialHistogramDataPoint.CopyTo(dst.ExponentialHistogramDataPoint) | ||
} | ||
|
||
type mustPoint[D Point[D]] struct{ _ D } | ||
|
||
var ( | ||
_ = mustPoint[Number]{} | ||
_ = mustPoint[Histogram]{} | ||
_ = mustPoint[ExpHistogram]{} | ||
) |
83 changes: 83 additions & 0 deletions
83
processor/deltatocumulativeprocessor/internal/delta/delta.go
This file contains 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,83 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package delta // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/deltatocumulativeprocessor/internal/delta" | ||
|
||
import ( | ||
"fmt" | ||
|
||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/deltatocumulativeprocessor/internal/data" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/deltatocumulativeprocessor/internal/streams" | ||
) | ||
|
||
func construct[D data.Point[D]]() streams.Aggregator[D] { | ||
acc := &Accumulator[D]{dps: make(map[streams.Ident]D)} | ||
return &Lock[D]{next: acc} | ||
} | ||
|
||
func Numbers() streams.Aggregator[data.Number] { | ||
return construct[data.Number]() | ||
} | ||
|
||
func Histograms() streams.Aggregator[data.Histogram] { | ||
return construct[data.Histogram]() | ||
} | ||
|
||
var _ streams.Aggregator[data.Number] = (*Accumulator[data.Number])(nil) | ||
|
||
type Accumulator[D data.Point[D]] struct { | ||
dps map[streams.Ident]D | ||
} | ||
|
||
// Aggregate implements delta-to-cumulative aggregation as per spec: | ||
// https://opentelemetry.io/docs/specs/otel/metrics/data-model/#sums-delta-to-cumulative | ||
func (a *Accumulator[D]) Aggregate(id streams.Ident, dp D) (D, error) { | ||
// make the accumulator to start with the current sample, discarding any | ||
// earlier data. return after use | ||
reset := func() (D, error) { | ||
a.dps[id] = dp.Clone() | ||
return a.dps[id], nil | ||
} | ||
|
||
aggr, ok := a.dps[id] | ||
|
||
// new series: reset | ||
if !ok { | ||
return reset() | ||
} | ||
// belongs to older series: drop | ||
if dp.StartTimestamp() < aggr.StartTimestamp() { | ||
return aggr, ErrOlderStart{Start: aggr.StartTimestamp(), Sample: dp.StartTimestamp()} | ||
} | ||
// belongs to later series: reset | ||
if dp.StartTimestamp() > aggr.StartTimestamp() { | ||
return reset() | ||
} | ||
// out of order: drop | ||
if dp.Timestamp() <= aggr.Timestamp() { | ||
return aggr, ErrOutOfOrder{Last: aggr.Timestamp(), Sample: dp.Timestamp()} | ||
} | ||
|
||
a.dps[id] = aggr.Add(dp) | ||
return a.dps[id], nil | ||
} | ||
|
||
type ErrOlderStart struct { | ||
Start pcommon.Timestamp | ||
Sample pcommon.Timestamp | ||
} | ||
|
||
func (e ErrOlderStart) Error() string { | ||
return fmt.Sprintf("dropped sample with start_time=%s, because series only starts at start_time=%s. consider checking for multiple processes sending the exact same series", e.Sample, e.Start) | ||
} | ||
|
||
type ErrOutOfOrder struct { | ||
Last pcommon.Timestamp | ||
Sample pcommon.Timestamp | ||
} | ||
|
||
func (e ErrOutOfOrder) Error() string { | ||
return fmt.Sprintf("out of order: dropped sample from time=%s, because series is already at time=%s", e.Sample, e.Last) | ||
} |
Oops, something went wrong.