Skip to content

Commit

Permalink
chore: some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
raj-k-singh committed Sep 25, 2024
1 parent c9e86ca commit 157e051
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 32 deletions.
25 changes: 13 additions & 12 deletions pkg/query-service/app/clickhouseReader/filter_suggestions.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,12 @@ func (r *ClickHouseReader) getValuesForLogAttributes(
var attribRanker = newRankingStrategy()

func newRankingStrategy() attribRankingStrategy {
// Some attributes should be ranked independent of rest of the synonyms
resourceAttrsInDecreasingRank := []string{
// Some special resource attributes should get ranked above all others.
interestingResourceAttrsInDescRank := []string{
"service", "service.name", "env", "k8s.namespace.name",
}
// add synonyms for interesting attributes for rest of the ranking

// Synonyms of interesting attributes should come next
resourceHierarchy := logsv2.ResourceHierarchy()
for _, attr := range []string{
"service.name",
Expand All @@ -268,28 +269,28 @@ func newRankingStrategy() attribRankingStrategy {
"k8s.container.name",
"k8s.node.name",
} {
resourceAttrsInDecreasingRank = append(resourceAttrsInDecreasingRank, resourceHierarchy.Synonyms(attr)...)
interestingResourceAttrsInDescRank = append(
interestingResourceAttrsInDescRank, resourceHierarchy.Synonyms(attr)...,
)
}

resourceAttrsInIncreasingRank := resourceAttrsInDecreasingRank[:]
slices.Reverse(resourceAttrsInIncreasingRank)
interestingResourceAttrsInAscRank := interestingResourceAttrsInDescRank[:]
slices.Reverse(interestingResourceAttrsInAscRank)

return attribRankingStrategy{
resourceAttrsInIncreasingRank: resourceAttrsInIncreasingRank,
interestingResourceAttrsInAscRank: interestingResourceAttrsInAscRank,
}
}

type attribRankingStrategy struct {
// in decreasing order of ranking
resourceAttrsInIncreasingRank []string
interestingResourceAttrsInAscRank []string
}

// The higher the score, the higher the rank
func (s *attribRankingStrategy) score(attrib v3.AttributeKey) int {

// Scoring criteria is expected to get more sophisticated in follow up changes
if attrib.Type == v3.AttributeKeyTypeResource {
return 3 + slices.Index(s.resourceAttrsInIncreasingRank, attrib.Key)
// 3 + (-1) if attrib.Key is not an interesting resource attribute
return 3 + slices.Index(s.interestingResourceAttrsInAscRank, attrib.Key)
}

if attrib.Type == v3.AttributeKeyTypeTag {
Expand Down
53 changes: 33 additions & 20 deletions pkg/query-service/tests/integration/filter_suggestions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,44 +141,57 @@ func TestLogsFilterSuggestionsWithExistingFilter(t *testing.T) {
func TestResourceAttribsRankedHigherInLogsFilterSuggestions(t *testing.T) {
require := require.New(t)

testAttribKeys := []v3.AttributeKey{}

tags := []string{"user_id", "user_email"}
for _, k := range tags {
testAttribKeys = append(testAttribKeys, v3.AttributeKey{
tagKeys := []v3.AttributeKey{}
for _, k := range []string{"user_id", "user_email"} {
tagKeys = append(tagKeys, v3.AttributeKey{
Key: k,
Type: v3.AttributeKeyTypeTag,
DataType: v3.AttributeKeyDataTypeArrayString,
DataType: v3.AttributeKeyDataTypeString,
IsColumn: false,
})
}

topResourceAttribs := []string{"service", "env"}
otherResourceAttribs := []string{"container_name", "container_id"}
specialResourceAttrKeys := []v3.AttributeKey{}
for _, k := range []string{"service", "env"} {
specialResourceAttrKeys = append(specialResourceAttrKeys, v3.AttributeKey{
Key: k,
Type: v3.AttributeKeyTypeResource,
DataType: v3.AttributeKeyDataTypeString,
IsColumn: false,
})
}

for _, k := range append(otherResourceAttribs, topResourceAttribs...) {
testAttribKeys = append(testAttribKeys, v3.AttributeKey{
otherResourceAttrKeys := []v3.AttributeKey{}
for _, k := range []string{"container_name", "container_id"} {
otherResourceAttrKeys = append(otherResourceAttrKeys, v3.AttributeKey{
Key: k,
Type: v3.AttributeKeyTypeResource,
DataType: v3.AttributeKeyDataTypeArrayString,
DataType: v3.AttributeKeyDataTypeString,
IsColumn: false,
})
}

tb := NewFilterSuggestionsTestBed(t)
tb.mockAttribKeysQueryResponse(testAttribKeys)

mockAttrKeysInDB := append(tagKeys, otherResourceAttrKeys...)
mockAttrKeysInDB = append(mockAttrKeysInDB, specialResourceAttrKeys...)

tb.mockAttribKeysQueryResponse(mockAttrKeysInDB)

expectedTopSuggestions := append(specialResourceAttrKeys, otherResourceAttrKeys...)
expectedTopSuggestions = append(expectedTopSuggestions, tagKeys...)

tb.mockAttribValuesQueryResponse(
expectedTopSuggestions[:2], [][]string{{"test"}, {"test"}},
)

suggestionsQueryParams := map[string]string{}
suggestionsResp := tb.GetQBFilterSuggestionsForLogs(suggestionsQueryParams)
suggestedKeys := []string{}
for _, k := range suggestionsResp.AttributeKeys {
suggestedKeys = append(suggestedKeys, k.Key)
}

expectedSuggestionKeys := append(topResourceAttribs, otherResourceAttribs...)
expectedSuggestionKeys = append(expectedSuggestionKeys, tags...)

require.Equal(expectedSuggestionKeys, suggestedKeys[:len(expectedSuggestionKeys)])
require.Equal(
expectedTopSuggestions,
suggestionsResp.AttributeKeys[:len(expectedTopSuggestions)],
)
}

// Mocks response for CH queries made by reader.GetLogAttributeKeys
Expand Down

0 comments on commit 157e051

Please sign in to comment.