You can use PromQL to query Prometheus monitoring data to verify the validity and timeliness of alert rules. This topic describes how to use PromQL to query Prometheus monitoring data.

Table of contents

Use the console to query Prometheus monitoring data

Use the console to query the monitoring data of Prometheus Service

  1. Log on to the ARMS console.
  2. In the left-side navigation pane, choose Prometheus Service > Prometheus Instances.
  3. In the upper-left corner of the Prometheus Monitoring page, select the region in which your Container Service for Kubernetes (ACK) cluster is deployed and click the Prometheus instance that you want to manage.
  4. In the left-side navigation pane, click Dashboards. On the page that appears, click the name of the dashboard that you want to view.
  5. In the left-side navigation pane of the dashboard page, click the Dashboard icon. In the upper part of the page, select a data source and enter a PromQL statement to query the monitoring data.

Use the console to query the monitoring data of open source Prometheus

  1. Run the following command to map Prometheus in the cluster to local port 9090:
    kubectl port-forward svc/ack-prometheus-operator-prometheus 9090:9090 -n monitoring
  2. Enter localhost:9090 into the address bar of the web browser to query the monitoring data by using PromQL on the homepage of the console.

Use the API to query Prometheus monitoring data

Use the API to query the monitoring data of Prometheus Service

  1. Log on to the ARMS console.
  2. In the left-side navigation pane, choose Prometheus Service > Prometheus Instances.
  3. On the Prometheus Monitoring page, select the region where the Prometheus instance that you want to manage is deployed and click Settings in the Actions column.
  4. On the Settings page, copy the public or internal endpoint of the Prometheus instance in the HTTP API Address section. The endpoint is used to import monitoring data to Grafana.
    pg_pm_settings_tab_agent_settings
  5. Optional:Click Generate Token to obtain the authentication token for the Prometheus instance. The token is used to ensure data security when monitoring data is imported to Grafana.
    Important 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
  6. Send HTTP API requests to query the monitoring data of Prometheus Service.
    The following code block provides an example. 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"
    
    }
                            

Use the API to query the monitoring data of open source Prometheus

  1. Obtain the HTTP API endpoint.

    If you access open source Prometheus from within the cluster, the default endpoint of the Service used to expose the ack-prometheus-operator component is http://ack-prometheus-operator-prometheus.monitoring:9090.

    If you access open source Prometheus over the Internet, you must associate a Server Load Balancer (SLB) instance with the Service used to expose the ack-prometheus-operator component. To do this, perform the following steps.

    1. Log on to the ACK console and click Clusters in the left-side navigation pane.
    2. On the Clusters page, click the name of a cluster and choose Network > Services in the left-side navigation pane.
    3. In the upper part of the Services page, set Namespace to monitoring. Then, find ack-prometheus-operator-prometheus and click Update in the Actions column.
    4. In the Update Service dialog box, set Type to Server Load Balancer and then select Public Access. Click Add next to Port Mapping. Enter a port name, set Service Port to 9090, Container Port to 9090, and Protocol to TCP. Then, click OK.
      On the Services page, you can view the public IP address of the SLB instance that is created for the ack-prometheus-operator-prometheus Service in the External Endpoint column. The IP address is the HTTP API endpoint.
  2. Send HTTP API requests to query the monitoring data of Prometheus Service.
    • Query real-time monitoring data. The following code block provides an example:
      GET {HTTP API}/api/v1/query
      
      Headers:
      Accept: application/json
      Content-Type: application/json
      Param:
      {
        "query":"{{PromQL}}",
        "time":"1635302655",
        "timeout":"1000"
      
      }
      • time: specifies the point in time when the monitoring data is generated.
      • timeout: specifies the timeout period of the query. Unit: milliseconds.
    • Query the monitoring data that is generated within the specified time range.
      GET {HTTP API}/api/v1/query_range
      
      Headers:
      Accept: application/json
      Content-Type: application/json
      Param:
      {
        "query":"{{PromQL}}",
        "start": 1673946024,
        "end": 1673949624,
          "step": 30
      }
      • start: the start time.
      • end: the end time.
      • step: the interval between data points.

References