Skip to content

Commit

Permalink
[exporter/loki] Allow nested attribute to be promoted to label (#16615)
Browse files Browse the repository at this point in the history
Fixes #16475

Signed-off-by: Juraci Paixão Kröhling <juraci@kroehling.de>
  • Loading branch information
jpkrohling authored Dec 15, 2022
1 parent 0411fd4 commit 284cb6f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
11 changes: 11 additions & 0 deletions .chloggen/loki-nested-attributes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: exporter/loki

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Allow nested attributes to be used in labels

# One or more tracking issues related to the change
issues: [16475]
23 changes: 22 additions & 1 deletion pkg/translator/loki/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,14 @@ func convertAttributesToLabels(attributes pcommon.Map, attrsToSelect pcommon.Val
attrs := parseAttributeNames(attrsToSelect)
for _, attr := range attrs {
attr = strings.TrimSpace(attr)
av, ok := attributes.Get(attr) // do we need to trim this?

av, ok := attributes.Get(attr)
if !ok {
// couldn't find the attribute under the given name directly
// perhaps it's a nested attribute?
av, ok = getNestedAttribute(attr, attributes) // shadows the OK from above on purpose
}

if ok {
out[model.LabelName(attr)] = model.LabelValue(av.AsString())
}
Expand All @@ -91,6 +98,20 @@ func convertAttributesToLabels(attributes pcommon.Map, attrsToSelect pcommon.Val
return out
}

func getNestedAttribute(attr string, attributes pcommon.Map) (pcommon.Value, bool) {
left, right, _ := strings.Cut(attr, ".")
av, ok := attributes.Get(left)
if !ok {
return pcommon.Value{}, false
}

if len(right) == 0 {
return av, ok
}

return getNestedAttribute(right, av.Map())
}

func parseAttributeNames(attrsToSelect pcommon.Value) []string {
var out []string

Expand Down
33 changes: 33 additions & 0 deletions pkg/translator/loki/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/prometheus/common/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/pdata/pcommon"
)

Expand Down Expand Up @@ -158,6 +159,20 @@ func TestConvertAttributesToLabels(t *testing.T) {
"pod.name": "pod-123",
},
},
{
desc: "nested attributes",
attrsAvailable: map[string]interface{}{
"host": map[string]interface{}{
"name": "guarana",
},
"pod.name": "pod-123",
},
attrsToSelect: attrsToSelectSlice,
expected: model.LabelSet{
"host.name": "guarana",
"pod.name": "pod-123",
},
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
Expand Down Expand Up @@ -213,3 +228,21 @@ func TestRemoveAttributes(t *testing.T) {
})
}
}

func TestGetNestedAttribute(t *testing.T) {
// prepare
attrs := pcommon.NewMap()
err := attrs.FromRaw(map[string]interface{}{
"host": map[string]interface{}{
"name": "guarana",
},
})
require.NoError(t, err)

// test
attr, ok := getNestedAttribute("host.name", attrs)

// verify
assert.Equal(t, "guarana", attr.AsString())
assert.True(t, ok)
}

0 comments on commit 284cb6f

Please sign in to comment.