1
- // Copyright (c) 2020-2021 VMware, Inc. or its affiliates. All rights reserved.
1
+ // Copyright (c) 2020-2022 VMware, Inc. or its affiliates. All rights reserved.
2
2
//
3
3
// This software, the RabbitMQ Stream Java client library, is dual-licensed under the
4
4
// Mozilla Public License 2.0 ("MPL"), and the Apache License version 2 ("ASL").
35
35
import java .util .concurrent .TimeUnit ;
36
36
import java .util .concurrent .atomic .AtomicInteger ;
37
37
import java .util .concurrent .atomic .AtomicLong ;
38
+ import java .util .function .BiFunction ;
38
39
import java .util .function .Function ;
39
40
import java .util .function .Supplier ;
40
41
import org .slf4j .Logger ;
@@ -45,7 +46,7 @@ class DefaultPerformanceMetrics implements PerformanceMetrics {
45
46
private static final Logger LOGGER = LoggerFactory .getLogger (DefaultPerformanceMetrics .class );
46
47
47
48
private final MetricRegistry metricRegistry ;
48
- private final Timer latency ;
49
+ private final Timer latency , confirmLatency ;
49
50
private final boolean summaryFile ;
50
51
private final PrintWriter out ;
51
52
private final boolean includeByteRates ;
@@ -60,6 +61,7 @@ class DefaultPerformanceMetrics implements PerformanceMetrics {
60
61
String metricsPrefix ,
61
62
boolean summaryFile ,
62
63
boolean includeByteRates ,
64
+ boolean confirmLatency ,
63
65
Supplier <String > memoryReportSupplier ,
64
66
PrintWriter out ) {
65
67
this .summaryFile = summaryFile ;
@@ -98,6 +100,17 @@ protected Double nullGaugeValue() {
98
100
.distributionStatisticExpiry (Duration .ofSeconds (1 ))
99
101
.serviceLevelObjectives ()
100
102
.register (meterRegistry );
103
+ if (confirmLatency ) {
104
+ this .confirmLatency =
105
+ Timer .builder (metricsPrefix + ".confirm_latency" )
106
+ .description ("publish confirm latency" )
107
+ .publishPercentiles (0.5 , 0.75 , 0.95 , 0.99 )
108
+ .distributionStatisticExpiry (Duration .ofSeconds (1 ))
109
+ .serviceLevelObjectives ()
110
+ .register (meterRegistry );
111
+ } else {
112
+ this .confirmLatency = null ;
113
+ }
101
114
}
102
115
103
116
private long getPublishedCount () {
@@ -117,6 +130,7 @@ public void start(String description) throws Exception {
117
130
String metricConsumed = "rabbitmqStreamConsumed" ;
118
131
String metricChunkSize = "rabbitmqStreamChunk_size" ;
119
132
String metricLatency = "rabbitmqStreamLatency" ;
133
+ String metricConfirmLatency = "rabbitmqStreamConfirm_latency" ;
120
134
String metricWrittenBytes = "rabbitmqStreamWritten_bytes" ;
121
135
String metricReadBytes = "rabbitmqStreamRead_bytes" ;
122
136
@@ -129,6 +143,10 @@ public void start(String description) throws Exception {
129
143
metricChunkSize ,
130
144
metricLatency ));
131
145
146
+ if (confirmLatency ()) {
147
+ allMetrics .add (metricConfirmLatency );
148
+ }
149
+
132
150
Map <String , String > metersNamesAndLabels = new LinkedHashMap <>();
133
151
metersNamesAndLabels .put (metricPublished , "published" );
134
152
metersNamesAndLabels .put (metricProducerConfirmed , "confirmed" );
@@ -184,14 +202,16 @@ public void start(String description) throws Exception {
184
202
histogram -> String .format ("chunk size %.0f" , histogram .getSnapshot ().getMean ());
185
203
186
204
com .codahale .metrics .Timer latency = metricRegistry .getTimers ().get (metricLatency );
205
+ com .codahale .metrics .Timer confirmLatency =
206
+ confirmLatency () ? metricRegistry .getTimers ().get (metricConfirmLatency ) : null ;
187
207
188
208
Function <Number , Number > convertDuration =
189
209
in -> in instanceof Long ? in .longValue () / 1_000_000 : in .doubleValue () / 1_000_000 ;
190
- Function < com .codahale .metrics .Timer , String > formatLatency =
191
- timer -> {
210
+ BiFunction < String , com .codahale .metrics .Timer , String > formatLatency =
211
+ ( name , timer ) -> {
192
212
Snapshot snapshot = timer .getSnapshot ();
193
213
return String .format (
194
- "latency min/median/75th/95th/99th %.0f/%.0f/%.0f/%.0f/%.0f ms" ,
214
+ name + " min/median/75th/95th/99th %.0f/%.0f/%.0f/%.0f/%.0f ms" ,
195
215
convertDuration .apply (snapshot .getMin ()),
196
216
convertDuration .apply (snapshot .getMedian ()),
197
217
convertDuration .apply (snapshot .get75thPercentile ()),
@@ -229,7 +249,12 @@ public void start(String description) throws Exception {
229
249
.compute (lastValue , currentValue , duration ));
230
250
lastMetersValues .put (meterName , currentValue );
231
251
});
232
- builder .append (formatLatency .apply (latency )).append (", " );
252
+ if (confirmLatency ()) {
253
+ builder
254
+ .append (formatLatency .apply ("confirm latency" , confirmLatency ))
255
+ .append (", " );
256
+ }
257
+ builder .append (formatLatency .apply ("latency" , latency )).append (", " );
233
258
builder .append (formatChunkSize .apply (chunkSize ));
234
259
this .out .println (builder );
235
260
String memoryReport = this .memoryReportSupplier .get ();
@@ -270,15 +295,20 @@ public void start(String description) throws Exception {
270
295
}
271
296
};
272
297
273
- Function < com .codahale .metrics .Timer , String > formatLatencySummary =
274
- histogram ->
298
+ BiFunction < String , com .codahale .metrics .Timer , String > formatLatencySummary =
299
+ ( name , histogram ) ->
275
300
String .format (
276
- "latency 95th %.0f ms" ,
301
+ name + " 95th %.0f ms" ,
277
302
convertDuration .apply (latency .getSnapshot ().get95thPercentile ()));
278
303
279
304
StringBuilder builder = new StringBuilder ("Summary: " );
280
305
meters .entrySet ().forEach (entry -> builder .append (formatMeterSummary .apply (entry )));
281
- builder .append (formatLatencySummary .apply (latency )).append (", " );
306
+ if (confirmLatency ()) {
307
+ builder
308
+ .append (formatLatencySummary .apply ("confirm latency" , confirmLatency ))
309
+ .append (", " );
310
+ }
311
+ builder .append (formatLatencySummary .apply ("latency" , latency )).append (", " );
282
312
builder .append (formatChunkSize .apply (chunkSize ));
283
313
this .out .println ();
284
314
this .out .println (builder );
@@ -369,6 +399,11 @@ public void latency(long latency, TimeUnit unit) {
369
399
this .latency .record (latency , unit );
370
400
}
371
401
402
+ @ Override
403
+ public void confirmLatency (long latency , TimeUnit unit ) {
404
+ this .confirmLatency .record (latency , unit );
405
+ }
406
+
372
407
@ Override
373
408
public void offset (long offset ) {
374
409
this .offset = offset ;
@@ -379,6 +414,10 @@ public void close() throws Exception {
379
414
this .closingSequence .close ();
380
415
}
381
416
417
+ private boolean confirmLatency () {
418
+ return this .confirmLatency != null ;
419
+ }
420
+
382
421
private interface FormatCallback {
383
422
384
423
String compute (long lastValue , long currentValue , Duration duration );
0 commit comments