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

Add internal package structure for aggregation #2954

Merged
merged 28 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
8fc1c74
Add the aggtor package
MrAlias Jun 13, 2022
dc1d912
Restrict to Go 1.18
MrAlias Jun 13, 2022
f9e8a49
Add missing build block to view_test.go
MrAlias Jun 14, 2022
59df1f7
Comment Aggregator iface
MrAlias Jun 14, 2022
0a700fe
Use Go 1.18 as the default ci version
MrAlias Jun 14, 2022
a5c5834
Update Aggregator iface from feedback
MrAlias Jun 15, 2022
538ff99
Merge branch 'new_sdk/main' into aggtor
MrAlias Jun 21, 2022
aa8b301
Accept hist conf
MrAlias Jun 21, 2022
f390e41
Flatten aggtor into just internal
MrAlias Jun 21, 2022
778c10a
Add Cycler interface
MrAlias Jun 21, 2022
bd9c963
Remove build flags for doc.go
MrAlias Jun 21, 2022
816f3dc
Clarify Cycler documentation
MrAlias Jun 21, 2022
0d7923c
Remove aggregation fold logic
MrAlias Jun 21, 2022
0287a06
Rename Number to Atomic
MrAlias Jun 21, 2022
36fad53
Add tests for Atomic impls
MrAlias Jun 21, 2022
47082f0
Remove unneeded Atomic implementation
MrAlias Jun 21, 2022
86061cc
Fix article in Float64 docs
MrAlias Jun 21, 2022
fc0f30e
Remove Atomic
MrAlias Jun 21, 2022
658f4a5
Add aggregator_example_test
MrAlias Jun 22, 2022
02488a2
Fix hist example
MrAlias Jun 22, 2022
c266e46
Add issue numbers to all TODO and FIXME
MrAlias Jun 22, 2022
3057ac2
Remove zero parameter comment
MrAlias Jun 22, 2022
dd82716
Merge branch 'new_sdk/main' into aggtor
MrAlias Jun 23, 2022
6b39c54
Combine the cycler into the aggregators
MrAlias Jun 23, 2022
02edebd
Remove the drop aggregator
MrAlias Jun 23, 2022
c7cfda1
Fix lint
MrAlias Jun 23, 2022
3d0d54e
Use attribute.Set instead of ptr to it
MrAlias Jun 24, 2022
42ba11d
Merge branch 'new_sdk/main' into aggtor
Aneurysm9 Jul 6, 2022
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ env:
# Path to where test results will be saved.
TEST_RESULTS: /tmp/test-results
# Default minimum version of Go to support.
DEFAULT_GO_VERSION: 1.17
DEFAULT_GO_VERSION: 1.18
jobs:
lint:
runs-on: ubuntu-latest
Expand Down
4 changes: 2 additions & 2 deletions sdk/metric/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build go1.17
// +build go1.17
//go:build go1.18
// +build go1.18

package metric // import "go.opentelemetry.io/otel/sdk/metric"

Expand Down
4 changes: 2 additions & 2 deletions sdk/metric/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build go1.17
// +build go1.17
//go:build go1.18
// +build go1.18

package metric

Expand Down
4 changes: 2 additions & 2 deletions sdk/metric/export/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build go1.17
// +build go1.17
//go:build go1.18
// +build go1.18

// TODO: NOTE this is a temporary space, it may be moved following the
// discussion of #2813, or #2841
Expand Down
4 changes: 2 additions & 2 deletions sdk/metric/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build go1.17
// +build go1.17
//go:build go1.18
// +build go1.18

package metric // import "go.opentelemetry.io/otel/sdk/metric"

Expand Down
2 changes: 1 addition & 1 deletion sdk/metric/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module go.opentelemetry.io/otel/sdk/metric

go 1.17
go 1.18

