-
Notifications
You must be signed in to change notification settings - Fork 9
/
metrics.go
210 lines (179 loc) · 6.65 KB
/
metrics.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
package rrtemporal
import (
"io"
"strconv"
"time"
"github.com/cactus/go-statsd-client/v5/statsd"
prom "github.com/prometheus/client_golang/prometheus"
"github.com/roadrunner-server/errors"
"github.com/roadrunner-server/pool/fsm"
"github.com/roadrunner-server/pool/state/process"
"github.com/uber-go/tally/v4"
"github.com/uber-go/tally/v4/prometheus"
tclient "go.temporal.io/sdk/client"
ttally "go.temporal.io/sdk/contrib/tally" // temporal tally hanlders
statsdreporter "go.temporal.io/server/common/metrics/tally/statsd"
"go.uber.org/zap"
)
const (
namespace = "rr_temporal"
)
func (p *Plugin) MetricsCollector() []prom.Collector {
// p - implements Exporter interface (workers)
// other - request duration and count
return []prom.Collector{p.statsExporter}
}
// Informer used to get workers from a particular plugin or set of plugins
type Informer interface {
Workers() []*process.State
}
func newStatsExporter(stats Informer) *StatsExporter {
return &StatsExporter{
TotalMemoryDesc: prom.NewDesc(prom.BuildFQName(namespace, "", "workers_memory_bytes"), "Memory usage by workers", nil, nil),
StateDesc: prom.NewDesc(prom.BuildFQName(namespace, "", "worker_state"), "Worker current state", []string{"state", "pid"}, nil),
WorkerMemoryDesc: prom.NewDesc(prom.BuildFQName(namespace, "", "worker_memory_bytes"), "Worker current memory usage", []string{"pid"}, nil),
TotalWorkersDesc: prom.NewDesc(prom.BuildFQName(namespace, "", "total_workers"), "Total number of workers used by the plugin", nil, nil),
WorkersReady: prom.NewDesc(prom.BuildFQName(namespace, "", "workers_ready"), "Workers currently in ready state", nil, nil),
WorkersWorking: prom.NewDesc(prom.BuildFQName(namespace, "", "workers_working"), "Workers currently in working state", nil, nil),
WorkersInvalid: prom.NewDesc(prom.BuildFQName(namespace, "", "workers_invalid"), "Workers currently in invalid,killing,destroyed,errored,inactive states", nil, nil),
Workers: stats,
}
}
func newPrometheusScope(c prometheus.Configuration, prefix string, log *zap.Logger) (tally.Scope, io.Closer, error) {
reporter, err := c.NewReporter(
prometheus.ConfigurationOptions{
Registry: prom.NewRegistry(),
OnError: func(err error) {
log.Error("prometheus registry", zap.Error(err))
},
},
)
if err != nil {
return nil, nil, err
}
// tally sanitizer options that satisfy Prometheus restrictions.
// This will rename metrics at the tally emission level, so the metrics name we use is
// maybe different from what gets emitted. In the current implementation
// it will replace - and . with _
sanitizeOptions := tally.SanitizeOptions{
NameCharacters: tally.ValidCharacters{
Ranges: tally.AlphanumericRange,
Characters: []rune{'_'},
},
KeyCharacters: tally.ValidCharacters{
Ranges: tally.AlphanumericRange,
Characters: []rune{'_'},
},
ValueCharacters: tally.ValidCharacters{
Ranges: tally.AlphanumericRange,
Characters: []rune{'_'},
},
ReplacementCharacter: tally.DefaultReplacementCharacter,
}
scopeOpts := tally.ScopeOptions{
CachedReporter: reporter,
Separator: prometheus.DefaultSeparator,
SanitizeOptions: &sanitizeOptions,
Prefix: prefix,
}
scope, closer := tally.NewRootScope(scopeOpts, time.Second)
return scope, closer, nil
}
// ref: https://github.dev/temporalio/temporal/common/metrics/config.go:391
func newStatsdScope(statsdConfig *Statsd) (tally.Scope, io.Closer, error) {
st, err := statsd.NewClientWithConfig(&statsd.ClientConfig{
Address: statsdConfig.HostPort,
Prefix: statsdConfig.Prefix,
UseBuffered: true,
FlushInterval: statsdConfig.FlushInterval,
FlushBytes: statsdConfig.FlushBytes,
})
if err != nil {
return nil, nil, err
}
// NOTE: according to (https://github.com/uber-go/tally) Tally's statsd implementation doesn't support tagging.
// Therefore, we implement Tally interface to have a statsd reporter that can support tagging
opts := statsdreporter.Options{
TagSeparator: statsdConfig.TagSeparator,
}
reporter := statsdreporter.NewReporter(st, opts)
scopeOpts := tally.ScopeOptions{
Tags: statsdConfig.Tags,
Prefix: statsdConfig.Prefix,
Reporter: reporter,
}
scope, closer := tally.NewRootScope(scopeOpts, time.Second)
return scope, closer, nil
}
// init RR metrics
func initMetrics(cfg *Config, log *zap.Logger) (tclient.MetricsHandler, io.Closer, error) {
switch cfg.Metrics.Driver {
case driverPrometheus:
ms, cl, err := newPrometheusScope(prometheus.Configuration{
ListenAddress: cfg.Metrics.Prometheus.Address,
TimerType: cfg.Metrics.Prometheus.Type,
}, cfg.Metrics.Prometheus.Prefix, log)
if err != nil {
return nil, nil, err
}
return ttally.NewMetricsHandler(ms), cl, nil
case driverStatsd:
ms, cl, err := newStatsdScope(cfg.Metrics.Statsd)
if err != nil {
return nil, nil, err
}
return ttally.NewMetricsHandler(ms), cl, nil
default:
return nil, nil, errors.Errorf("unknown driver provided: %s", cfg.Metrics.Driver)
}
}
type StatsExporter struct {
TotalMemoryDesc *prom.Desc
StateDesc *prom.Desc
WorkerMemoryDesc *prom.Desc
TotalWorkersDesc *prom.Desc
WorkersReady *prom.Desc
WorkersWorking *prom.Desc
WorkersInvalid *prom.Desc
Workers Informer
}
func (s *StatsExporter) Describe(d chan<- *prom.Desc) {
// send description
d <- s.TotalWorkersDesc
d <- s.TotalMemoryDesc
d <- s.StateDesc
d <- s.WorkerMemoryDesc
d <- s.WorkersReady
d <- s.WorkersWorking
d <- s.WorkersInvalid
}
func (s *StatsExporter) Collect(ch chan<- prom.Metric) {
// get the copy of the processes
workerStates := s.Workers.Workers()
// cumulative RSS memory in bytes
var cum float64
var ready float64
var working float64
var invalid float64
// collect the memory
for i := 0; i < len(workerStates); i++ {
cum += float64(workerStates[i].MemoryUsage)
ch <- prom.MustNewConstMetric(s.StateDesc, prom.GaugeValue, 0, workerStates[i].StatusStr, strconv.Itoa(int(workerStates[i].Pid)))
ch <- prom.MustNewConstMetric(s.WorkerMemoryDesc, prom.GaugeValue, float64(workerStates[i].MemoryUsage), strconv.Itoa(int(workerStates[i].Pid)))
// sync with sdk/worker/state.go
switch workerStates[i].Status {
case fsm.StateReady:
ready++
case fsm.StateWorking:
working++
default:
invalid++
}
}
ch <- prom.MustNewConstMetric(s.WorkersReady, prom.GaugeValue, ready)
ch <- prom.MustNewConstMetric(s.WorkersWorking, prom.GaugeValue, working)
ch <- prom.MustNewConstMetric(s.WorkersInvalid, prom.GaugeValue, invalid)
// send the values to the prometheus
ch <- prom.MustNewConstMetric(s.TotalWorkersDesc, prom.GaugeValue, float64(len(workerStates)))
ch <- prom.MustNewConstMetric(s.TotalMemoryDesc, prom.GaugeValue, cum)
}