-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprometheus.go
90 lines (72 loc) · 2.19 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
87
88
89
90
package gcs_monitor
import (
"net/http"
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
promNamespace = "gcs_monitor"
)
var (
labelNames = []string{"type", "project", "bucket"}
)
type PromMetrics struct {
Project string
sizeSummary *prometheus.SummaryVec
fileCounter *prometheus.CounterVec
zeroFileCounter *prometheus.CounterVec
}
func metricStringCleanup(in string) string {
return strings.Replace(in, "-", "_", -1)
}
// et == eventType, b == bucket
func (p *PromMetrics) makeLabels(et string, b string) prometheus.Labels {
labels := make(prometheus.Labels)
labels["project"] = p.Project
labels["type"] = metricStringCleanup(et)
labels["bucket"] = metricStringCleanup(b)
return labels
}
func (p *PromMetrics) SetupPrometheus(project string) (http.Handler, error) {
p.Project = project
p.sizeSummary = prometheus.NewSummaryVec(prometheus.SummaryOpts{
Namespace: promNamespace,
Name: "size",
Help: "bucket object file size summary",
}, labelNames)
p.fileCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: promNamespace,
Subsystem: "file",
Name: "count",
Help: "file per bucket count",
}, labelNames)
p.zeroFileCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: promNamespace,
Subsystem: "zero",
Name: "count",
Help: "zero file per bucket count",
}, labelNames)
if err := prometheus.Register(p.sizeSummary); err != nil {
return nil, err
}
if err := prometheus.Register(p.fileCounter); err != nil {
return nil, err
}
if err := prometheus.Register(p.zeroFileCounter); err != nil {
return nil, err
}
return promhttp.Handler(), nil
}
func (p *PromMetrics) ObserveSize(eventType string, bucket string, size float64) {
labels := p.makeLabels(eventType, bucket)
p.sizeSummary.With(labels).Observe(size)
}
func (p *PromMetrics) IncFileCounter(eventType string, bucket string) {
labels := p.makeLabels(eventType, bucket)
p.fileCounter.With(labels).Inc()
}
func (p *PromMetrics) IncZeroFileCounter(eventType string, bucket string) {
labels := p.makeLabels(eventType, bucket)
p.zeroFileCounter.With(labels).Inc()
}