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
Log on to the Managed Service for Prometheus console.
In the left-side navigation pane, click Integration Management.
On the Integrated Environments tab of the Integration Management page, click the name of the environment that you want to manage.
On the environment details page, click the Metrics Explorer tab.
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.
NoteClick 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
Log on to the Managed Service for Prometheus console.
In the left-side navigation pane, click Instances.
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.
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.
NoteIf 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.
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
In this example, a self-managed Grafana v10.0.x system is used.
Log on to the Grafana system as an administrator.
NoteThe 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.
After you log on to the Grafana system, click the icon in the upper-left corner.
In the left-side navigation pane, choose Configuration > Data sources.
On the Data sources page, click Add data source.
On the Add data source page, click Prometheus.
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.
NoteIf 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.
Click Save & test.
Step 3: Verify the result
Log on to the Grafana system as an administrator.
Click the icon in the upper-left corner of the page. In the left-side navigation pane, click Dashboards.
On the Dashboards page, click New, and then select New dashboard from the drop-down menu.
Click + Add visualization. On the Select data source page, select a custom data source.
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.
NoteIf 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
Log on to the Managed Service for Prometheus console
In the left-side navigation pane, click Instances.
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.
On the Settings tab, select a public or internal remote read endpoint.
Step 2: Configure the remote read endpoint
Visit the Prometheus official website to download open source Prometheus.
Decompress the Prometheus installation package.
Open the Prometheus configuration file
prometheus.yml
, add the following content to the end of the file, replaceremote_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 search box, and click Execute on the right side of the page to remotely read the monitoring data of Managed Service for Prometheus.