-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics.go
More file actions
153 lines (137 loc) · 5.06 KB
/
metrics.go
File metadata and controls
153 lines (137 loc) · 5.06 KB
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
package workers
import (
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var (
promMetricsMu sync.RWMutex
promMetricsCache = map[string]*prometheusMetrics{}
)
// Metrics collects worker lifecycle metrics.
// Implement this interface to provide custom metrics (e.g., Datadog, StatsD).
// Use BaseMetrics{} to disable metrics, or NewPrometheusMetrics for the built-in
// Prometheus implementation.
type Metrics interface {
WorkerStarted(name string)
WorkerStopped(name string)
WorkerPanicked(name string)
WorkerFailed(name string, err error)
WorkerRestarted(name string, attempt int)
ObserveRunDuration(name string, duration time.Duration)
SetActiveWorkers(count int)
}
// BaseMetrics provides no-op implementations of all Metrics methods.
// Embed it in custom Metrics implementations so that new methods added
// to the Metrics interface in future versions get safe no-op defaults
// instead of breaking your build:
//
// type myMetrics struct {
// workers.BaseMetrics // forward-compatible
// client *statsd.Client
// }
//
// func (m *myMetrics) WorkerStarted(name string) {
// m.client.Incr("worker.started", []string{"worker:" + name}, 1)
// }
type BaseMetrics struct{}
func (BaseMetrics) WorkerStarted(string) {}
func (BaseMetrics) WorkerStopped(string) {}
func (BaseMetrics) WorkerPanicked(string) {}
func (BaseMetrics) WorkerFailed(string, error) {}
func (BaseMetrics) WorkerRestarted(string, int) {}
func (BaseMetrics) ObserveRunDuration(string, time.Duration) {}
func (BaseMetrics) SetActiveWorkers(int) {}
// prometheusMetrics implements Metrics using Prometheus counters, histograms,
// and gauges registered via promauto.
type prometheusMetrics struct {
started *prometheus.CounterVec
stopped *prometheus.CounterVec
panicked *prometheus.CounterVec
failed *prometheus.CounterVec
restarted *prometheus.CounterVec
runDuration *prometheus.HistogramVec
activeCount prometheus.Gauge
}
// NewPrometheusMetrics creates a Metrics implementation backed by Prometheus.
// The namespace is prepended to all metric names (e.g., "myapp" →
// "myapp_worker_started_total"). Metrics are auto-registered with the
// default Prometheus registry. Safe to call multiple times with the same
// namespace — returns the cached instance. The cache is process-global;
// use a small number of static namespaces (not per-request/tenant values).
func NewPrometheusMetrics(namespace string) Metrics {
promMetricsMu.RLock()
if m, ok := promMetricsCache[namespace]; ok {
promMetricsMu.RUnlock()
return m
}
promMetricsMu.RUnlock()
promMetricsMu.Lock()
defer promMetricsMu.Unlock()
// Double-check after acquiring write lock.
if m, ok := promMetricsCache[namespace]; ok {
return m
}
m := &prometheusMetrics{
started: promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "worker_started_total",
Help: "Total number of worker starts.",
}, []string{"worker"}),
stopped: promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "worker_stopped_total",
Help: "Total number of worker stops.",
}, []string{"worker"}),
panicked: promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "worker_panicked_total",
Help: "Total number of worker panics.",
}, []string{"worker"}),
failed: promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "worker_failed_total",
Help: "Total number of worker failures.",
}, []string{"worker"}),
restarted: promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "worker_restarted_total",
Help: "Total number of worker restarts.",
}, []string{"worker"}),
runDuration: promauto.NewHistogramVec(prometheus.HistogramOpts{
Namespace: namespace,
Name: "worker_run_duration_seconds",
Help: "Duration of worker run cycles in seconds.",
Buckets: prometheus.DefBuckets,
}, []string{"worker"}),
activeCount: promauto.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "worker_active_count",
Help: "Number of currently active workers.",
}),
}
promMetricsCache[namespace] = m
return m
}
func (p *prometheusMetrics) WorkerStarted(name string) {
p.started.WithLabelValues(name).Inc()
}
func (p *prometheusMetrics) WorkerStopped(name string) {
p.stopped.WithLabelValues(name).Inc()
}
func (p *prometheusMetrics) WorkerPanicked(name string) {
p.panicked.WithLabelValues(name).Inc()
}
func (p *prometheusMetrics) WorkerFailed(name string, _ error) {
p.failed.WithLabelValues(name).Inc()
}
func (p *prometheusMetrics) WorkerRestarted(name string, _ int) {
p.restarted.WithLabelValues(name).Inc()
}
func (p *prometheusMetrics) ObserveRunDuration(name string, duration time.Duration) {
p.runDuration.WithLabelValues(name).Observe(duration.Seconds())
}
func (p *prometheusMetrics) SetActiveWorkers(count int) {
p.activeCount.Set(float64(count))
}