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

Commit 1c451f3

Browse files
authoredMar 15, 2018
Merge pull request #872 from grafana/issue_845
use nameWithTags to match schema by pattern
2 parents 4ce2457 + abb45f5 commit 1c451f3

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed
 

‎idx/memory/memory.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,8 @@ func (m *MemoryIdx) Load(defs []schema.MetricDefinition) int {
334334
func (m *MemoryIdx) add(def *schema.MetricDefinition) idx.Archive {
335335
path := def.NameWithTags()
336336

337-
schemaId, _ := mdata.MatchSchema(def.Name, def.Interval)
338-
aggId, _ := mdata.MatchAgg(def.Name)
337+
schemaId, _ := mdata.MatchSchema(path, def.Interval)
338+
aggId, _ := mdata.MatchAgg(path)
339339
sort.Strings(def.Tags)
340340
archive := &idx.Archive{
341341
MetricDefinition: *def,

‎idx/memory/memory_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ package memory
33
import (
44
"crypto/rand"
55
"fmt"
6+
"regexp"
67
"strconv"
78
"strings"
89
"testing"
910
"time"
1011

12+
"github.com/grafana/metrictank/conf"
1113
"github.com/grafana/metrictank/idx"
14+
"github.com/grafana/metrictank/mdata"
1215
. "github.com/smartystreets/goconvey/convey"
1316
"gopkg.in/raintank/schema.v1"
1417
)
@@ -732,3 +735,45 @@ func BenchmarkDeletes(b *testing.B) {
732735

733736
ix.Delete(1, "some.*")
734737
}
738+
739+
func TestMatchSchemaWithTags(t *testing.T) {
740+
_tagSupport := TagSupport
741+
_schemas := mdata.Schemas
742+
defer func() { TagSupport = _tagSupport }()
743+
defer func() { mdata.Schemas = _schemas }()
744+
745+
TagSupport = true
746+
mdata.Schemas = conf.NewSchemas([]conf.Schema{
747+
{
748+
Name: "tag1_is_value3_or_value5",
749+
Pattern: regexp.MustCompile(".*;tag1=value[35](;.*|$)"),
750+
Retentions: conf.Retentions([]conf.Retention{conf.NewRetentionMT(1, 3600*24*1, 600, 2, true)}),
751+
},
752+
})
753+
754+
ix := New()
755+
ix.Init()
756+
757+
data := make([]*schema.MetricDefinition, 10)
758+
archives := make([]idx.Archive, 10)
759+
for i := 0; i < 10; i++ {
760+
name := fmt.Sprintf("some.id.of.a.metric.%d", i)
761+
data[i] = &schema.MetricDefinition{
762+
Name: name,
763+
Metric: name,
764+
OrgId: 1,
765+
Interval: 1,
766+
Tags: []string{fmt.Sprintf("tag1=value%d", i), "tag2=othervalue"},
767+
}
768+
data[i].SetId()
769+
archives[i] = ix.add(data[i])
770+
}
771+
772+
// only those MDs with tag1=value3 or tag1=value5 should get the first schema id
773+
expectedSchemas := []uint16{1, 1, 1, 0, 1, 0, 1, 1, 1, 1}
774+
for i := 0; i < 10; i++ {
775+
if archives[i].SchemaId != expectedSchemas[i] {
776+
t.Fatalf("Expected schema of archive %d to be %d, but it was %d", i, expectedSchemas[i], archives[i].SchemaId)
777+
}
778+
}
779+
}

0 commit comments

Comments
 (0)
This repository has been archived.