| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package prometheus // import "go.opentelemetry.io/otel/exporters/prometheus" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/prometheus/client_golang/prometheus" |
| 11 |
"github.com/prometheus/otlptranslator" |
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
|
| 14 |
"go.opentelemetry.io/otel/sdk/metric" |
| 15 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 16 |
) |
| 17 |
|
| 18 |
func TestNewConfig(t *testing.T) { |
| 19 |
registry := prometheus.NewRegistry() |
| 20 |
|
| 21 |
aggregationSelector := func(metric.InstrumentKind) metric.Aggregation { return nil } |
| 22 |
producer := &noopProducer{} |
| 23 |
|
| 24 |
testCases := []struct { |
| 25 |
name string |
| 26 |
options []Option |
| 27 |
wantConfig config |
| 28 |
}{ |
| 29 |
{ |
| 30 |
name: "Default", |
| 31 |
options: nil, |
| 32 |
wantConfig: config{ |
| 33 |
translationStrategy: otlptranslator.UnderscoreEscapingWithSuffixes, |
| 34 |
registerer: prometheus.DefaultRegisterer, |
| 35 |
}, |
| 36 |
}, |
| 37 |
{ |
| 38 |
name: "WithRegisterer", |
| 39 |
options: []Option{ |
| 40 |
WithRegisterer(registry), |
| 41 |
}, |
| 42 |
wantConfig: config{ |
| 43 |
translationStrategy: otlptranslator.UnderscoreEscapingWithSuffixes, |
| 44 |
registerer: registry, |
| 45 |
}, |
| 46 |
}, |
| 47 |
{ |
| 48 |
name: "WithAggregationSelector", |
| 49 |
options: []Option{ |
| 50 |
WithAggregationSelector(aggregationSelector), |
| 51 |
}, |
| 52 |
wantConfig: config{ |
| 53 |
translationStrategy: otlptranslator.UnderscoreEscapingWithSuffixes, |
| 54 |
registerer: prometheus.DefaultRegisterer, |
| 55 |
readerOpts: []metric.ManualReaderOption{metric.WithAggregationSelector(aggregationSelector)}, |
| 56 |
}, |
| 57 |
}, |
| 58 |
{ |
| 59 |
name: "WithProducer", |
| 60 |
options: []Option{ |
| 61 |
WithProducer(producer), |
| 62 |
}, |
| 63 |
wantConfig: config{ |
| 64 |
translationStrategy: otlptranslator.UnderscoreEscapingWithSuffixes, |
| 65 |
registerer: prometheus.DefaultRegisterer, |
| 66 |
readerOpts: []metric.ManualReaderOption{metric.WithProducer(producer)}, |
| 67 |
}, |
| 68 |
}, |
| 69 |
{ |
| 70 |
name: "With Multiple Options", |
| 71 |
options: []Option{ |
| 72 |
WithRegisterer(registry), |
| 73 |
WithAggregationSelector(aggregationSelector), |
| 74 |
WithProducer(producer), |
| 75 |
}, |
| 76 |
|
| 77 |
wantConfig: config{ |
| 78 |
translationStrategy: otlptranslator.UnderscoreEscapingWithSuffixes, |
| 79 |
registerer: registry, |
| 80 |
readerOpts: []metric.ManualReaderOption{ |
| 81 |
metric.WithAggregationSelector(aggregationSelector), |
| 82 |
metric.WithProducer(producer), |
| 83 |
}, |
| 84 |
}, |
| 85 |
}, |
| 86 |
{ |
| 87 |
name: "nil options do nothing", |
| 88 |
options: []Option{ |
| 89 |
WithRegisterer(nil), |
| 90 |
}, |
| 91 |
wantConfig: config{ |
| 92 |
translationStrategy: otlptranslator.UnderscoreEscapingWithSuffixes, |
| 93 |
registerer: prometheus.DefaultRegisterer, |
| 94 |
}, |
| 95 |
}, |
| 96 |
{ |
| 97 |
name: "without target_info metric", |
| 98 |
options: []Option{ |
| 99 |
WithoutTargetInfo(), |
| 100 |
}, |
| 101 |
wantConfig: config{ |
| 102 |
translationStrategy: otlptranslator.UnderscoreEscapingWithSuffixes, |
| 103 |
registerer: prometheus.DefaultRegisterer, |
| 104 |
disableTargetInfo: true, |
| 105 |
}, |
| 106 |
}, |
| 107 |
{ |
| 108 |
name: "legacy validation mode default", |
| 109 |
options: []Option{}, |
| 110 |
wantConfig: config{ |
| 111 |
translationStrategy: otlptranslator.UnderscoreEscapingWithSuffixes, |
| 112 |
registerer: prometheus.DefaultRegisterer, |
| 113 |
}, |
| 114 |
}, |
| 115 |
{ |
| 116 |
name: "legacy validation mode, unit suffixes disabled", |
| 117 |
options: []Option{ |
| 118 |
WithoutUnits(), |
| 119 |
}, |
| 120 |
wantConfig: config{ |
| 121 |
translationStrategy: otlptranslator.UnderscoreEscapingWithSuffixes, |
| 122 |
registerer: prometheus.DefaultRegisterer, |
| 123 |
withoutUnits: true, |
| 124 |
}, |
| 125 |
}, |
| 126 |
{ |
| 127 |
name: "legacy validation mode, counter suffixes disabled", |
| 128 |
options: []Option{ |
| 129 |
WithoutCounterSuffixes(), |
| 130 |
}, |
| 131 |
wantConfig: config{ |
| 132 |
translationStrategy: otlptranslator.UnderscoreEscapingWithSuffixes, |
| 133 |
registerer: prometheus.DefaultRegisterer, |
| 134 |
withoutCounterSuffixes: true, |
| 135 |
}, |
| 136 |
}, |
| 137 |
{ |
| 138 |
name: "unit suffixes disabled", |
| 139 |
options: []Option{ |
| 140 |
WithoutUnits(), |
| 141 |
}, |
| 142 |
wantConfig: config{ |
| 143 |
translationStrategy: otlptranslator.UnderscoreEscapingWithSuffixes, |
| 144 |
registerer: prometheus.DefaultRegisterer, |
| 145 |
withoutUnits: true, |
| 146 |
}, |
| 147 |
}, |
| 148 |
{ |
| 149 |
name: "NoTranslation implies no suffixes", |
| 150 |
options: []Option{ |
| 151 |
WithTranslationStrategy(otlptranslator.NoTranslation), |
| 152 |
}, |
| 153 |
wantConfig: config{ |
| 154 |
translationStrategy: otlptranslator.NoTranslation, |
| 155 |
withoutUnits: true, |
| 156 |
withoutCounterSuffixes: true, |
| 157 |
registerer: prometheus.DefaultRegisterer, |
| 158 |
}, |
| 159 |
}, |
| 160 |
{ |
| 161 |
name: "translation strategy does not override unit suffixes disabled", |
| 162 |
options: []Option{ |
| 163 |
WithTranslationStrategy(otlptranslator.NoUTF8EscapingWithSuffixes), |
| 164 |
WithoutUnits(), |
| 165 |
}, |
| 166 |
wantConfig: config{ |
| 167 |
translationStrategy: otlptranslator.NoUTF8EscapingWithSuffixes, |
| 168 |
registerer: prometheus.DefaultRegisterer, |
| 169 |
withoutUnits: true, |
| 170 |
}, |
| 171 |
}, |
| 172 |
{ |
| 173 |
name: "translation strategy does not override counter suffixes disabled", |
| 174 |
options: []Option{ |
| 175 |
WithTranslationStrategy(otlptranslator.UnderscoreEscapingWithSuffixes), |
| 176 |
WithoutCounterSuffixes(), |
| 177 |
}, |
| 178 |
wantConfig: config{ |
| 179 |
translationStrategy: otlptranslator.UnderscoreEscapingWithSuffixes, |
| 180 |
registerer: prometheus.DefaultRegisterer, |
| 181 |
withoutCounterSuffixes: true, |
| 182 |
}, |
| 183 |
}, |
| 184 |
{ |
| 185 |
name: "with namespace", |
| 186 |
options: []Option{ |
| 187 |
WithNamespace("test"), |
| 188 |
}, |
| 189 |
wantConfig: config{ |
| 190 |
translationStrategy: otlptranslator.UnderscoreEscapingWithSuffixes, |
| 191 |
registerer: prometheus.DefaultRegisterer, |
| 192 |
namespace: "test", |
| 193 |
}, |
| 194 |
}, |
| 195 |
{ |
| 196 |
name: "with namespace with trailing underscore", |
| 197 |
options: []Option{ |
| 198 |
WithNamespace("test"), |
| 199 |
}, |
| 200 |
wantConfig: config{ |
| 201 |
translationStrategy: otlptranslator.UnderscoreEscapingWithSuffixes, |
| 202 |
registerer: prometheus.DefaultRegisterer, |
| 203 |
namespace: "test", |
| 204 |
}, |
| 205 |
}, |
| 206 |
{ |
| 207 |
name: "with unsanitized namespace", |
| 208 |
options: []Option{ |
| 209 |
WithNamespace("test/"), |
| 210 |
}, |
| 211 |
wantConfig: config{ |
| 212 |
translationStrategy: otlptranslator.UnderscoreEscapingWithSuffixes, |
| 213 |
registerer: prometheus.DefaultRegisterer, |
| 214 |
namespace: "test/", |
| 215 |
}, |
| 216 |
}, |
| 217 |
} |
| 218 |
for _, tt := range testCases { |
| 219 |
t.Run(tt.name, func(t *testing.T) { |
| 220 |
cfg := newConfig(tt.options...) |
| 221 |
// only check the length of readerOpts, since they are not comparable |
| 222 |
assert.Len(t, cfg.readerOpts, len(tt.wantConfig.readerOpts)) |
| 223 |
cfg.readerOpts = nil |
| 224 |
tt.wantConfig.readerOpts = nil |
| 225 |
|
| 226 |
assert.Equal(t, tt.wantConfig, cfg) |
| 227 |
}) |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
type noopProducer struct{} |
| 232 |
|
| 233 |
func (*noopProducer) Produce(context.Context) ([]metricdata.ScopeMetrics, error) { |
| 234 |
return nil, nil |
| 235 |
} |