代码拉取完成,页面将自动刷新
package testing
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"testing"
"time"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/mb/module"
)
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
}
if len(events) == 0 {
return fmt.Errorf("no events were generated")
}
return createEvent(events[0], f)
}
func createEvent(event common.MapStr, m mb.MetricSet) error {
path, err := os.Getwd()
if err != nil {
return err
}
startTime, _ := time.Parse(time.RFC3339Nano, "2016-05-23T08:05:34.853Z")
build := module.EventBuilder{
ModuleName: m.Module().Name(),
MetricSetName: m.Name(),
Host: m.Host(),
StartTime: startTime,
FetchDuration: 115 * time.Microsecond,
Event: event,
}
fullEvent, _ := build.Build()
fullEvent["beat"] = common.MapStr{
"name": "host.example.com",
"hostname": "host.example.com",
}
// Delete meta data as not needed for the event output here
delete(fullEvent, "_event_metadata")
output, _ := json.MarshalIndent(fullEvent, "", " ")
err = ioutil.WriteFile(path+"/_meta/data.json", output, 0644)
if err != nil {
return err
}
return nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。