1 Star 0 Fork 0

sqos/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
prometheus.go 1.12 KB
一键复制 编辑 原始数据 按行查看 历史
package helper
import (
"fmt"
"github.com/elastic/beats/metricbeat/mb"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"
)
// Prometheus helper retrieves prometheus formatted metrics
type Prometheus struct {
HTTP
}
// NewPrometheusClient creates new prometheus helper
func NewPrometheusClient(base mb.BaseMetricSet) *Prometheus {
http := NewHTTP(base)
return &Prometheus{*http}
}
// GetFamilies requests metric families from prometheus endpoint and returns them
func (p *Prometheus) GetFamilies() ([]*dto.MetricFamily, error) {
resp, err := p.FetchResponse()
if err != nil {
return nil, err
}
defer resp.Body.Close()
format := expfmt.ResponseFormat(resp.Header)
if format == "" {
return nil, fmt.Errorf("Invalid format for response of response")
}
decoder := expfmt.NewDecoder(resp.Body, format)
if decoder == nil {
return nil, fmt.Errorf("Unable to create decoder to decode response")
}
families := []*dto.MetricFamily{}
for err == nil {
mf := &dto.MetricFamily{}
err = decoder.Decode(mf)
if err == nil {
families = append(families, mf)
}
}
return families, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sqos/beats.git
git@gitee.com:sqos/beats.git
sqos
beats
beats
v6.1.4

搜索帮助

0d507c66 1850385 C8b1a773 1850385