-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
179 lines (149 loc) · 5.46 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"context"
"errors"
"os"
"github.com/ONSdigital/dp-api-clients-go/dataset"
"github.com/ONSdigital/dp-api-clients-go/zebedee"
esauth "github.com/ONSdigital/dp-elasticsearch/v2/awsauth"
elastic "github.com/ONSdigital/dp-elasticsearch/v2/elasticsearch"
"github.com/ONSdigital/dp-healthcheck/healthcheck"
dphttp "github.com/ONSdigital/dp-net/http"
"github.com/ONSdigital/dp-dimension-search-api/config"
"github.com/ONSdigital/dp-dimension-search-api/elasticsearch"
"github.com/ONSdigital/dp-dimension-search-api/searchoutputqueue"
"github.com/ONSdigital/dp-dimension-search-api/service"
kafka "github.com/ONSdigital/dp-kafka/v4"
dpotelgo "github.com/ONSdigital/dp-otel-go"
"github.com/ONSdigital/log.go/v2/log"
)
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 = "dp-dimension-search-api"
ctx := context.Background()
cfg, err := config.Get()
if err != nil {
log.Fatal(ctx, "failed to retrieve configuration", err)
os.Exit(1)
}
// sensitive fields are omitted from config.String().
log.Info(ctx, "config on startup", log.Data{"config": cfg})
var esSigner *esauth.Signer
if cfg.SignElasticsearchRequests {
esSigner, err = esauth.NewAwsSigner("", "", cfg.AwsRegion, cfg.AwsService)
if err != nil {
log.Error(ctx, "failed to create aws v4 signer", err)
os.Exit(1)
}
}
// 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.Error(ctx, "error setting up OpenTelemetry - hint: ensure OTEL_EXPORTER_OTLP_ENDPOINT is set", oErr)
}
// Handle shutdown properly so nothing leaks.
defer func() {
err = errors.Join(err, otelShutdown(context.Background()))
}()
elasticHTTPClient := dphttp.NewClient()
elasticsearch := elasticsearch.NewElasticSearchAPI(elasticHTTPClient, cfg.ElasticSearchAPIURL, cfg.SignElasticsearchRequests, esSigner, cfg.AwsService, cfg.AwsRegion)
pConfig := &kafka.ProducerConfig{
KafkaVersion: &cfg.KafkaVersion,
MaxMessageBytes: &cfg.KafkaMaxBytes,
BrokerAddrs: cfg.Brokers,
Topic: cfg.HierarchyBuiltTopic,
}
if cfg.KafkaSecProtocol == "TLS" {
pConfig.SecurityConfig = kafka.GetSecurityConfig(
cfg.KafkaSecCACerts,
cfg.KafkaSecClientCert,
cfg.KafkaSecClientKey,
cfg.KafkaSecSkipVerify,
)
}
hierarchyBuiltProducer, err := kafka.NewProducer(
ctx,
pConfig,
)
exitIfError(ctx, err, "error creating kafka hierarchyBuiltProducer")
hierarchyBuiltProducer.LogErrors(ctx)
outputQueue := searchoutputqueue.CreateOutputQueue(hierarchyBuiltProducer.Channels().Output)
datasetAPIClient := dataset.NewAPIClient(cfg.DatasetAPIURL)
hc := configureHealthChecks(ctx, cfg, elasticHTTPClient, esSigner, hierarchyBuiltProducer, datasetAPIClient)
svc := &service.Service{
AuthAPIURL: cfg.AuthAPIURL,
BindAddr: cfg.BindAddr,
DatasetAPIClient: datasetAPIClient,
DefaultMaxResults: cfg.MaxSearchResultsOffset,
Elasticsearch: elasticsearch,
ElasticsearchURL: cfg.ElasticSearchAPIURL,
HasPrivateEndpoints: cfg.HasPrivateEndpoints,
HealthCheck: hc,
MaxRetries: cfg.MaxRetries,
OutputQueue: outputQueue,
SearchAPIURL: cfg.SearchAPIURL,
HierarchyBuiltProducer: hierarchyBuiltProducer,
OTServiceName: cfg.OTServiceName,
ServiceAuthToken: cfg.ServiceAuthToken,
Shutdown: cfg.GracefulShutdownTimeout,
SignElasticsearchRequests: cfg.SignElasticsearchRequests,
}
svc.Start(ctx)
}
func configureHealthChecks(ctx context.Context,
cfg *config.Config,
elasticHTTPClient dphttp.Clienter,
esSigner *esauth.Signer,
producer *kafka.Producer,
datasetAPIClient *dataset.Client) *healthcheck.HealthCheck {
hasErrors := false
versionInfo, err := healthcheck.NewVersionInfo(BuildTime, GitCommit, Version)
if err != nil {
log.Fatal(ctx, "error creating version info", err)
hasErrors = true
}
hc := healthcheck.New(versionInfo, cfg.HealthCheckCriticalTimeout, cfg.HealthCheckInterval)
if err = hc.AddCheck("Dataset API", datasetAPIClient.Checker); err != nil {
log.Error(ctx, "error creating dataset API health check", err)
hasErrors = true
}
elasticClient := elastic.NewClientWithHTTPClientAndAwsSigner(cfg.ElasticSearchAPIURL, esSigner, cfg.SignElasticsearchRequests, elasticHTTPClient)
if err = hc.AddCheck("Elasticsearch", elasticClient.Checker); err != nil {
log.Error(ctx, "error creating elasticsearch health check", err)
hasErrors = true
}
if err = hc.AddCheck("Kafka Producer", producer.Checker); err != nil {
log.Error(ctx, "error adding check for kafka producer", err)
hasErrors = true
}
if cfg.HasPrivateEndpoints {
// zebedee is used only for identity checking
zebedeeClient := zebedee.New(cfg.AuthAPIURL)
if err = hc.AddCheck("Zebedee", zebedeeClient.Checker); err != nil {
log.Error(ctx, "error creating zebedee health check", err)
hasErrors = true
}
}
if hasErrors {
os.Exit(1)
}
return &hc
}
func exitIfError(ctx context.Context, err error, message string) {
if err != nil {
log.Fatal(ctx, message, err)
os.Exit(1)
}
}