Observe metrics using Prometheus

Ballerina provides metrics compatible with Prometheus, which is widely used worldwide to monitor open-source systems.

The sample shop service will be used in this guide. Follow the steps given below to observe Ballerina metrics in Prometheus.

Step 1 - Set up Prometheus

Prometheus is used as the monitoring system, which pulls out the metrics collected from the Ballerina /metrics service. This section focuses on the quick installation of Prometheus with Docker and the configuration required to collect metrics from the Ballerina service with the default configurations. Follow the steps below to configure Prometheus.

Tip: There are many other ways to install Prometheus and you can find possible options from the installation guide. The easiest option is to use precompiled binaries listed in Downloads.

  1. Create a prometheus.yml file in a directory.

  2. Add the following content to the prometheus.yml file.

    Copy
    global:
      scrape_interval:     15s
      evaluation_interval: 15s
    
    scrape_configs:
      - job_name: 'prometheus'
        static_configs:
          - targets: ['a.b.c.d:9797']

    Here, the 'a.b.c.d:9797' targets should contain the host and port of the /metrics service that is exposed from Ballerina for metrics collection. Add the IP of the host in which the Ballerina service is running as a.b.c.d and its port (default 9797). If you need more information, go to the Prometheus documentation. If your Ballerina service is running on localhost and Prometheus in a Docker container, add the target as host.docker.internal:9797 to access the localhost from Docker.

  3. Start the Prometheus server in a Docker container with the command below.

    Copy
    $ docker run -p 9090:9090 -v <path_to_prometheus.yml>:/etc/prometheus/ prom/prometheus
    

Step 2 - Import Ballerina Prometheus extension

To include the Prometheus extension into the executable, the ballerinax/prometheus module needs to be imported into your Ballerina project main.bal file.

Copy
import ballerinax/prometheus as _;

To support Prometheus as the metrics reporter, an HTTP endpoint starts with the context of /metrics in the default port 9797 when starting the Ballerina service.

Step 3 - Configure Ballerina runtime configurations

You can set up Prometheus for your Ballerina project using configurations similar to the following in your Config.toml file.

Copy
[ballerina.observe]
metricsEnabled=true
metricsReporter="prometheus"

[ballerinax.prometheus]
port=9797
host="0.0.0.0"
Configuration keyDescriptionDefault valuePossible values
ballerinax.prometheus.portThe value of the port to which the '/metrics' service will bind. This service will be used by Prometheus to scrape the information of the Ballerina service.9797Any suitable value for port 0 - 0 - 65535. However, within that range, ports 0 - 1023 are generally reserved for specific purposes. Therefore, it is advisable to select a port outside that range.
ballerinax.prometheus.hostThe name of the host to which the '/metrics' service will bind. This service will be used by Prometheus to scrape the information of the Ballerina service.0.0.0.0IP or Hostname or 0.0.0.0 of the node in which the Ballerina service is running.

Step 4 - Run the Ballerina service

When Ballerina observability is enabled, the Ballerina runtime exposes internal metrics via an HTTP endpoint (/metrics) for metrics monitoring, and the metrics will be published to Prometheus. Prometheus should be configured to scrape metrics from the metrics HTTP endpoint in Ballerina.

Run the following command to start the Ballerina service.

Copy
$ bal run

Compiling source

Running executable

ballerina: started Prometheus HTTP listener 0.0.0.0:9797

Step 5 - Send requests

Send requests to http://localhost:8090/shop/products.

Example cURL commands:

Copy
$ curl -X GET http://localhost:8090/shop/products
Copy
$ curl -X POST http://localhost:8090/shop/product \
-H "Content-Type: application/json" \
-d '{
    "id": 4, 
    "name": "Laptop Charger", 
    "price": 50.00
}'
Copy
$ curl -X POST http://localhost:8090/shop/order \
-H "Content-Type: application/json" \
-d '{
    "productId": 1, 
    "quantity": 1
}'
Copy
$ curl -X GET http://localhost:8090/shop/order/0

Step 6 - View metrics on the Prometheus server

Go to http://localhost:19090/ and check whether you can see the Prometheus graph. Ballerina metrics should appear in the Prometheus graph's metrics list when the Ballerina service is started.

Ballerina metrics listed in Prometheus

Ballerina metric in graph format

You can also use the following command to get the metrics.

Copy
$ curl http://localhost:9797/metrics

Set up Grafana

Grafana can be used to visualize Ballerina metrics provided for Prometheus. First, users need to set up the Ballerina project to observe metrics in Prometheus and follow the steps mentioned above.

Let’s use Grafana to visualize metrics in a dashboard. For this, we need to install Grafana and configure Prometheus as a data source. Follow the steps below to configure Grafana.

  1. Start Grafana as a Docker container with the command below.

    Copy
    $ docker run -d --name=grafana -p 3000:3000 grafana/grafana
    

    For more information, go to Grafana in Docker Hub.

  2. Go to http://localhost:3000/ to access the Grafana dashboard running on Docker.

  3. Login to the dashboard with the default user, username: admin and password: admin

  4. Add Prometheus as a data source with the Browser access configuration as provided below.

    Grafana Prometheus data source

  5. Import the Grafana dashboard designed to visualize Ballerina metrics from https://grafana.com/dashboards/5841 as shown below. Import dashboard for Ballerina

This dashboard consists of service and client invocation level metrics in near real-time view.

The Ballerina HTTP service metrics dashboard panel will be as shown below.

Ballerina metrics dashboard