require (
github.com/go-logr/logr v1.2.3
Expand Down
1 change: 0 additions & 1 deletion sdk/metric/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbV
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
4 changes: 2 additions & 2 deletions sdk/metric/instrumentkind.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build go1.17
// +build go1.17
//go:build go1.18
// +build go1.18

package metric // import "go.opentelemetry.io/otel/sdk/metric"

Expand Down
106 changes: 106 additions & 0 deletions sdk/metric/internal/aggtor/aggregation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build go1.18
// +build go1.18

package aggtor // import "go.opentelemetry.io/otel/sdk/metric/internal/aggtor"

import (
"errors"
"fmt"
)

// Aggregation is a single data point in a timeseries that summarizes
// measurements made during a time span.
type Aggregation struct {
// Timestamp defines the time the last measurement was made. If zero, no
// measurements were made for this time span. The time is represented as a
// unix timestamp with nanosecond precision.
Timestamp uint64

// Value is the summarization of the measurements made.
Value value
}

var errIncompatible = errors.New("incompatible aggregation")

// Fold combines other into a.
func (a Aggregation) Fold(other Aggregation) error {
if other.Timestamp > a.Timestamp {
a.Timestamp = other.Timestamp
}
return a.Value.fold(other.Value)
}

type value interface {
// fold combines other into the value. It will return an errIncompatible
// if other is not a compatible type with value.
fold(other value) error
}

// SingleValue summarizes a set of measurements as a single numeric value.
type SingleValue[N int64 | float64] struct {
Value N
}

func (v SingleValue[N]) fold(other value) error {
o, ok := other.(SingleValue[N])
if !ok {
return fmt.Errorf("%w: value types %T and %T", errIncompatible, v, other)
}
v.Value += o.Value
return nil
}

// HistogramValue summarizes a set of measurements as a histogram.
type HistogramValue struct {
Bounds []float64
Counts []uint64
Sum float64
Min, Max float64
}

func (v HistogramValue) fold(other value) error {
o, ok := other.(HistogramValue)
if !ok {
return fmt.Errorf("%w: value types %T and %T", errIncompatible, v, other)
}
if !sliceEqual[float64](v.Bounds, o.Bounds) || len(o.Counts) != len(v.Counts) {
return fmt.Errorf("%w: different histogram binning", errIncompatible)
}
v.Sum += o.Sum
for i, c := range o.Counts {
v.Counts[i] += c
}
if o.Min < v.Min {
v.Min = o.Min
}
if o.Max > v.Max {
v.Max = o.Max
}
return nil
}

func sliceEqual[T comparable](a, b []T) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}
27 changes: 27 additions & 0 deletions sdk/metric/internal/aggtor/aggregator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build go1.18
// +build go1.18

package aggtor // import "go.opentelemetry.io/otel/sdk/metric/internal/aggtor"

// Aggregator forms an aggregation from a collection of recorded measurements.
type Aggregator[N int64 | float64] interface {
// Record includes value in the aggregation.
Record(value N)

// Aggregate returns an aggregation of all recorded values.
Aggregate() Aggregation
}
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
20 changes: 20 additions & 0 deletions sdk/metric/internal/aggtor/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build go1.18
// +build go1.18

// Package aggtor provides types and functionality to accumulate and aggregate
// measurements.
package aggtor // import "go.opentelemetry.io/otel/sdk/metric/internal/aggtor"
29 changes: 29 additions & 0 deletions sdk/metric/internal/aggtor/drop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build go1.18
// +build go1.18

package aggtor // import "go.opentelemetry.io/otel/sdk/metric/internal/aggtor"

// dropAgg drops all recorded data and returns an empty Aggregation.
type dropAgg[N int64 | float64] struct{}

// NewDrop returns an Aggregator that drops all recorded data and returns an
// empty Aggregation.
func NewDrop[N int64 | float64]() Aggregator[N] { return &dropAgg[N]{} }

func (s *dropAgg[N]) Record(value N) {}

func (s *dropAgg[N]) Aggregate() Aggregation { return Aggregation{} }
40 changes: 40 additions & 0 deletions sdk/metric/internal/aggtor/histogram.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build go1.18
// +build go1.18

package aggtor // import "go.opentelemetry.io/otel/sdk/metric/internal/aggtor"

// histogramAgg summarizes a set of measurements as an histogram with
// explicitly defined buckets.
type histogramAgg[N int64 | float64] struct {
// TODO: implement.
}

// NewHistogram returns an Aggregator that summarizes a set of measurements as
// an histogram. The zero value will be used as the start value for all the
// buckets of new Aggregations.
func NewHistogram[N int64 | float64](zero Number[N], bounds []float64, recordMinMax bool) Aggregator[N] {
return &histogramAgg[N]{}
}

func (s *histogramAgg[N]) Record(value N) {
// TODO: implement.
}

func (s *histogramAgg[N]) Aggregate() Aggregation {
// TODO: implement.
return Aggregation{Value: HistogramValue{ /* TODO: calculate. */ }}
}
39 changes: 39 additions & 0 deletions sdk/metric/internal/aggtor/lastvalue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build go1.18
// +build go1.18

package aggtor // import "go.opentelemetry.io/otel/sdk/metric/internal/aggtor"

// lastValueAgg summarizes a set of measurements as the last one made.
type lastValueAgg[N int64 | float64] struct {
// TODO: implement.
}

// NewLastValue returns an Aggregator that summarizes a set of measurements as
// the last one made. The zero value will be used as the start value for all
// new Aggregations.
func NewLastValue[N int64 | float64](zero Number[N]) Aggregator[N] {
return &lastValueAgg[N]{}
}

func (s *lastValueAgg[N]) Record(value N) {
// TODO: implement.
}

func (s *lastValueAgg[N]) Aggregate() Aggregation {
// TODO: implement.
return Aggregation{Value: SingleValue[N]{ /* TODO: calculate */ }}
}
Loading