Skip to content

Commit 1deab3c

Browse files
committed
add test with tag costs from config
1 parent b15c144 commit 1deab3c

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

finder/tagged_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,18 +605,30 @@ func TestParseSeriesByTagWithCostsFromCountTable(t *testing.T) {
605605
// 2022-11-11 00:01:00 +05:00 && 2022-11-11 00:01:10 +05:00
606606
from, until = 1668106860, 1668106870
607607

608+
taggedCosts := map[string]*config.Costs{
609+
"environment": {Cost: newInt(100)},
610+
"dc": {Cost: newInt(60)},
611+
"project": {Cost: newInt(50)},
612+
"__name__": {Cost: newInt(0), ValuesCost: map[string]int{"high_cost": 70}},
613+
"key": {ValuesCost: map[string]int{"value2": 70, "value3": -1, "val*4": -1, "^val.*4$": -1}},
614+
}
615+
608616
ok := func(
609617
testName, query, sql string,
610618
response *chtest.TestResponse,
611619
expected []TaggedTerm,
612620
metricMightExist bool,
613621
expectedErr error,
622+
useTagCostsFromConfig bool,
614623
) {
615624
srv := chtest.NewTestServer()
616625
defer srv.Close()
617626

618627
cfg, _ := config.DefaultConfig()
619628
cfg.ClickHouse.URL = srv.URL
629+
if useTagCostsFromConfig {
630+
cfg.ClickHouse.TaggedCosts = taggedCosts
631+
}
620632

621633
srv.AddResponce(sql, response)
622634

@@ -680,6 +692,7 @@ func TestParseSeriesByTagWithCostsFromCountTable(t *testing.T) {
680692
},
681693
true,
682694
nil,
695+
false,
683696
)
684697

685698
ok(
@@ -699,6 +712,7 @@ func TestParseSeriesByTagWithCostsFromCountTable(t *testing.T) {
699712
},
700713
false,
701714
nil,
715+
false,
702716
)
703717

704718
ok(
@@ -718,6 +732,7 @@ func TestParseSeriesByTagWithCostsFromCountTable(t *testing.T) {
718732
},
719733
false,
720734
nil,
735+
false,
721736
)
722737

723738
ok(
@@ -737,6 +752,7 @@ func TestParseSeriesByTagWithCostsFromCountTable(t *testing.T) {
737752
},
738753
true,
739754
nil,
755+
false,
740756
)
741757

742758
ok(
@@ -757,6 +773,7 @@ func TestParseSeriesByTagWithCostsFromCountTable(t *testing.T) {
757773
},
758774
true,
759775
nil,
776+
false,
760777
)
761778

762779
ok(
@@ -777,6 +794,7 @@ func TestParseSeriesByTagWithCostsFromCountTable(t *testing.T) {
777794
},
778795
true,
779796
nil,
797+
false,
780798
)
781799

782800
ok(
@@ -797,6 +815,7 @@ func TestParseSeriesByTagWithCostsFromCountTable(t *testing.T) {
797815
},
798816
true,
799817
nil,
818+
false,
800819
)
801820

802821
ok(
@@ -811,6 +830,7 @@ func TestParseSeriesByTagWithCostsFromCountTable(t *testing.T) {
811830
},
812831
true,
813832
nil,
833+
false,
814834
)
815835

816836
ok(
@@ -830,6 +850,7 @@ func TestParseSeriesByTagWithCostsFromCountTable(t *testing.T) {
830850
},
831851
true,
832852
nil,
853+
false,
833854
)
834855

835856
ok(
@@ -849,6 +870,7 @@ func TestParseSeriesByTagWithCostsFromCountTable(t *testing.T) {
849870
},
850871
true,
851872
nil,
873+
false,
852874
)
853875

854876
ok(
@@ -868,6 +890,7 @@ func TestParseSeriesByTagWithCostsFromCountTable(t *testing.T) {
868890
},
869891
false,
870892
nil,
893+
false,
871894
)
872895

873896
ok(
@@ -883,6 +906,27 @@ func TestParseSeriesByTagWithCostsFromCountTable(t *testing.T) {
883906
nil,
884907
true,
885908
fmt.Errorf("failed to parse result from clickhouse while querying for tag costs: no tag count"),
909+
false,
910+
)
911+
912+
ok(
913+
`3 TaggedTermEq, 1 of them has __name__ key, 1 does not exist in count table, fallback to config costs`,
914+
`seriesByTag('name=high_cost', 'environment=production', 'dc=west')`,
915+
`SELECT Tag1, sum(Count) as cnt FROM tag1_count_table WHERE `+
916+
`(((Tag1='__name__=high_cost') OR (Tag1='environment=production')) OR (Tag1='dc=west')) `+
917+
`AND (Date >= '`+date.FromTimestampToDaysFormat(from)+`' AND Date <= '`+date.FromTimestampToDaysFormat(until)+`') `+
918+
`GROUP BY Tag1 FORMAT TabSeparatedRaw`,
919+
&chtest.TestResponse{
920+
Body: []byte("environment=production\t100\n__name__=load.avg\t10000\n"),
921+
},
922+
[]TaggedTerm{
923+
{Op: TaggedTermEq, Key: "dc", Value: "west", Cost: 60, NonDefaultCost: true},
924+
{Op: TaggedTermEq, Key: "__name__", Value: "high_cost", Cost: 70, NonDefaultCost: true},
925+
{Op: TaggedTermEq, Key: "environment", Value: "production", Cost: 100, NonDefaultCost: true},
926+
},
927+
false,
928+
nil,
929+
true,
886930
)
887931
}
888932

0 commit comments

Comments
 (0)