2

I'm new to using Micrometer and am trying to see if there's a way to use a Timer that would also include a count of the number of items in a batch processing scenario. Since I'm processing the batch with Java streams, I didn't see an obvious way to record the timer for each item processed, so I was looking for a way to set a batch size attribute. One way I think that could work is to use the FunctionTimer from https://micrometer.io/docs/concepts#_function_tracking_timers, but I believe that requires the app to maintain a persistent monotonically increasing set of values for the total count and total time.

Is there a simpler way this can be done? Ultimately this data will be fed to New Relic. I've also tried setting tags for the batch size, but those seem to be reported as strings so I can't do any type of aggregation on the values.

Thanks!

  • Welcome to Stack Overflow! Can you please clarify if you are intending to time how long the entire batch is taking? Or just how long the individual elements in the batch take? – checketts Apr 18 '19 at 21:40
  • @checketts I think either way could work since we currently have Micrometer set to report every 30 seconds. Since I'm using Java Streams to process though, I didn't see a good way to time the individual processing. Thanks. – purginglisp Apr 18 '19 at 23:33

1 Answers1

0

A timer is intended for measuring an action and at a minimum results in two measurements: a count and a duration.

So a timer will work perfectly for your batch processing. In the the java stream, a peek operation might be a good place to put a timer.

If you were about to process 20 elements and you were just measuring the time for all 20 elements, you would need to create a new Counter for measuring the batch size. You could them divide the timer's total duration against your counter to get a per-item duration or divide it against the timer's total count to get a per-batch duration.

Feel free to add code snippets if you would like feedback for those.

checketts
  • 14,167
  • 10
  • 53
  • 82
  • Thanks. For using `peek`, could you clarify how you would see that working with the Timer? It's not clear to me how I could measure the elapsed time of processing each element in the stream independently. My stream processing is pretty straightforward, just doing map and collect, eg: `batch.stream().map(x -> x.doSomething()).map(y -> y.doSomething()).collect(toList())` For using both a Counter and a Timer, is what you are suggesting something we would do in our app code or where the data is being collected, in this case New Relic. – purginglisp Apr 19 '19 at 22:45