Light-4j provide Prometheus metrics handler as MiddlewareHandler to collects the API runtime information and save it to Prometheus Metric data module. And then use pre-defined HttpHandler (PrometheusGetHandler) to expose the prometheus Metric data module;
Prometheus server will pull the metric from metrics_path which is configurable on
Prometheus config yml file (for exampe: /v1/prometheus) ; The pulling interval is second base on the config file (scrape_interval: 30s).
Configuration file (prometheus.yml) for the MiddlewareHandler (com.networknt.metrics.prometheus.PrometheusHandler)
# Metrics handler configuration
# If prometheus metrics handler is enabled or not
Change the enabled value to true to enable the Prometheus metrics handler;
Configuration for services
If user want to use Prometheus metrics handler as metrics MiddlewareHandler handler for the service, simply set the config “prometheusMetrics” as true
Config to generate new service by light-codegen
Here is an example of config.json for openapi generator.
The generated service will use Prometheus metrics handler as metrics MiddlewareHandler handler and the the API runtime information will be save to prometheus Metric data module.
Change existing service to use light-4j Prometheus MiddlewareHandler handler
If user want to change existing service which created before based on light-4j palteform (default use [InfluxDB metrics handler]: /concern/metrics/) to use Prometheus metrics handler, please change the service by following steps:
Change the service POM to add light-4j Prometheus MiddlewareHandler handler dependency:
Change the service.yml file to use Prometheus MiddlewareHandler handler:
# Exception Global exception handler that needs to be called first to wrap all middleware handlers and business handlers
# Metrics handler to calculate response time accurately, this needs to be the second handler in the chain.
Change the api spec (openapi.json or swagger.json) by adding Prometheus metrics_path:
The metrics_path is configurable. In light-codegen, we set it as /prometheus, but it can be change to any url user want. Then user just to to set the same “metrics_path” in the Prometheus config file.
Configuration for Prometheus (config with Consul )
Prometheus is configured via a configuration file. The file is written in YAML format, defined by the Prometheus scheme. By default, the targets ( nodes to monitor ) are provided statically in the prometheus config file. Static targets are simple when we know in advance our infrastructure.
But for the microservice architecture, service API will have dynamic target on the cloud. So our solution is use consul to provide targets for prometheus to monitor dynamically
In prometheus, we need to configure it use consul_sd_targets. Prometheus will then query consul http interface for the catalog of targets to monitor.
Here is the sample config file. It can be found from link: