-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
87 lines (74 loc) · 1.93 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
"net/http"
)
var dvs []Device
var version string
type Device struct {
ID int `json:"id"`
Mac string `json:"mac"`
Firmware string `json:"firmware"`
}
type metrics struct {
devices prometheus.Gauge
info *prometheus.GaugeVec
}
func main() {
reg := prometheus.NewRegistry()
reg.MustRegister(collectors.NewGoCollector())
m := NewMetrics(reg)
m.devices.Set(float64(len(dvs)))
m.info.With(prometheus.Labels{"version": version}).Set(1)
promHandler := promhttp.HandlerFor(reg, promhttp.HandlerOpts{Registry: reg})
http.Handle("/metrics", promHandler)
http.HandleFunc("/", HelloServer)
http.HandleFunc("/devices", getDevices)
err := http.ListenAndServe(":8081", nil)
if err != nil {
return
}
}
func HelloServer(w http.ResponseWriter, r *http.Request) {
_, err := fmt.Fprintf(w, "Hello, %sMetrics", r.URL.Path[1:])
if err != nil {
return
}
}
func init() {
version = "2.10.5"
dvs = []Device{
{1, "5F-33-CC-1F-43-82", "2.1.6"},
{2, "EF-2B-C4-F5-D6-34", "2.1.6"},
}
}
func getDevices(w http.ResponseWriter, r *http.Request) {
b, err := json.Marshal(dvs)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(b)
}
func NewMetrics(reg prometheus.Registerer) *metrics {
m := &metrics{
devices: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "myapp",
Name: "connected_devices",
Help: "Number of currently connected devices.",
}),
info: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "myapp",
Name: "info",
Help: "Information about the My App environment.",
}, []string{"version"}),
}
reg.MustRegister(m.devices, m.info)
return m
}