Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split by all parent keys if fields has only one empty string #300

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading