Realization with Java EE

The following statistics implementation observes the domain event specified earlier and stores the information in the Prometheus counter metric:

import io.prometheus.client.Counter;

@ApplicationScoped
public class ManufacturingStatistics {

    private Counter createdCars;

    @PostConstruct
    private void initMetrics() {
        createdCars = Counter.build("cars_manufactured_total",
"Total number of manufactured cars") .labelNames("color", "engine") .register(); } public void carCreated(@Observes(during =
TransactionPhase.AFTER_SUCCESS) Specification spec) {

createdCars.labels(spec.getColor().name(),
spec.getEngine().name()).inc(); } }

The counter metric is created and registered to the Prometheus Client API. Measured values are qualified by the car color and engine type, which are taken into account when scraping the values.

In order to emit this information, the Prometheus servlet library can be included. This outputs all the registered metrics in the correct format. The monitoring servlet is configured via web.xml. It's also possible to include a JAX-RS resource to emit the data by accessing CollectorRegistry.defaultRegistry.

The emitted output will look similar to the following:

...
cars_manufactured_total{color="RED", engine="DIESEL"} 4.0
cars_manufactured_total{color="BLACK", engine="DIESEL"} 1.0

Java EE components, such as CDI events, support developers in integrating domain event metrics in a lean way. In the preceding example, the ManufacturingStatistics class is the only point that depends on the Prometheus API.

It's highly advisable to include the Prometheus Client API as a separate container image layer and not in the application artifact.

The monitoring solution scrapes and further processes the provided information, in order to gather the required business metrics. Scraping the counter of manufactured cars over time leads to the number of created cars per hour. This metric can be queried for the total number of cars as well as for specific color and engine combinations. The queries that define the business metrics can also be adapted and refined due to the requirements. The application ideally emits the needed atomic business-relevant metrics.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.164.34