Skip to content
This repository was archived by the owner on Aug 23, 2023. It is now read-only.

Commit b7ab32f

Browse files
authored
Merge pull request #1371 from grafana/remove_tilde_when_indexing_names_as_tag_values
remove tilde from name values when indexing tags
2 parents 4fb720e + 23d3f14 commit b7ab32f

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

idx/memory/memory.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ func (m *UnpartitionedMemoryIdx) indexTags(def *schema.MetricDefinition) {
543543
tagValue := tagSplits[1]
544544
tags.addTagId(tagName, tagValue, def.Id)
545545
}
546-
tags.addTagId("name", def.Name, def.Id)
546+
tags.addTagId("name", def.NameSanitizedAsTagValue(), def.Id)
547547

548548
m.defByTagSet.add(def)
549549
}
@@ -569,7 +569,7 @@ func (m *UnpartitionedMemoryIdx) deindexTags(tags TagIndex, def *schema.MetricDe
569569
tags.delTagId(tagName, tagValue, def.Id)
570570
}
571571

572-
tags.delTagId("name", def.Name, def.Id)
572+
tags.delTagId("name", def.NameSanitizedAsTagValue(), def.Id)
573573

574574
m.defByTagSet.del(def)
575575

@@ -968,7 +968,7 @@ func (m *UnpartitionedMemoryIdx) FindTagValuesWithQuery(orgId uint32, tag, prefi
968968

969969
// special case if the tag to complete values for is "name"
970970
if tag == "name" {
971-
valueMap[def.Name] = struct{}{}
971+
valueMap[def.NameSanitizedAsTagValue()] = struct{}{}
972972
} else {
973973
for _, tag := range def.Tags {
974974
if !strings.HasPrefix(tag, tagPrefix) {

idx/memory/memory_test.go

+60
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,66 @@ func testSingleNodeMetric(t *testing.T) {
10141014
ix.AddOrUpdate(mkey, data, getPartition(data))
10151015
}
10161016

1017+
func TestMetricNameStartingWithTilde(t *testing.T) {
1018+
withAndWithoutPartitonedIndex(testMetricNameStartingWithTilde)(t)
1019+
}
1020+
1021+
func testMetricNameStartingWithTilde(t *testing.T) {
1022+
ix := New()
1023+
ix.Init()
1024+
defer func() {
1025+
ix.Stop()
1026+
ix = nil
1027+
}()
1028+
1029+
metricName := "~~~weird~.~metric~"
1030+
expectedNameTag := "weird~.~metric~"
1031+
1032+
data := &schema.MetricData{
1033+
Name: metricName,
1034+
Interval: 1,
1035+
OrgId: 1,
1036+
}
1037+
data.SetId()
1038+
mkey, err := schema.MKeyFromString(data.Id)
1039+
if err != nil {
1040+
t.Fatal(err)
1041+
}
1042+
1043+
arch, _, _ := ix.AddOrUpdate(mkey, data, getPartition(data))
1044+
if arch.Name != metricName {
1045+
t.Fatalf("Expected metric name to be %q, but it was %q", metricName, arch.Name)
1046+
}
1047+
1048+
// query by the name minus the leading ~ characters
1049+
query, err := tagquery.NewQueryFromStrings([]string{"name=" + expectedNameTag}, 0)
1050+
if err != nil {
1051+
t.Fatalf("Unexpected error when parsing query expression: %q", err)
1052+
}
1053+
1054+
findResult := ix.FindByTag(1, query)
1055+
if len(findResult) != 1 {
1056+
t.Fatalf("Expected 1 result, but got %d", len(findResult))
1057+
}
1058+
1059+
if findResult[0].Path != metricName {
1060+
t.Fatalf("Expected metric name to be %q, but it was %q", metricName, findResult[0].Path)
1061+
}
1062+
1063+
tagDetails := ix.TagDetails(1, "name", nil, 0)
1064+
if len(tagDetails) != 1 {
1065+
t.Fatalf("Expected 1 result, but got %d", len(tagDetails))
1066+
}
1067+
1068+
count, ok := tagDetails[expectedNameTag]
1069+
if !ok {
1070+
t.Fatalf("Did not get expected name as tag value in result")
1071+
}
1072+
if count != 1 {
1073+
t.Fatalf("Expected 1 result, but got %d", count)
1074+
}
1075+
}
1076+
10171077
// TestMemoryIndexHeapUsageWithTagsUniqueNone5 gives a rough estimate of how much memory the index is using with tag support turned on.
10181078
// It uses 0 unique tags and 10 identical tags
10191079
func TestMemoryIndexHeapUsageWithTagsUniqueNone5(t *testing.T) {

idx/memory/tag_query.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ func (q *TagQueryContext) testByMatch(def *idx.Archive, exprs []kvRe, not bool)
326326
EXPRS:
327327
for _, e := range exprs {
328328
if e.Key == "name" {
329-
if e.Regex == nil || e.Regex.MatchString(def.Name) {
329+
if e.Regex == nil || e.Regex.MatchString(def.NameSanitizedAsTagValue()) {
330330
if not {
331331
return false
332332
}
@@ -447,7 +447,7 @@ func (q *TagQueryContext) testByFrom(def *idx.Archive) bool {
447447
func (q *TagQueryContext) testByPrefix(def *idx.Archive, exprs []kv) bool {
448448
EXPRS:
449449
for _, e := range exprs {
450-
if e.Key == "name" && strings.HasPrefix(def.Name, e.Value) {
450+
if e.Key == "name" && strings.HasPrefix(def.NameSanitizedAsTagValue(), e.Value) {
451451
continue EXPRS
452452
}
453453

0 commit comments

Comments
 (0)