All Products
Search
Document Center

Managed Service for Prometheus:Query metric data and configure a data source for a self-managed Grafana system

Last Updated:Jul 19, 2024

Alibaba Cloud Managed Service for Prometheus allows you to query metric data in multiple scenarios. This topic describes how to query metric data in the Managed Service for Prometheus console, use the Prometheus HTTP API to obtain data, configure a Prometheus data source for a self-managed Grafana system, and configure a remote read endpoint for a self-managed Prometheus system.

Prerequisites

Managed Service for Prometheus is activated. At least one component is enabled. For more information, see Component management.

Use the Managed Service for Prometheus console to query metric data

  1. Log on to the Managed Service for Prometheus console.

  2. In the left-side navigation pane, click Integration Management.

  3. On the Integrated Environments tab of the Integration Management page, click the name of the environment that you want to manage.

    image

  4. On the environment details page, click the Metrics Explorer tab.

  5. In the Explore section, select a metric from the Metric drop-down list and add a key-value pair in the Labels field. Then, click Run query.

    image

    Note
    • Click image to select a time range to query. By default, data within one hour is queried.

    • Click + Operations to add a PromQL function, such as sum(), irate(), or absent(). For more information, see Prometheus documentation.

    • Change to the Code mode to manually enter a PromQL statement. For more information about the PromQL syntax, see Prometheus documentation.

Use the Prometheus HTTP API to obtain metric data

Step 1: Obtain the HTTP API URL

  1. Log on to the Managed Service for Prometheus console.

  2. In the left-side navigation pane, click Instances.

  3. In the top navigation bar, select the region where the Prometheus instance resides. On the Instances page, find the instance and click Settings in the Actions column.

  4. On the Settings tab of the page that appears, select a public or internal HTTP API URL in the HTTP API URL (Grafana Read URL) section.

    image

    Note

    If you want to improve the security of data reads, click Generate Token to obtain the token of the Prometheus instance. After the token is generated, you must configure the token whenever you initiate an HTTP request. Otherwise, the monitoring data of the Prometheus instance cannot be read.

Step 2: Use the HTTP API URL to obtain monitoring data

The following example shows how to query Prometheus monitoring data. For more information about how to use the Prometheus HTTP API to query Prometheus monitoring data, see Prometheus documentation.

Note
  • Replace {HTTP API} and {Token} with the HTTP API and token in the HTTP API URL.

  • Replace {PromQL} with the PromQL statement.

Example:

curl -H "Authorization:{Token}" "{HTTP API}/api/v1/query?query={PromQL}&time=1709171676&timeout=1000"

Configure a Prometheus data source for a self-managed Grafana system

Step 1: Obtain the URL of the Prometheus server

For information about how to obtain the URL of the Prometheus server, see Step 1: Obtain the HTTP API URL.

Step 2: Configure a data source for a self-managed Grafana system

Note

In this example, a self-managed Grafana v10.0.x system is used.

  1. Log on to the Grafana system as an administrator.

    Note

    The first time that you log on to a self-managed Grafana system, use the initial username and password, which are both admin. Then, change the password of the Grafana system as prompted.

  2. After you log on to the Grafana system, click the image icon in the upper-left corner.

  3. In the left-side navigation pane, choose Configuration > Data sources.

  4. On the Data sources page, click Add data source.

  5. On the Add data source page, click Prometheus.

  6. On the Settings tab, enter a dashboard name in the Name field, enter the Prometheus server URL that you obtained in Step 1: obtain the URL of the Prometheus server in the Prometheus server URL field of the HTTP section.

    image

    Note

    If you have generated a token, click + Add header in the Custom HTTP Headers section, set the Header parameter to Authorization, and then set the Value parameter to the token generated in Step 1: obtain the URL of the Prometheus server.

  7. Click Save & test.

Step 3: Verify the result

  1. Log on to the Grafana system as an administrator.

  2. Click the image icon in the upper-left corner of the page. In the left-side navigation pane, click Dashboards.

  3. On the Dashboards page, click New, and then select New dashboard from the drop-down menu.

  4. Click + Add visualization. On the Select data source page, select a custom data source.

  5. In the A section, enter a metric name in the Metrics field and press Enter. If a chart of the metric is displayed, the Prometheus data source is added.

    image

    Note

    If no chart is displayed, the Prometheus data source fails to be added. Check whether the API URL or token is valid, and whether the data source has monitoring data.

Configure a remote read endpoint for a self-managed Prometheus system

Limits

Managed Service for Prometheus remote write does not support HTTP/2.

Step 1: Obtain a remote read endpoint

  1. Log on to the Managed Service for Prometheus console

  2. In the left-side navigation pane, click Instances.

  3. In the top navigation bar, select the region where the Prometheus instance resides. On the Instances page, find the instance and click Settings in the Actions column.

  4. On the Settings tab, select a public or internal remote read endpoint.

    image

Step 2: Configure the remote read endpoint

  1. Visit the Prometheus official website to download open source Prometheus.

  2. Decompress the Prometheus installation package.

  3. Open the Prometheus configuration file prometheus.yml, add the following content to the end of the file, replace remote_read.url with the remote read endpoint obtained in Step 1, save the file, and start open source Prometheus.

    remote_read:
      # Enter the remote read endpoint.
      - url: "http://xx-xxx.aliyuncs.com:9443/xxx/api/v1/read"
        read_recent: true

Step 3: View the data of Managed Service for Prometheus in open source Prometheus

Access http://localhost:9090 in a browser, enter a metric name in the image.png search box, and click Execute on the right side of the page to remotely read the monitoring data of Managed Service for Prometheus.

image