Skip to content

Commit

Permalink
Split by all parent keys if fields has only one empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilernerd committed Sep 25, 2023
1 parent 3520875 commit 6da2aeb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 8 deletions.
57 changes: 53 additions & 4 deletions transformer/split.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
7 changes: 3 additions & 4 deletions transformer/split_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestSplit(t *testing.T) {
"metrics": [
{
"data": {
"data": [
"data1": [
{
"splitField": "key1",
"data": "yes"
Expand All @@ -49,7 +49,6 @@ func TestSplit(t *testing.T) {
}
]
}
},
{
"data": {
Expand All @@ -58,7 +57,7 @@ func TestSplit(t *testing.T) {
},
{
"data": {
"data": [
"data1": [
{
"splitField": "key3",
"data": "2yes"
Expand All @@ -78,7 +77,7 @@ func TestSplit(t *testing.T) {
return
}

split_path := "data"
split_path := ""
metadata := transformer.Split{
Field: []string{split_path},
MetadataName: "arrayidx",
Expand Down

0 comments on commit 6da2aeb

Please sign in to comment.