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

Added WithAggregationSelector to prometheus #3341

Merged
merged 4 commits into from Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 an option to change the AggregationSelector for prometheus. (#????)
MadVikingGod marked this conversation as resolved.
Show resolved Hide resolved

### Changed

Expand Down
60 changes: 56 additions & 4 deletions exporters/prometheus/confg_test.go
Expand Up @@ -19,29 +19,51 @@ 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 +80,33 @@ func TestNewConfig(t *testing.T) {
})
}
}

func TestConfigManualReaderOptions(t *testing.T) {
aggregationSelector := func(view.InstrumentKind) aggregation.Aggregation { return nil }

testCases := []struct {
name string
config config
wantOption []metric.ManualReaderOption
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
}{
{
name: "Default",
config: config{},
wantOption: []metric.ManualReaderOption{},
},

{
name: "WithAggregationSelector",
config: config{aggregation: aggregationSelector},
wantOption: []metric.ManualReaderOption{
metric.WithAggregationSelector(aggregationSelector),
},
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
opts := tt.config.manualReaderOptions()
assert.Len(t, opts, len(tt.wantOption))
})
}
}
19 changes: 18 additions & 1 deletion exporters/prometheus/config.go
Expand Up @@ -16,11 +16,13 @@ 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 +39,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 +67,10 @@ func WithRegisterer(reg prometheus.Registerer) Option {
return cfg
})
}

func WithAggregationSelector(agg metric.AggregationSelector) Option {
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
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