-
Notifications
You must be signed in to change notification settings - Fork 1
/
prometheus.go
110 lines (101 loc) · 2.91 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package gojob
import (
"fmt"
"log/slog"
"sync"
"github.com/WangYihang/gojob/pkg/runner"
"github.com/WangYihang/gojob/pkg/utils"
"github.com/WangYihang/gojob/pkg/version"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/push"
io_prometheus_client "github.com/prometheus/client_model/go"
)
var (
numTotal = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "num_total",
Help: "Total number of processed events",
},
)
numFailed = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "num_failed",
Help: "Total number of failed events",
},
)
numSucceed = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "num_succeed",
Help: "Total number of succeeded events",
},
)
numFinished = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "num_finished",
Help: "Total number of finished events",
},
)
)
type customMetricsRegistry struct {
*prometheus.Registry
customLabels []*io_prometheus_client.LabelPair
}
func NewRegistryWithLabels(labels map[string]string) *customMetricsRegistry {
c := &customMetricsRegistry{
Registry: prometheus.NewRegistry(),
}
for k, v := range labels {
c.customLabels = append(c.customLabels, &io_prometheus_client.LabelPair{
Name: &k,
Value: &v,
})
}
return c
}
func (g *customMetricsRegistry) Gather() ([]*io_prometheus_client.MetricFamily, error) {
metricFamilies, err := g.Registry.Gather()
for _, metricFamily := range metricFamilies {
metrics := metricFamily.Metric
for _, metric := range metrics {
metric.Label = append(metric.Label, g.customLabels...)
}
}
return metricFamilies, err
}
func prometheusPusher(url, job string, statusChan <-chan Status, wg *sync.WaitGroup) {
instance := fmt.Sprintf(
"gojob-%s-%s-%s-%s",
version.Version,
utils.Sanitize(runner.Runner.Country),
utils.Sanitize(runner.Runner.City),
runner.Runner.IP,
)
registry := NewRegistryWithLabels(map[string]string{
"gojob_version": version.Version,
"gojob_runner_ip": runner.Runner.IP,
"gojob_runner_country": runner.Runner.Country,
"gojob_runner_region": runner.Runner.Region,
"gojob_runner_city": runner.Runner.City,
})
registry.MustRegister(numTotal, numFailed, numSucceed, numFinished)
registry.MustRegister(
collectors.NewGoCollector(),
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
)
go func() {
for status := range statusChan {
slog.Info("promehteus pusher", slog.Any("status", status))
numTotal.Set(float64(status.NumTotal))
numFailed.Set(float64(status.NumFailed))
numSucceed.Set(float64(status.NumSucceed))
numFinished.Set(float64(status.NumFinished))
if err := push.New(url, job).Grouping(
"instance", instance,
).Gatherer(registry).Push(); err != nil {
slog.Error("error occurred while pushing to prometheus", slog.String("error", err.Error()))
}
}
wg.Done()
}()
}