|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +package cmd |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/microsoft/retina/cmd/standalone" |
| 11 | + "github.com/microsoft/retina/cmd/standard" |
| 12 | + "github.com/microsoft/retina/internal/buildinfo" |
| 13 | + "github.com/microsoft/retina/pkg/config" |
| 14 | + "github.com/microsoft/retina/pkg/log" |
| 15 | + "github.com/microsoft/retina/pkg/telemetry" |
| 16 | + "go.uber.org/zap" |
| 17 | +) |
| 18 | + |
| 19 | +const ( |
| 20 | + logFileName = "retina.log" |
| 21 | +) |
| 22 | + |
| 23 | +type BootstrapManager struct { |
| 24 | + metricsAddr string |
| 25 | + probeAddr string |
| 26 | + enableLeaderElection bool |
| 27 | + configFile string |
| 28 | +} |
| 29 | + |
| 30 | +func NewBootstrapManager(metricsAddr, probeAddr, configFile string, enableLeaderElection bool) *BootstrapManager { |
| 31 | + return &BootstrapManager{ |
| 32 | + metricsAddr: metricsAddr, |
| 33 | + probeAddr: probeAddr, |
| 34 | + enableLeaderElection: enableLeaderElection, |
| 35 | + configFile: configFile, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func (b *BootstrapManager) Start() error { |
| 40 | + if buildinfo.ApplicationInsightsID != "" { |
| 41 | + telemetry.InitAppInsights(buildinfo.ApplicationInsightsID, buildinfo.Version) |
| 42 | + defer telemetry.ShutdownAppInsights() |
| 43 | + defer telemetry.TrackPanic() |
| 44 | + } |
| 45 | + |
| 46 | + daemonConfig, err := config.GetConfig(b.configFile) |
| 47 | + if err != nil { |
| 48 | + panic(err) |
| 49 | + } |
| 50 | + |
| 51 | + fmt.Println("init logger") |
| 52 | + zl, err := log.SetupZapLogger(&log.LogOpts{ |
| 53 | + Level: daemonConfig.LogLevel, |
| 54 | + File: false, |
| 55 | + FileName: logFileName, |
| 56 | + MaxFileSizeMB: 100, //nolint:gomnd // defaults |
| 57 | + MaxBackups: 3, //nolint:gomnd // defaults |
| 58 | + MaxAgeDays: 30, //nolint:gomnd // defaults |
| 59 | + ApplicationInsightsID: buildinfo.ApplicationInsightsID, |
| 60 | + EnableTelemetry: daemonConfig.EnableTelemetry, |
| 61 | + }, |
| 62 | + zap.String("version", buildinfo.Version), |
| 63 | + zap.String("plugins", strings.Join(daemonConfig.EnabledPlugin, `,`)), |
| 64 | + zap.String("data aggregation level", daemonConfig.DataAggregationLevel.String()), |
| 65 | + ) |
| 66 | + if err != nil { |
| 67 | + panic(err) |
| 68 | + } |
| 69 | + defer zl.Close() |
| 70 | + |
| 71 | + if daemonConfig.EnableStandalone { |
| 72 | + sd := standalone.NewDaemon(daemonConfig) |
| 73 | + if err = sd.Start(zl); err != nil { |
| 74 | + return fmt.Errorf("starting standalone daemon: %w", err) |
| 75 | + } |
| 76 | + return nil |
| 77 | + } |
| 78 | + |
| 79 | + d := standard.NewDaemon(daemonConfig, b.metricsAddr, b.probeAddr, b.enableLeaderElection) |
| 80 | + if err := d.Start(zl); err != nil { |
| 81 | + return fmt.Errorf("starting daemon: %w", err) |
| 82 | + } |
| 83 | + return nil |
| 84 | +} |
0 commit comments