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