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

Commit d8f92ae

Browse files
authored
Merge pull request #1515 from grafana/save_unnecesary_meta_tag_enrichment_in_tag_value_auto_complete
skip meta tag enrichment when we can
2 parents dbe6c96 + c773afb commit d8f92ae

File tree

2 files changed

+48
-15
lines changed

2 files changed

+48
-15
lines changed

idx/memory/memory.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -1197,10 +1197,14 @@ func (m *UnpartitionedMemoryIdx) FindTagValuesWithQuery(orgId uint32, tag, prefi
11971197
return nil
11981198
}
11991199

1200+
// check whether the tag of which we're looking up values is a metric tag
1201+
// if so we can skip the meta tag enrichment
1202+
_, isMetricTag := tags[tag]
1203+
12001204
resMap := make(map[string]struct{})
12011205

12021206
var enricher *enricher
1203-
if MetaTagSupport {
1207+
if MetaTagSupport && !isMetricTag {
12041208
mtr, ok := m.metaTagRecords[orgId]
12051209
if ok {
12061210
enricher = mtr.getEnricher(tags.idHasTag)

idx/memory/memory_find_test.go

+43-14
Original file line numberDiff line numberDiff line change
@@ -1002,8 +1002,8 @@ func testAutoCompleteTagValuesWithQuery(t *testing.T) {
10021002
},
10031003
}
10041004

1005-
for _, tc := range testCases {
1006-
autoCompleteTagValuesWithQueryAndCompare(t, tc.tag, tc.prefix, tc.expr, tc.limit, tc.expRes)
1005+
for i, tc := range testCases {
1006+
autoCompleteTagValuesWithQueryAndCompare(t, i, tc.tag, tc.prefix, tc.expr, tc.limit, tc.expRes)
10071007
}
10081008
}
10091009

@@ -1022,18 +1022,29 @@ func testAutoCompleteTagValuesWithQueryWithMetaTagSupport(t *testing.T) {
10221022
if err != nil {
10231023
t.Fatalf("Unexpected error when parsing expression: %s", err)
10241024
}
1025+
// since "direction" is a tag in the metric tag index, we don't
1026+
// include values of the meta tag index in the autocomplete results
10251027
ix.MetaTagRecordUpsert(1, tagquery.MetaTagRecord{
10261028
MetaTags: tagquery.Tags{tagquery.Tag{Key: "direction", Value: "none"}},
10271029
Expressions: tagquery.Expressions{metaRecordExpression1},
10281030
})
10291031

1030-
metaRecordExpression2, err := tagquery.ParseExpression("name!=")
1032+
metaRecordExpressions2, err := tagquery.ParseExpressions([]string{"direction=~.+"})
1033+
if err != nil {
1034+
t.Fatalf("Unexpected error when parsing expressions: %s", err)
1035+
}
1036+
ix.MetaTagRecordUpsert(1, tagquery.MetaTagRecord{
1037+
MetaTags: tagquery.Tags{{Key: "has_direction", Value: "true"}, {Key: "has_direction", Value: "yes"}},
1038+
Expressions: metaRecordExpressions2,
1039+
})
1040+
1041+
metaRecordExpression3, err := tagquery.ParseExpression("name!=")
10311042
if err != nil {
10321043
t.Fatalf("Unexpected error when parsing expression: %s", err)
10331044
}
10341045
ix.MetaTagRecordUpsert(1, tagquery.MetaTagRecord{
10351046
MetaTags: tagquery.Tags{tagquery.Tag{Key: "all", Value: "metrics"}},
1036-
Expressions: tagquery.Expressions{metaRecordExpression2},
1047+
Expressions: tagquery.Expressions{metaRecordExpression3},
10371048
})
10381049

10391050
type testCase struct {
@@ -1056,7 +1067,7 @@ func testAutoCompleteTagValuesWithQueryWithMetaTagSupport(t *testing.T) {
10561067
prefix: "",
10571068
expr: []string{"host=~.+"},
10581069
limit: 100,
1059-
expRes: []string{"none", "read", "write"},
1070+
expRes: []string{"read", "write"},
10601071
}, {
10611072
tag: "direction",
10621073
prefix: "wr",
@@ -1068,45 +1079,63 @@ func testAutoCompleteTagValuesWithQueryWithMetaTagSupport(t *testing.T) {
10681079
prefix: "no",
10691080
expr: []string{"host=~.+"},
10701081
limit: 100,
1071-
expRes: []string{"none"},
1082+
expRes: []string{},
10721083
}, {
10731084
tag: "direction",
10741085
prefix: "",
10751086
expr: []string{"host=~.+"},
1076-
limit: 2,
1077-
expRes: []string{"none", "read"},
1087+
limit: 1,
1088+
expRes: []string{"read"},
10781089
}, {
10791090
tag: "all",
10801091
prefix: "",
10811092
expr: []string{"__tag=name"},
10821093
limit: 100,
10831094
expRes: []string{"metrics"},
1095+
}, {
1096+
tag: "has_direction",
1097+
prefix: "",
1098+
expr: []string{"name=~.+"},
1099+
limit: 100,
1100+
expRes: []string{"true", "yes"},
1101+
}, {
1102+
tag: "has_direction",
1103+
prefix: "tr",
1104+
expr: []string{"name=~.+"},
1105+
limit: 100,
1106+
expRes: []string{"true"},
1107+
}, {
1108+
tag: "has_direction",
1109+
prefix: "",
1110+
expr: []string{"name=~.+"},
1111+
limit: 1,
1112+
expRes: []string{"true"},
10841113
},
10851114
}
10861115

1087-
for _, tc := range testCases {
1088-
autoCompleteTagValuesWithQueryAndCompare(t, tc.tag, tc.prefix, tc.expr, tc.limit, tc.expRes)
1116+
for i, tc := range testCases {
1117+
autoCompleteTagValuesWithQueryAndCompare(t, i, tc.tag, tc.prefix, tc.expr, tc.limit, tc.expRes)
10891118
}
10901119
}
10911120

1092-
func autoCompleteTagValuesWithQueryAndCompare(t testing.TB, tag, prefix string, expr []string, limit uint, expRes []string) {
1121+
func autoCompleteTagValuesWithQueryAndCompare(t testing.TB, tc int, tag, prefix string, expr []string, limit uint, expRes []string) {
10931122
t.Helper()
10941123

10951124
query, err := tagquery.NewQueryFromStrings(expr, 0)
10961125
if err != nil {
1097-
t.Fatalf("Unexpected error when instantiating query: %s", err)
1126+
t.Fatalf("TC %d: Unexpected error when instantiating query: %s", tc, err)
10981127
}
10991128
res := ix.FindTagValuesWithQuery(1, tag, prefix, query, limit)
11001129

11011130
if len(res) != len(expRes) {
1102-
t.Fatalf("Wrong result, Expected:\n%s\nGot:\n%s\n", expRes, res)
1131+
t.Fatalf("TC %d: Wrong result, Expected:\n%s\nGot:\n%s\n", tc, expRes, res)
11031132
}
11041133

11051134
sort.Strings(expRes)
11061135
sort.Strings(res)
11071136
for i := range res {
11081137
if expRes[i] != res[i] {
1109-
t.Fatalf("Wrong result, Expected:\n%s\nGot:\n%s\n", expRes, res)
1138+
t.Fatalf("TC %d: Wrong result, Expected:\n%s\nGot:\n%s\n", tc, expRes, res)
11101139
}
11111140
}
11121141
}

0 commit comments

Comments
 (0)