Skip to content

Commit f4fbfa1

Browse files
garyrussellartembilan
authored andcommitted
INT-4395: More micrometer meters
JIRA: https://jira.spring.io/browse/INT-4395 - pollable channel counts - component counts (gauges) Polishing - PR comments Fix switch in test * Simple code style polishing and fix JavaDocs
1 parent 5f509ac commit f4fbfa1

File tree

7 files changed

+290
-31
lines changed

7 files changed

+290
-31
lines changed

spring-integration-core/src/main/java/org/springframework/integration/channel/AbstractPollableChannel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -103,7 +103,7 @@ public Message<?> receive(long timeout) {
103103
}
104104
}
105105
Message<?> message = this.doReceive(timeout);
106-
if (countsEnabled) {
106+
if (countsEnabled && message != null) {
107107
getMetrics().afterReceive();
108108
counted = true;
109109
}

spring-integration-core/src/main/java/org/springframework/integration/support/management/AbstractMessageChannelMetrics.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* Abstract base class for channel metrics implementations.
3030
*
3131
* @author Gary Russell
32+
*
3233
* @since 4.2
3334
*
3435
*/
@@ -42,32 +43,43 @@ public abstract class AbstractMessageChannelMetrics implements ConfigurableMetri
4243

4344
private final Counter errorCounter;
4445

46+
private final Counter receiveCounter;
47+
48+
private final Counter receiveErrorCounter;
49+
4550
private volatile boolean fullStatsEnabled;
4651

4752
/**
4853
* Construct an instance with the provided name.
4954
* @param name the name.
5055
*/
5156
public AbstractMessageChannelMetrics(String name) {
52-
this(name, null, null);
57+
this(name, null, null, null, null);
5358
}
5459

5560
/**
56-
* Construct an instance with the provided name, timer and error counter.
61+
* Construct an instance with the provided name, timer, error counter, receive counter
62+
* and receive error counter.
5763
* A non-null timer requires a non-null error counter. When a timer is provided,
5864
* Micrometer metrics are used and the legacy metrics are not maintained.
5965
* @param name the name.
6066
* @param timer the timer.
6167
* @param errorCounter the error counter.
68+
* @param receiveCounter the receive counter.
69+
* @param receiveErrorCounter the receive error counter.
6270
* @since 5.0.2
6371
*/
64-
public AbstractMessageChannelMetrics(String name, Timer timer, Counter errorCounter) {
72+
public AbstractMessageChannelMetrics(String name, Timer timer, Counter errorCounter, Counter receiveCounter,
73+
Counter receiveErrorCounter) {
74+
6575
if (timer != null) {
6676
Assert.notNull(errorCounter, "'errorCounter' cannot be null if a timer is provided");
6777
}
6878
this.name = name;
6979
this.timer = timer;
7080
this.errorCounter = errorCounter;
81+
this.receiveCounter = receiveCounter;
82+
this.receiveErrorCounter = receiveErrorCounter;
7183
}
7284

7385
/**
@@ -124,6 +136,26 @@ public Counter getErrorCounter() {
124136
return this.errorCounter;
125137
}
126138

139+
/**
140+
* Return the receive counter if Micrometer metrics are being used.
141+
* @return the counter or null if Micrometer is not being used.
142+
* @since 5.0.2
143+
*/
144+
@Nullable
145+
public Counter getReceiveCounter() {
146+
return this.receiveCounter;
147+
}
148+
149+
/**
150+
* Return the receive error counter if Micrometer metrics are being used.
151+
* @return the counter or null if Micrometer is not being used.
152+
* @since 5.0.2
153+
*/
154+
@Nullable
155+
public Counter getReceiveErrorCounter() {
156+
return this.receiveErrorCounter;
157+
}
158+
127159
public abstract int getSendCount();
128160

129161
public abstract long getSendCountLong();

spring-integration-core/src/main/java/org/springframework/integration/support/management/DefaultMessageChannelMetrics.java

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* @author Helena Edelson
2929
* @author Gary Russell
3030
* @author Ivan Krizsan
31+
*
3132
* @since 2.0
3233
*/
3334
public class DefaultMessageChannelMetrics extends AbstractMessageChannelMetrics {
@@ -64,7 +65,7 @@ public DefaultMessageChannelMetrics() {
6465
* @param name the name.
6566
*/
6667
public DefaultMessageChannelMetrics(String name) {
67-
this(name, null, null);
68+
this(name, null, null, null, (Counter) null);
6869
}
6970

7071
/**
@@ -73,17 +74,21 @@ public DefaultMessageChannelMetrics(String name) {
7374
* @param name the name.
7475
* @param timer a timer.
7576
* @param errorCounter a counter.
77+
* @param receiveCounter a counter for receives.
78+
* @param receiveErrorCounter a counter for receive errors.
7679
* @since 5.0.2
7780
*/
78-
public DefaultMessageChannelMetrics(String name, Timer timer, Counter errorCounter) {
81+
public DefaultMessageChannelMetrics(String name, Timer timer, Counter errorCounter, Counter receiveCounter,
82+
Counter receiveErrorCounter) {
83+
7984
this(name, new ExponentialMovingAverage(DEFAULT_MOVING_AVERAGE_WINDOW, 1000000.),
80-
new ExponentialMovingAverageRate(
81-
ONE_SECOND_SECONDS, ONE_MINUTE_SECONDS, DEFAULT_MOVING_AVERAGE_WINDOW, true),
82-
new ExponentialMovingAverageRatio(
83-
ONE_MINUTE_SECONDS, DEFAULT_MOVING_AVERAGE_WINDOW, true),
84-
new ExponentialMovingAverageRate(
85-
ONE_SECOND_SECONDS, ONE_MINUTE_SECONDS, DEFAULT_MOVING_AVERAGE_WINDOW, true),
86-
timer, errorCounter);
85+
new ExponentialMovingAverageRate(
86+
ONE_SECOND_SECONDS, ONE_MINUTE_SECONDS, DEFAULT_MOVING_AVERAGE_WINDOW, true),
87+
new ExponentialMovingAverageRatio(
88+
ONE_MINUTE_SECONDS, DEFAULT_MOVING_AVERAGE_WINDOW, true),
89+
new ExponentialMovingAverageRate(
90+
ONE_SECOND_SECONDS, ONE_MINUTE_SECONDS, DEFAULT_MOVING_AVERAGE_WINDOW, true),
91+
timer, errorCounter, receiveCounter, receiveErrorCounter);
8792
}
8893

8994
/**
@@ -100,7 +105,8 @@ public DefaultMessageChannelMetrics(String name, Timer timer, Counter errorCount
100105
public DefaultMessageChannelMetrics(String name, ExponentialMovingAverage sendDuration,
101106
ExponentialMovingAverageRate sendErrorRate, ExponentialMovingAverageRatio sendSuccessRatio,
102107
ExponentialMovingAverageRate sendRate) {
103-
this(name, sendDuration, sendErrorRate, sendSuccessRatio, sendRate, null, null);
108+
109+
this(name, sendDuration, sendErrorRate, sendSuccessRatio, sendRate, null, null, null, null);
104110
}
105111

106112
/**
@@ -113,13 +119,17 @@ public DefaultMessageChannelMetrics(String name, ExponentialMovingAverage sendDu
113119
* @param sendSuccessRatio an {@link ExponentialMovingAverageRatio} for calculating the success ratio.
114120
* @param sendRate an {@link ExponentialMovingAverageRate} for calculating the send rate.
115121
* @param timer a timer.
116-
* @param errorCounter a counter.
122+
* @param errorCounter a counter for sends.
123+
* @param receiveCounter a counter for receives.
124+
* @param receiveErrorCounter a counter for receive errors.
117125
* @since 5.0.2
118126
*/
119127
public DefaultMessageChannelMetrics(String name, ExponentialMovingAverage sendDuration,
120128
ExponentialMovingAverageRate sendErrorRate, ExponentialMovingAverageRatio sendSuccessRatio,
121-
ExponentialMovingAverageRate sendRate, Timer timer, Counter errorCounter) {
122-
super(name, timer, errorCounter);
129+
ExponentialMovingAverageRate sendRate, Timer timer, Counter errorCounter, Counter receiveCounter,
130+
Counter receiveErrorCounter) {
131+
132+
super(name, timer, errorCounter, receiveCounter, receiveErrorCounter);
123133
this.sendDuration = sendDuration;
124134
this.sendErrorRate = sendErrorRate;
125135
this.sendSuccessRatio = sendSuccessRatio;
@@ -251,12 +261,22 @@ public Statistics getErrorRate() {
251261

252262
@Override
253263
public void afterReceive() {
254-
this.receiveCount.incrementAndGet();
264+
if (getReceiveCounter() != null) {
265+
getReceiveCounter().increment();
266+
}
267+
else {
268+
this.receiveCount.incrementAndGet();
269+
}
255270
}
256271

257272
@Override
258273
public void afterError() {
259-
this.receiveErrorCount.incrementAndGet();
274+
if (getReceiveErrorCounter() != null) {
275+
getReceiveErrorCounter().increment();
276+
}
277+
else {
278+
this.receiveErrorCount.incrementAndGet();
279+
}
260280
}
261281

262282
@Override

spring-integration-core/src/main/java/org/springframework/integration/support/management/IntegrationManagementConfigurer.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,13 @@ else if (bean instanceof MessageSourceMetrics) {
236236

237237
@SuppressWarnings("unchecked")
238238
private void configureChannelMetrics(String name, MessageChannelMetrics bean) {
239-
AbstractMessageChannelMetrics metrics = this.metricsFactory.createChannelMetrics(name);
239+
AbstractMessageChannelMetrics metrics;
240+
if (bean instanceof PollableChannelManagement) {
241+
metrics = this.metricsFactory.createPollableChannelMetrics(name);
242+
}
243+
else {
244+
metrics = this.metricsFactory.createChannelMetrics(name);
245+
}
240246
Assert.state(metrics != null, "'metrics' must not be null");
241247
ManagementOverrides overrides = bean.getOverrides();
242248
Boolean enabled = PatternMatchUtils.smartMatch(name, this.enabledCountsPatterns);

spring-integration-core/src/main/java/org/springframework/integration/support/management/MetricsFactory.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2016 the original author or authors.
2+
* Copyright 2015-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.integration.support.management;
1818

19-
2019
/**
2120
* Factories implementing this interface provide metric objects for message channels and
2221
* message handlers.
@@ -34,6 +33,17 @@ public interface MetricsFactory {
3433
*/
3534
AbstractMessageChannelMetrics createChannelMetrics(String name);
3635

36+
/**
37+
* Factory method to create an {@link AbstractMessageChannelMetrics} for
38+
* a pollable channel.
39+
* @param name the name.
40+
* @return the metrics.
41+
* @since 5.0.2
42+
*/
43+
default AbstractMessageChannelMetrics createPollableChannelMetrics(String name) {
44+
return createChannelMetrics(name);
45+
}
46+
3747
/**
3848
* Factory method to create an {@link AbstractMessageHandlerMetrics}.
3949
* @param name the name.

0 commit comments

Comments
 (0)