1 Star 0 Fork 0

sqos/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
data_generator.go 2.57 KB
一键复制 编辑 原始数据 按行查看 历史
package testing
import (
"flag"
"fmt"
"io/ioutil"
"os"
"testing"
"time"
"github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/outputs/codec/json"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/mb/module"
)
var (
// Use `go test -data` to update files.
dataFlag = flag.Bool("data", false, "Write updated data.json files")
)
// WriteEvent fetches a single event writes the output to a ./_meta/data.json
// file.
func WriteEvent(f mb.EventFetcher, t testing.TB) error {
if !*dataFlag {
t.Skip("skip data generation tests")
}
event, err := f.Fetch()
if err != nil {
return err
}
fullEvent := CreateFullEvent(f, event)
WriteEventToDataJSON(t, fullEvent)
return nil
}
// WriteEvents fetches events and writes the first event to a ./_meta/data.json
// file.
func WriteEvents(f mb.EventsFetcher, t testing.TB) 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")
}
fullEvent := CreateFullEvent(f, events[0])
WriteEventToDataJSON(t, fullEvent)
return nil
}
// CreateFullEvent builds a full event given the data generated by a MetricSet.
// This simulates the output of Metricbeat as if it were
// 2016-05-23T08:05:34.853Z and the hostname is host.example.com.
func CreateFullEvent(ms mb.MetricSet, metricSetData common.MapStr) beat.Event {
startTime, err := time.Parse(time.RFC3339Nano, "2016-05-23T08:05:34.853Z")
if err != nil {
panic(err)
}
build := module.EventBuilder{
ModuleName: ms.Module().Name(),
MetricSetName: ms.Name(),
Host: ms.Host(),
StartTime: startTime,
FetchDuration: 115 * time.Microsecond,
Event: metricSetData,
}
fullEvent, err := build.Build()
if err != nil {
panic(err)
}
fullEvent.Fields["beat"] = common.MapStr{
"name": "host.example.com",
"hostname": "host.example.com",
}
return fullEvent
}
// WriteEventToDataJSON writes the given event as "pretty" JSON to
// a ./_meta/data.json file. If the -data CLI flag is unset or false then the
// method is a no-op.
func WriteEventToDataJSON(t testing.TB, fullEvent beat.Event) {
if !*dataFlag {
return
}
path, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
// use json output codec to encode event to json
output, err := json.New(true, "1.2.3").Encode("noindex", &fullEvent)
if err != nil {
t.Fatal(err)
}
if err = ioutil.WriteFile(path+"/_meta/data.json", output, 0644); err != nil {
t.Fatal(err)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sqos/beats.git
git@gitee.com:sqos/beats.git
sqos
beats
beats
v6.0.1

搜索帮助

0d507c66 1850385 C8b1a773 1850385