1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-01-26 03:52:03 +02:00

Added WithAggregationSelector to prometheus (#3341)

* Added WithAggregationSelector to prometheus

* Update CHANGELOG.md

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>

* Address PR comments

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
This commit is contained in:
Aaron Clawson 2022-10-18 16:07:24 -05:00 committed by GitHub
parent 8b25cb2a85
commit 99153429d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 79 additions and 6 deletions

View File

@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Added
- Prometheus exporter will register with a prometheus registerer on creation, there are options to control this. (#3239)
- Added the `WithAggregationSelector` option to the `go.opentelemetry.io/otel/exporters/prometheus` package to change the `AggregationSelector` used. (#3341)
### Changed

View File

@ -19,22 +19,28 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/aggregation"
"go.opentelemetry.io/otel/sdk/metric/view"
)
func TestNewConfig(t *testing.T) {
registry := prometheus.NewRegistry()
aggregationSelector := func(view.InstrumentKind) aggregation.Aggregation { return nil }
testCases := []struct {
name string
options []Option
wantRegisterer prometheus.Registerer
name string
options []Option
wantRegisterer prometheus.Registerer
wantAggregation metric.AggregationSelector
}{
{
name: "Default",
options: nil,
wantRegisterer: prometheus.DefaultRegisterer,
},
{
name: "WithRegisterer",
options: []Option{
@ -42,6 +48,23 @@ func TestNewConfig(t *testing.T) {
},
wantRegisterer: registry,
},
{
name: "WithAggregationSelector",
options: []Option{
WithAggregationSelector(aggregationSelector),
},
wantRegisterer: prometheus.DefaultRegisterer,
wantAggregation: aggregationSelector,
},
{
name: "With Multiple Options",
options: []Option{
WithRegisterer(registry),
WithAggregationSelector(aggregationSelector),
},
wantRegisterer: registry,
wantAggregation: aggregationSelector,
},
{
name: "nil options do nothing",
options: []Option{
@ -58,3 +81,31 @@ func TestNewConfig(t *testing.T) {
})
}
}
func TestConfigManualReaderOptions(t *testing.T) {
aggregationSelector := func(view.InstrumentKind) aggregation.Aggregation { return nil }
testCases := []struct {
name string
config config
wantOptionCount int
}{
{
name: "Default",
config: config{},
wantOptionCount: 0,
},
{
name: "WithAggregationSelector",
config: config{aggregation: aggregationSelector},
wantOptionCount: 1,
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
opts := tt.config.manualReaderOptions()
assert.Len(t, opts, tt.wantOptionCount)
})
}
}

View File

@ -16,11 +16,14 @@ package prometheus // import "go.opentelemetry.io/otel/exporters/prometheus"
import (
"github.com/prometheus/client_golang/prometheus"
"go.opentelemetry.io/otel/sdk/metric"
)
// config contains options for the exporter.
type config struct {
registerer prometheus.Registerer
registerer prometheus.Registerer
aggregation metric.AggregationSelector
}
// newConfig creates a validated config configured with options.
@ -37,6 +40,14 @@ func newConfig(opts ...Option) config {
return cfg
}
func (cfg config) manualReaderOptions() []metric.ManualReaderOption {
opts := []metric.ManualReaderOption{}
if cfg.aggregation != nil {
opts = append(opts, metric.WithAggregationSelector(cfg.aggregation))
}
return opts
}
// Option sets exporter option values.
type Option interface {
apply(config) config
@ -57,3 +68,13 @@ func WithRegisterer(reg prometheus.Registerer) Option {
return cfg
})
}
// WithAggregationSelector configure the Aggregation Selector the exporter will
// use. If no AggregationSelector is provided the DefaultAggregationSelector is
// used.
func WithAggregationSelector(agg metric.AggregationSelector) Option {
return optionFunc(func(cfg config) config {
cfg.aggregation = agg
return cfg
})
}

View File

@ -50,7 +50,7 @@ func New(opts ...Option) (*Exporter, error) {
// this assumes that the default temporality selector will always return cumulative.
// we only support cumulative temporality, so building our own reader enforces this.
// TODO (#3244): Enable some way to configure the reader, but not change temporality.
reader := metric.NewManualReader()
reader := metric.NewManualReader(cfg.manualReaderOptions()...)
collector := &collector{
reader: reader,