相关文章推荐
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have a java application written on top of Spring Boot and I can see the metrics being generated through the /metrics management API. I would like to filter the metrics that are being generated (based on metric prefix) and print to stdout OR send the selected metrics to a 3rd party aggregator (not the ones referenced here )

I tried the code suggested by this answer but it didn't result in any metrics being written to the stdout. This is what I added to my Application.java class:

@Bean
@ServiceActivator(inputChannel = "metricsChannel")
    public MessageHandler metricsHandler() {
        return System.out::println;

What is the best way to intercept the metrics on a preconfigured cadence so I can process and write them to stdout or publish them to an aggregator?

Thanks.

Would you mind to share a simple sample application to see more context? Maybe there is just something disabled... – Artem Bilan Mar 28, 2016 at 22:49

Looks like it's a bug in the Spring Boot: https://github.com/spring-projects/spring-boot/issues/5517.

We have to declare something like this ourselves as a workaround:

@Bean
public MessageChannel metricsChannel() {
    return new DirectChannel();
@Bean
@ExportMetricWriter
public MessageChannelMetricWriter messageChannelMetricWriter() {
    return new MessageChannelMetricWriter(metricsChannel());
@Bean
@ServiceActivator(inputChannel = "metricsChannel")
public MessageHandler metricsHandler() {
    return System.out::println;
                Thank you @artem. I think it got further but not quite outputting the message. The error is 2016-03-29 09:27:16.924  WARN 11264 --- [ask-scheduler-2] o.s.b.a.m.export.AbstractMetricExporter  : Could not write to MetricWriter: class org.springframework.messaging.MessageDeliveryException: Dispatcher has no subscribers for channel 'unknown.channel.name'.; nested exception is org.springframework.integration.MessageDispatchingException: Dispatcher has no subscribers ... I wonder which one is unknown.channel.name? Also, where would I inspect to only output metrics with a given prefix? Thanks.
– pastafarian
                Mar 29, 2016 at 16:30
                You should share your application. That isn't clear. Maybe you have a metricsChannel, but don't have metricsHandler bean for that...
– Artem Bilan
                Mar 29, 2016 at 16:33
                Thanks. I pushed the latest to the project above. I will follow also up on the metricsHandler hint.
– pastafarian
                Mar 29, 2016 at 16:48
                Well, you have to change @EnableAutoConfiguration and @ComponentScan to just the single @SpringBootApplication. Those doesn't mark the class with the @Component which is important for the scanning @ServiceActivator by Spring Integration.
– Artem Bilan
                Mar 29, 2016 at 17:00
                Thank you! I also implemented a MessageHandler in which I can look at the content of the message and take the next step. I pushed the working version to  github.com/khoubyari/spring-boot-metrics
– pastafarian
                Mar 29, 2016 at 19:09
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

 
推荐文章