-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
data_generator.go
78 lines (63 loc) · 1.44 KB
/
data_generator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package testing
import (
"encoding/json"
"flag"
"io/ioutil"
"os"
"testing"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/metricbeat/mb"
)
var (
// To enable the data building, run go test `github.com/elastic/beats/metricbeat/module/system/memory/... -data=true`
dataFlag = flag.Bool("data", false, "Enabled creating of data")
)
func WriteEvent(f mb.EventFetcher, t *testing.T) error {
if !*dataFlag {
t.Skip("Skip data generation tests")
}
event, err := f.Fetch()
if err != nil {
return err
}
return createEvent(event, f)
}
func WriteEvents(f mb.EventsFetcher, t *testing.T) error {
if !*dataFlag {
t.Skip("Skip data generation tests")
}
events, err := f.Fetch()
if err != nil {
return err
}
return createEvent(events[0], f)
}
func createEvent(event common.MapStr, m mb.MetricSet) error {
path, err := os.Getwd()
if err != nil {
return err
}
fullEvent := common.MapStr{
"@timestamp": "2016-05-23T08:05:34.853Z",
"beat": common.MapStr{
"hostname": "host.example.com",
"name": "host.example.com",
},
"metricset": common.MapStr{
"host": "localhost",
"module": m.Module().Name(),
"name": m.Name(),
"rtt": 115,
},
m.Module().Name(): common.MapStr{
m.Name(): event,
},
"type": "metricsets",
}
output, _ := json.MarshalIndent(fullEvent, "", " ")
err = ioutil.WriteFile(path+"/_beat/data.json", output, 0644)
if err != nil {
return err
}
return nil
}