Micrometer: Able to push data to kafka?

Created on 20 Feb 2019  路  10Comments  路  Source: micrometer-metrics/micrometer

Is it possible to push data to Kafka directly from the Spring app?
Ideally what I am trying to do is implementing a change where the metrics would be able to get published to Kafka using the line protocol (similar to a statsd format).
I know I can send the metrics to telegraf and from there to kafka but I was wondering if it'd be possible to send it directly?

I'm planning on implementing this feature but not sure if it's being worked on yet

question

All 10 comments

i was thinking of creating a kafka producer in the StatsdMetricsRegistry class using the reactor-kafka api? MY question would be would I be able to send the message as-is to Kafka using the api or would I have to format the message from a line protocol to something kafka would be able to recognise?

The StatsdMeterRegistry already has a Consumer<String> lineSink you can configure, which could be used to publish the StatsD format lines to Kafka (or anything else). See this part of the documentation: http://micrometer.io/docs/registry/statsD#_customizing_the_metrics_sink
As for the specifics of publishing Strings to Kafka, that's not related to Micrometer so it's better to find that answer elsewhere.

@shakuzen Thanks for the input. I'm now able to send messages to Kafka!

So I cloned the repo and made my changes on there. I now want to add this change to my personal project but cant since I'm unable to edit the StatsdMeterRegistry class (non-writable).. how would I then go about and implement my change so that the messages go to kafka?

You don't need to edit the StatsdMeterRegistry source to configure a line sink. You can configure it when building a StatsdMeterRegistry like the following (also mentioned in the documentation):

StatsdMeterRegistry.builder(statsdConfig)
        .lineSink(myLineSink) // <-- configure your line sink here
        .build();

If you're using Spring Boot, you can register the StatsdMeterRegistry as a bean and autowire the statsdConfig that Spring Boot creates.

@shakuzen Thanks for the response!

One question, what would happen to the implementation of the start() method in the StatsdMeterReigstry class? What I mean is that since I'll be sending out these messages to kafka using linkSink, will micrometer still continue to pass the messages to the udp port configured in StatsdProperties and attempt to send it on that port?

will micrometer still continue to pass the messages to the udp port configured in StatsdProperties and attempt to send it on that port?

No. If a lineSink is configured, only that will be used instead of the default behavior of sending to StatsD directly via UDP or TCP. I'll update the documentation to make this more clear. See also https://github.com/micrometer-metrics/micrometer-docs/issues/45

The code with this logic is
https://github.com/micrometer-metrics/micrometer/blob/6b13b1edad4675a2a3d7a774ee60fe9527a35fe9/implementations/micrometer-registry-statsd/src/main/java/io/micrometer/statsd/StatsdMeterRegistry.java#L188-L189

@shakuzen Ah okay. Thanks for the quick replies. And so just to confirm:

You don't need to edit the StatsdMeterRegistry source to configure a line sink. You can configure it when building a StatsdMeterRegistry like the following (also mentioned in the documentation):
If you're using Spring Boot, you can register the StatsdMeterRegistry as a bean and autowire the statsdConfig that Spring Boot creates.

So based on this, can I simply create a class, create an instance of StatsdMeterRegistry using the builder method, turn that instance into a bean and spring will take care of the rest? Also how would the metrics know to use my registry? For example if i use the @Timed annotation, how would it know to use my custom registry.. or would I have to implement all individual metric components and register them to my registry like this:

StatsdMeterRegistry kafkaregistry = StatsdMeterRegistry
                .builder(StatsdConfig.DEFAULT)
                .lineSink(s -> {
                            /*Kafka implementation here*/
                        }
                ).build();
    Timer timer = Timer.builder("my.timer")
            .description("random desc")
            .register(kafkaregistry);`

So based on this, can I simply create a class, create an instance of StatsdMeterRegistry using the builder method, turn that instance into a bean and spring will take care of the rest?

That's really a Spring Boot question rather than a Micrometer question, but the simple answer is yes. If you make your own StatsdMeterRegistry bean, Spring Boot will use that the same way as the one it would have made in auto-configuration if yours didn't exist.

https://github.com/spring-projects/spring-boot/blob/ca0de4385c01cbbd120e96ec9f83427fb48c9a15/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdMetricsExportAutoConfiguration.java#L60-L65

@shakuzen

Thanks for the replies. Honestly been a great help!

Sorry for replying late but I now want your input as to what I'm planning on doing.

Essentially I have a few teams in the company. I want them to send their metrics using spring micrometer to my influx db through the use of kafka.

What would be the best way to achieve this? What I was thinking is that I could implement my feature on the influxdb module in this repo and then package this project and use it within the company (sort of like jfrog, nexus etc) . The downside of this is that with every release of micrometer I'd need to manually implement my feature..

I'm new to contributing to open source as well as this scene and even spring in general. so I'd like your opinion on how to achieve this.

Thank you. You've been great help.

Assuming all of the applications use Spring Boot, you could make an auto-configuration project that all the applications depend on, and in the auto-config provide the StatsdMeterRegistry bean with the line sink that publishes to Kafka. That would not require maintaining a fork of Micrometer.
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-developing-auto-configuration

Was this page helpful?
0 / 5 - 0 ratings