Alibaba Cloud Prometheus Service (Prometheus) provides HTTP API URLs. You can use an HTTP API URL to connect a Prometheus instance to your custom Grafana dashboard and obtain the monitoring data of the Prometheus instance for custom development. This topic shows you how to use an HTTP API URL to connect a Prometheus instance to your custom Grafana dashboard and obtain the monitoring data of the Prometheus instance.

Obtain an HTTP API URL

  1. Log on to the ARMS console.
  2. In the left-side navigation pane, choose Prometheus Monitoring > Prometheus Instances.
  3. On the Prometheus Monitoring page, select the region where the Prometheus instance that you want to manage is located and click Settings in the Actions column of the instance.
  4. On the page that appears, click the Agent Settings tab.
  5. On the Agent Settings tab, copy an HTTP API URL for access from the Internet or from an internal network as needed.
    Note If the Prometheus instance is created to monitor a cloud service, copy the corresponding HTTP API URL based on the type of the cloud service.
    pg_pm_settings_tab_agent_settings
  6. Optional:Click Generate a token in the upper part of the tab to obtain the authentication token of the Prometheus instance. This way, data can be transferred from the Prometheus instance to your Grafana dashboard in a more secure way.
    Notice After a token is generated, you must specify the token when you add the Prometheus instance to your Grafana dashboard as a data source. Otherwise, monitoring data cannot be read from the Prometheus instance.
    Copy a token

Connect the Prometheus instance to your custom Grafana dashboard

  1. Configure a data source on the Grafana dashboard.
    1. Log on to the local Grafana system as the administrator.
    2. In the left-side navigation pane, choose Configuration > Data Sources.
      Note This menu is visible only to the administrator.
    3. On the Data Sources tab, click Add Data Source.
    4. On the Add Data Source page, click Prometheus.
    5. On the Settings tab, enter a name in the Name field and set the URL parameter to the HTTP API URL that you obtained in the Obtain an HTTP API URL section of this topic.
    6. Optional:In the Custom HTTP Headers section of the Settings tab, click Add header and set the Header parameter to Authorization and the Value parameter to the authentication token that you obtained in the Obtain an HTTP API URL section of this topic.
      tab_settings
    7. Click Save & Test in the lower part of the tab.
  2. Verify the data source.
    1. Log on to the local Grafana system.
    2. In the left-side navigation pane, choose + > Create.
    3. On the New dashboard page, click Add new panel.
    4. On the Query tab of the New dashboard / Edit Panel page, select the data source added in Step 1 from the drop-down list. In the Metrics field of the A section, enter a metric name and press Enter.
      If a chart is displayed for the metric, the data source is added. If no chart is displayed for the metric, check whether the HTTP API URL or the authentication token you entered is correct and whether the data source contains monitoring data. pg_explore_with_metrics

Obtain the monitoring data of the Prometheus instance

The following sample code shows how to send a request to obtain the monitoring data of the Prometheus instance by using the HTTP API URL. For more information, see HTTP API.

GET {HTTP API}/api/v1/query

Accept: application/json
Content-Type: application/json
Authorization: {Token}

{
  "query":"arms_prometheus_target_interval_length_seconds_sum",
  "time":"1635302655",
  "timeout":"1000"

}
Note Replace {HTTP API} and {Token} with the HTTP API URL and the authentication token that you obtained in the Obtain an HTTP API URL section of this topic.

Sample response:

{
    "status": "success",
    "data": {
        "resultType": "vector",
        "result": [
            {
                "metric": {
                    "__name__": "arms_prometheus_target_interval_length_seconds_sum",
                    "instance": "localhost:9335",
                    "interval": "15s",
                    "job": "_arms-prom/kubelet/1"
                },
                "value": [
                    1635302655,
                    "146655.24420603667"
                ]
            },
            {
                "metric": {
                    "__name__": "arms_prometheus_target_interval_length_seconds_sum",
                    "instance": "localhost:9335",
                    "interval": "30s",
                    "job": "_arms-prom/kubelet/1"
                },
                "value": [
                    1635302655,
                    "879810.747346541"
                ]
            },
            {
                "metric": {
                    "__name__": "arms_prometheus_target_interval_length_seconds_sum",
                    "instance": "localhost:9335",
                    "interval": "20s",
                    "job": "_arms-prom/kubelet/1"
                },
                "value": [
                    1635302655,
                    "73320.13578499513"
                ]
            }
        ]
    }