@@ -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}
0 commit comments