From 6da2aebd3f77a5023d35066e4c68761f87222690 Mon Sep 17 00:00:00 2001 From: Kamil Date: Mon, 25 Sep 2023 16:09:18 +0200 Subject: [PATCH] Split by all parent keys if fields has only one empty string --- transformer/split.go | 57 ++++++++++++++++++++++++++++++++++++--- transformer/split_test.go | 7 +++-- 2 files changed, 56 insertions(+), 8 deletions(-) diff --git a/transformer/split.go b/transformer/split.go index b0c03bec..38ca5747 100644 --- a/transformer/split.go +++ b/transformer/split.go @@ -31,9 +31,41 @@ import ( // Split is the configuration for the split transformer type Split struct { - Field []string `doc:"Split into multiple metrics based on this field (each field denotes the path to a nested object element)."` - MetadataName string `doc:"If specified, the index of the array being split will be stored as the named metadata field. E.g.: The first element will have a metadata field matching MetadataName with a value of 0, the second will have a 1, and so on. If left blank, the array index will be discarded."` - Fail bool `doc:"Fail the transformer entirely if split is unsuccsessful on a metric container. This will prevent successive transformers from working."` + Field []string `doc:"Split into multiple metrics based on this field (each field denotes the path to a nested object element). In case the first element is set to \"\" the default behaviour is to split all the metrics based on their parent key. Keys consisting of empty strings are not splitted" + + Input + { + "metrics": [ + { + "data": { + "data1": [ + { + "splitField": "key1", + "data": "yes" + } + ] + } + } + ] + } + + The output + { + "metrics": [ + { + "data": { + { + "splitField": "key1", + "data": "yes" + } + } + } + ] + } + + ` + MetadataName string `doc:"If specified, the index of the array being split will be stored as the named metadata field. E.g.: The first element will have a metadata field matching MetadataName with a value of 0, the second will have a 1, and so on. If left blank, the array index will be discarded."` + Fail bool `doc:"Fail the transformer entirely if split is unsuccsessful on a metric container. This will prevent successive transformers from working."` } type DictSplit struct { @@ -59,13 +91,30 @@ func (split *Split) Transform(c *skogul.Container) error { return nil } +func (split *Split) getMetricKeys(metric *skogul.Metric) []string { + keys := []string{} + for k, _ := range metric.Data { + keys = append(keys, k) + } + return keys +} + // splitMetricsByObjectKey splits the metrics into multiple metrics based on a key in a list of sub-metrics func (split *Split) splitMetricsByObjectKey(metrics *[]*skogul.Metric) ([]*skogul.Metric, error) { origMetrics := *metrics var newMetrics []*skogul.Metric for mi := range origMetrics { - splitObj, err := skogul.ExtractNestedObject(origMetrics[mi].Data, split.Field) + var splitObj map[string]interface{} + var err error + + if split.Field[0] == "" && len(split.Field) == 1 { + metricKeys := split.getMetricKeys(origMetrics[mi]) + split.Field = metricKeys + splitObj, err = skogul.ExtractNestedObject(origMetrics[mi].Data, split.Field) + } else { + splitObj, err = skogul.ExtractNestedObject(origMetrics[mi].Data, split.Field) + } if err != nil { if !split.Fail { diff --git a/transformer/split_test.go b/transformer/split_test.go index 77d3be44..76e78d3b 100644 --- a/transformer/split_test.go +++ b/transformer/split_test.go @@ -38,7 +38,7 @@ func TestSplit(t *testing.T) { "metrics": [ { "data": { - "data": [ + "data1": [ { "splitField": "key1", "data": "yes" @@ -49,7 +49,6 @@ func TestSplit(t *testing.T) { } ] } - }, { "data": { @@ -58,7 +57,7 @@ func TestSplit(t *testing.T) { }, { "data": { - "data": [ + "data1": [ { "splitField": "key3", "data": "2yes" @@ -78,7 +77,7 @@ func TestSplit(t *testing.T) { return } - split_path := "data" + split_path := "" metadata := transformer.Split{ Field: []string{split_path}, MetadataName: "arrayidx",