Skip to content

Commit a28f696

Browse files
authored
Use fields if synthetic mode is enabled (#1283)
Use field definitions from `fields` key of the documents when synthetic source mode is enabled. In case synthetic mode is enabled, sample_event files are going to be generated using the same `field` definitions and they will be formatted expanding objects, as they were when field were retrieve from `_source` key.
1 parent 1623fb2 commit a28f696

File tree

23 files changed

+1011
-29
lines changed

23 files changed

+1011
-29
lines changed

internal/common/helpers.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
package common
66

7-
import "strings"
7+
import (
8+
"fmt"
9+
"strings"
10+
)
811

912
// TrimStringSlice removes whitespace from the beginning and end of the contents of a []string.
1013
func TrimStringSlice(slice []string) {
@@ -36,3 +39,21 @@ func StringSlicesUnion(slices ...[]string) (result []string) {
3639
}
3740
return
3841
}
42+
43+
// ToStringSlice returns the list of strings from an interface variable
44+
func ToStringSlice(val interface{}) ([]string, error) {
45+
vals, ok := val.([]interface{})
46+
if !ok {
47+
return nil, fmt.Errorf("conversion error")
48+
}
49+
50+
var s []string
51+
for _, v := range vals {
52+
str, ok := v.(string)
53+
if !ok {
54+
return nil, fmt.Errorf("conversion error")
55+
}
56+
s = append(s, str)
57+
}
58+
return s, nil
59+
}

internal/common/mapstr_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License;
3+
// you may not use this file except in compliance with the Elastic License.
4+
5+
package common
6+
7+
import (
8+
"encoding/json"
9+
"os"
10+
"testing"
11+
12+
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
14+
)
15+
16+
func TestMapStrGetValue(t *testing.T) {
17+
18+
cases := []struct {
19+
title string
20+
testFile string
21+
fieldKey string
22+
expectedValue interface{}
23+
}{
24+
{
25+
title: "string value",
26+
testFile: "./testdata/source.json",
27+
fieldKey: "host.architecture",
28+
expectedValue: "x86_64",
29+
},
30+
{
31+
title: "float64 value",
32+
testFile: "./testdata/source.json",
33+
fieldKey: "metricset.period",
34+
expectedValue: float64(10000),
35+
},
36+
{
37+
title: "slice value",
38+
testFile: "./testdata/source.json",
39+
fieldKey: "tags",
40+
expectedValue: []interface{}{"apache_tomcat-cache", "forwarded"},
41+
},
42+
{
43+
title: "map value",
44+
testFile: "./testdata/source.json",
45+
fieldKey: "data_stream",
46+
expectedValue: map[string]interface{}{"dataset": "apache_tomcat.cache", "namespace": "ep", "type": "metrics"},
47+
},
48+
}
49+
50+
for _, c := range cases {
51+
t.Run(c.title, func(t *testing.T) {
52+
b, err := os.ReadFile("./testdata/source.json")
53+
require.NoError(t, err)
54+
55+
var given MapStr
56+
err = json.Unmarshal(b, &given)
57+
require.NoError(t, err)
58+
59+
val, err := given.GetValue(c.fieldKey)
60+
assert.NoError(t, err)
61+
assert.Equal(t, c.expectedValue, val)
62+
})
63+
}
64+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
{
2+
"@timestamp": "2023-05-30T11:42:07.923Z",
3+
"agent": {
4+
"ephemeral_id": "7dd994f8-d866-4068-9f29-d6c90a28738e",
5+
"id": "5cad1bda-ddc8-4333-8d88-333b6ef311b5",
6+
"name": "docker-fleet-agent",
7+
"type": "metricbeat",
8+
"version": "8.8.0"
9+
},
10+
"apache_tomcat": {
11+
"cache": {
12+
"application_name": "/",
13+
"hit": {
14+
"count": 15.0
15+
},
16+
"lookup": {
17+
"count": 30.0
18+
},
19+
"object": {
20+
"size": {
21+
"max": {
22+
"kb": 512.0
23+
}
24+
}
25+
},
26+
"size": {
27+
"current": {
28+
"kb": 19.0
29+
},
30+
"max": {
31+
"kb": 10240.0
32+
}
33+
},
34+
"ttl": {
35+
"ms": 5000.0
36+
}
37+
}
38+
},
39+
"data_stream": {
40+
"dataset": "apache_tomcat.cache",
41+
"namespace": "ep",
42+
"type": "metrics"
43+
},
44+
"ecs": {
45+
"version": "8.7.0"
46+
},
47+
"elastic_agent": {
48+
"id": "5cad1bda-ddc8-4333-8d88-333b6ef311b5",
49+
"snapshot": true,
50+
"version": "8.8.0"
51+
},
52+
"event": {
53+
"agent_id_status": "verified",
54+
"category": "web",
55+
"dataset": "apache_tomcat.cache",
56+
"duration": 136411023,
57+
"ingested": "2023-05-30T11:42:09Z",
58+
"kind": "metric",
59+
"module": "apache_tomcat",
60+
"type": "info"
61+
},
62+
"host": {
63+
"architecture": "x86_64",
64+
"containerized": false,
65+
"hostname": "docker-fleet-agent",
66+
"id": "4e0d02de87bd4101b7215d3aaf5f623b",
67+
"ip": "172.23.0.7",
68+
"mac": "02-42-AC-17-00-07",
69+
"name": "docker-fleet-agent",
70+
"os": {
71+
"codename": "focal",
72+
"family": "debian",
73+
"kernel": "5.19.0-42-generic",
74+
"name": "Ubuntu",
75+
"platform": "ubuntu",
76+
"type": "linux",
77+
"version": "20.04.6 LTS (Focal Fossa)"
78+
}
79+
},
80+
"metricset": {
81+
"name": "collector",
82+
"period": 10000
83+
},
84+
"service": {
85+
"address": "http://elastic-package-service-apache_tomcat-1:9090/metrics",
86+
"type": "prometheus"
87+
},
88+
"tags": [
89+
"apache_tomcat-cache",
90+
"forwarded"
91+
]
92+
}

0 commit comments

Comments
 (0)