-
Notifications
You must be signed in to change notification settings - Fork 1
/
prometheus.go
57 lines (52 loc) · 1.44 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
package hoopsnake
import (
"context"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"tailscale.com/tsnet"
)
func (s *TailnetSSH) setupPrometheus(ctx context.Context, srv *tsnet.Server) error {
if s.prometheusAddr == "" {
return nil
}
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
listener, err := srv.Listen("tcp", s.prometheusAddr)
if err != nil {
return fmt.Errorf("could not listen on prometheus address %v: %w", s.prometheusAddr, err)
}
go func() {
server := http.Server{
Handler: mux,
ReadHeaderTimeout: 1 * time.Second,
}
err := server.Serve(listener)
if err != nil && ctx.Err() == nil {
// Failed to listen but not asked to shut down:
log.Printf("Failed to listen on prometheus address: %v", err)
os.Exit(20)
}
}()
v4, v6 := srv.TailscaleIPs()
promauto.NewGaugeFunc(prometheus.GaugeOpts{
Name: "hoopsnake_running",
Help: "A counter set to 1.0 if hoopsnake is running.",
ConstLabels: prometheus.Labels{
"ipv4": v4.String(),
"ipv6": v6.String(),
"hostname": srv.Hostname,
},
}, func() float64 { return 1.0 })
listenAddr := s.prometheusAddr
if listenAddr[0] == ':' {
listenAddr = v4.String() + listenAddr
}
log.Printf("Serving prometheus metrics at http://%s/metrics", listenAddr)
return nil
}