2022-10-14 16:22:43 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package prometheus // import "go.opentelemetry.io/otel/exporters/prometheus"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2022-10-18 23:07:24 +02:00
|
|
|
|
2022-11-19 19:05:20 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric"
|
2022-10-18 23:07:24 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregation"
|
2022-10-14 16:22:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewConfig(t *testing.T) {
|
|
|
|
registry := prometheus.NewRegistry()
|
|
|
|
|
2022-11-19 19:05:20 +02:00
|
|
|
aggregationSelector := func(metric.InstrumentKind) aggregation.Aggregation { return nil }
|
2022-10-18 23:07:24 +02:00
|
|
|
|
2022-10-14 16:22:43 +02:00
|
|
|
testCases := []struct {
|
2022-10-19 18:22:46 +02:00
|
|
|
name string
|
|
|
|
options []Option
|
|
|
|
wantConfig config
|
2022-10-14 16:22:43 +02:00
|
|
|
}{
|
|
|
|
{
|
2022-10-19 18:22:46 +02:00
|
|
|
name: "Default",
|
|
|
|
options: nil,
|
|
|
|
wantConfig: config{
|
|
|
|
registerer: prometheus.DefaultRegisterer,
|
|
|
|
},
|
2022-10-14 16:22:43 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "WithRegisterer",
|
|
|
|
options: []Option{
|
|
|
|
WithRegisterer(registry),
|
|
|
|
},
|
2022-10-19 18:22:46 +02:00
|
|
|
wantConfig: config{
|
|
|
|
registerer: registry,
|
|
|
|
},
|
2022-10-14 16:22:43 +02:00
|
|
|
},
|
2022-10-18 23:07:24 +02:00
|
|
|
{
|
|
|
|
name: "WithAggregationSelector",
|
|
|
|
options: []Option{
|
|
|
|
WithAggregationSelector(aggregationSelector),
|
|
|
|
},
|
2022-10-19 18:22:46 +02:00
|
|
|
wantConfig: config{
|
|
|
|
registerer: prometheus.DefaultRegisterer,
|
|
|
|
},
|
2022-10-18 23:07:24 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "With Multiple Options",
|
|
|
|
options: []Option{
|
|
|
|
WithRegisterer(registry),
|
|
|
|
WithAggregationSelector(aggregationSelector),
|
|
|
|
},
|
2022-10-19 18:22:46 +02:00
|
|
|
|
|
|
|
wantConfig: config{
|
|
|
|
registerer: registry,
|
|
|
|
},
|
2022-10-18 23:07:24 +02:00
|
|
|
},
|
2022-10-14 16:22:43 +02:00
|
|
|
{
|
|
|
|
name: "nil options do nothing",
|
|
|
|
options: []Option{
|
|
|
|
WithRegisterer(nil),
|
|
|
|
},
|
2022-10-19 18:22:46 +02:00
|
|
|
wantConfig: config{
|
|
|
|
registerer: prometheus.DefaultRegisterer,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "without target_info metric",
|
|
|
|
options: []Option{
|
|
|
|
WithoutTargetInfo(),
|
|
|
|
},
|
|
|
|
wantConfig: config{
|
|
|
|
registerer: prometheus.DefaultRegisterer,
|
|
|
|
disableTargetInfo: true,
|
|
|
|
},
|
2022-10-14 16:22:43 +02:00
|
|
|
},
|
2022-10-19 21:25:18 +02:00
|
|
|
{
|
|
|
|
name: "unit suffixes disabled",
|
|
|
|
options: []Option{
|
|
|
|
WithoutUnits(),
|
|
|
|
},
|
|
|
|
wantConfig: config{
|
|
|
|
registerer: prometheus.DefaultRegisterer,
|
|
|
|
withoutUnits: true,
|
|
|
|
},
|
|
|
|
},
|
2023-04-06 20:44:13 +02: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 16:22:43 +02:00
|
|
|
}
|
|
|
|
for _, tt := range testCases {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
cfg := newConfig(tt.options...)
|
2022-10-19 18:22:46 +02:00
|
|
|
// tested by TestConfigManualReaderOptions
|
|
|
|
cfg.aggregation = nil
|
|
|
|
|
|
|
|
assert.Equal(t, tt.wantConfig, cfg)
|
2022-10-14 16:22:43 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-10-18 23:07:24 +02:00
|
|
|
|
|
|
|
func TestConfigManualReaderOptions(t *testing.T) {
|
2022-11-19 19:05:20 +02:00
|
|
|
aggregationSelector := func(metric.InstrumentKind) aggregation.Aggregation { return nil }
|
2022-10-18 23:07:24 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|