/
SourceStatsManager.java
366 lines (296 loc) · 13.6 KB
/
SourceStatsManager.java
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
354
355
356
357
358
359
360
361
362
363
364
365
366
/**
* 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 org.apache.pulsar.functions.instance.stats;
import com.google.common.collect.EvictingQueue;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.Counter;
import io.prometheus.client.Gauge;
import lombok.Getter;
import org.apache.pulsar.common.util.RateLimiter;
import org.apache.pulsar.functions.proto.InstanceCommunication;
import java.util.Arrays;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class SourceStatsManager extends ComponentStatsManager {
public static final String PULSAR_SOURCE_METRICS_PREFIX = "pulsar_source_";
/** Declare metric names **/
public static final String SYSTEM_EXCEPTIONS_TOTAL = "system_exceptions_total";
public static final String SOURCE_EXCEPTIONS_TOTAL = "source_exceptions_total";
public static final String LAST_INVOCATION = "last_invocation";
public static final String RECEIVED_TOTAL = "received_total";
public static final String WRITTEN_TOTAL = "written_total";
public static final String SYSTEM_EXCEPTIONS_TOTAL_1min = "system_exceptions_total_1min";
public static final String SOURCE_EXCEPTIONS_TOTAL_1min = "source_exceptions_total_1min";
public static final String RECEIVED_TOTAL_1min = "received_total_1min";
public static final String WRITTEN_TOTAL_1min = "written_total_1min";
/** Declare Prometheus stats **/
private final Counter statTotalRecordsReceived;
private final Counter statTotalSysExceptions;
private final Counter statTotalSourceExceptions;
private final Counter statTotalWritten;
private final Gauge statlastInvocation;
// windowed metrics
private final Counter statTotalRecordsReceived1min;
private final Counter statTotalSysExceptions1min;
private final Counter statTotalSourceExceptions1min;
private final Counter statTotalWritten1min;
// exceptions
final Gauge sysExceptions;
final Gauge sourceExceptions;
// As an optimization
private final Counter.Child _statTotalRecordsReceived;
private final Counter.Child _statTotalSysExceptions;
private final Counter.Child _statTotalSourceExceptions;
private final Counter.Child _statTotalWritten;
private final Gauge.Child _statlastInvocation;
private Counter.Child _statTotalRecordsReceived1min;
private Counter.Child _statTotalSysExceptions1min;
private Counter.Child _statTotalSourceExceptions1min;
private Counter.Child _statTotalWritten1min;
@Getter
private EvictingQueue<InstanceCommunication.FunctionStatus.ExceptionInformation> latestSystemExceptions = EvictingQueue.create(10);
@Getter
private EvictingQueue<InstanceCommunication.FunctionStatus.ExceptionInformation> latestSourceExceptions = EvictingQueue.create(10);
protected final RateLimiter sysExceptionRateLimiter;
protected final RateLimiter sourceExceptionRateLimiter;
public SourceStatsManager(FunctionCollectorRegistry collectorRegistry, String[] metricsLabels, ScheduledExecutorService
scheduledExecutorService) {
super(collectorRegistry, metricsLabels, scheduledExecutorService);
statTotalRecordsReceived = collectorRegistry.registerIfNotExist(
PULSAR_SOURCE_METRICS_PREFIX + RECEIVED_TOTAL,
Counter.build()
.name(PULSAR_SOURCE_METRICS_PREFIX + RECEIVED_TOTAL)
.help("Total number of records received from source.")
.labelNames(metricsLabelNames)
.create());
_statTotalRecordsReceived = statTotalRecordsReceived.labels(metricsLabels);
statTotalSysExceptions = collectorRegistry.registerIfNotExist(
PULSAR_SOURCE_METRICS_PREFIX + SYSTEM_EXCEPTIONS_TOTAL,
Counter.build()
.name(PULSAR_SOURCE_METRICS_PREFIX + SYSTEM_EXCEPTIONS_TOTAL)
.help("Total number of system exceptions.")
.labelNames(metricsLabelNames)
.create());
_statTotalSysExceptions = statTotalSysExceptions.labels(metricsLabels);
statTotalSourceExceptions = collectorRegistry.registerIfNotExist(
PULSAR_SOURCE_METRICS_PREFIX + SOURCE_EXCEPTIONS_TOTAL,
Counter.build()
.name(PULSAR_SOURCE_METRICS_PREFIX + SOURCE_EXCEPTIONS_TOTAL)
.help("Total number of source exceptions.")
.labelNames(metricsLabelNames)
.create());
_statTotalSourceExceptions = statTotalSourceExceptions.labels(metricsLabels);
statTotalWritten = collectorRegistry.registerIfNotExist(
PULSAR_SOURCE_METRICS_PREFIX + WRITTEN_TOTAL,
Counter.build()
.name(PULSAR_SOURCE_METRICS_PREFIX + WRITTEN_TOTAL)
.help("Total number of records written to a Pulsar topic.")
.labelNames(metricsLabelNames)
.create());
_statTotalWritten = statTotalWritten.labels(metricsLabels);
statlastInvocation = collectorRegistry.registerIfNotExist(
PULSAR_SOURCE_METRICS_PREFIX + LAST_INVOCATION,
Gauge.build()
.name(PULSAR_SOURCE_METRICS_PREFIX + LAST_INVOCATION)
.help("The timestamp of the last invocation of the source.")
.labelNames(metricsLabelNames)
.create());
_statlastInvocation = statlastInvocation.labels(metricsLabels);
statTotalRecordsReceived1min = collectorRegistry.registerIfNotExist(
PULSAR_SOURCE_METRICS_PREFIX + RECEIVED_TOTAL_1min,
Counter.build()
.name(PULSAR_SOURCE_METRICS_PREFIX + RECEIVED_TOTAL_1min)
.help("Total number of records received from source in the last 1 minute.")
.labelNames(metricsLabelNames)
.create());
_statTotalRecordsReceived1min = statTotalRecordsReceived1min.labels(metricsLabels);
statTotalSysExceptions1min = collectorRegistry.registerIfNotExist(
PULSAR_SOURCE_METRICS_PREFIX + SYSTEM_EXCEPTIONS_TOTAL_1min,
Counter.build()
.name(PULSAR_SOURCE_METRICS_PREFIX + SYSTEM_EXCEPTIONS_TOTAL_1min)
.help("Total number of system exceptions in the last 1 minute.")
.labelNames(metricsLabelNames)
.create());
_statTotalSysExceptions1min = statTotalSysExceptions1min.labels(metricsLabels);
statTotalSourceExceptions1min = collectorRegistry.registerIfNotExist(
PULSAR_SOURCE_METRICS_PREFIX + SOURCE_EXCEPTIONS_TOTAL_1min,
Counter.build()
.name(PULSAR_SOURCE_METRICS_PREFIX + SOURCE_EXCEPTIONS_TOTAL_1min)
.help("Total number of source exceptions in the last 1 minute.")
.labelNames(metricsLabelNames)
.create());
_statTotalSourceExceptions1min = statTotalSourceExceptions1min.labels(metricsLabels);
statTotalWritten1min = collectorRegistry.registerIfNotExist(
PULSAR_SOURCE_METRICS_PREFIX + WRITTEN_TOTAL_1min,
Counter.build()
.name(PULSAR_SOURCE_METRICS_PREFIX + WRITTEN_TOTAL_1min)
.help("Total number of records written to a Pulsar topic in the last 1 minute.")
.labelNames(metricsLabelNames)
.create());
_statTotalWritten1min = statTotalWritten1min.labels(metricsLabels);
sysExceptions = collectorRegistry.registerIfNotExist(
PULSAR_SOURCE_METRICS_PREFIX + "system_exception",
Gauge.build()
.name(PULSAR_SOURCE_METRICS_PREFIX + "system_exception")
.labelNames(exceptionMetricsLabelNames)
.help("Exception from system code.")
.create());
sourceExceptions = collectorRegistry.registerIfNotExist(
PULSAR_SOURCE_METRICS_PREFIX + "source_exception",
Gauge.build()
.name(PULSAR_SOURCE_METRICS_PREFIX + "source_exception")
.labelNames(exceptionMetricsLabelNames)
.help("Exception from source.")
.create());
sysExceptionRateLimiter = new RateLimiter(scheduledExecutorService, 5, 1, TimeUnit.MINUTES);
sourceExceptionRateLimiter = new RateLimiter(scheduledExecutorService, 5, 1, TimeUnit.MINUTES);
}
@Override
public void reset() {
statTotalRecordsReceived1min.clear();
_statTotalRecordsReceived1min = statTotalRecordsReceived1min.labels(metricsLabels);
statTotalSysExceptions1min.clear();
_statTotalSysExceptions1min = statTotalSysExceptions1min.labels(metricsLabels);
statTotalSourceExceptions1min.clear();
_statTotalSourceExceptions1min = statTotalSourceExceptions1min.labels(metricsLabels);
statTotalWritten1min.clear();
_statTotalWritten1min = statTotalWritten1min.labels(metricsLabels);
}
@Override
public void incrTotalReceived() {
_statTotalRecordsReceived.inc();
_statTotalRecordsReceived1min.inc();
}
@Override
public void incrTotalProcessedSuccessfully() {
_statTotalWritten.inc();
_statTotalWritten1min.inc();
}
@Override
public void incrSysExceptions(Throwable ex) {
_statTotalSysExceptions.inc();
_statTotalSysExceptions1min.inc();
long ts = System.currentTimeMillis();
InstanceCommunication.FunctionStatus.ExceptionInformation info = getExceptionInfo(ex, ts);
latestSystemExceptions.add(info);
// report exception throw prometheus
if (sysExceptionRateLimiter.tryAcquire()) {
String[] exceptionMetricsLabels = getExceptionMetricsLabels(ex);
sysExceptions.labels(exceptionMetricsLabels).set(1.0);
}
}
@Override
public void incrUserExceptions(Throwable ex) {
incrSysExceptions(ex);
}
@Override
public void incrSourceExceptions(Throwable ex) {
_statTotalSourceExceptions.inc();
_statTotalSourceExceptions1min.inc();
long ts = System.currentTimeMillis();
InstanceCommunication.FunctionStatus.ExceptionInformation info = getExceptionInfo(ex, ts);
latestSourceExceptions.add(info);
// report exception throw prometheus
if (sourceExceptionRateLimiter.tryAcquire()) {
String[] exceptionMetricsLabels = getExceptionMetricsLabels(ex);
sourceExceptions.labels(exceptionMetricsLabels).set(1.0);
}
}
private String[] getExceptionMetricsLabels(Throwable ex) {
String[] exceptionMetricsLabels = Arrays.copyOf(metricsLabels, metricsLabels.length + 1);
exceptionMetricsLabels[exceptionMetricsLabels.length - 1] = ex.getMessage() != null ? ex.getMessage() : "";
return exceptionMetricsLabels;
}
@Override
public void incrSinkExceptions(Throwable ex) {
incrSysExceptions(ex);
}
@Override
public void setLastInvocation(long ts) {
_statlastInvocation.set(ts);
}
@Override
public void processTimeStart() {
//no-op
}
@Override
public void processTimeEnd() {
//no-op
}
@Override
public double getTotalProcessedSuccessfully() {
return _statTotalWritten.get();
}
@Override
public double getTotalRecordsReceived() {
return _statTotalRecordsReceived.get();
}
@Override
public double getTotalSysExceptions() {
return _statTotalSysExceptions.get();
}
@Override
public double getTotalUserExceptions() {
return 0;
}
@Override
public double getLastInvocation() {
return _statlastInvocation.get();
}
@Override
public double getAvgProcessLatency() {
return 0;
}
@Override
public double getTotalProcessedSuccessfully1min() {
return _statTotalWritten1min.get();
}
@Override
public double getTotalRecordsReceived1min() {
return _statTotalRecordsReceived1min.get();
}
@Override
public double getTotalSysExceptions1min() {
return _statTotalSysExceptions1min.get();
}
@Override
public double getTotalUserExceptions1min() {
return 0;
}
@Override
public double getAvgProcessLatency1min() {
return 0;
}
@Override
public EvictingQueue<InstanceCommunication.FunctionStatus.ExceptionInformation> getLatestUserExceptions() {
return EvictingQueue.create(0);
}
@Override
public EvictingQueue<InstanceCommunication.FunctionStatus.ExceptionInformation> getLatestSystemExceptions() {
return EMPTY_QUEUE;
}
@Override
public EvictingQueue<InstanceCommunication.FunctionStatus.ExceptionInformation> getLatestSourceExceptions() {
return EMPTY_QUEUE;
}
@Override
public EvictingQueue<InstanceCommunication.FunctionStatus.ExceptionInformation> getLatestSinkExceptions() {
return EvictingQueue.create(0);
}
}