1 Star 0 Fork 0

zhangjungang/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
stats.go 1.79 KB
一键复制 编辑 原始数据 按行查看 历史
ruflin 提交于 2016-12-15 16:00 . Add Prometheus module to Metricbeat
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
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhangjungang/beats.git
git@gitee.com:zhangjungang/beats.git
zhangjungang
beats
beats
v5.2.1

搜索帮助

0d507c66 1850385 C8b1a773 1850385