forked from kuskoman/logstash-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (63 loc) · 2.01 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
package main
import (
"flag"
"fmt"
"log"
"log/slog"
"os"
"strconv"
"github.com/joho/godotenv"
"github.com/prometheus/client_golang/prometheus"
"github.com/kuskoman/logstash-exporter/internal/server"
"github.com/kuskoman/logstash-exporter/pkg/config"
"github.com/kuskoman/logstash-exporter/pkg/manager"
)
func main() {
versionFlag := flag.Bool("version", false, "prints the version and exits")
helpFlag := flag.Bool("help", false, "prints the help message and exits")
configLocationFlag := flag.String("config", config.ExporterConfigLocation, "location of the exporter config file")
flag.Parse()
if *helpFlag {
fmt.Printf("Usage of %s:\n", os.Args[0])
fmt.Println()
fmt.Println("Flags:")
flag.PrintDefaults()
return
}
if *versionFlag {
fmt.Printf("%s\n", config.SemanticVersion)
return
}
warn := godotenv.Load()
exporterConfig, err := config.GetConfig(*configLocationFlag)
if err != nil {
log.Fatalf("failed to get exporter config: %s", err)
os.Exit(1)
}
logger, err := config.SetupSlog(exporterConfig.Logging.Level, exporterConfig.Logging.Format)
if err != nil {
log.Printf("failed to load .env file: %s", err)
log.Fatalf("failed to setup slog: %s", err)
}
slog.SetDefault(logger)
if warn != nil {
slog.Warn("failed to load .env file", "error", warn)
}
host := exporterConfig.Server.Host
port := strconv.Itoa(exporterConfig.Server.Port)
slog.Debug("application starting... ")
versionInfo := config.GetVersionInfo()
slog.Info(versionInfo.String())
slog.Debug("http timeout", "timeout", exporterConfig.Logstash.HttpTimeout)
collectorManager := manager.NewCollectorManager(
exporterConfig.Logstash.Servers,
exporterConfig.Logstash.HttpTimeout,
)
prometheus.MustRegister(collectorManager)
appServer := server.NewAppServer(host, port, exporterConfig, exporterConfig.Logstash.HttpTimeout)
slog.Info("starting server on", "host", host, "port", port)
if err := appServer.ListenAndServe(); err != nil {
slog.Error("failed to listen and serve", "err", err)
os.Exit(1)
}
}