@@ -3,12 +3,15 @@ package memory
3
3
import (
4
4
"crypto/rand"
5
5
"fmt"
6
+ "regexp"
6
7
"strconv"
7
8
"strings"
8
9
"testing"
9
10
"time"
10
11
12
+ "github.com/grafana/metrictank/conf"
11
13
"github.com/grafana/metrictank/idx"
14
+ "github.com/grafana/metrictank/mdata"
12
15
. "github.com/smartystreets/goconvey/convey"
13
16
"gopkg.in/raintank/schema.v1"
14
17
)
@@ -732,3 +735,45 @@ func BenchmarkDeletes(b *testing.B) {
732
735
733
736
ix .Delete (1 , "some.*" )
734
737
}
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