Skip to content

Add gauges for queue channel size #3349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.concurrent.TimeUnit;

import org.springframework.integration.core.MessageSelector;
import org.springframework.integration.support.management.metrics.GaugeFacade;
import org.springframework.integration.support.management.metrics.MetricsCaptor;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.util.Assert;
Expand All @@ -49,6 +51,12 @@ public class QueueChannel extends AbstractPollableChannel implements QueueChanne

protected final Semaphore queueSemaphore = new Semaphore(0); // NOSONAR final

@Nullable
private GaugeFacade sizeGauge;

@Nullable
private GaugeFacade remainingCapacityGauge;

/**
* Create a channel with the specified queue.
*
Expand Down Expand Up @@ -79,6 +87,26 @@ public QueueChannel() {
this(new LinkedBlockingQueue<>());
}

@Override
public void registerMetricsCaptor(MetricsCaptor metricsCaptor) {
super.registerMetricsCaptor(metricsCaptor);
this.sizeGauge =
metricsCaptor.gaugeBuilder("spring.integration.channel.queue.size", this,
(channel) -> getQueueSize())
.tag("name", getComponentName() == null ? "unknown" : getComponentName())
.tag("type", "channel")
.description("The size of the queue channel")
.build();

this.remainingCapacityGauge =
metricsCaptor.gaugeBuilder("spring.integration.channel.queue.remaining.capacity", this,
(channel) -> getRemainingCapacity())
.tag("name", getComponentName() == null ? "unknown" : getComponentName())
.tag("type", "channel")
.description("The remaining capacity of the queue channel")
.build();
}

@Override
protected boolean doSend(Message<?> message, long timeout) {
Assert.notNull(message, "'message' must not be null");
Expand Down Expand Up @@ -207,4 +235,15 @@ public int getRemainingCapacity() {
}
}

@Override
public void destroy() {
super.destroy();
if (this.sizeGauge != null) {
this.sizeGauge.remove();
}
if (this.remainingCapacityGauge != null) {
this.remainingCapacityGauge.remove();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@ public void afterSingletonsInstantiated() {
}

private void injectCaptor() {
Map<String, IntegrationManagement> managed = this.applicationContext
.getBeansOfType(IntegrationManagement.class);
Map<String, IntegrationManagement> managed =
this.applicationContext.getBeansOfType(IntegrationManagement.class);
for (Entry<String, IntegrationManagement> entry : managed.entrySet()) {
IntegrationManagement bean = entry.getValue();
if (!getOverrides(bean).loggingConfigured) {
Expand All @@ -299,7 +299,7 @@ private void injectCaptor() {
@Override
public Object postProcessAfterInitialization(Object bean, String name) throws BeansException {
if (this.singletonsInstantiated) {
if (bean instanceof IntegrationManagement) {
if (this.metricsCaptor != null && bean instanceof IntegrationManagement) {
((IntegrationManagement) bean).registerMetricsCaptor(this.metricsCaptor);
}
return doConfigureMetrics(bean, name);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018-2019 the original author or authors.
* Copyright 2018-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -94,7 +94,7 @@ public class MicrometerMetricsTests {

@SuppressWarnings("unchecked")
@Test
public void testSend() {
public void testMicrometerMetrics() {
GenericMessage<String> message = new GenericMessage<>("foo");
this.channel.send(message);
assertThatExceptionOfType(MessagingException.class)
Expand Down Expand Up @@ -162,6 +162,16 @@ public void testSend() {
.tag("result", "success")
.counter().count()).isEqualTo(1);

this.queue.send(message);

assertThat(registry.get("spring.integration.channel.queue.size")
.tag("name", "queue")
.gauge().value()).isEqualTo(2d);

assertThat(registry.get("spring.integration.channel.queue.remaining.capacity")
.tag("name", "queue")
.gauge().value()).isEqualTo(8d);

assertThat(registry.get("spring.integration.send")
.tag("name", "nullChannel")
.tag("result", "success")
Expand Down Expand Up @@ -260,7 +270,7 @@ protected Object doReceive() {

@Bean
public QueueChannel queue() {
return new QueueChannel();
return new QueueChannel(10);
}

@Bean
Expand Down
14 changes: 14 additions & 0 deletions src/reference/asciidoc/metrics.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,20 @@ It is possible to customize the names and tags of `Meters` created by integratio
The https://github.com/spring-projects/spring-integration/blob/master/spring-integration-core/src/test/java/org/springframework/integration/support/management/micrometer/MicrometerCustomMetricsTests.java[MicrometerCustomMetricsTests] test case shows a simple example of how to do that.
You can also further customize the meters by overloading the `build()` methods on builder subclasses.

Starting with version 5.1.13, the `QueueChannel` exposes Micrometer gauges for queue size and remaining capacity:
Copy link
Contributor

@micheljung micheljung Jul 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@artembilan
Isn't this misleading? In the 5.1.x branch this might be true but 5.2.8.RELEASE and 5.3.2.RELEASE don't have this feature.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it even misleads you when you try to upgrade from 5.1.13 to those 5.2.8 or 5.3.2, because there is no that fix yet.
So, it was always about upgrading to the latest minor version of the particular point generation.


* `name`: `spring.integration.channel.queue.size`
* `tag`: `type:channel`
* `tag`: `name:<componentName>`
* `description`: `The size of queue channel`

and

* `name`: `spring.integration.channel.queue.remaining.capacity`
* `tag`: `type:channel`
* `tag`: `name:<componentName>`
* `description`: `The remaining.capacity of queue channel`

[[mgmt-channel-features]]
==== `MessageChannel` Metric Features

Expand Down