-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (45 loc) · 1.23 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
package main
import (
"log"
"net/http"
"regexp"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
requestsTotal = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "appcast_requests_total",
}, []string{"app_version"})
uaAppVersionRe = regexp.MustCompile(`(?:^|\s+)LinearMouse/(\d+\.\d+\.\d+(?:-beta\.\d+)?)`)
)
func handle(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/appcast.xml" {
http.NotFound(w, r)
return
}
match := uaAppVersionRe.FindStringSubmatch(r.UserAgent())
if match != nil {
appVersion := match[1]
if appVersion != "" {
requestsTotal.With(prometheus.Labels{"app_version": appVersion}).Inc()
}
}
appcast, err := getAppCast()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/xml")
w.Write(appcast)
}
func main() {
go startMetricsServer()
http.HandleFunc("/", handle)
log.Fatal(http.ListenAndServe(":3000", nil))
}
func startMetricsServer() {
metricsMux := http.NewServeMux()
metricsMux.Handle("/metrics", promhttp.Handler())
log.Fatalln(http.ListenAndServe(":9100", metricsMux))
}