Help Center/ Cloud Container Engine/ Best Practices/ Monitoring/ Obtaining Prometheus Data Using PromQL Statements
Updated on 2025-01-08 GMT+08:00

Obtaining Prometheus Data Using PromQL Statements

Prometheus Query Language (PromQL) is a language that retrieves and consolidates time series data. Prometheus, an open-source monitoring system, is responsible for collecting and storing this data. Each time series has a unique identifier and a series of timestamp-value pairs. PromQL is a fundamental part of Prometheus, using simple expressions that include identifiers and tags to retrieve and consolidate time series data. This allows you to quickly identify and resolve issues as needed.

For details about how to use PromQL, see QUERYING PROMETHEUS.

Obtaining Huawei Cloud Prometheus Monitoring Data from the Console

  1. Install the Cloud Native Cluster Monitoring add-on in a cluster to collect Prometheus monitoring data.

    1. Log in to the CCE console and click the cluster name to access the cluster console. In the navigation pane, choose Add-ons.
    2. Locate the Cloud Native Cluster Monitoring add-on and click Install.
      You are advised to focus on the following configurations, and adjust any other configurations as necessary. For details, see Cloud Native Cluster Monitoring.
      • Report Monitoring Data to AOM: After this function is enabled, the add-on will report monitoring data to AOM.
      • Target AOM Instance: Select an AOM instance for the add-on to report metrics.

    3. Click Install.

  2. Go to the AOM console to check monitoring data.

    1. Log in to the AOM 2.0 console. In the navigation pane, choose Metric Browsing.
    2. Select the Prometheus instance interconnected with AOM, select Prometheus statement.
      For example, to obtain the number of requests received by CoreDNS and the response success rate, run the following PromQL statement:
      sum(rate(coredns_dns_responses_total{instance=~"10.0.0.2:9153",rcode=~"NOERROR|NXDOMAIN"}[5m]))/sum(rate(coredns_dns_responses_total{instance=~"10.0.0.2:9153"}[5m]))

      10.0.0.2 is the IP address of the CoreDNS container. The following figure shows the command output.

Obtaining Open-Source Prometheus Monitoring Data Through the Console

  1. After the Cloud Native Cluster Monitoring add-on is installed in a cluster, create a NodePort or LoadBalancer Service for Prometheus to access the external networks.

    After external access is provided, you can access the Prometheus web page without authentication, which poses security risks. If you have high security requirements, you can do security hardening. You can use, for example, the Nginx reverse proxy and HTTP basic authentication to protect the console and restrict users who can access the Prometheus web page.

    The following shows an example for creating a NodePort Service:
    apiVersion: v1
    kind: Service
    metadata:
      name: prom-np
      labels:
        app.kubernetes.io/name: prometheus
        prometheus: server
      namespace: monitoring
    spec:
      selector:
        app.kubernetes.io/name: prometheus
        prometheus: server
      externalTrafficPolicy: Cluster
      ports:
        - name: cce-service-0
          targetPort: 9090
          nodePort: 0
          port: 9090
          protocol: TCP
      type: NodePort

  2. After an EIP is bound to a node, enter Node EIP:Node port in the address box of a browser to obtain the monitoring data through PromQL.

Obtaining Huawei Cloud Prometheus Monitoring Data Through APIs

  1. Log in to the AOM 2.0 console. In the navigation pane, choose Prometheus Monitoring > Instances.
  2. Click the name of the Prometheus instance interconnected with AOM and click Settings.
  3. Check the Grafana data source configurations and copy the public or intranet HTTP URL as required.

    If no access code is created, create one first.

  4. Use an HTTP URL to obtain Prometheus monitoring data.

    The following shows an example. (For more information about how to use HTTP APIs to obtain Prometheus monitoring data, see HTTP API.)

    GET <HTTP URL>/api/v1/query
    Content-Type: application/json
    Authorization: <Token>
    {
    "query":" coredns_dns_responses_total",
    "time":" 1708655317.719",
    "timeout":"1000"
    }

    In the preceding command, <HTTP URL> specifies the HTTP URL in the previous step, and <Token> specifies the password obtained in the previous step. Other URL query parameters are as follows:

    • query: Prometheus expression query string
    • time: timestamp for obtaining monitoring data
    • timeout: timeout interval, in milliseconds

    The following shows an example command:

    curl -H 'Authorization: <Token>' -H 'Content-Type: application/json' -X GET '<HTTP URL>/api/v1/query?query=coredns_dns_responses_total&time=1708655317.719&timeout=1000'

    The expected results are as follows:

Obtaining Open-Source Prometheus Monitoring Data Through APIs

  1. Obtain the HTTP API URL.

    • If you access open-source Prometheus in a cluster, the HTTP API UR is the default Service address http://<cluster IP>:9090 of Prometheus server.
    • If you use the public network to access open-source Prometheus, you need to associate a LoadBalancer Service to the Prometheus server. http://<ELB IP>:9090 is the HTTP API URL.

  2. Use HTTP APIs to obtain Prometheus monitoring data.

    The following describes how to obtain open-source Prometheus monitoring data through the HTTP APIs in the cluster.

    • Obtain instantaneous monitoring data.
      GET <HTTP API>/api/v1/query
      Content-Type: application/json
      Param:
      {
      "query":" coredns_dns_responses_total",
      "time":" 1708655317.719",
      "timeout":"1000"
      }

      The following shows an example command:

      curl -X GET -H "Content-Type: application/json" '<HTTP API>/api/v1/query?query=coredns_dns_responses_total&time=1708655317&timeout=1000'

      The expected results are as follows:

    • Obtain monitoring data within a specified time range.
      GET <HTTP API>/api/v1/query
      Content-Type: application/json
      Param:
      {
      "query":" coredns_dns_responses_total",
      "start":1708655317,
      "end":1708655318,
      "step":30
      }

      Other URL query parameters are as follows:

      • start: time when you start to obtain monitoring data
      • end: time when you stop obtaining the monitoring data
      • step: step of the data interval when the monitoring data is returned

      The following shows an example statement:

      curl -X GET -H "Content-Type: application/json" -H "Accept: application/json" '<HTTP API>/api/v1/query?query=coredns_dns_responses_total&start=1708655317&end=1708655318&setp=30'

      The expected results are as follows: