-
Notifications
You must be signed in to change notification settings - Fork 0
/
aurora.go
84 lines (70 loc) · 1.87 KB
/
aurora.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
package main
import (
"fmt"
"log"
"net/http"
"os"
"os/signal"
"strconv"
"sync"
"syscall"
"github.com/giuseppe7/aurora/internal/workers"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const applicationNamespace = "aurora"
const defaultWatchedFolder = "./test/data"
// Variable to be set by the Go linker at build time.
var version string
// Set up observability with Prometheus handler for metrics.
func initObservability() {
go func() {
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":2112", nil)
}()
// Register a version gauge.
versionGauge := prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: applicationNamespace,
Name: "version_info",
Help: "Version of the application.",
},
)
prometheus.MustRegister(versionGauge)
versionValue, err := strconv.ParseFloat(version, 64)
if err != nil {
versionValue = 0.0
}
versionGauge.Set(versionValue)
}
func main() {
log.Println("Coming online...")
log.Println("Version:", version)
// Channel to be aware of an OS interrupt like Control-C.
var waiter sync.WaitGroup
waiter.Add(1)
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
// Set up observability.
initObservability()
// Set up file watcher to detect and create metrics.
path := defaultWatchedFolder
if os.Getenv("WATCHED_DIR") != "" {
path = os.Getenv(("WATCHED_DIR"))
}
// Set up file watcher to detect and create metrics.
log.Println("Folder being watched:", path)
w := workers.NewMetricsFolderWatcher(path) // TODO: Singleton?
w.WatchAndEmit()
// Function and waiter to wait for the OS interrupt and do any clean-up.
log.Println("Running.")
go func() {
<-c
fmt.Println("\r")
log.Println("Interrupt captured.")
waiter.Done()
}()
waiter.Wait()
// Shut down the application.
log.Println("Shutting down.")
}