/
prometheus.go
86 lines (73 loc) · 2.13 KB
/
prometheus.go
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
package prometheus
// Format switches between Table, Time series, or Heatmap. Table will only work
// in the Table panel. Heatmap is suitable for displaying metrics of the
// Histogram type on a Heatmap panel. Under the hood, it converts cumulative
// histograms to regular ones and sorts series by the bucket bound.
type FormatMode string
const (
FormatTable FormatMode = "table"
FormatHeatmap FormatMode = "heatmap"
FormatTimeSeries FormatMode = "time_series"
)
// Option represents an option that can be used to configure a prometheus query.
type Option func(target *Prometheus)
// Prometheus represents a prometheus query.
type Prometheus struct {
Ref string
Hidden bool
Expr string
IntervalFactor int
Interval string
Step int
LegendFormat string
Instant bool
Format string
}
// New creates a new prometheus query.
func New(query string, options ...Option) *Prometheus {
prometheus := &Prometheus{
Expr: query,
Format: string(FormatTimeSeries),
}
for _, opt := range options {
opt(prometheus)
}
return prometheus
}
// Legend sets the legend format.
func Legend(legend string) Option {
return func(prometheus *Prometheus) {
prometheus.LegendFormat = legend
}
}
// Ref sets the reference ID for this query.
func Ref(ref string) Option {
return func(prometheus *Prometheus) {
prometheus.Ref = ref
}
}
// Hide the query. Grafana does not send hidden queries to the data source,
// but they can still be referenced in alerts.
func Hide() Option {
return func(prometheus *Prometheus) {
prometheus.Hidden = true
}
}
// Instant marks the query as "instant, which means Prometheus will only return the latest scrapped value.
func Instant() Option {
return func(prometheus *Prometheus) {
prometheus.Instant = true
}
}
// Format indicates how the data should be returned.
func Format(format FormatMode) Option {
return func(prometheus *Prometheus) {
prometheus.Format = string(format)
}
}
// IntervalFactor sets the resolution factor.
func IntervalFactor(factor int) Option {
return func(prometheus *Prometheus) {
prometheus.IntervalFactor = factor
}
}