代码拉取完成,页面将自动刷新
package collector
import (
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/metricbeat/helper"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/mb/parse"
)
const (
defaultScheme = "http"
defaultPath = "/metrics"
)
var (
hostParser = parse.URLHostParserBuilder{
DefaultScheme: defaultScheme,
DefaultPath: defaultPath,
PathConfigKey: "metrics_path",
}.Build()
)
func init() {
if err := mb.Registry.AddMetricSet("prometheus", "collector", New, hostParser); err != nil {
panic(err)
}
}
type MetricSet struct {
mb.BaseMetricSet
http *helper.HTTP
namespace string
}
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
logp.Warn("BETA: The prometheus collector metricset is beta")
config := struct {
Namespace string `config:"namespace" validate:"required"`
}{}
err := base.Module().UnpackConfig(&config)
if err != nil {
return nil, err
}
return &MetricSet{
BaseMetricSet: base,
http: helper.NewHTTP(base),
namespace: config.Namespace,
}, nil
}
func (m *MetricSet) Fetch() ([]common.MapStr, error) {
scanner, err := m.http.FetchScanner()
if err != nil {
return nil, err
}
eventList := map[string]common.MapStr{}
// Iterate through all events to gather data
for scanner.Scan() {
line := scanner.Text()
// Skip comment lines
if line[0] == '#' {
continue
}
promEvent := NewPromEvent(line)
if promEvent.value == nil {
continue
}
// If MapString for this label group does not exist yet, it is created
if _, ok := eventList[promEvent.labelHash]; !ok {
eventList[promEvent.labelHash] = common.MapStr{}
// Add labels
if len(promEvent.labels) > 0 {
eventList[promEvent.labelHash]["label"] = promEvent.labels
}
}
eventList[promEvent.labelHash][promEvent.key] = promEvent.value
}
// Converts hash list to slice
events := []common.MapStr{}
for _, e := range eventList {
e["_namespace"] = m.namespace
events = append(events, e)
}
return events, err
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。