-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
79 lines (61 loc) · 1.84 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
package main
import (
"flag"
"log/slog"
"os"
"os/signal"
"syscall"
"github.com/andrewhowdencom/courses.pito/delivery-service/carriers"
"github.com/andrewhowdencom/courses.pito/delivery-service/server"
"github.com/andrewhowdencom/courses.pito/delivery-service/telemetry"
"go.opentelemetry.io/contrib/instrumentation/runtime"
)
// flags that influence the programs behavior
var addr = flag.String("a", "localhost:9093", "the address on which the server should listen")
var log *slog.Logger
func main() {
log.Info("application started")
// Bind signal handlers
ch := make(chan os.Signal, 1)
// SIGINT is the signal to terminate ("interrupt") the process.
signal.Notify(ch, syscall.SIGINT)
carriers, err := carriers.New(carriers.Defaults...)
if err != nil {
log.Error("failed to bootstrap carriers", "error", err)
}
// Setup the server
srv := server.New(carriers)
// Run the server, but in its own goroutine without blocking this thread.
go func() {
if err := srv.Listen(*addr); err != nil {
log.Error("failed to start server", "error", err, "addr", *addr)
os.Exit(1)
}
}()
log.Info("awaiting shutdown signal (SIGINT)")
<-ch
log.Info("received shutdown signal")
if err := srv.Shutdown(); err != nil {
log.Error("failed to shutdown server", "error", err)
os.Exit(1)
}
os.Exit(0)
}
func init() {
// Parse the flags
flag.Parse()
// Bootstrap the logger
log = slog.New(slog.NewJSONHandler(
os.Stderr, nil,
))
// Bind the log to the telemetry package.
telemetry.Log = log
// Bootstrap the metrics
if err := telemetry.SetupOTelMetrics(telemetry.WithPrometheusHTTP("localhost:9094")); err != nil {
log.Error("failed to bootstrap metrics", "err", err)
}
// Start exporting runtime metrics (e.g. gc, memory, uptime)
if err := runtime.Start(); err != nil {
log.Error("failed to enable runtime metrics", "err", err)
}
}