Developing a metrics monitoring component seems simple, but deceptively so.
While adding a simple counter component is easy enough, more advanced functionalities (polling, filtering, exporting the results… ) are somewhat trickier to code. Also since the metrics must be read and written in parallel with the core application logic, the code will be multithreaded, which adds another challenge: concurrency is hard.
So an in-house solution is doable, but will probably take at least a few days to develop, and that’s assuming it’s bug free.
The alternative is to re-use existing libraries which neatly solve this problem by providing monitoring components such as counter, gauges, timers, and the ability to export the data via JMX. For instance:
Example: the java code below calculates as many prime numbers as possible for a specified period of time, and uses Servo’s counter and peak rate counter to keep track of the total number of primes discovered, and the maximum rate of primes discovered per second.
The metrics are automatically exposed via JMX so Java Mission Control can pick it up:
…and chart the resulting data.