/
metrics_reporter.go
71 lines (60 loc) · 1.57 KB
/
metrics_reporter.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
package metrics_reporter
import (
"os"
"time"
"code.cloudfoundry.org/cf-tcp-router/metrics_reporter/haproxy_client"
"code.cloudfoundry.org/cf-tcp-router/models"
"code.cloudfoundry.org/clock"
)
type MetricsReport struct {
TotalCurrentQueuedRequests uint64
TotalBackendConnectionErrors uint64
AverageQueueTimeMs uint64
AverageConnectTimeMs uint64
ProxyMetrics map[models.RoutingKey]ProxyStats
RouteErrorMap map[string]uint64
}
type RouteErrorReport struct {
RouteErrors map[models.RoutingKey]uint64
}
type ProxyStats struct {
ConnectionTime uint64
CurrentSessions uint64
}
type MetricsReporter struct {
clock clock.Clock
emitInterval time.Duration
haproxyClient haproxy_client.HaproxyClient
metricsEmitter MetricsEmitter
}
func NewMetricsReporter(clock clock.Clock, haproxyClient haproxy_client.HaproxyClient, metricsEmitter MetricsEmitter, interval time.Duration) *MetricsReporter {
return &MetricsReporter{
clock: clock,
haproxyClient: haproxyClient,
metricsEmitter: metricsEmitter,
emitInterval: interval,
}
}
func (r *MetricsReporter) Run(signals <-chan os.Signal, ready chan<- struct{}) error {
ticker := r.clock.NewTicker(r.emitInterval)
close(ready)
for {
select {
case <-ticker.C():
r.emitStats()
case <-signals:
ticker.Stop()
return nil
}
}
}
func (r *MetricsReporter) emitStats() {
// get stats
stats := r.haproxyClient.GetStats()
if len(stats) > 0 {
// convert to report
report := Convert(stats)
// emit to firehose
r.metricsEmitter.Emit(report)
}
}