-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
184 lines (135 loc) · 5.92 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
180
181
182
183
184
// package main reads & validates configuration for the proxy service
// and if the config is valid starts and monitors an instance of the
// proxy service and any background routines
package main
import (
"context"
"errors"
"fmt"
"github.com/kava-labs/kava-proxy-service/config"
"github.com/kava-labs/kava-proxy-service/logging"
"github.com/kava-labs/kava-proxy-service/routines"
"github.com/kava-labs/kava-proxy-service/service"
)
var (
serviceConfig config.Config
serviceLogger logging.ServiceLogger
serviceContext = context.Background()
)
func init() {
serviceConfig = config.ReadConfig()
err := config.Validate(serviceConfig)
if err != nil {
panic(err)
}
serviceLogger, err = logging.New(serviceConfig.LogLevel)
if err != nil {
panic(err)
}
}
func startMetricPartitioningRoutine(serviceConfig config.Config, service service.ProxyService, serviceLogger logging.ServiceLogger) <-chan error {
metricPartitioningRoutineConfig := routines.MetricPartitioningRoutineConfig{
Interval: serviceConfig.MetricPartitioningRoutineInterval,
DelayFirstRun: serviceConfig.MetricPartitioningRoutineDelayFirstRun,
PrefillPeriodDays: serviceConfig.MetricPartitioningPrefillPeriodDays,
Database: service.Database,
Logger: serviceLogger,
}
metricPartitioningRoutine, err := routines.NewMetricPartitioningRoutine(metricPartitioningRoutineConfig)
if err != nil {
serviceLogger.Error().Msg(fmt.Sprintf("error %s creating metric partitioning routine with config %+v", err, metricPartitioningRoutineConfig))
return nil
}
errChan, err := metricPartitioningRoutine.Run()
if err != nil {
serviceLogger.Error().Msg(fmt.Sprintf("error %s starting metric partitioning routine with config %+v", err, metricPartitioningRoutineConfig))
return nil
}
serviceLogger.Debug().Msg(fmt.Sprintf("started metric partitioning routine with config %+v", metricPartitioningRoutineConfig))
return errChan
}
func startMetricCompactionRoutine(serviceConfig config.Config, service service.ProxyService, serviceLogger logging.ServiceLogger) <-chan error {
metricCompactionRoutineConfig := routines.MetricCompactionRoutineConfig{
Interval: serviceConfig.MetricCompactionRoutineInterval,
Database: service.Database,
Logger: serviceLogger,
}
metricCompactionRoutine, err := routines.NewMetricCompactionRoutine(metricCompactionRoutineConfig)
if err != nil {
serviceLogger.Error().Msg(fmt.Sprintf("error %s creating metric compaction routine with config %+v", err, metricCompactionRoutineConfig))
return nil
}
errChan, err := metricCompactionRoutine.Run()
if err != nil {
serviceLogger.Error().Msg(fmt.Sprintf("error %s starting metric compaction routine with config %+v", err, metricCompactionRoutineConfig))
return nil
}
serviceLogger.Debug().Msg(fmt.Sprintf("started metric compaction routine with config %+v", metricCompactionRoutineConfig))
return errChan
}
func startMetricPruningRoutine(serviceConfig config.Config, service service.ProxyService, serviceLogger logging.ServiceLogger) <-chan error {
if !serviceConfig.MetricPruningEnabled {
serviceLogger.Info().Msg("skipping starting metric pruning routine since it is disabled via config")
return make(<-chan error)
}
metricPruningRoutineConfig := routines.MetricPruningRoutineConfig{
Interval: serviceConfig.MetricPruningRoutineInterval,
StartDelay: serviceConfig.MetricPartitioningRoutineDelayFirstRun,
MaxRequestMetricsHistoryDays: int64(serviceConfig.MetricPruningMaxRequestMetricsHistoryDays),
Database: service.Database,
Logger: serviceLogger,
}
metricPruningRoutine, err := routines.NewMetricPruningRoutine(metricPruningRoutineConfig)
if err != nil {
serviceLogger.Error().Msg(fmt.Sprintf("error %s creating metric pruning routine with config %+v", err, metricPruningRoutineConfig))
return nil
}
errChan, err := metricPruningRoutine.Run()
if err != nil {
serviceLogger.Error().Msg(fmt.Sprintf("error %s starting metric pruning routine with config %+v", err, metricPruningRoutineConfig))
return nil
}
serviceLogger.Debug().Msg(fmt.Sprintf("started metric pruning routine with config %+v", metricPruningRoutineConfig))
return errChan
}
func main() {
serviceLogger.Debug().Msg(fmt.Sprintf("initial config: %+v", serviceConfig))
// create the main proxy service
service, err := service.New(serviceContext, serviceConfig, &serviceLogger)
serviceLogger.Trace().Msg(fmt.Sprintf("service initialized: %+v", service))
if err != nil {
serviceLogger.Panic().Msg(fmt.Sprintf("%v", errors.Unwrap(err)))
}
// if we use metrics with database, we run some background routines
if serviceConfig.MetricDatabaseEnabled {
// configure and run background routines
// metric partitioning routine
go func() {
metricPartitioningErrs := startMetricPartitioningRoutine(serviceConfig, service, serviceLogger)
for routineErr := range metricPartitioningErrs {
serviceLogger.Error().Msg(fmt.Sprintf("metric partitioning routine encountered error %s", routineErr))
}
}()
// metric compaction routine
go func() {
metricCompactionErrs := startMetricCompactionRoutine(serviceConfig, service, serviceLogger)
for routineErr := range metricCompactionErrs {
serviceLogger.Error().Msg(fmt.Sprintf("metric compaction routine encountered error %s", routineErr))
}
}()
// metric pruning routine
go func() {
metricPruningErrs := startMetricPruningRoutine(serviceConfig, service, serviceLogger)
for routineErr := range metricPruningErrs {
serviceLogger.Error().Msg(fmt.Sprintf("metric pruning routine encountered error %s", routineErr))
}
}()
} else {
serviceLogger.Info().Msg("skipping starting metric partitioning, compaction, and pruning routines since metric database is disabled")
}
// run the proxy service
finalErr := service.Run()
if finalErr != nil {
serviceLogger.Debug().Msg(fmt.Sprintf("service stopped with error %s", finalErr))
}
}