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

remove tilde from name values when indexing tags #1371

Merged
merged 2 commits into from
Jul 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions idx/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ func (m *UnpartitionedMemoryIdx) indexTags(def *schema.MetricDefinition) {
tagValue := tagSplits[1]
tags.addTagId(tagName, tagValue, def.Id)
}
tags.addTagId("name", def.Name, def.Id)
tags.addTagId("name", def.NameSanitizedAsTagValue(), def.Id)

m.defByTagSet.add(def)
}
Expand All @@ -569,7 +569,7 @@ func (m *UnpartitionedMemoryIdx) deindexTags(tags TagIndex, def *schema.MetricDe
tags.delTagId(tagName, tagValue, def.Id)
}

tags.delTagId("name", def.Name, def.Id)
tags.delTagId("name", def.NameSanitizedAsTagValue(), def.Id)

m.defByTagSet.del(def)

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

// special case if the tag to complete values for is "name"
if tag == "name" {
valueMap[def.Name] = struct{}{}
valueMap[def.NameSanitizedAsTagValue()] = struct{}{}
} else {
for _, tag := range def.Tags {
if !strings.HasPrefix(tag, tagPrefix) {
Expand Down
60 changes: 60 additions & 0 deletions idx/memory/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,66 @@ func testSingleNodeMetric(t *testing.T) {
ix.AddOrUpdate(mkey, data, getPartition(data))
}

func TestMetricNameStartingWithTilde(t *testing.T) {
withAndWithoutPartitonedIndex(testMetricNameStartingWithTilde)(t)
}

func testMetricNameStartingWithTilde(t *testing.T) {
ix := New()
ix.Init()
defer func() {
ix.Stop()
ix = nil
}()

metricName := "~~~weird~.~metric~"
expectedNameTag := "weird~.~metric~"

data := &schema.MetricData{
Name: metricName,
Interval: 1,
OrgId: 1,
}
data.SetId()
mkey, err := schema.MKeyFromString(data.Id)
if err != nil {
t.Fatal(err)
}

arch, _, _ := ix.AddOrUpdate(mkey, data, getPartition(data))
if arch.Name != metricName {
t.Fatalf("Expected metric name to be %q, but it was %q", metricName, arch.Name)
}

// query by the name minus the leading ~ characters
query, err := tagquery.NewQueryFromStrings([]string{"name=" + expectedNameTag}, 0)
if err != nil {
t.Fatalf("Unexpected error when parsing query expression: %q", err)
}

findResult := ix.FindByTag(1, query)
if len(findResult) != 1 {
t.Fatalf("Expected 1 result, but got %d", len(findResult))
}

if findResult[0].Path != metricName {
t.Fatalf("Expected metric name to be %q, but it was %q", metricName, findResult[0].Path)
}

tagDetails := ix.TagDetails(1, "name", nil, 0)
if len(tagDetails) != 1 {
t.Fatalf("Expected 1 result, but got %d", len(tagDetails))
}

count, ok := tagDetails[expectedNameTag]
if !ok {
t.Fatalf("Did not get expected name as tag value in result")
}
if count != 1 {
t.Fatalf("Expected 1 result, but got %d", count)
}
}

// TestMemoryIndexHeapUsageWithTagsUniqueNone5 gives a rough estimate of how much memory the index is using with tag support turned on.
// It uses 0 unique tags and 10 identical tags
func TestMemoryIndexHeapUsageWithTagsUniqueNone5(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions idx/memory/tag_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ func (q *TagQueryContext) testByMatch(def *idx.Archive, exprs []kvRe, not bool)
EXPRS:
for _, e := range exprs {
if e.Key == "name" {
if e.Regex == nil || e.Regex.MatchString(def.Name) {
if e.Regex == nil || e.Regex.MatchString(def.NameSanitizedAsTagValue()) {
if not {
return false
}
Expand Down Expand Up @@ -447,7 +447,7 @@ func (q *TagQueryContext) testByFrom(def *idx.Archive) bool {
func (q *TagQueryContext) testByPrefix(def *idx.Archive, exprs []kv) bool {
EXPRS:
for _, e := range exprs {
if e.Key == "name" && strings.HasPrefix(def.Name, e.Value) {
if e.Key == "name" && strings.HasPrefix(def.NameSanitizedAsTagValue(), e.Value) {
continue EXPRS
}

Expand Down