2022-10-14 09:22:43 -05:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2024-02-29 07:05:28 +01:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2022-10-14 09:22:43 -05:00
|
|
|
|
|
|
|
package prometheus // import "go.opentelemetry.io/otel/exporters/prometheus"
|
|
|
|
|
|
|
|
import (
|
2023-08-31 14:17:07 -04:00
|
|
|
"context"
|
2022-10-14 09:22:43 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2022-10-18 16:07:24 -05:00
|
|
|
|
2022-11-19 09:05:20 -08:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric"
|
2023-08-31 14:17:07 -04:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/metricdata"
|
2022-10-14 09:22:43 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewConfig(t *testing.T) {
|
|
|
|
registry := prometheus.NewRegistry()
|
|
|
|
|
2023-08-14 08:15:15 -07:00
|
|
|
aggregationSelector := func(metric.InstrumentKind) metric.Aggregation { return nil }
|
2023-08-31 14:17:07 -04:00
|
|
|
producer := &noopProducer{}
|
2022-10-18 16:07:24 -05:00
|
|
|
|
2022-10-14 09:22:43 -05:00
|
|
|
testCases := []struct {
|
2022-10-19 13:22:46 -03:00
|
|
|
name string
|
|
|
|
options []Option
|
|
|
|
wantConfig config
|
2022-10-14 09:22:43 -05:00
|
|
|
}{
|
|
|
|
{
|
2022-10-19 13:22:46 -03:00
|
|
|
name: "Default",
|
|
|
|
options: nil,
|
|
|
|
wantConfig: config{
|
|
|
|
registerer: prometheus.DefaultRegisterer,
|
|
|
|
},
|
2022-10-14 09:22:43 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "WithRegisterer",
|
|
|
|
options: []Option{
|
|
|
|
WithRegisterer(registry),
|
|
|
|
},
|
2022-10-19 13:22:46 -03:00
|
|
|
wantConfig: config{
|
|
|
|
registerer: registry,
|
|
|
|
},
|
2022-10-14 09:22:43 -05:00
|
|
|
},
|
2022-10-18 16:07:24 -05:00
|
|
|
{
|
|
|
|
name: "WithAggregationSelector",
|
|
|
|
options: []Option{
|
|
|
|
WithAggregationSelector(aggregationSelector),
|
|
|
|
},
|
2022-10-19 13:22:46 -03:00
|
|
|
wantConfig: config{
|
|
|
|
registerer: prometheus.DefaultRegisterer,
|
2023-08-31 14:17:07 -04:00
|
|
|
readerOpts: []metric.ManualReaderOption{metric.WithAggregationSelector(aggregationSelector)},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "WithProducer",
|
|
|
|
options: []Option{
|
|
|
|
WithProducer(producer),
|
|
|
|
},
|
|
|
|
wantConfig: config{
|
|
|
|
registerer: prometheus.DefaultRegisterer,
|
|
|
|
readerOpts: []metric.ManualReaderOption{metric.WithProducer(producer)},
|
2022-10-19 13:22:46 -03:00
|
|
|
},
|
2022-10-18 16:07:24 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "With Multiple Options",
|
|
|
|
options: []Option{
|
|
|
|
WithRegisterer(registry),
|
|
|
|
WithAggregationSelector(aggregationSelector),
|
2023-08-31 14:17:07 -04:00
|
|
|
WithProducer(producer),
|
2022-10-18 16:07:24 -05:00
|
|
|
},
|
2022-10-19 13:22:46 -03:00
|
|
|
|
|
|
|
wantConfig: config{
|
|
|
|
registerer: registry,
|
2023-08-31 14:17:07 -04:00
|
|
|
readerOpts: []metric.ManualReaderOption{
|
|
|
|
metric.WithAggregationSelector(aggregationSelector),
|
|
|
|
metric.WithProducer(producer),
|
|
|
|
},
|
2022-10-19 13:22:46 -03:00
|
|
|
},
|
2022-10-18 16:07:24 -05:00
|
|
|
},
|
2022-10-14 09:22:43 -05:00
|
|
|
{
|
|
|
|
name: "nil options do nothing",
|
|
|
|
options: []Option{
|
|
|
|
WithRegisterer(nil),
|
|
|
|
},
|
2022-10-19 13:22:46 -03:00
|
|
|
wantConfig: config{
|
|
|
|
registerer: prometheus.DefaultRegisterer,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "without target_info metric",
|
|
|
|
options: []Option{
|
|
|
|
WithoutTargetInfo(),
|
|
|
|
},
|
|
|
|
wantConfig: config{
|
|
|
|
registerer: prometheus.DefaultRegisterer,
|
|
|
|
disableTargetInfo: true,
|
|
|
|
},
|
2022-10-14 09:22:43 -05:00
|
|
|
},
|
2022-10-19 15:25:18 -04:00
|
|
|
{
|
|
|
|
name: "unit suffixes disabled",
|
|
|
|
options: []Option{
|
|
|
|
WithoutUnits(),
|
|
|
|
},
|
|
|
|
wantConfig: config{
|
|
|
|
registerer: prometheus.DefaultRegisterer,
|
|
|
|
withoutUnits: true,
|
|
|
|
},
|
|
|
|
},
|
2023-04-06 15:44:13 -03:00
|
|
|
{
|
|
|
|
name: "with namespace",
|
|
|
|
options: []Option{
|
|
|
|
WithNamespace("test"),
|
|
|
|
},
|
|
|
|
wantConfig: config{
|
|
|
|
registerer: prometheus.DefaultRegisterer,
|
|
|
|
namespace: "test_",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "with namespace with trailing underscore",
|
|
|
|
options: []Option{
|
|
|
|
WithNamespace("test_"),
|
|
|
|
},
|
|
|
|
wantConfig: config{
|
|
|
|
registerer: prometheus.DefaultRegisterer,
|
|
|
|
namespace: "test_",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "with unsanitized namespace",
|
|
|
|
options: []Option{
|
|
|
|
WithNamespace("test/"),
|
|
|
|
},
|
|
|
|
wantConfig: config{
|
|
|
|
registerer: prometheus.DefaultRegisterer,
|
|
|
|
namespace: "test_",
|
|
|
|
},
|
|
|
|
},
|
2022-10-14 09:22:43 -05:00
|
|
|
}
|
|
|
|
for _, tt := range testCases {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
cfg := newConfig(tt.options...)
|
2023-09-26 01:05:53 -07:00
|
|
|
// only check the length of readerOpts, since they are not comparable
|
2023-08-31 14:17:07 -04:00
|
|
|
assert.Equal(t, len(tt.wantConfig.readerOpts), len(cfg.readerOpts))
|
|
|
|
cfg.readerOpts = nil
|
|
|
|
tt.wantConfig.readerOpts = nil
|
2022-10-19 13:22:46 -03:00
|
|
|
|
|
|
|
assert.Equal(t, tt.wantConfig, cfg)
|
2022-10-14 09:22:43 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-10-18 16:07:24 -05:00
|
|
|
|
2023-08-31 14:17:07 -04:00
|
|
|
type noopProducer struct{}
|
2022-10-18 16:07:24 -05:00
|
|
|
|
2023-08-31 14:17:07 -04:00
|
|
|
func (*noopProducer) Produce(ctx context.Context) ([]metricdata.ScopeMetrics, error) {
|
|
|
|
return nil, nil
|
2022-10-18 16:07:24 -05:00
|
|
|
}
|