代码拉取完成,页面将自动刷新
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)
}
}
}
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。