-
Notifications
You must be signed in to change notification settings - Fork 17
/
monitor.go
182 lines (154 loc) · 4.86 KB
/
monitor.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
package monitoring
import (
"context"
"fmt"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/smartcontractkit/chainlink-common/pkg/logger"
commonMonitor "github.com/smartcontractkit/chainlink-common/pkg/monitoring"
"github.com/smartcontractkit/chainlink-common/pkg/monitoring/config"
)
// Builds monitor instance with only the prometheus exporter.
// Does not contain kafka exporter.
func NewMonitorPrometheusOnly(
rootCtx context.Context,
log commonMonitor.Logger,
chainConfig commonMonitor.ChainConfig,
envelopeSourceFactory commonMonitor.SourceFactory,
txResultsSourceFactory commonMonitor.SourceFactory,
feedsParser commonMonitor.FeedsParser,
nodesParser commonMonitor.NodesParser,
) (*commonMonitor.Monitor, error) {
cfg, err := ParseWithoutKafka()
if err != nil {
return nil, fmt.Errorf("failed to parse generic configuration: %w", err)
}
metrics := commonMonitor.NewMetrics(logger.With(log, "component", "metrics"))
chainMetrics := commonMonitor.NewChainMetrics(chainConfig)
sourceFactories := []commonMonitor.SourceFactory{envelopeSourceFactory, txResultsSourceFactory}
prometheusExporterFactory := commonMonitor.NewPrometheusExporterFactory(
logger.With(log, "component", "prometheus-exporter"),
metrics,
)
exporterFactories := []commonMonitor.ExporterFactory{prometheusExporterFactory}
rddSource := commonMonitor.NewRDDSource(
cfg.Feeds.URL, feedsParser, cfg.Feeds.IgnoreIDs,
cfg.Nodes.URL, nodesParser,
logger.With(log, "component", "rdd-source"),
)
rddPoller := commonMonitor.NewSourcePoller(
rddSource,
logger.With(log, "component", "rdd-poller"),
cfg.Feeds.RDDPollInterval,
cfg.Feeds.RDDReadTimeout,
0, // no buffering!
)
manager := commonMonitor.NewManager(
logger.With(log, "component", "manager"),
rddPoller,
)
// Configure HTTP server
httpServer := commonMonitor.NewHTTPServer(rootCtx, cfg.HTTP.Address, logger.With(log, "component", "http-server"))
httpServer.Handle("/metrics", metrics.HTTPHandler())
httpServer.Handle("/debug", manager.HTTPHandler())
// Required for k8s.
httpServer.Handle("/health", http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
}))
return &commonMonitor.Monitor{
RootContext: rootCtx,
ChainConfig: chainConfig,
Config: cfg,
Log: log,
// no kafka
Producer: nil,
Metrics: metrics,
ChainMetrics: chainMetrics,
// no kafka
SchemaRegistry: nil,
SourceFactories: sourceFactories,
ExporterFactories: exporterFactories,
RDDSource: rddSource,
RDDPoller: rddPoller,
Manager: manager,
HTTPServer: httpServer,
}, nil
}
type MyConfig = config.Config
func ParseWithoutKafka() (MyConfig, error) {
cfg := MyConfig{}
if err := parseConfigEnvVars(&cfg); err != nil {
return cfg, err
}
applyConfigDefaults(&cfg)
err := validateConfigWithoutKafka(cfg)
return cfg, err
}
func applyConfigDefaults(cfg *MyConfig) {
if cfg.Feeds.RDDReadTimeout == 0 {
cfg.Feeds.RDDReadTimeout = 1 * time.Second
}
if cfg.Feeds.RDDPollInterval == 0 {
cfg.Feeds.RDDPollInterval = 10 * time.Second
}
}
func parseConfigEnvVars(cfg *MyConfig) error {
if value, isPresent := os.LookupEnv("FEEDS_URL"); isPresent {
cfg.Feeds.URL = value
}
if value, isPresent := os.LookupEnv("FEEDS_RDD_READ_TIMEOUT"); isPresent {
readTimeout, err := time.ParseDuration(value)
if err != nil {
return fmt.Errorf("failed to parse env var FEEDS_RDD_READ_TIMEOUT, see https://pkg.go.dev/time#ParseDuration: %w", err)
}
cfg.Feeds.RDDReadTimeout = readTimeout
}
if value, isPresent := os.LookupEnv("FEEDS_RDD_POLL_INTERVAL"); isPresent {
pollInterval, err := time.ParseDuration(value)
if err != nil {
return fmt.Errorf("failed to parse env var FEEDS_RDD_POLL_INTERVAL, see https://pkg.go.dev/time#ParseDuration: %w", err)
}
cfg.Feeds.RDDPollInterval = pollInterval
}
if value, isPresent := os.LookupEnv("FEEDS_IGNORE_IDS"); isPresent {
ids := strings.Split(value, ",")
for _, id := range ids {
if id == "" {
continue
}
cfg.Feeds.IgnoreIDs = append(cfg.Feeds.IgnoreIDs, strings.TrimSpace(id))
}
}
if value, isPresent := os.LookupEnv("NODES_URL"); isPresent {
cfg.Nodes.URL = value
}
if value, isPresent := os.LookupEnv("HTTP_ADDRESS"); isPresent {
cfg.HTTP.Address = value
}
return nil
}
func validateConfigWithoutKafka(cfg MyConfig) error {
// Required config
for envVarName, currentValue := range map[string]string{
"FEEDS_URL": cfg.Feeds.URL,
"NODES_URL": cfg.Nodes.URL,
"HTTP_ADDRESS": cfg.HTTP.Address,
} {
if currentValue == "" {
return fmt.Errorf("'%s' env var is required", envVarName)
}
}
// Validate URLs.
for envVarName, currentValue := range map[string]string{
"FEEDS_URL": cfg.Feeds.URL,
"NODES_URL": cfg.Nodes.URL,
} {
if _, err := url.ParseRequestURI(currentValue); err != nil {
return fmt.Errorf("%s='%s' is not a valid URL: %w", envVarName, currentValue, err)
}
}
return nil
}