-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathprometheus_parser.go
More file actions
101 lines (93 loc) · 2.8 KB
/
prometheus_parser.go
File metadata and controls
101 lines (93 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// cSpell:ignore quantiles
package main
import (
"bufio"
"errors"
"fmt"
"io"
"strings"
// cp "github.com/mitchellh/copystructure"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"
)
func ParsePrometheusResponse(data []byte) (any, error) {
buf := bufio.NewReader(strings.NewReader(string(data)))
// buf := bytes.NewReader(data)
format := expfmt.NewFormat(expfmt.TypeTextPlain)
decoder := expfmt.NewDecoder(buf, format)
// var metrics []*dto.MetricFamily
// results := make([]map[string]any, 0, 10)
results := make(map[string]any)
for {
var (
mf dto.MetricFamily
name string
)
if err := decoder.Decode(&mf); err != nil {
if errors.Is(err, io.EOF) {
break
}
return nil, fmt.Errorf("unmarshaling failed: %v", err)
}
res := make(map[string]any)
name = *mf.Name
res["name"] = name
res["help"] = *mf.Help
res["type"] = strings.ToLower(mf.Type.String())
res_metrics := make([]map[string]any, 0, 10)
for _, metric := range mf.Metric {
res_metric := make(map[string]any)
labels := make(map[string]string, len(metric.Label))
if len(metric.Label) > 0 {
for _, label := range metric.Label {
labels[*label.Name] = *label.Value
}
}
res_metric["labels"] = labels
switch *mf.Type {
case dto.MetricType_GAUGE:
if metric.Gauge != nil {
res_metric["value"] = metric.Gauge.GetValue()
}
case dto.MetricType_COUNTER:
if metric.Counter != nil {
res_metric["value"] = metric.Counter.GetValue()
}
case dto.MetricType_HISTOGRAM:
if metric.Histogram != nil {
histogram := make(map[string]any)
histogram["sample_count"] = *metric.Histogram.SampleCount
histogram["sample_sum"] = *metric.Histogram.SampleSum
res_buckets := make([]any, 0)
for _, bucket := range metric.Histogram.Bucket {
res_bucket := make(map[string]any)
res_bucket["le"] = fmt.Sprintf("%g", *bucket.UpperBound)
res_bucket["value"] = *bucket.CumulativeCount
res_buckets = append(res_buckets, res_bucket)
}
histogram["buckets"] = res_buckets
res_metric["histogram"] = histogram
}
case dto.MetricType_SUMMARY:
summary := make(map[string]any)
summary["sample_count"] = *metric.Summary.SampleCount
summary["sample_sum"] = *metric.Summary.SampleSum
res_quantiles := make([]any, 0)
for _, quantile := range metric.Summary.Quantile {
res_quantile := make(map[string]float64)
res_quantile["quantile"] = *quantile.Quantile
res_quantile["value"] = *quantile.Value
res_quantiles = append(res_quantiles, res_quantile)
}
summary["quantile"] = res_quantiles
res_metric["summary"] = summary
}
res_metrics = append(res_metrics, res_metric)
}
if len(res) > 0 {
res["metrics"] = res_metrics
results[name] = res
}
}
return results, nil
}