Skip to content

Commit

Permalink
sanitize namespace, add config test
Browse files Browse the repository at this point in the history
  • Loading branch information
paivagustavo committed Apr 4, 2023
1 parent 6c648d2 commit 5f89a61
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
1 change: 1 addition & 0 deletions exporters/prometheus/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func WithoutScopeInfo() Option {
// have special behavior based on their name.
func WithNamespace(ns string) Option {
return optionFunc(func(cfg config) config {
ns = sanitizeName(ns)
if !strings.HasSuffix(ns, "_") {
// namespace and metric names should be separated with an underscore,
// adds a trailing underscore if there is not one already.
Expand Down
30 changes: 30 additions & 0 deletions exporters/prometheus/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,36 @@ func TestNewConfig(t *testing.T) {
withoutUnits: true,
},
},
{
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_",
},
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
Expand Down
20 changes: 0 additions & 20 deletions exporters/prometheus/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,26 +278,6 @@ func TestPrometheusExporter(t *testing.T) {
counter.Add(ctx, 9, attrs...)
},
},
{
name: "with namespace with trailing underscore",
expectedFile: "testdata/with_namespace.txt",
options: []Option{
WithNamespace("test_"),
},
recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
attrs := []attribute.KeyValue{
attribute.Key("A").String("B"),
attribute.Key("C").String("D"),
attribute.Key("E").Bool(true),
attribute.Key("F").Int(42),
}
counter, err := meter.Float64Counter("foo", instrument.WithDescription("a simple counter"))
require.NoError(t, err)
counter.Add(ctx, 5, attrs...)
counter.Add(ctx, 10.3, attrs...)
counter.Add(ctx, 9, attrs...)
},
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 5f89a61

Please sign in to comment.