-
Notifications
You must be signed in to change notification settings - Fork 2
/
metrics.go
48 lines (40 loc) · 1.39 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
package main
import (
"context"
"log"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type prometheusMetrics struct {
goGetRequestsTotal *prometheus.CounterVec
githubRateLimit *prometheus.GaugeVec
}
var metrics = &prometheusMetrics{
goGetRequestsTotal: prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "home_go_get_requests_total",
Help: "Total number of ?go-get=1 requests.",
}, []string{"path"}),
githubRateLimit: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "home_github_rate_limit",
Help: "Remaining requests the GitHub client can make this hour.",
}, []string{"client"}),
}
func (m *prometheusMetrics) IncGoGetRequestsTotal(importPath string) {
m.goGetRequestsTotal.With(prometheus.Labels{"path": importPath}).Inc()
}
func (m *prometheusMetrics) SetGitHubRateLimit(clientName string, remaining int) {
m.githubRateLimit.With(prometheus.Labels{"client": clientName}).Set(float64(remaining))
}
func initMetrics(cancel context.CancelFunc, httpAddr string) {
r := prometheus.NewRegistry()
r.MustRegister(metrics.goGetRequestsTotal)
r.MustRegister(metrics.githubRateLimit)
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.HandlerFor(r, promhttp.HandlerOpts{}))
go func() {
err := http.ListenAndServe(httpAddr, mux)
log.Println("initMetrics: http.ListenAndServe:", err)
cancel()
}()
}