-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.go
249 lines (206 loc) · 7.31 KB
/
cli.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
// cli.go contains types, functions and methods for creating
// and updating the command line interface (CLI) to the doctor program
package main
import (
"context"
"fmt"
"log"
"github.com/kava-labs/doctor/collect"
dconfig "github.com/kava-labs/doctor/config"
"github.com/kava-labs/doctor/metric"
)
// CLIConfig wraps values
// used to configure the CLI
// display mode of the doctor program
type CLIConfig struct {
KavaURL string
MaxMetricSamplesToRetainPerNode int
MetricSamplesForSyntheticMetricCalculation int
MetricCollectors []string
AWSRegion string
MetricNamespace string
Logger *log.Logger
}
// CLI controls the display
// mode of the doctor program
// using either stdout or file based
// output devices
type CLI struct {
kavaEndpoint *Endpoint
*log.Logger
metricCollectors []collect.Collector
}
// Watch watches for new measurements and log messages for the kava node with the
// specified rpc api url, outputting them to the cli device in the desired format
func (c *CLI) Watch(metricReadOnlyChannels MetricReadOnlyChannels, logMessages <-chan string, kavaNodeRPCURL string) error {
// handle logging in separate go-routines to avoid
// congestion with metric event emission
go func() {
for logMessage := range logMessages {
c.Println(logMessage)
}
}()
// event handlers for non-interactive mode
// loop over events
for {
select {
case syncStatusMetrics := <-metricReadOnlyChannels.SyncStatusMetrics:
// record sample in-memory for use in synthetic metric calculation
c.kavaEndpoint.AddSample(syncStatusMetrics.NodeId, NodeMetrics{
SyncStatusMetrics: &syncStatusMetrics,
})
// calculate hash rate for this node
nodeId := syncStatusMetrics.NodeId
hashRatePerSecond, err := c.kavaEndpoint.CalculateNodeHashRatePerSecond(nodeId)
if err != nil {
c.Printf("error %s calculating hash rate for node %s\n", err, nodeId)
}
latestBlockHeight := syncStatusMetrics.SyncStatus.LatestBlockHeight
secondsBehindLive := syncStatusMetrics.SecondsBehindLive
syncStatusLatencyMilliseconds := syncStatusMetrics.SampleLatencyMilliseconds
// log to stdout
fmt.Printf("%s node %s is synched up to block %d, %d seconds behind live, hashing %f blocks per second, status check took %d milliseconds\n", kavaNodeRPCURL, nodeId, latestBlockHeight, secondsBehindLive, hashRatePerSecond, syncStatusLatencyMilliseconds)
// collect metrics to external storage backends
var metrics []metric.Metric
hashRateMetric := metric.Metric{
Name: "BlocksHashedPerSecond",
Dimensions: map[string]string{
"node_id": nodeId,
},
Data: metric.HashRateMetric{
NodeId: nodeId,
BlocksPerSecond: hashRatePerSecond,
},
Value: float64(hashRatePerSecond),
Timestamp: syncStatusMetrics.SampledAt,
CollectToFile: true,
CollectToCloudwatch: true,
}
metrics = append(metrics, hashRateMetric)
syncStatusMetric := metric.Metric{
Name: "SyncStatus",
Dimensions: map[string]string{
"node_id": nodeId,
},
Data: syncStatusMetrics,
Timestamp: syncStatusMetrics.SampledAt,
CollectToFile: true,
CollectToCloudwatch: false,
}
metrics = append(metrics, syncStatusMetric)
latestBlockHeightMetric := metric.Metric{
Name: "LatestBlockHeight",
Dimensions: map[string]string{
"node_id": nodeId,
},
Value: float64(latestBlockHeight),
Timestamp: syncStatusMetrics.SampledAt,
CollectToFile: false,
CollectToCloudwatch: true,
}
metrics = append(metrics, latestBlockHeightMetric)
secondsBehindLiveMetric := metric.Metric{
Name: "SecondsBehindLive",
Dimensions: map[string]string{
"node_id": nodeId,
},
Value: float64(secondsBehindLive),
Timestamp: syncStatusMetrics.SampledAt,
CollectToFile: false,
CollectToCloudwatch: true,
}
metrics = append(metrics, secondsBehindLiveMetric)
statusCheckMillisecondLatencyMetric := metric.Metric{
Name: "StatusCheckLatencyMilliseconds",
Dimensions: map[string]string{
"node_id": nodeId,
},
Value: float64(syncStatusLatencyMilliseconds),
Timestamp: syncStatusMetrics.SampledAt,
CollectToFile: false,
CollectToCloudwatch: true,
}
metrics = append(metrics, statusCheckMillisecondLatencyMetric)
for _, collector := range c.metricCollectors {
for _, metric := range metrics {
err := collector.Collect(metric)
if err != nil {
c.Printf("error %s collecting metric %+v\n", err, metric)
}
}
}
case uptimeMetric := <-metricReadOnlyChannels.UptimeMetrics:
endpointURL := uptimeMetric.EndpointURL
// record sample in-memory for use in synthetic metric calculation
c.kavaEndpoint.AddSample(endpointURL, NodeMetrics{
UptimeMetric: &uptimeMetric,
})
// calculate uptime
uptime, err := c.kavaEndpoint.CalculateUptime(endpointURL)
if err != nil {
c.Printf(fmt.Sprintf("error %s calculating uptime for %s\n", err, endpointURL))
continue
}
// log to stdout
fmt.Printf("%s uptime %f%% \n", endpointURL, uptime*100)
// collect metrics to external storage backends
var metrics []metric.Metric
uptimeMetric.RollingAveragePercentAvailable = uptime * 100
uptimeMetricForCollection := metric.Metric{
Name: "Uptime",
Dimensions: map[string]string{
"endpoint_url": endpointURL,
},
Data: uptimeMetric,
Value: float64(uptimeMetric.RollingAveragePercentAvailable),
Timestamp: uptimeMetric.SampledAt,
CollectToFile: true,
CollectToCloudwatch: true,
}
metrics = append(metrics, uptimeMetricForCollection)
for _, collector := range c.metricCollectors {
for _, metric := range metrics {
err := collector.Collect(metric)
if err != nil {
c.Printf("error %s collecting metric %+v\n", err, metric)
}
}
}
}
}
}
// NewCLI creates and returns a new cli
// using the provided configuration and error (if any)
func NewCLI(config CLIConfig) (*CLI, error) {
endpoint := NewEndpoint(EndpointConfig{URL: config.KavaURL,
MetricSamplesToKeepPerNode: config.MaxMetricSamplesToRetainPerNode,
MetricSamplesForSyntheticMetricCalculation: config.MetricSamplesForSyntheticMetricCalculation,
})
collectors := []collect.Collector{}
for _, collector := range config.MetricCollectors {
switch collector {
case dconfig.FileMetricCollector:
fileCollector, err := collect.NewFileCollector(collect.FileCollectorConfig{})
if err != nil {
return nil, err
}
collectors = append(collectors, fileCollector)
case dconfig.CloudwatchMetricCollector:
cloudwatchConfig := collect.CloudWatchCollectorConfig{
Ctx: context.Background(),
AWSRegion: config.AWSRegion,
MetricNamespace: config.MetricNamespace,
}
cloudwatchCollector, err := collect.NewCloudWatchCollector(cloudwatchConfig)
if err != nil {
return nil, err
}
collectors = append(collectors, cloudwatchCollector)
}
}
return &CLI{
kavaEndpoint: endpoint,
Logger: config.Logger,
metricCollectors: collectors,
}, nil
}