Skip to content

Commit 9c2c26a

Browse files
authored
Add tests for period character (#88)
* Add test for period character * Remove extra line in README I missed * Run formatting
1 parent 830cb7b commit 9c2c26a

File tree

3 files changed

+53
-12
lines changed

3 files changed

+53
-12
lines changed

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,6 @@ Geras additionally listens on a HTTP port for Prometheus `/metrics` queries and
7070
not block anything. The expected use of this is to block problematic
7171
queries as a fast mitigation therefore an error is returned when a
7272
metric is blocked.
73-
-metrics-name-response-rewriting
74-
Rewrite '.' to ':' and '-' to '_' in all responses (Prometheus
75-
remote_read won't accept these, while Thanos will) (default true)
7673
-metrics-name-response-rewriting
7774
Rewrite '.' to a defined character and other bad characters to '_' in all responses (Prometheus
7875
remote_read won't accept these, while Thanos will) (default true)

pkg/store/store.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,9 +379,12 @@ func (store *OpenTSDBStore) getMatchingMetricNames(matcher storepb.LabelMatcher)
379379
if matcher.Name != "__name__" {
380380
return nil, errors.New("getMatchingMetricNames must be called on __name__ matcher")
381381
}
382-
if matcher.Type == storepb.LabelMatcher_EQ && store.periodCharacter != "" {
383-
value := strings.Replace(matcher.Value, store.periodCharacter, ".", -1)
384-
return []string{value}, nil
382+
if matcher.Type == storepb.LabelMatcher_EQ {
383+
if store.periodCharacter != "" {
384+
value := strings.Replace(matcher.Value, store.periodCharacter, ".", -1)
385+
return []string{value}, nil
386+
}
387+
return []string{matcher.Value}, nil
385388
} else if matcher.Type == storepb.LabelMatcher_NEQ {
386389
// we can support this, but we should not.
387390
return nil, errors.New("NEQ (!=) is not supported for __name__")

pkg/store/store_test.go

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,32 +1264,36 @@ func TestConvertOpenTSDBResultsToSeriesResponse(t *testing.T) {
12641264

12651265
func TestGetMatchingMetricNames(t *testing.T) {
12661266
testCases := []struct {
1267-
input storepb.LabelMatcher
1268-
expectedOutput []string
1267+
input storepb.LabelMatcher
1268+
expectedOutput []string
1269+
periodCharacter string
12691270
}{
12701271
{
12711272
input: storepb.LabelMatcher{
12721273
Name: "tagk",
12731274
Type: storepb.LabelMatcher_EQ,
12741275
Value: "tagv",
12751276
},
1276-
expectedOutput: nil,
1277+
expectedOutput: nil,
1278+
periodCharacter: ":",
12771279
},
12781280
{
12791281
input: storepb.LabelMatcher{
12801282
Name: "__name__",
12811283
Type: storepb.LabelMatcher_NEQ,
12821284
Value: "value",
12831285
},
1284-
expectedOutput: nil,
1286+
expectedOutput: nil,
1287+
periodCharacter: ":",
12851288
},
12861289
{
12871290
input: storepb.LabelMatcher{
12881291
Name: "__name__",
12891292
Type: storepb.LabelMatcher_NRE,
12901293
Value: "value",
12911294
},
1292-
expectedOutput: nil,
1295+
expectedOutput: nil,
1296+
periodCharacter: ":",
12931297
},
12941298
{
12951299
input: storepb.LabelMatcher{
@@ -1300,6 +1304,7 @@ func TestGetMatchingMetricNames(t *testing.T) {
13001304
expectedOutput: []string{
13011305
"metric.name",
13021306
},
1307+
periodCharacter: ":",
13031308
},
13041309
{
13051310
input: storepb.LabelMatcher{
@@ -1314,6 +1319,7 @@ func TestGetMatchingMetricNames(t *testing.T) {
13141319
"tsd.rpc.received",
13151320
"tsd.rpc.unauthorized",
13161321
},
1322+
periodCharacter: ":",
13171323
},
13181324
{
13191325
input: storepb.LabelMatcher{
@@ -1326,6 +1332,41 @@ func TestGetMatchingMetricNames(t *testing.T) {
13261332
"cpu.irq",
13271333
"cpu.nice",
13281334
},
1335+
periodCharacter: ":",
1336+
},
1337+
{
1338+
input: storepb.LabelMatcher{
1339+
Name: "__name__",
1340+
Type: storepb.LabelMatcher_EQ,
1341+
Value: "cpu:idle",
1342+
},
1343+
expectedOutput: []string{
1344+
"cpu.idle",
1345+
},
1346+
periodCharacter: ":",
1347+
},
1348+
{
1349+
input: storepb.LabelMatcher{
1350+
Name: "__name__",
1351+
Type: storepb.LabelMatcher_EQ,
1352+
Value: "cpu__idle_time",
1353+
},
1354+
expectedOutput: []string{
1355+
"cpu.idle_time",
1356+
},
1357+
// '__' means '_' can still be passed through as an underscore
1358+
periodCharacter: "__",
1359+
},
1360+
{
1361+
input: storepb.LabelMatcher{
1362+
Name: "__name__",
1363+
Type: storepb.LabelMatcher_EQ,
1364+
Value: "cpu.idle",
1365+
},
1366+
expectedOutput: []string{
1367+
"cpu.idle",
1368+
},
1369+
periodCharacter: "",
13291370
},
13301371
}
13311372

@@ -1343,7 +1384,7 @@ func TestGetMatchingMetricNames(t *testing.T) {
13431384
"tsd.rpc.received",
13441385
"tsd.rpc.unauthorized",
13451386
},
1346-
periodCharacter: ":",
1387+
periodCharacter: test.periodCharacter,
13471388
}
13481389
output, err := store.getMatchingMetricNames(test.input)
13491390

0 commit comments

Comments
 (0)