forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.go
353 lines (311 loc) · 11.8 KB
/
stats.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
package pf
import (
"fmt"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/apache/pulsar/pulsar-function-go/logutil"
"github.com/prometheus/client_golang/prometheus"
prometheus_client "github.com/prometheus/client_model/go"
)
var (
metricsLabelNames = []string{"tenant", "namespace", "name", "instance_id", "cluster", "fqfn"}
exceptionLabelNames = []string{"error"}
exceptionMetricsLabelNames = append(metricsLabelNames, exceptionLabelNames...)
)
const (
PulsarFunctionMetricsPrefix = "pulsar_function_"
TotalSuccessfullyProcessed = "processed_successfully_total"
TotalSystemExceptions = "system_exceptions_total"
TotalUserExceptions = "user_exceptions_total"
ProcessLatencyMs = "process_latency_ms"
LastInvocation = "last_invocation"
TotalReceived = "received_total"
TotalSuccessfullyProcessed1min = "processed_successfully_total_1min"
TotalSystemExceptions1min = "system_exceptions_total_1min"
TotalUserExceptions1min = "user_exceptions_total_1min"
ProcessLatencyMs1min = "process_latency_ms_1min"
TotalReceived1min = "received_total_1min"
)
// Declare Prometheus
var (
statTotalProcessedSuccessfully = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: PulsarFunctionMetricsPrefix + TotalSuccessfullyProcessed,
Help: "Total number of messages processed successfully."},
metricsLabelNames)
statTotalSysExceptions = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: PulsarFunctionMetricsPrefix + TotalSystemExceptions,
Help: "Total number of system exceptions."},
metricsLabelNames)
statTotalUserExceptions = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: PulsarFunctionMetricsPrefix + TotalUserExceptions,
Help: "Total number of user exceptions."},
metricsLabelNames)
statProcessLatencyMs = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Name: PulsarFunctionMetricsPrefix + ProcessLatencyMs,
Help: "Process latency in milliseconds."}, metricsLabelNames)
statLastInvocation = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: PulsarFunctionMetricsPrefix + LastInvocation,
Help: "The timestamp of the last invocation of the function."}, metricsLabelNames)
statTotalReceived = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: PulsarFunctionMetricsPrefix + TotalReceived,
Help: "Total number of messages received from source."}, metricsLabelNames)
// 1min windowed metrics
statTotalProcessedSuccessfully1min = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: PulsarFunctionMetricsPrefix + TotalSuccessfullyProcessed1min,
Help: "Total number of messages processed successfully in the last 1 minute."}, metricsLabelNames)
statTotalSysExceptions1min = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: PulsarFunctionMetricsPrefix + TotalSystemExceptions1min,
Help: "Total number of system exceptions in the last 1 minute."},
metricsLabelNames)
statTotalUserExceptions1min = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: PulsarFunctionMetricsPrefix + TotalUserExceptions1min,
Help: "Total number of user exceptions in the last 1 minute."},
metricsLabelNames)
statProcessLatencyMs1min = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Name: PulsarFunctionMetricsPrefix + ProcessLatencyMs1min,
Help: "Process latency in milliseconds in the last 1 minute."}, metricsLabelNames)
statTotalReceived1min = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: PulsarFunctionMetricsPrefix + TotalReceived1min,
Help: "Total number of messages received from source in the last 1 minute."}, metricsLabelNames)
userExceptions = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: PulsarFunctionMetricsPrefix + "user_exception",
Help: "Exception from user code."}, exceptionMetricsLabelNames)
systemExceptions = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: PulsarFunctionMetricsPrefix + "system_exception",
Help: "Exception from system code."}, exceptionMetricsLabelNames)
)
type MetricsServicer struct {
goInstance *goInstance
server *http.Server
}
var reg *prometheus.Registry
func init() {
reg = prometheus.NewRegistry()
reg.MustRegister(statTotalProcessedSuccessfully)
reg.MustRegister(statTotalSysExceptions)
reg.MustRegister(statTotalUserExceptions)
reg.MustRegister(statProcessLatencyMs)
reg.MustRegister(statLastInvocation)
reg.MustRegister(statTotalReceived)
reg.MustRegister(statTotalProcessedSuccessfully1min)
reg.MustRegister(statTotalSysExceptions1min)
reg.MustRegister(statTotalUserExceptions1min)
reg.MustRegister(statProcessLatencyMs1min)
reg.MustRegister(statTotalReceived1min)
reg.MustRegister(userExceptions)
reg.MustRegister(systemExceptions)
}
type LatestException struct {
exception error
timestamp int64
}
// Be sure to use the constructor method: NewStatWithLabelValues
type StatWithLabelValues struct {
statTotalProcessedSuccessfully prometheus.Gauge
statTotalSysExceptions prometheus.Gauge
statTotalUserExceptions prometheus.Gauge
statProcessLatencyMs prometheus.Observer
statLastInvocation prometheus.Gauge
statTotalReceived prometheus.Gauge
statTotalProcessedSuccessfully1min prometheus.Gauge
statTotalSysExceptions1min prometheus.Gauge
statTotalUserExceptions1min prometheus.Gauge
statTotalReceived1min prometheus.Gauge
latestUserException []LatestException
latestSysException []LatestException
processStartTime int64
metricsLabels []string
}
func NewStatWithLabelValues(metricsLabels ...string) StatWithLabelValues {
// as optimization
var statTotalProcessedSuccessfully = statTotalProcessedSuccessfully.WithLabelValues(metricsLabels...)
var statTotalSysExceptions = statTotalSysExceptions.WithLabelValues(metricsLabels...)
var statTotalUserExceptions = statTotalUserExceptions.WithLabelValues(metricsLabels...)
var statProcessLatencyMs = statProcessLatencyMs.WithLabelValues(metricsLabels...)
var statLastInvocation = statLastInvocation.WithLabelValues(metricsLabels...)
var statTotalReceived = statTotalReceived.WithLabelValues(metricsLabels...)
var statTotalProcessedSuccessfully1min = statTotalProcessedSuccessfully1min.WithLabelValues(metricsLabels...)
var statTotalSysExceptions1min = statTotalSysExceptions1min.WithLabelValues(metricsLabels...)
var statTotalUserExceptions1min = statTotalUserExceptions1min.WithLabelValues(metricsLabels...)
//var _stat_process_latency_ms_1min = stat_process_latency_ms_1min.WithLabelValues(metrics_labels...)
var statTotalReceived1min = statTotalReceived1min.WithLabelValues(metricsLabels...)
statObj := StatWithLabelValues{
statTotalProcessedSuccessfully,
statTotalSysExceptions,
statTotalUserExceptions,
statProcessLatencyMs,
statLastInvocation,
statTotalReceived,
statTotalProcessedSuccessfully1min,
statTotalSysExceptions1min,
statTotalUserExceptions1min,
statTotalReceived1min,
[]LatestException{},
[]LatestException{},
0,
metricsLabels,
}
return statObj
}
func filter(
ss []*prometheus_client.MetricFamily,
test func(*prometheus_client.MetricFamily) bool) (ret []*prometheus_client.MetricFamily) {
for _, s := range ss {
if test(s) {
ret = append(ret, s)
}
}
return
}
func getFirstMatch(
metrics []*prometheus_client.Metric,
test func(*prometheus_client.LabelPair) bool) *prometheus_client.Metric {
for _, met := range metrics {
for _, lbl := range met.Label {
if test(lbl) {
return met
}
}
}
return nil
}
func (stat *StatWithLabelValues) setLastInvocation() {
now := time.Now()
stat.statLastInvocation.Set(float64(now.UnixNano()))
}
func (stat *StatWithLabelValues) processTimeStart() {
now := time.Now()
stat.processStartTime = now.UnixNano()
}
func (stat *StatWithLabelValues) processTimeEnd() {
if stat.processStartTime != 0 {
now := time.Now()
duration := now.UnixNano() - stat.processStartTime
stat.statProcessLatencyMs.Observe(float64(duration))
}
}
func (stat *StatWithLabelValues) incrTotalUserExceptions(err error) {
stat.statTotalUserExceptions.Inc()
stat.statTotalUserExceptions1min.Inc()
stat.addUserException(err)
}
func (stat *StatWithLabelValues) addUserException(err error) {
now := time.Now()
ts := now.UnixNano()
errorTs := LatestException{err, ts}
stat.latestUserException = append(stat.latestUserException, errorTs)
if len(stat.latestUserException) > 10 {
stat.latestUserException = stat.latestUserException[1:]
}
// report exception via prometheus
stat.reportUserExceptionPrometheus(err)
}
//@limits(calls=5, period=60)
func (stat *StatWithLabelValues) reportUserExceptionPrometheus(exception error) {
errorTs := []string{exception.Error()}
exceptionMetricLabels := append(stat.metricsLabels, errorTs...)
userExceptions.WithLabelValues(exceptionMetricLabels...).Set(1.0)
}
func (stat *StatWithLabelValues) incrTotalProcessedSuccessfully() {
stat.statTotalProcessedSuccessfully.Inc()
stat.statTotalProcessedSuccessfully1min.Inc()
}
func (stat *StatWithLabelValues) incrTotalSysExceptions(exception error) {
stat.statTotalSysExceptions.Inc()
stat.statTotalSysExceptions1min.Inc()
stat.addSysException(exception)
}
func (stat *StatWithLabelValues) addSysException(exception error) {
now := time.Now()
ts := now.UnixNano()
errorTs := LatestException{exception, ts}
stat.latestSysException = append(stat.latestSysException, errorTs)
if len(stat.latestSysException) > 10 {
stat.latestSysException = stat.latestSysException[1:]
}
// report exception via prometheus
stat.reportSystemExceptionPrometheus(exception)
}
//@limits(calls=5, period=60)
func (stat *StatWithLabelValues) reportSystemExceptionPrometheus(exception error) {
errorTs := []string{exception.Error()}
exceptionMetricLabels := append(stat.metricsLabels, errorTs...)
systemExceptions.WithLabelValues(exceptionMetricLabels...).Set(1.0)
}
func (stat *StatWithLabelValues) incrTotalReceived() {
stat.statTotalReceived.Inc()
stat.statTotalReceived1min.Inc()
}
func (stat *StatWithLabelValues) reset() {
stat.statTotalProcessedSuccessfully1min.Set(0.0)
stat.statTotalUserExceptions1min.Set(0.0)
stat.statTotalSysExceptions1min.Set(0.0)
stat.statTotalReceived1min.Set(0.0)
}
func NewMetricsServicer(goInstance *goInstance) *MetricsServicer {
serveMux := http.NewServeMux()
pHandler := promhttp.HandlerFor(
reg,
promhttp.HandlerOpts{
EnableOpenMetrics: true,
},
)
serveMux.Handle("/", pHandler)
serveMux.Handle("/metrics", pHandler)
server := &http.Server{
Addr: fmt.Sprintf(":%d", goInstance.context.GetMetricsPort()),
Handler: serveMux,
}
return &MetricsServicer{
goInstance,
server,
}
}
func (s *MetricsServicer) serve() {
go func() {
// create a listener on metrics port
log.Infof("Starting metrics server on port %d", s.goInstance.context.GetMetricsPort())
err := s.server.ListenAndServe()
if err != nil {
log.Fatalf("failed to start metrics server: %v", err)
}
}()
}
func (s *MetricsServicer) close() {
err := s.server.Close()
if err != nil {
log.Fatalf("failed to close metrics server: %v", err)
}
}