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

use nameWithTags to match schema by pattern #872

Merged
merged 1 commit into from
Mar 15, 2018
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
4 changes: 2 additions & 2 deletions idx/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,8 @@ func (m *MemoryIdx) Load(defs []schema.MetricDefinition) int {
func (m *MemoryIdx) add(def *schema.MetricDefinition) idx.Archive {
path := def.NameWithTags()

schemaId, _ := mdata.MatchSchema(def.Name, def.Interval)
aggId, _ := mdata.MatchAgg(def.Name)
schemaId, _ := mdata.MatchSchema(path, def.Interval)
aggId, _ := mdata.MatchAgg(path)
sort.Strings(def.Tags)
archive := &idx.Archive{
MetricDefinition: *def,
Expand Down
45 changes: 45 additions & 0 deletions idx/memory/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package memory
import (
"crypto/rand"
"fmt"
"regexp"
"strconv"
"strings"
"testing"
"time"

"github.com/grafana/metrictank/conf"
"github.com/grafana/metrictank/idx"
"github.com/grafana/metrictank/mdata"
. "github.com/smartystreets/goconvey/convey"
"gopkg.in/raintank/schema.v1"
)
Expand Down Expand Up @@ -732,3 +735,45 @@ func BenchmarkDeletes(b *testing.B) {

ix.Delete(1, "some.*")
}

func TestMatchSchemaWithTags(t *testing.T) {
_tagSupport := TagSupport
_schemas := mdata.Schemas
defer func() { TagSupport = _tagSupport }()
defer func() { mdata.Schemas = _schemas }()

TagSupport = true
mdata.Schemas = conf.NewSchemas([]conf.Schema{
{
Name: "tag1_is_value3_or_value5",
Pattern: regexp.MustCompile(".*;tag1=value[35](;.*|$)"),
Retentions: conf.Retentions([]conf.Retention{conf.NewRetentionMT(1, 3600*24*1, 600, 2, true)}),
},
})

ix := New()
ix.Init()

data := make([]*schema.MetricDefinition, 10)
archives := make([]idx.Archive, 10)
for i := 0; i < 10; i++ {
name := fmt.Sprintf("some.id.of.a.metric.%d", i)
data[i] = &schema.MetricDefinition{
Name: name,
Metric: name,
OrgId: 1,
Interval: 1,
Tags: []string{fmt.Sprintf("tag1=value%d", i), "tag2=othervalue"},
}
data[i].SetId()
archives[i] = ix.add(data[i])
}

// only those MDs with tag1=value3 or tag1=value5 should get the first schema id
expectedSchemas := []uint16{1, 1, 1, 0, 1, 0, 1, 1, 1, 1}
for i := 0; i < 10; i++ {
if archives[i].SchemaId != expectedSchemas[i] {
t.Fatalf("Expected schema of archive %d to be %d, but it was %d", i, expectedSchemas[i], archives[i].SchemaId)
}
}
}