-
Notifications
You must be signed in to change notification settings - Fork 8
/
metrics.go
114 lines (99 loc) · 4.03 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
package roadrunner_temporal //nolint:revive,stylecheck
import (
"io"
"time"
"github.com/cactus/go-statsd-client/statsd"
prom "github.com/prometheus/client_golang/prometheus"
"github.com/roadrunner-server/api/v2/plugins/informer"
"github.com/roadrunner-server/sdk/v2/metrics"
"github.com/uber-go/tally/v4"
"github.com/uber-go/tally/v4/prometheus"
statsdreporter "go.temporal.io/server/common/metrics/tally/statsd"
"go.uber.org/zap"
)
func (p *Plugin) MetricsCollector() []prom.Collector {
// p - implements Exporter interface (workers)
// other - request duration and count
return []prom.Collector{p.statsExporter}
}
const (
namespace = "rr_temporal"
)
func newStatsExporter(stats informer.Informer) *metrics.StatsExporter {
return &metrics.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 metrics name we
// use 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
}