1 Star 0 Fork 0

igo/pkg

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
prom2json.go 3.63 KB
一键复制 编辑 原始数据 按行查看 历史
layte.xiao 提交于 2022-10-15 11:20 +08:00 . init
package xprom
import (
"fmt"
dto "github.com/prometheus/client_model/go"
)
// source https://github.com/prometheus/prom2json/blob/master/prom2json.go
// Family mirrors the MetricFamily proto message.
type Family struct {
Name string `json:"name"`
Help string `json:"help"`
Type string `json:"type"`
Metrics []interface{} `json:"metrics,omitempty"` // Either metric or summary.
}
// Metric is for all "single value" metrics, i.e. Counter, Gauge, and Untyped.
type Metric struct {
Labels map[string]string `json:"labels,omitempty"`
TimestampMs string `json:"timestamp_ms,omitempty"`
Value string `json:"value"`
}
// Summary mirrors the Summary proto message.
type Summary struct {
Labels map[string]string `json:"labels,omitempty"`
TimestampMs string `json:"timestamp_ms,omitempty"`
Quantiles map[string]string `json:"quantiles,omitempty"`
Count string `json:"count"`
Sum string `json:"sum"`
}
// Histogram mirrors the Histogram proto message.
type Histogram struct {
Labels map[string]string `json:"labels,omitempty"`
TimestampMs string `json:"timestamp_ms,omitempty"`
Buckets map[string]string `json:"buckets,omitempty"`
Count string `json:"count"`
Sum string `json:"sum"`
}
// NewFamily consumes a MetricFamily and transforms it to the local Family type.
func NewFamily(dtoMF *dto.MetricFamily) *Family {
mf := &Family{
Name: dtoMF.GetName(),
Help: dtoMF.GetHelp(),
Type: dtoMF.GetType().String(),
Metrics: make([]interface{}, len(dtoMF.Metric)),
}
for i, m := range dtoMF.Metric {
switch dtoMF.GetType() {
case dto.MetricType_SUMMARY:
mf.Metrics[i] = Summary{
Labels: makeLabels(m),
TimestampMs: makeTimestamp(m),
Quantiles: makeQuantiles(m),
Count: fmt.Sprint(m.GetSummary().GetSampleCount()),
Sum: fmt.Sprint(m.GetSummary().GetSampleSum()),
}
case dto.MetricType_HISTOGRAM:
mf.Metrics[i] = Histogram{
Labels: makeLabels(m),
TimestampMs: makeTimestamp(m),
Buckets: makeBuckets(m),
Count: fmt.Sprint(m.GetHistogram().GetSampleCount()),
Sum: fmt.Sprint(m.GetHistogram().GetSampleSum()),
}
default:
mf.Metrics[i] = Metric{
Labels: makeLabels(m),
TimestampMs: makeTimestamp(m),
Value: fmt.Sprint(getValue(m)),
}
}
}
return mf
}
func MetricTypePtr(mt dto.MetricType) *dto.MetricType {
return &mt
}
func getValue(m *dto.Metric) float64 {
switch {
case m.Gauge != nil:
return m.GetGauge().GetValue()
case m.Counter != nil:
return m.GetCounter().GetValue()
case m.Untyped != nil:
return m.GetUntyped().GetValue()
default:
return 0.
}
}
func makeLabels(m *dto.Metric) map[string]string {
result := map[string]string{}
for _, lp := range m.Label {
result[lp.GetName()] = lp.GetValue()
}
return result
}
func makeTimestamp(m *dto.Metric) string {
if m.TimestampMs == nil {
return ""
}
return fmt.Sprint(m.GetTimestampMs())
}
func makeQuantiles(m *dto.Metric) map[string]string {
result := map[string]string{}
for _, q := range m.GetSummary().Quantile {
result[fmt.Sprint(q.GetQuantile())] = fmt.Sprint(q.GetValue())
}
return result
}
func makeBuckets(m *dto.Metric) map[string]string {
result := map[string]string{}
for _, b := range m.GetHistogram().Bucket {
result[fmt.Sprint(b.GetUpperBound())] = fmt.Sprint(b.GetCumulativeCount())
}
return result
}
func (f *Family) AddLabel(key, val string) {
for i, item := range f.Metrics {
switch m := item.(type) {
case Metric:
m.Labels[key] = val
f.Metrics[i] = m
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/igolang/pkg.git
git@gitee.com:igolang/pkg.git
igolang
pkg
pkg
v1.29.8

搜索帮助