-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
93 lines (77 loc) · 2.32 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
package main
import (
"context"
goerrors "errors"
"os"
"os/signal"
"syscall"
"github.com/ONSdigital/dp-search-api/config"
"github.com/ONSdigital/dp-search-api/service"
"github.com/ONSdigital/log.go/v2/log"
"github.com/pkg/errors"
dpotelgo "github.com/ONSdigital/dp-otel-go"
)
const serviceName = "dp-search-api"
var (
// BuildTime represents the time in which the service was built
BuildTime string
// GitCommit represents the commit (SHA-1) hash of the service that is running
GitCommit string
// Version represents the version of the service that is running
Version string
)
func main() {
log.Namespace = serviceName
ctx := context.Background()
if isSvcError, err := run(ctx); err != nil {
if isSvcError {
log.Fatal(ctx, "search api error received", err)
}
log.Error(ctx, "application unexpectedly failed", err)
os.Exit(1)
}
}
func run(ctx context.Context) (bool, error) {
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
// Create the service, providing an error channel for fatal errors
svcErrors := make(chan error, 1)
svcList := service.NewServiceList(&service.Init{})
// Read config
cfg, err := config.Get()
if err != nil {
log.Fatal(ctx, "error retrieving Config", err)
return false, err
}
log.Info(ctx, "config on startup", log.Data{"config": cfg, "build_time": BuildTime, "git-commit": GitCommit})
if cfg.OtelEnabled {
// Set up OpenTelemetry
otelConfig := dpotelgo.Config{
OtelServiceName: cfg.OTServiceName,
OtelExporterOtlpEndpoint: cfg.OTExporterOTLPEndpoint,
OtelBatchTimeout: cfg.OTBatchTimeout,
}
otelShutdown, oErr := dpotelgo.SetupOTelSDK(ctx, otelConfig)
if oErr != nil {
log.Fatal(ctx, "error setting up OpenTelemetry - hint: ensure OTEL_EXPORTER_OTLP_ENDPOINT is set", oErr)
return false, oErr
}
// Handle shutdown properly so nothing leaks.
defer func() {
err = goerrors.Join(err, otelShutdown(context.Background()))
}()
}
// Run the service
svc, err := service.Run(ctx, cfg, svcList, BuildTime, GitCommit, Version, svcErrors)
if err != nil {
return false, errors.Wrap(err, "running service failed")
}
// Blocks until a fatal error occurs
select {
case err := <-svcErrors:
return true, err
case <-signals:
log.Info(ctx, "os signal received")
}
return false, svc.Close(ctx)
}