This repository has been archived by the owner on Mar 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
118 lines (106 loc) · 2.66 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
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
113
114
115
116
117
118
package main
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"runtime/debug"
"syscall"
"time"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/johejo/sora_exporter/collector"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
listenAddr string
metricsPath string
timeout time.Duration
soraURL string
printVersion bool
version string
commit string
date string
)
func init() {
flag.StringVar(&listenAddr, "listen-addr", ":9199", "Address to listen for telemetry.")
flag.StringVar(&metricsPath, "metrics-path", "/metrics", "Path under which to expose metrics.")
flag.DurationVar(&timeout, "timeout", 5*time.Second, "Timeout for scraping to sora.")
flag.StringVar(&soraURL, "sora-url", "http://127.0.0.1:3000", "URL for sora stats endpoint.")
flag.BoolVar(&printVersion, "version", false, "Print version.")
}
func main() {
lg := log.NewJSONLogger(os.Stderr)
flag.Parse()
if printVersion {
if version == "" {
info, ok := debug.ReadBuildInfo()
if ok {
version = info.Main.Version
} else {
version = "devel"
}
}
if commit == "" {
commit = "unknown"
}
if date == "" {
date = "unknown"
}
b, err := json.Marshal(struct {
Version string `json:"version"`
Commit string `json:"commit"`
Date string `json:"date"`
}{
Version: version,
Commit: commit,
Date: date,
})
if err != nil {
level.Error(lg).Log("err", err)
os.Exit(1)
}
fmt.Println(string(b))
os.Exit(0)
}
reg := prometheus.NewRegistry()
reg.MustRegister(
prometheus.NewBuildInfoCollector(),
prometheus.NewGoCollector(),
collector.New(collector.WithLogger(lg), collector.WithTimeout(timeout), collector.WithSoraURL(soraURL)),
)
mux := http.NewServeMux()
mux.Handle(metricsPath, promhttp.InstrumentMetricHandler(reg, promhttp.HandlerFor(reg, promhttp.HandlerOpts{})))
srv := &http.Server{
Addr: listenAddr,
Handler: mux,
}
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cancel()
finish := make(chan struct{})
go func() {
defer close(finish)
<-ctx.Done()
shutDownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(shutDownCtx); err != nil {
if errors.Is(err, http.ErrServerClosed) {
return
}
level.Error(lg).Log("msg", "failed to shutdown", "err", err)
}
}()
if err := srv.ListenAndServe(); err != nil {
if !errors.Is(err, http.ErrServerClosed) {
level.Error(lg).Log("msg", "failed to listen", "err", err)
}
os.Exit(1)
}
<-finish
os.Exit(0)
}