Skip to content

Commit 9c2e181

Browse files
andrewkrohmergify-bot
authored andcommitted
[libbeat] Fix add_labels flattening of arrays values (#29211)
* Fix add_labels flattening of arrays values The processor was not working as described in the docs. For example: ```yaml processors: - add_labels: labels: number: 1 with.dots: test nested: with.dots: nested array: - do - re - with.field: mi ``` ```json { "labels": { "array": [ "do", "re", { "with": { "field": "mi" } } ], "nested.with.dots": "nested", "number": 1, "with.dots": "test" } ``` ```json { "labels": { "number": 1, "with.dots": "test", "nested.with.dots": "nested", "array.0": "do", "array.1": "re", "array.2.with.field": "mi" } } ``` * Expose ucfg FlattenedKeys() in common.Config (cherry picked from commit 1dd9714)
1 parent bc97755 commit 9c2e181

4 files changed

Lines changed: 52 additions & 6 deletions

File tree

CHANGELOG.next.asciidoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
133133
- Fix the wrong beat name on monitoring and state endpoint {issue}27755[27755]
134134
- Skip configuration checks in autodiscover for configurations that are already running {pull}29048[29048]
135135
- Fix `decode_json_processor` to always respect `add_error_key` {pull}29107[29107]
136+
- Fix `add_labels` flattening of array values. {pull}29211[29211]
136137

137138
*Auditbeat*
138139

@@ -175,7 +176,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
175176
- Revert usageDetails api version to 2019-01-01. {pull}28995[28995]
176177
- Fix in `aws-s3` input regarding provider discovery through endpoint {pull}28963[28963]
177178
- Fix `threatintel.misp` filters configuration. {issue}27970[27970]
178-
- Fix opening files on Windows in filestream so open files can be deleted. {issue}29113[29113] {pull}29180[29180]
179+
- Fix opening files on Windows in filestream so open files can be deleted. {issue}29113[29113] {pull}29180[29180]
179180

180181
*Heartbeat*
181182

libbeat/common/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,11 @@ func (c *Config) IsArray() bool {
259259
return c.access().IsArray()
260260
}
261261

262+
// FlattenedKeys return a sorted flattened views of the set keys in the configuration.
263+
func (c *Config) FlattenedKeys() []string {
264+
return c.access().FlattenedKeys(configOpts...)
265+
}
266+
262267
func (c *Config) PrintDebugf(msg string, params ...interface{}) {
263268
selector := selectorConfigWithPassword
264269
filtered := false

libbeat/processors/actions/add_labels.go

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,15 @@ func createAddLabels(c *common.Config) (processors.Processor, error) {
4141
}{}
4242
err := c.Unpack(&config)
4343
if err != nil {
44-
return nil, fmt.Errorf("fail to unpack the add_fields configuration: %s", err)
44+
return nil, fmt.Errorf("fail to unpack the add_fields configuration: %w", err)
4545
}
4646

47-
return makeFieldsProcessor(LabelsKey, config.Labels.Flatten(), true), nil
47+
flatLabels, err := flattenLabels(config.Labels)
48+
if err != nil {
49+
return nil, fmt.Errorf("failed to flatten labels: %w", err)
50+
}
51+
52+
return makeFieldsProcessor(LabelsKey, flatLabels, true), nil
4853
}
4954

5055
// NewAddLabels creates a new processor adding the given object to events. Set
@@ -53,8 +58,32 @@ func createAddLabels(c *common.Config) (processors.Processor, error) {
5358
// If labels contains nested objects, NewAddLabels will flatten keys into labels by
5459
// by joining names with a dot ('.') .
5560
// The labels will be inserted into the 'labels' field.
56-
func NewAddLabels(labels common.MapStr, shared bool) processors.Processor {
61+
func NewAddLabels(labels common.MapStr, shared bool) (processors.Processor, error) {
62+
flatLabels, err := flattenLabels(labels)
63+
if err != nil {
64+
return nil, fmt.Errorf("failed to flatten labels: %w", err)
65+
}
66+
5767
return NewAddFields(common.MapStr{
58-
LabelsKey: labels.Flatten(),
59-
}, shared, true)
68+
LabelsKey: flatLabels,
69+
}, shared, true), nil
70+
}
71+
72+
func flattenLabels(labels common.MapStr) (common.MapStr, error) {
73+
labelConfig, err := common.NewConfigFrom(labels)
74+
if err != nil {
75+
return nil, err
76+
}
77+
78+
flatKeys := labelConfig.FlattenedKeys()
79+
flatMap := make(common.MapStr, len(flatKeys))
80+
for _, k := range flatKeys {
81+
v, err := labelConfig.String(k, -1)
82+
if err != nil {
83+
return nil, err
84+
}
85+
flatMap[k] = v
86+
}
87+
88+
return flatMap, nil
6089
}

libbeat/processors/actions/add_labels_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,16 @@ func TestAddLabels(t *testing.T) {
5959
`{add_labels.labels: {l2: b, lc: b}}`,
6060
),
6161
},
62+
"add array": {
63+
event: common.MapStr{},
64+
want: common.MapStr{
65+
"labels": common.MapStr{
66+
"array.0": "foo",
67+
"array.1": "bar",
68+
"array.2.hello": "world",
69+
},
70+
},
71+
cfg: single(`{add_labels: {labels: {array: ["foo", "bar", {"hello": "world"}]}}}`),
72+
},
6273
})
6374
}

0 commit comments

Comments
 (0)