1 Star 0 Fork 0

sqos/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
server.go 2.47 KB
一键复制 编辑 原始数据 按行查看 历史
package server
import (
"github.com/pkg/errors"
"github.com/elastic/beats/libbeat/common/cfgwarn"
"github.com/elastic/beats/libbeat/logp"
serverhelper "github.com/elastic/beats/metricbeat/helper/server"
"github.com/elastic/beats/metricbeat/helper/server/tcp"
"github.com/elastic/beats/metricbeat/helper/server/udp"
"github.com/elastic/beats/metricbeat/mb"
)
// init registers the MetricSet with the central registry.
// The New method will be called after the setup of the module and before starting to fetch data
func init() {
if err := mb.Registry.AddMetricSet("graphite", "server", New); err != nil {
panic(err)
}
}
// MetricSet type defines all fields of the MetricSet
// As a minimum it must inherit the mb.BaseMetricSet fields, but can be extended with
// additional entries. These variables can be used to persist data or configuration between
// multiple fetch calls.
type MetricSet struct {
mb.BaseMetricSet
server serverhelper.Server
processor *metricProcessor
}
// New create a new instance of the MetricSet
// Part of new is also setting up the configuration by processing additional
// configuration entries if needed.
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
cfgwarn.Beta("The graphite server metricset is beta")
config := DefaultGraphiteCollectorConfig()
if err := base.Module().UnpackConfig(&config); err != nil {
return nil, err
}
var s serverhelper.Server
var err error
if config.Protocol == "tcp" {
s, err = tcp.NewTcpServer(base)
} else {
s, err = udp.NewUdpServer(base)
}
if err != nil {
return nil, err
}
processor := NewMetricProcessor(config.Templates, config.DefaultTemplate)
return &MetricSet{
BaseMetricSet: base,
server: s,
processor: processor,
}, nil
}
// Run method provides the Graphite server with a reporter with which events can be reported.
func (m *MetricSet) Run(reporter mb.PushReporter) {
// Start event watcher
if err := m.server.Start(); err != nil {
err = errors.Wrap(err, "failed to start graphite server")
logp.Err("%v", err)
reporter.Error(err)
return
}
for {
select {
case <-reporter.Done():
m.server.Stop()
return
case msg := <-m.server.GetEvents():
input := msg.GetEvent()
bytesRaw, ok := input[serverhelper.EventDataKey]
if ok {
bytes, ok := bytesRaw.([]byte)
if ok && len(bytes) != 0 {
event, err := m.processor.Process(string(bytes))
if err != nil {
reporter.Error(err)
} else {
reporter.Event(event)
}
}
}
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sqos/beats.git
git@gitee.com:sqos/beats.git
sqos
beats
beats
v6.2.2

搜索帮助