代码拉取完成,页面将自动刷新
package stats
import (
"bufio"
"fmt"
"net/http"
"strings"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/mb/parse"
)
const (
defaultScheme = "http"
defaultPath = "/metrics"
)
var (
debugf = logp.MakeDebug("prometheus-stats")
hostParser = parse.URLHostParserBuilder{
DefaultScheme: defaultScheme,
DefaultPath: defaultPath,
}.Build()
)
func init() {
if err := mb.Registry.AddMetricSet("prometheus", "stats", New, hostParser); err != nil {
panic(err)
}
}
type MetricSet struct {
mb.BaseMetricSet
client *http.Client
}
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
logp.Warn("EXPERIMENTAL: The prometheus stats metricset is experimental")
return &MetricSet{
BaseMetricSet: base,
client: &http.Client{Timeout: base.Module().Config().Timeout},
}, nil
}
func (m *MetricSet) Fetch() (common.MapStr, error) {
req, err := http.NewRequest("GET", m.HostData().SanitizedURI, nil)
if m.HostData().User != "" || m.HostData().Password != "" {
req.SetBasicAuth(m.HostData().User, m.HostData().Password)
}
resp, err := m.client.Do(req)
if err != nil {
return nil, fmt.Errorf("error making http request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("HTTP error %d: %s", resp.StatusCode, resp.Status)
}
scanner := bufio.NewScanner(resp.Body)
entries := map[string]interface{}{}
// Iterate through all events to gather data
for scanner.Scan() {
line := scanner.Text()
// Skip comments and calculated lines
if line[0] == '#' || strings.Contains(line, "quantile=") {
continue
}
split := strings.Split(line, " ")
entries[split[0]] = split[1]
}
data, err := eventMapping(entries)
return data, err
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。