/
boostrap.go
420 lines (371 loc) · 12.3 KB
/
boostrap.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package bootstrap
import (
"context"
json2 "encoding/json"
"fmt"
stdlog "log"
"net"
"os"
"path"
"runtime/debug"
"strconv"
"strings"
"time"
"github.com/integration-system/isp-kit/app"
"github.com/integration-system/isp-kit/cluster"
"github.com/integration-system/isp-kit/config"
"github.com/integration-system/isp-kit/healthcheck"
"github.com/integration-system/isp-kit/infra"
"github.com/integration-system/isp-kit/infra/pprof"
"github.com/integration-system/isp-kit/json"
"github.com/integration-system/isp-kit/log"
"github.com/integration-system/isp-kit/log/file"
"github.com/integration-system/isp-kit/metrics"
"github.com/integration-system/isp-kit/metrics/app_metrics"
"github.com/integration-system/isp-kit/observability/sentry"
"github.com/integration-system/isp-kit/observability/tracing"
"github.com/integration-system/isp-kit/rc"
"github.com/integration-system/isp-kit/validator"
"github.com/pkg/errors"
"go.uber.org/zap/zapcore"
)
type Bootstrap struct {
App *app.Application
ClusterCli *cluster.Client
RemoteConfig *rc.Config
MetricsRegistry *metrics.Registry
InfraServer *infra.Server
HealthcheckRegistry *healthcheck.Registry
BindingAddress string
MigrationsDir string
ModuleName string
SentryHub sentry.Hub
TracingProvider tracing.Provider
}
func New(moduleVersion string, remoteConfig any, endpoints []cluster.EndpointDescriptor) *Bootstrap {
isDev := strings.ToLower(os.Getenv("APP_MODE")) == "dev"
appConfig, err := appConfig(isDev)
if err != nil {
stdlog.Fatal(errors.WithMessage(err, "app config"))
}
app, err := app.NewFromConfig(*appConfig)
if err != nil {
stdlog.Fatal(errors.WithMessage(err, "create app"))
return nil
}
localConfig, err := localConfig(app.Config())
if err != nil {
app.Logger().Fatal(app.Context(), errors.WithMessage(err, "create local config"))
}
sentryHub, err := sentry.NewHubFromConfiguration(sentry.Config{
Enable: localConfig.Observability.Sentry.Enable,
Dsn: localConfig.Observability.Sentry.Dsn,
ModuleName: localConfig.ModuleName,
Environment: localConfig.Observability.Sentry.Environment,
Tags: localConfig.Observability.Sentry.Tags,
InstanceId: localConfig.GrpcOuterAddress.IP,
ModuleVersion: moduleVersion,
})
if err != nil {
app.Logger().Fatal(app.Context(), errors.WithMessage(err, "create sentry error reporter"))
}
boot, err := bootstrap(isDev, app, sentryHub, *localConfig, moduleVersion, remoteConfig, endpoints)
if err != nil {
err = errors.WithMessage(err, "create bootstrap")
sentryHub.CatchError(app.Context(), err, log.FatalLevel)
app.Logger().Fatal(app.Context(), err)
}
return boot
}
func bootstrap(
isDev bool,
application *app.Application,
sentryHub sentry.Hub,
localConfig LocalConfig,
moduleVersion string,
remoteConfig any,
endpoints []cluster.EndpointDescriptor,
) (*Bootstrap, error) {
configServiceHosts, err := parseConfigServiceHosts(localConfig.ConfigServiceAddress)
if err != nil {
return nil, errors.WithMessage(err, "parse config service hosts")
}
broadcastHost := localConfig.GrpcOuterAddress.IP
if broadcastHost == "" {
broadcastHost, err = resolveHost(configServiceHosts[0])
if err != nil {
return nil, errors.WithMessage(err, "resolve local host")
}
}
moduleInfo := cluster.ModuleInfo{
ModuleName: localConfig.ModuleName,
ModuleVersion: moduleVersion,
LibVersion: kitVersion(),
GrpcOuterAddress: cluster.AddressConfiguration{
IP: broadcastHost,
Port: strconv.Itoa(localConfig.GrpcOuterAddress.Port),
},
Endpoints: endpoints,
}
schema := rc.GenerateConfigSchema(remoteConfig)
schemaData, err := json.Marshal(schema)
if err != nil {
return nil, errors.WithMessage(err, "marshal schema")
}
defaultConfig, err := readDefaultRemoteConfig(isDev, localConfig)
if err != nil {
return nil, errors.WithMessage(err, "read default remote config")
}
configData := cluster.ConfigData{
Version: moduleVersion,
Schema: schemaData,
Config: defaultConfig,
}
clusterCli := cluster.NewClient(
moduleInfo,
configData,
configServiceHosts,
sentry.WrapErrorLogger(application.Logger(), sentryHub),
)
rc := rc.New(validator.Default, []byte(localConfig.RemoteConfigOverride))
bindingAddress := net.JoinHostPort(localConfig.GrpcInnerAddress.IP, strconv.Itoa(localConfig.GrpcInnerAddress.Port))
migrationsDir, err := migrationsDirPath(isDev, localConfig)
if err != nil {
return nil, errors.WithMessage(err, "resolve migrations dir path")
}
healthcheckRegistry := healthcheck.NewRegistry()
healthcheckRegistry.Register("configServiceConnection", clusterCli)
infraServer := infraServer(localConfig, application)
metricsRegistry := metrics.DefaultRegistry
infraServer.Handle("/internal/metrics", metricsRegistry.MetricsHandler())
infraServer.Handle("/internal/metrics/descriptions", metricsRegistry.MetricsDescriptionHandler())
infraServer.Handle("/internal/health", healthcheckRegistry.Handler())
pprof.RegisterHandlers("/internal", infraServer)
application.Logger().Info(application.Context(),
"infra server handlers",
log.Any("infraServerHandlers", append([]string{
"/internal/metrics",
"/internal/metrics/descriptions",
"/internal/health",
}, pprof.Endpoints("/internal")...)),
)
application.AddClosers(app.CloserFunc(func() error {
sentryHub.Flush()
return nil
}))
tracingConfig := tracing.Config{
Enable: localConfig.Observability.Tracing.Enable,
Address: localConfig.Observability.Tracing.Address,
ModuleName: localConfig.ModuleName,
ModuleVersion: moduleVersion,
Environment: localConfig.Observability.Tracing.Environment,
InstanceId: localConfig.GrpcOuterAddress.IP,
Attributes: localConfig.Observability.Tracing.Attributes,
}
tracingProvider, err := tracing.NewProviderFromConfiguration(
application.Context(),
application.Logger(),
tracingConfig,
)
if err != nil {
err = errors.WithMessage(err, "new tracing provider, tracing will be disabled")
sentryHub.CatchError(application.Context(), err, log.ErrorLevel)
application.Logger().Error(application.Context(), err)
tracingProvider = tracing.NewNoopProvider()
}
tracing.DefaultProvider = tracingProvider
application.AddClosers(app.CloserFunc(func() error {
err := tracingProvider.Shutdown(context.Background())
if err != nil {
return errors.WithMessage(err, "shutdown tracing provider")
}
return nil
}))
return &Bootstrap{
App: application,
ClusterCli: clusterCli,
RemoteConfig: rc,
BindingAddress: bindingAddress,
ModuleName: localConfig.ModuleName,
MigrationsDir: migrationsDir,
InfraServer: infraServer,
MetricsRegistry: metricsRegistry,
HealthcheckRegistry: healthcheckRegistry,
SentryHub: sentryHub,
TracingProvider: tracingProvider,
}, nil
}
func (b *Bootstrap) Fatal(err error) {
b.SentryHub.CatchError(b.App.Context(), err, log.FatalLevel)
b.App.Close()
time.Sleep(500 * time.Millisecond)
b.App.Logger().Fatal(context.Background(), err)
}
func parseConfigServiceHosts(cfg ConfigServiceAddr) ([]string, error) {
hosts := strings.Split(cfg.IP, ";")
ports := strings.Split(cfg.Port, ";")
if len(hosts) != len(ports) {
return nil, errors.New("len(hosts) != len(ports)")
}
arr := make([]string, 0)
for i, host := range hosts {
arr = append(arr, net.JoinHostPort(host, ports[i]))
}
return arr, nil
}
func resolveHost(target string) (string, error) {
conn, err := net.Dial("udp", target)
if err != nil {
return "", err
}
defer conn.Close()
return conn.LocalAddr().(*net.UDPAddr).IP.To4().String(), nil
}
func readDefaultRemoteConfig(isDev bool, cfg LocalConfig) (json2.RawMessage, error) {
path, err := defaultRemoteConfigPath(isDev, cfg)
if err != nil {
return nil, errors.WithMessage(err, "resolve path")
}
data, err := os.ReadFile(path)
if err != nil {
return nil, errors.WithMessage(err, "read file")
}
remoteConfig := json2.RawMessage{}
err = json.Unmarshal(data, &remoteConfig)
if err != nil {
return nil, errors.WithMessage(err, "unmarshal json")
}
return remoteConfig, nil
}
func defaultRemoteConfigPath(isDev bool, cfg LocalConfig) (string, error) {
if cfg.DefaultRemoteConfigPath != "" {
return cfg.DefaultRemoteConfigPath, nil
}
if isDev {
return "conf/default_remote_config.json", nil
}
return relativePathFromBin("default_remote_config.json")
}
func configFilePath(isDev bool) (string, error) {
cfgPath := os.Getenv("APP_CONFIG_PATH")
if cfgPath != "" {
return cfgPath, nil
}
if isDev {
return "./conf/config_dev.yml", nil
}
return relativePathFromBin("config.yml")
}
func migrationsDirPath(isDev bool, cfg LocalConfig) (string, error) {
if cfg.MigrationsDirPath != "" {
return cfg.MigrationsDirPath, nil
}
if isDev {
return "./migrations", nil
}
return relativePathFromBin("migrations")
}
func relativePathFromBin(part string) (string, error) {
ex, err := os.Executable()
if err != nil {
return "", errors.WithMessage(err, "get executable path")
}
return path.Join(path.Dir(ex), part), nil
}
func infraServer(localConfig LocalConfig, application *app.Application) *infra.Server {
infraServer := infra.NewServer()
infraServerPort := localConfig.GrpcInnerAddress.Port + 1
if localConfig.InfraServerPort != 0 {
infraServerPort = localConfig.InfraServerPort
}
infraServerAddress := fmt.Sprintf(":%d", infraServerPort)
application.AddRunners(app.RunnerFunc(func(ctx context.Context) error {
application.Logger().Info(ctx, "run infra server", log.String("infraServerAddress", infraServerAddress))
err := infraServer.ListenAndServe(infraServerAddress)
if err != nil {
return errors.WithMessagef(err, "run infra server on %s", infraServerAddress)
}
return nil
}))
application.AddClosers(app.CloserFunc(func() error {
return infraServer.Shutdown()
}))
return infraServer
}
func localConfig(config *config.Config) (*LocalConfig, error) {
localConfig := LocalConfig{}
err := config.Read(&localConfig)
if err != nil {
return nil, errors.WithMessage(err, "read local config")
}
if localConfig.GrpcInnerAddress.Port != localConfig.GrpcOuterAddress.Port {
return nil, errors.Errorf("grpcInnerAddress.port is not equal grpcOuterAddress.port. potential mistake")
}
return &localConfig, nil
}
func kitVersion() string {
info, ok := debug.ReadBuildInfo()
if !ok {
return "0.0.0"
}
for _, dep := range info.Deps {
if dep.Path == "github.com/integration-system/isp-kit" {
return dep.Version
}
}
return "0.0.0"
}
func appConfig(isDev bool) (*app.Config, error) {
localConfigPath, err := configFilePath(isDev)
if err != nil {
return nil, errors.WithMessage(err, "resolve local config path")
}
configsOpts := []config.Option{
config.WithValidator(validator.Default),
config.WithEnvPrefix(os.Getenv("APP_CONFIG_ENV_PREFIX")),
}
if localConfigPath != "" {
configsOpts = append(configsOpts, config.WithExtraSource(config.NewYamlConfig(localConfigPath)))
}
logConfigSupplier := app.LoggerConfigSupplier(func(cfg *config.Config) log.Config {
initialLevel := log.InfoLevel
if isDev {
initialLevel = log.DebugLevel
}
var outputPaths []string
logFilePath := cfg.Optional().String("LOGFILE.PATH", "")
if !isDev && logFilePath != "" {
fileOutput := file.Output{
File: logFilePath,
MaxSizeMb: cfg.Optional().Int("LOGFILE.MAXSIZEMB", 512),
MaxDays: 0,
MaxBackups: cfg.Optional().Int("LOGFILE.MAXBACKUPS", 4),
Compress: cfg.Optional().Bool("LOGFILE.COMPRESS", true),
}
outputPaths = append(outputPaths, file.ConfigToUrl(fileOutput).String())
}
logCounter := app_metrics.NewLogCounter(metrics.DefaultRegistry)
var sampling *log.SamplingConfig
isEnableSampling := cfg.Optional().Bool("LOGS.SAMPLING.ENABLE", true)
if !isDev && isEnableSampling {
sampling = &log.SamplingConfig{
Initial: cfg.Optional().Int("LOGS.SAMPLING.MAXPERSECOND", 1000),
Thereafter: cfg.Optional().Int("LOGS.SAMPLING.PASSEVERY", 100),
Hook: logCounter.DroppedLogCounter(),
}
}
return log.Config{
IsInDevMode: isDev,
OutputPaths: outputPaths,
Sampling: sampling,
InitialLevel: initialLevel,
Hooks: []func(entry zapcore.Entry) error{
logCounter.SampledLogCounter(),
},
}
})
return &app.Config{
LoggerConfigSupplier: logConfigSupplier,
ConfigOptions: configsOpts,
}, nil
}