-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
60 lines (49 loc) · 1.47 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
package main
import (
"context"
"os"
"os/signal"
"runtime/debug"
"syscall"
"time"
"github.com/equinor/radix-cost-allocation/config"
"github.com/equinor/radix-cost-allocation/run"
_ "github.com/microsoft/go-mssqldb"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/vrischmann/envconfig"
)
var appConfig config.AppConfig
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGTERM)
defer cancel()
if err := envconfig.Init(&appConfig); err != nil {
log.Fatal().Err(err).Msg("Failed to initialize config")
}
ctx, err := setupLogger(ctx, appConfig.LogLevel, appConfig.PrettyPrint)
if err != nil {
log.Fatal().Err(err).Msg("Failed to initialize logger")
}
info, _ := debug.ReadBuildInfo()
log.Info().Str("version", info.Main.Version).Msg("Starting")
go func() {
if err := run.InitAndStartCollector(ctx, appConfig.SQL, appConfig.Schedule, appConfig.AppNameExcludeList); err != nil {
log.Fatal().Msg(err.Error())
}
}()
<-ctx.Done()
}
func setupLogger(ctx context.Context, logLevel string, prettyPrint bool) (context.Context, error) {
zerolog.DurationFieldUnit = time.Millisecond
level, err := zerolog.ParseLevel(logLevel)
if err != nil {
return nil, err
}
zerolog.SetGlobalLevel(level)
if prettyPrint {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.TimeOnly})
}
zerolog.DefaultContextLogger = &log.Logger
ctx = log.Logger.WithContext(ctx)
return ctx, nil
}