Skip to content

Instantly share code, notes, and snippets.

@fastnsilver
Last active October 30, 2020 21:09
Show Gist options
  • Save fastnsilver/f24e6172ea90e7f829b6 to your computer and use it in GitHub Desktop.
Save fastnsilver/f24e6172ea90e7f829b6 to your computer and use it in GitHub Desktop.
Spring Integration Management metrics exposed as Spring Boot Actuator PublicMetrics
public class SpringIntegrationManagementMetrics implements PublicMetrics, ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public Collection<Metric<?>> metrics() {
Collection<Metric<?>> result = new LinkedHashSet<Metric<?>>();
// Collect Spring Integration metrics; if available
// keys in each map are the bean names
// @formatter:off
Map<String, MessageChannelMetrics> messageChannelMetrics = applicationContext.getBeansOfType(MessageChannelMetrics.class);
Map<String, MessageHandlerMetrics> messageHandlerMetrics = applicationContext.getBeansOfType(MessageHandlerMetrics.class);
Map<String, MessageSourceMetrics> messageSourceMetrics = applicationContext.getBeansOfType(MessageSourceMetrics.class);
// @formatter:on
// one-by-one add Spring Boot Actuate Metrics
addChannelMetrics(messageChannelMetrics, result);
addHandlerMetrics(messageHandlerMetrics, result);
addSourceMetrics(messageSourceMetrics, result);
return result;
}
private void addChannelMetrics(Map<String, MessageChannelMetrics> messageChannelMetrics,
Collection<Metric<?>> result) {
// @formatter:off
if (!messageChannelMetrics.isEmpty()) {
String beanName = null;
MessageChannelMetrics metrics = null;
for (Map.Entry<String, MessageChannelMetrics> entry: messageChannelMetrics.entrySet()) {
beanName = entry.getKey();
metrics = entry.getValue();
result.add(new Metric<Double>(name(beanName, "send.max.duration"), metrics.getMaxSendDuration()));
result.add(new Metric<Double>(name(beanName, "error.mean.rate"), metrics.getMeanErrorRate()));
result.add(new Metric<Double>(name(beanName, "error.mean.ratio"), metrics.getMeanErrorRatio()));
result.add(new Metric<Double>(name(beanName, "send.mean.duration"), metrics.getMeanSendDuration()));
result.add(new Metric<Double>(name(beanName, "send.mean.rate"), metrics.getMeanSendRate()));
result.add(new Metric<Double>(name(beanName, "send.min.duration"), metrics.getMinSendDuration()));
result.add(new Metric<Long>(name(beanName, "send.success.count"), metrics.getSendCountLong()));
result.add(new Metric<Long>(name(beanName, "send.error.count"), metrics.getSendErrorCountLong()));
result.add(new Metric<Double>(name(beanName, "send.standarddeviation.duration"), metrics.getStandardDeviationSendDuration()));
result.add(new Metric<Double>(name(beanName, "send.time.since.lastSend"), metrics.getTimeSinceLastSend()));
}
}
// @formatter:on
}
private void addHandlerMetrics(Map<String, MessageHandlerMetrics> messageHandlerMetrics,
Collection<Metric<?>> result) {
// @formatter:off
if (!messageHandlerMetrics.isEmpty()) {
String beanName = null;
MessageHandlerMetrics metrics = null;
for (Map.Entry<String, MessageHandlerMetrics> entry: messageHandlerMetrics.entrySet()) {
beanName = entry.getKey();
metrics = entry.getValue();
result.add(new Metric<Long>(name(beanName, "active.count"), metrics.getActiveCountLong()));
result.add(new Metric<Long>(name(beanName, "error.count"), metrics.getErrorCountLong()));
result.add(new Metric<Long>(name(beanName, "handle.count"), metrics.getHandleCountLong()));
result.add(new Metric<Double>(name(beanName, "max.duration"), metrics.getMaxDuration()));
result.add(new Metric<Double>(name(beanName, "mean.duration"), metrics.getMeanDuration()));
result.add(new Metric<Double>(name(beanName, "min.duration"), metrics.getMinDuration()));
result.add(new Metric<Double>(name(beanName, "standarddeviation.duration"), metrics.getStandardDeviationDuration()));
}
}
// @formatter:on
}
private void addSourceMetrics(Map<String, MessageSourceMetrics> messageSourceMetrics,
Collection<Metric<?>> result) {
// @formatter:off
if (!messageSourceMetrics.isEmpty()) {
String beanName = null;
MessageSourceMetrics metrics = null;
for (Map.Entry<String, MessageSourceMetrics> entry: messageSourceMetrics.entrySet()) {
beanName = entry.getKey();
metrics = entry.getValue();
result.add(new Metric<Long>(name(beanName, "message.count"), metrics.getMessageCountLong()));
}
}
// @formatter:on
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
private static String name(String beanName, String suffix) {
return String.format("%s.%s", beanName, suffix);
}
}
@sworisbreathing
Copy link

any plans to PR this back into spring boot actuator?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment