-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
MicrometerMeterListener.java
322 lines (282 loc) · 12.4 KB
/
MicrometerMeterListener.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
/*
* Copyright (c) 2022 VMware Inc. or its affiliates, All Rights Reserved.
*
* Licensed 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
*
* https://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 reactor.core.observability.micrometer;
import java.util.concurrent.TimeUnit;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.DistributionSummary;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.Timer;
import reactor.core.observability.SignalListener;
import reactor.core.publisher.Flux;
import reactor.core.publisher.SignalType;
import reactor.util.annotation.Nullable;
/**
* A {@link SignalListener} that activates metrics gathering using Micrometer 1.x.
*
* @author Simon Baslé
*/
final class MicrometerMeterListener<T> implements SignalListener<T> {
final MicrometerMeterListenerConfiguration configuration;
@Nullable
final DistributionSummary requestedCounter;
@Nullable
final Timer onNextIntervalTimer;
Timer.Sample subscribeToTerminateSample;
long lastNextEventNanos = -1L;
boolean valued;
MicrometerMeterListener(MicrometerMeterListenerConfiguration configuration) {
this.configuration = configuration;
this.valued = false;
if (configuration.isMono) {
//for Mono we don't record onNextInterval (since there is at most 1 onNext).
//Note that we still need a mean to distinguish between empty Mono and valued Mono for recordOnCompleteEmpty
onNextIntervalTimer = null;
//we also don't count the number of request calls (there should be only one)
requestedCounter = null;
}
else {
this.onNextIntervalTimer = Timer.builder(configuration.sequenceName + METER_ON_NEXT_DELAY)
.tags(configuration.commonTags)
.description(
"Measures delays between onNext signals (or between onSubscribe and first onNext)")
.register(configuration.registry);
if (!Micrometer.DEFAULT_METER_PREFIX.equals(configuration.sequenceName)) {
this.requestedCounter = DistributionSummary.builder(configuration.sequenceName + METER_REQUESTED)
.tags(configuration.commonTags)
.description(
"Counts the amount requested to a named Flux by all subscribers, until at least one requests an unbounded amount")
.register(configuration.registry);
}
else {
requestedCounter = null;
}
}
}
@Override
public void doOnCancel() {
//we don't record the time between last onNext and cancel,
// because it would skew the onNext count by one
recordCancel(configuration.sequenceName, configuration.commonTags, configuration.registry, subscribeToTerminateSample);
}
@Override
public void doOnComplete() {
//we don't record the time between last onNext and onComplete,
// because it would skew the onNext count by one.
// We differentiate between empty completion and value completion, however, via tags.
if (!valued) {
recordOnCompleteEmpty(configuration.sequenceName, configuration.commonTags, configuration.registry, subscribeToTerminateSample);
}
else if (!configuration.isMono) {
//recordOnComplete is done directly in onNext for the Mono(valued) case
recordOnComplete(configuration.sequenceName, configuration.commonTags, configuration.registry, subscribeToTerminateSample);
}
}
@Override
public void doOnMalformedOnComplete() {
recordMalformed(configuration.sequenceName, configuration.commonTags, configuration.registry);
}
@Override
public void doOnError(Throwable e) {
//we don't record the time between last onNext and onError,
// because it would skew the onNext count by one
recordOnError(configuration.sequenceName, configuration.commonTags, configuration.registry, subscribeToTerminateSample, e);
}
@Override
public void doOnMalformedOnError(Throwable e) {
recordMalformed(configuration.sequenceName, configuration.commonTags, configuration.registry);
}
@Override
public void doOnNext(T t) {
valued = true;
if (onNextIntervalTimer == null) { //NB: interval timer is only null if isMono
//record valued completion directly
recordOnComplete(configuration.sequenceName, configuration.commonTags, configuration.registry, subscribeToTerminateSample);
return;
}
//record the delay since previous onNext/onSubscribe. This also records the count.
long last = this.lastNextEventNanos;
this.lastNextEventNanos = configuration.registry.config().clock().monotonicTime();
this.onNextIntervalTimer.record(lastNextEventNanos - last, TimeUnit.NANOSECONDS);
}
@Override
public void doOnMalformedOnNext(T value) {
recordMalformed(configuration.sequenceName, configuration.commonTags, configuration.registry);
}
@Override
public void doOnSubscription() {
recordOnSubscribe(configuration.sequenceName, configuration.commonTags, configuration.registry);
this.subscribeToTerminateSample = Timer.start(configuration.registry);
this.lastNextEventNanos = configuration.registry.config().clock().monotonicTime();
}
@Override
public void doOnRequest(long l) {
if (requestedCounter != null) {
requestedCounter.record(l);
}
}
//unused hooks
@Override
public void doFirst() {
// NO-OP
}
@Override
public void doOnFusion(int negotiatedFusion) throws Throwable {
// NO-OP
//TODO metrics counting fused (with ASYNC/SYNC tags) could be implemented to supplement METER_SUBSCRIBED
}
@Override
public void doFinally(SignalType terminationType) {
// NO-OP
}
@Override
public void doAfterComplete() {
// NO-OP
}
@Override
public void doAfterError(Throwable error) {
// NO-OP
}
@Override
public void handleListenerError(Throwable listenerError) {
// NO-OP
}
/**
* Meter that counts the number of events received from a malformed source (ie an onNext after an onComplete).
*/
static final String METER_MALFORMED = ".malformed.source";
/**
* Meter that counts the number of subscriptions to a sequence.
*/
static final String METER_SUBSCRIBED = ".subscribed";
/**
* Meter that times the duration elapsed between a subscription and the termination or cancellation of the sequence.
* A status tag is added to specify what event caused the timer to end (completed, completedEmpty, error, cancelled).
*/
static final String METER_FLOW_DURATION = ".flow.duration";
/**
* Meter that times the delays between each onNext (or between the first onNext and the onSubscribe event).
*/
static final String METER_ON_NEXT_DELAY = ".onNext.delay";
/**
* Meter that tracks the request amount, in {@link Flux#name(String) named} sequences only.
*/
static final String METER_REQUESTED = ".requested";
/**
* Tag used by {@link #METER_FLOW_DURATION} when "status" is {@link #TAG_ON_ERROR}, to store the
* exception that occurred.
*/
static final String TAG_KEY_EXCEPTION = "exception";
/**
* Tag bearing the sequence's name, as given by the {@link Flux#name(String)} operator.
*/
static final Tags DEFAULT_TAGS_FLUX = Tags.of("type", "Flux");
static final Tags DEFAULT_TAGS_MONO = Tags.of("type", "Mono");
// === Operator ===
static final String TAG_KEY_STATUS = "status";
static final String TAG_STATUS_CANCELLED = "cancelled";
static final String TAG_STATUS_COMPLETED = "completed";
static final String TAG_STATUS_COMPLETED_EMPTY = "completedEmpty";
static final String TAG_STATUS_ERROR = "error";
static final Tag TAG_ON_ERROR = Tag.of(TAG_KEY_STATUS, TAG_STATUS_ERROR);
static final Tags TAG_ON_COMPLETE = Tags.of(TAG_KEY_STATUS, TAG_STATUS_COMPLETED, TAG_KEY_EXCEPTION, "");
static final Tags TAG_ON_COMPLETE_EMPTY = Tags.of(TAG_KEY_STATUS, TAG_STATUS_COMPLETED_EMPTY, TAG_KEY_EXCEPTION, "");
static final Tags TAG_CANCEL = Tags.of(TAG_KEY_STATUS, TAG_STATUS_CANCELLED, TAG_KEY_EXCEPTION, "");
/*
* This method calls the registry, which can be costly. However the cancel signal is only expected
* once per Subscriber. So the net effect should be that the registry is only called once, which
* is equivalent to registering the meter as a final field, with the added benefit of paying that
* cost only in case of cancellation.
*/
static void recordCancel(String name, Tags commonTags, MeterRegistry registry, Timer.Sample flowDuration) {
Timer timer = Timer.builder(name + METER_FLOW_DURATION)
.tags(commonTags.and(TAG_CANCEL))
.description(
"Times the duration elapsed between a subscription and the cancellation of the sequence")
.register(registry);
flowDuration.stop(timer);
}
/*
* This method calls the registry, which can be costly. However a malformed signal is generally
* not expected, or at most once per Subscriber. So the net effect should be that the registry
* is only called once, which is equivalent to registering the meter as a final field,
* with the added benefit of paying that cost only in case of onNext/onError after termination.
*/
static void recordMalformed(String name, Tags commonTags, MeterRegistry registry) {
registry.counter(name + METER_MALFORMED, commonTags)
.increment();
}
/*
* This method calls the registry, which can be costly. However the onError signal is expected
* at most once per Subscriber. So the net effect should be that the registry is only called once,
* which is equivalent to registering the meter as a final field, with the added benefit of paying
* that cost only in case of error.
*/
static void recordOnError(String name, Tags commonTags, MeterRegistry registry, Timer.Sample flowDuration,
Throwable e) {
Timer timer = Timer.builder(name + METER_FLOW_DURATION)
.tags(commonTags.and(TAG_ON_ERROR))
.tag(TAG_KEY_EXCEPTION,
e.getClass()
.getName())
.description(
"Times the duration elapsed between a subscription and the onError termination of the sequence, with the exception name as a tag.")
.register(registry);
flowDuration.stop(timer);
}
/*
* This method calls the registry, which can be costly. However the onComplete signal is expected
* at most once per Subscriber. So the net effect should be that the registry is only called once,
* which is equivalent to registering the meter as a final field, with the added benefit of paying
* that cost only in case of completion (which is not always occurring).
*/
static void recordOnComplete(String name, Tags commonTags, MeterRegistry registry, Timer.Sample flowDuration) {
Timer timer = Timer.builder(name + METER_FLOW_DURATION)
.tags(commonTags.and(TAG_ON_COMPLETE))
.description(
"Times the duration elapsed between a subscription and the onComplete termination of a sequence that did emit some elements")
.register(registry);
flowDuration.stop(timer);
}
/*
* This method calls the registry, which can be costly. However the onComplete signal is expected
* at most once per Subscriber. So the net effect should be that the registry is only called once,
* which is equivalent to registering the meter as a final field, with the added benefit of paying
* that cost only in case of completion (which is not always occurring).
*/
static void recordOnCompleteEmpty(String name, Tags commonTags, MeterRegistry registry, Timer.Sample flowDuration) {
Timer timer = Timer.builder(name + METER_FLOW_DURATION)
.tags(commonTags.and(TAG_ON_COMPLETE_EMPTY))
.description(
"Times the duration elapsed between a subscription and the onComplete termination of a sequence that didn't emit any element")
.register(registry);
flowDuration.stop(timer);
}
/*
* This method calls the registry, which can be costly. However the onSubscribe signal is expected
* at most once per Subscriber. So the net effect should be that the registry is only called once,
* which is equivalent to registering the meter as a final field, with the added benefit of paying
* that cost only in case of subscription.
*/
static void recordOnSubscribe(String name, Tags commonTags, MeterRegistry registry) {
Counter.builder(name + METER_SUBSCRIBED)
.tags(commonTags)
.description("Counts how many Reactor sequences have been subscribed to")
.register(registry)
.increment();
}
}