-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmetrics.go
88 lines (75 loc) · 2.24 KB
/
metrics.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
package main
import (
"net/http"
"strconv"
"github.com/jamesog/scan/internal/sqlite"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
gaugeTotal = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "scan",
Subsystem: "ips",
Name: "total",
Help: "Total IPs found",
})
gaugeLatest = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "scan",
Subsystem: "ips",
Name: "latest",
Help: "Latest IPs found",
})
gaugeNew = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "scan",
Subsystem: "ips",
Name: "new",
Help: "New IPs found",
})
gaugeSubmission = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "scan",
Name: "last_submission_time",
Help: "Last submission time in seconds since the Unix epoch",
})
gaugeJobs = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "scan",
Name: "job",
Help: "Number of IPs found in each each job, with submitted and received times",
},
[]string{"id", "submitted", "received"})
gaugeJobSubmission = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "scan",
Subsystem: "job",
Name: "last_submission_time",
Help: "Last job submission time in seconds since the Unix epoch",
})
)
func init() {
prometheus.MustRegister(gaugeTotal)
prometheus.MustRegister(gaugeLatest)
prometheus.MustRegister(gaugeNew)
prometheus.MustRegister(gaugeSubmission)
prometheus.MustRegister(gaugeJobs)
prometheus.MustRegister(gaugeJobSubmission)
}
func (app *App) metrics() http.Handler {
results, err := app.db.ResultData("", "", "")
if err == nil {
gaugeTotal.Set(float64(results.Total))
gaugeLatest.Set(float64(results.Latest))
gaugeNew.Set(float64(results.New))
}
jobs, _ := app.db.LoadJobs(sqlite.SQLFilter{
Where: []string{`received IS NOT NULL`},
})
for _, job := range jobs {
gaugeJobs.With(prometheus.Labels{
"id": strconv.Itoa(job.ID),
"submitted": strconv.FormatInt(job.Submitted.Unix(), 10),
"received": strconv.FormatInt(job.Received.Unix(), 10),
}).Set(float64(job.Count))
}
sub, _ := app.db.LoadSubmission(sqlite.SQLFilter{})
gaugeSubmission.Set(float64(sub.Time.Unix()))
return promhttp.Handler()
}