Skip to content

Commit

Permalink
feat: resource hierarchy: add helper for finding synonyms of an attri…
Browse files Browse the repository at this point in the history
…bute in a hierarchy (#404)

This helper will be used for identifying synonyms of `service.name` and
`deployment.environment` to help rank those attributes higher in logs
filter suggestions
  • Loading branch information
raj-k-singh committed Sep 18, 2024
1 parent 4d5843d commit 4920ea0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
21 changes: 21 additions & 0 deletions exporter/clickhouselogsexporter/logsv2/fingerprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package logsv2

import (
"fmt"
"slices"
"strings"
)

Expand Down Expand Up @@ -45,6 +46,26 @@ func (node *DimensionHierarchyNode) Identifier(attributes map[string]any) []IdLa
return result
}

// Get list of synonymous labels in a hierarchy for `attribute` or `nil`
func (node *DimensionHierarchyNode) Synonyms(attribute string) []string {
if node == nil {
return nil
}

if slices.Contains(node.labels, attribute) {
return node.labels[:]
}

for _, h := range node.subHierachies {
synonyms := h.Synonyms(attribute)
if len(synonyms) > 0 {
return synonyms
}
}

return nil
}

// TODO(Raj/Nitya): Consider parsing this stuff out from json
func ResourceHierarchy() *DimensionHierarchyNode {
return &DimensionHierarchyNode{
Expand Down
41 changes: 41 additions & 0 deletions exporter/clickhouselogsexporter/logsv2/fingerprint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestCalculateFingerprint(t *testing.T) {
Expand Down Expand Up @@ -39,3 +40,43 @@ func TestCalculateFingerprint(t *testing.T) {
assert.Equal(t, ts.FingerPrint, res)
}
}

func TestFindAttributeSynonyms(t *testing.T) {
testHierarchy := &DimensionHierarchyNode{
labels: []string{"1.a", "1.b"},

subHierachies: []DimensionHierarchyNode{{
labels: []string{"1.1.a", "1.1.b"},

subHierachies: []DimensionHierarchyNode{{
labels: []string{"1.1.1.a", "1.1.1.b"},
}},
}, {
labels: []string{"1.2.a", "1.2.b"},
}},
}

for _, tc := range []struct {
Attribute string
ExpectedSynonyms []string
}{
{
Attribute: "non-existent-attribute",
ExpectedSynonyms: nil,
}, {
Attribute: "1.b",
ExpectedSynonyms: []string{"1.a", "1.b"},
}, {
Attribute: "1.2.a",
ExpectedSynonyms: []string{"1.2.a", "1.2.b"},
}, {
Attribute: "1.1.1.b",
ExpectedSynonyms: []string{"1.1.1.a", "1.1.1.b"},
},
} {

synonyms := testHierarchy.Synonyms(tc.Attribute)
require.Equal(t, tc.ExpectedSynonyms, synonyms)
}

}

0 comments on commit 4920ea0

Please sign in to comment.