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:
parent
8b25cb2a85
commit
99153429d5
@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
|
|||||||
### Added
|
### Added
|
||||||
|
|
||||||
- Prometheus exporter will register with a prometheus registerer on creation, there are options to control this. (#3239)
|
- 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
|
### Changed
|
||||||
|
|
||||||
|
@ -19,22 +19,28 @@ import (
|
|||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
"github.com/stretchr/testify/assert"
|
"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) {
|
func TestNewConfig(t *testing.T) {
|
||||||
registry := prometheus.NewRegistry()
|
registry := prometheus.NewRegistry()
|
||||||
|
|
||||||
|
aggregationSelector := func(view.InstrumentKind) aggregation.Aggregation { return nil }
|
||||||
|
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
name string
|
name string
|
||||||
options []Option
|
options []Option
|
||||||
wantRegisterer prometheus.Registerer
|
wantRegisterer prometheus.Registerer
|
||||||
|
wantAggregation metric.AggregationSelector
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "Default",
|
name: "Default",
|
||||||
options: nil,
|
options: nil,
|
||||||
wantRegisterer: prometheus.DefaultRegisterer,
|
wantRegisterer: prometheus.DefaultRegisterer,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
name: "WithRegisterer",
|
name: "WithRegisterer",
|
||||||
options: []Option{
|
options: []Option{
|
||||||
@ -42,6 +48,23 @@ func TestNewConfig(t *testing.T) {
|
|||||||
},
|
},
|
||||||
wantRegisterer: 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",
|
name: "nil options do nothing",
|
||||||
options: []Option{
|
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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -16,11 +16,14 @@ package prometheus // import "go.opentelemetry.io/otel/exporters/prometheus"
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
|
||||||
|
"go.opentelemetry.io/otel/sdk/metric"
|
||||||
)
|
)
|
||||||
|
|
||||||
// config contains options for the exporter.
|
// config contains options for the exporter.
|
||||||
type config struct {
|
type config struct {
|
||||||
registerer prometheus.Registerer
|
registerer prometheus.Registerer
|
||||||
|
aggregation metric.AggregationSelector
|
||||||
}
|
}
|
||||||
|
|
||||||
// newConfig creates a validated config configured with options.
|
// newConfig creates a validated config configured with options.
|
||||||
@ -37,6 +40,14 @@ func newConfig(opts ...Option) config {
|
|||||||
return cfg
|
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.
|
// Option sets exporter option values.
|
||||||
type Option interface {
|
type Option interface {
|
||||||
apply(config) config
|
apply(config) config
|
||||||
@ -57,3 +68,13 @@ func WithRegisterer(reg prometheus.Registerer) Option {
|
|||||||
return cfg
|
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
|
||||||
|
})
|
||||||
|
}
|
||||||
|
@ -50,7 +50,7 @@ func New(opts ...Option) (*Exporter, error) {
|
|||||||
// this assumes that the default temporality selector will always return cumulative.
|
// this assumes that the default temporality selector will always return cumulative.
|
||||||
// we only support cumulative temporality, so building our own reader enforces this.
|
// 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.
|
// TODO (#3244): Enable some way to configure the reader, but not change temporality.
|
||||||
reader := metric.NewManualReader()
|
reader := metric.NewManualReader(cfg.manualReaderOptions()...)
|
||||||
|
|
||||||
collector := &collector{
|
collector := &collector{
|
||||||
reader: reader,
|
reader: reader,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user