Skip to content

Commit

Permalink
Added WithAggregationSelector to prometheus (#3341)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
MadVikingGod and MrAlias committed Oct 18, 2022
1 parent 8b25cb2 commit 9915342
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
59 changes: 55 additions & 4 deletions exporters/prometheus/confg_test.go
Expand Up @@ -19,29 +19,52 @@ 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{
WithRegisterer(registry),
},
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{
Expand All @@ -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)
})
}
}
23 changes: 22 additions & 1 deletion exporters/prometheus/config.go
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
})
}
2 changes: 1 addition & 1 deletion exporters/prometheus/exporter.go
Expand Up @@ -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,
Expand Down

0 comments on commit 9915342

Please sign in to comment.