Skip to content

Commit 3ef667f

Browse files
committed
Fix MetricWriterMessageHandler to deal with reset
Update MetricWriterMessageHandler to deal with 'reset' messages and to log unsupported payload types. Fixes gh-3378
1 parent fa7199d commit 3ef667f

File tree

4 files changed

+143
-23
lines changed

4 files changed

+143
-23
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2013 the original author or authors.
2+
* Copyright 2012-2015 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.
@@ -18,21 +18,17 @@
1818

1919
import org.springframework.boot.actuate.metrics.Metric;
2020
import org.springframework.messaging.MessageChannel;
21-
import org.springframework.messaging.support.MessageBuilder;
2221

2322
/**
2423
* A {@link MetricWriter} that publishes the metric updates on a {@link MessageChannel}.
2524
* The messages have the writer input ({@link Delta} or {@link Metric}) as payload, and
2625
* carry an additional header "metricName" with the name of the metric in it.
2726
*
2827
* @author Dave Syer
28+
* @see MetricWriterMessageHandler
2929
*/
3030
public class MessageChannelMetricWriter implements MetricWriter {
3131

32-
private static final String METRIC_NAME = "metricName";
33-
34-
private final String DELETE = "delete";
35-
3632
private final MessageChannel channel;
3733

3834
public MessageChannelMetricWriter(MessageChannel channel) {
@@ -41,20 +37,17 @@ public MessageChannelMetricWriter(MessageChannel channel) {
4137

4238
@Override
4339
public void increment(Delta<?> delta) {
44-
this.channel.send(MessageBuilder.withPayload(delta)
45-
.setHeader(METRIC_NAME, delta.getName()).build());
40+
this.channel.send(MetricMessage.forIncrement(delta));
4641
}
4742

4843
@Override
4944
public void set(Metric<?> value) {
50-
this.channel.send(MessageBuilder.withPayload(value)
51-
.setHeader(METRIC_NAME, value.getName()).build());
45+
this.channel.send(MetricMessage.forSet(value));
5246
}
5347

5448
@Override
5549
public void reset(String metricName) {
56-
this.channel.send(MessageBuilder.withPayload(this.DELETE)
57-
.setHeader(METRIC_NAME, metricName).build());
50+
this.channel.send(MetricMessage.forReset(metricName));
5851
}
5952

6053
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2012-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.metrics.writer;
18+
19+
import org.springframework.boot.actuate.metrics.Metric;
20+
import org.springframework.messaging.Message;
21+
import org.springframework.messaging.support.MessageBuilder;
22+
23+
/**
24+
* A metric message sent via Spring Integration.
25+
*
26+
* @author Phillip Webb
27+
*/
28+
class MetricMessage {
29+
30+
private static final String METRIC_NAME = "metricName";
31+
32+
private static final String DELETE = "delete";
33+
34+
private final Message<?> message;
35+
36+
public MetricMessage(Message<?> message) {
37+
this.message = message;
38+
}
39+
40+
public boolean isReset() {
41+
return DELETE.equals(getPayload());
42+
}
43+
44+
public Object getPayload() {
45+
return this.message.getPayload();
46+
}
47+
48+
public String getMetricName() {
49+
return this.message.getHeaders().get(METRIC_NAME, String.class);
50+
}
51+
52+
public static Message<?> forIncrement(Delta<?> delta) {
53+
return forPayload(delta.getName(), delta);
54+
}
55+
56+
public static Message<?> forSet(Metric<?> value) {
57+
return forPayload(value.getName(), value);
58+
}
59+
60+
public static Message<?> forReset(String metricName) {
61+
return forPayload(metricName, DELETE);
62+
}
63+
64+
private static Message<?> forPayload(String metricName, Object payload) {
65+
MessageBuilder<Object> builder = MessageBuilder.withPayload(payload);
66+
builder.setHeader(METRIC_NAME, metricName);
67+
return builder.build();
68+
}
69+
70+
}

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/writer/MetricWriterMessageHandler.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2013 the original author or authors.
2+
* Copyright 2012-2015 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,6 +16,8 @@
1616

1717
package org.springframework.boot.actuate.metrics.writer;
1818

19+
import org.apache.commons.logging.Log;
20+
import org.apache.commons.logging.LogFactory;
1921
import org.springframework.boot.actuate.metrics.Metric;
2022
import org.springframework.messaging.Message;
2123
import org.springframework.messaging.MessageHandler;
@@ -26,9 +28,12 @@
2628
* {@link MetricWriter}.
2729
*
2830
* @author Dave Syer
31+
* @see MessageChannelMetricWriter
2932
*/
3033
public final class MetricWriterMessageHandler implements MessageHandler {
3134

35+
private static final Log logger = LogFactory.getLog(MetricWriterMessageHandler.class);
36+
3237
private final MetricWriter observer;
3338

3439
public MetricWriterMessageHandler(MetricWriter observer) {
@@ -37,14 +42,28 @@ public MetricWriterMessageHandler(MetricWriter observer) {
3742

3843
@Override
3944
public void handleMessage(Message<?> message) throws MessagingException {
45+
handleMessage(new MetricMessage(message));
46+
}
47+
48+
private void handleMessage(MetricMessage message) {
4049
Object payload = message.getPayload();
41-
if (payload instanceof Delta) {
50+
if (message.isReset()) {
51+
this.observer.reset(message.getMetricName());
52+
}
53+
else if (payload instanceof Delta) {
4254
Delta<?> value = (Delta<?>) payload;
4355
this.observer.increment(value);
4456
}
45-
else {
57+
else if (payload instanceof Metric) {
4658
Metric<?> value = (Metric<?>) payload;
4759
this.observer.set(value);
4860
}
61+
else {
62+
if (logger.isWarnEnabled()) {
63+
logger.warn("Unsupported metric payload "
64+
+ (payload == null ? "null" : payload.getClass().getName()));
65+
}
66+
}
4967
}
50-
}
68+
69+
}

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/writer/MessageChannelMetricWriterTests.java

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2013 the original author or authors.
2+
* Copyright 2012-2015 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,35 +16,73 @@
1616

1717
package org.springframework.boot.actuate.metrics.writer;
1818

19+
import org.junit.Before;
1920
import org.junit.Test;
21+
import org.mockito.Mock;
22+
import org.mockito.MockitoAnnotations;
23+
import org.mockito.invocation.InvocationOnMock;
24+
import org.mockito.stubbing.Answer;
2025
import org.springframework.boot.actuate.metrics.Metric;
2126
import org.springframework.messaging.Message;
2227
import org.springframework.messaging.MessageChannel;
2328

29+
import static org.mockito.BDDMockito.given;
2430
import static org.mockito.Matchers.any;
25-
import static org.mockito.Mockito.mock;
2631
import static org.mockito.Mockito.verify;
2732

2833
/**
34+
* Tests for {@link MessageChannelMetricWriter} and {@link MetricWriterMessageHandler}.
35+
*
2936
* @author Dave Syer
3037
*/
3138
public class MessageChannelMetricWriterTests {
3239

33-
private final MessageChannel channel = mock(MessageChannel.class);
40+
@Mock
41+
private MessageChannel channel;
42+
43+
@Mock
44+
private MetricWriter observer;
45+
46+
private MessageChannelMetricWriter writer;
3447

35-
private final MessageChannelMetricWriter observer = new MessageChannelMetricWriter(
36-
this.channel);
48+
private MetricWriterMessageHandler handler;
49+
50+
@Before
51+
public void setup() {
52+
MockitoAnnotations.initMocks(this);
53+
given(this.channel.send(any(Message.class))).willAnswer(new Answer<Object>() {
54+
55+
@Override
56+
public Object answer(InvocationOnMock invocation) throws Throwable {
57+
MessageChannelMetricWriterTests.this.handler.handleMessage(invocation
58+
.getArgumentAt(0, Message.class));
59+
return true;
60+
}
61+
62+
});
63+
this.writer = new MessageChannelMetricWriter(this.channel);
64+
this.handler = new MetricWriterMessageHandler(this.observer);
65+
}
3766

3867
@Test
3968
public void messageSentOnAdd() {
40-
this.observer.increment(new Delta<Integer>("foo", 1));
69+
this.writer.increment(new Delta<Integer>("foo", 1));
4170
verify(this.channel).send(any(Message.class));
71+
verify(this.observer).increment(any(Delta.class));
4272
}
4373

4474
@Test
4575
public void messageSentOnSet() {
46-
this.observer.set(new Metric<Double>("foo", 1d));
76+
this.writer.set(new Metric<Double>("foo", 1d));
77+
verify(this.channel).send(any(Message.class));
78+
verify(this.observer).set(any(Metric.class));
79+
}
80+
81+
@Test
82+
public void messageSentOnReset() throws Exception {
83+
this.writer.reset("foo");
4784
verify(this.channel).send(any(Message.class));
85+
verify(this.observer).reset("foo");
4886
}
4987

5088
}

0 commit comments

Comments
 (0)