-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmetrics.go
112 lines (93 loc) · 2.95 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"net/http"
"strconv"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
tsUpdated = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "tailscale_last_updated",
Help: "Timestamp of when Tailscale data was last updated from the API.",
},
[]string{},
)
tsDeviceInfo = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "tailscale_device_info",
Help: "Information about the tailscale device.",
},
[]string{"name", "id", "external", "hostname"},
)
tsUpgradeAvailable = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "tailscale_device_upgrade_available",
Help: "Whether this device has an update available.",
},
[]string{"name", "id"},
)
tsLastSeen = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "tailscale_device_last_seen",
Help: "The last time this device was seen.",
},
[]string{"name", "id"},
)
tsExpires = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "tailscale_device_expires",
Help: "When this device's key will expire.",
},
[]string{"name", "id"},
)
tsBlocksIncoming = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "tailscale_device_blocks_incoming",
Help: "Whether this device blocks incoming connections.",
},
[]string{"name", "id"},
)
tsExternal = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "tailscale_device_external",
Help: "Whether this device is external to this Tailnet.",
},
[]string{"name", "id"},
)
)
type metrics struct {
registry *prometheus.Registry
}
func (metrics *metrics) registerMetrics() {
metrics.registry.MustRegister(tsUpdated)
metrics.registry.MustRegister(tsDeviceInfo)
metrics.registry.MustRegister(tsUpgradeAvailable)
metrics.registry.MustRegister(tsLastSeen)
metrics.registry.MustRegister(tsExpires)
metrics.registry.MustRegister(tsBlocksIncoming)
metrics.registry.MustRegister(tsExternal)
}
func (metrics *metrics) generateMetrics(device Device) {
// create a new registry instance to ensure old data is cleaned up
metrics.registry = prometheus.NewRegistry()
metrics.registerMetrics()
tsUpdated.WithLabelValues([]string{}...).Set(float64(time.Now().Unix()))
tsDeviceInfo.WithLabelValues(device.Name, device.ID, strconv.FormatBool(device.External), device.Hostname).Set(1)
tsUpgradeAvailable.WithLabelValues(device.Name, device.ID).Set(b2f(device.UpdateAvailable))
tsLastSeen.WithLabelValues(device.Name, device.ID).Set(float64(device.LastSeen.Unix()))
tsExpires.WithLabelValues(device.Name, device.ID).Set(float64(device.Expires.Unix()))
tsBlocksIncoming.WithLabelValues(device.Name, device.ID).Set(b2f(device.BlocksIncomingConnections))
tsExternal.WithLabelValues(device.Name, device.ID).Set(b2f(device.External))
}
func (metrics *metrics) metricsHandler() http.Handler {
return promhttp.HandlerFor(metrics.registry, promhttp.HandlerOpts{})
}
func b2f(b bool) float64 {
set := 1.0
if !b {
set = 0.0
}
return set
}