Skip to content

Commit b662224

Browse files
Merge pull request #231302 from EdB-MSFT/query-awm-promql
New Article: Query Azure Monitor workspaces using REST and PromQL
2 parents 8135baa + 074f9a0 commit b662224

File tree

6 files changed

+215
-0
lines changed

6 files changed

+215
-0
lines changed
91 KB
Loading
65.6 KB
Loading
66.1 KB
Loading
53.3 KB
Loading
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
---
2+
title: Query Prometheus metrics using the API and PromQL
3+
description: Describes how to use the API to Query metrics in an Azure Monitor workspace using PromQL.
4+
ms.topic: how-to
5+
author: EdB-MSFT
6+
ms.author: edbaynash
7+
ms.date: 09/28/2022
8+
ms.reviewer: aul
9+
---
10+
11+
# Query Prometheus metrics using the API and PromQL
12+
13+
Azure Monitor managed service for Prometheus (preview), collects metrics from Azure Kubernetes Clusters and stores them in an Azure Monitor workspace. PromQL - Prometheus query language, is a functional query language that allows you to query and aggregate time series data. Use PromQL to query and aggregate metrics stored in an Azure Monitor workspace.
14+
15+
This article describes how to query an Azure Monitor workspace using PromQL via the REST API.
16+
For more information on PromQL, see [Querying prometheus](https://prometheus.io/docs/prometheus/latest/querying/basics/).
17+
18+
## Prerequisites
19+
To query an Azure monitor workspace using PromQL, you need the following prerequisites:
20+
+ An Azure Kubernetes Cluster or remote Kubernetes cluster.
21+
+ Azure Monitor managed service for Prometheus (preview) scraping metrics from a Kubernetes cluster
22+
+ An Azure Monitor Workspace where Prometheus metrics are being stored.
23+
24+
## Authentication
25+
26+
To query your Azure Monitor workspace, authenticate using Azure Active Directory.
27+
The API supports Azure Active Directory authentication using Client credentials. Register a client app with Azure Active Directory and request a token.
28+
29+
To set up Azure Active Directory authentication, follow the steps below:
30+
31+
1. Register an app with Azure Active Directory.
32+
1. Grant access for the app to your Azure Monitor workspace.
33+
1. Request a token.
34+
35+
36+
### Register an app with Azure Active Directory
37+
38+
1. To register an app, follow the steps in [Register an App to request authorization tokens and work with APIs](../logs/api/register-app-for-token.md?tabs=portal)
39+
40+
### Allow your app access to your workspace
41+
Allow your app to query data from your Azure Monitor workspace.
42+
43+
1. Open your Azure Monitor workspace in the Azure portal.
44+
45+
1. On the Overview page, take note of your Query endpoint for use in your REST request.
46+
47+
1. Select Access control (IAM).
48+
49+
1. Select **Add**, then **Add role assignment** from the Access Control (IAM) page.
50+
51+
:::image type="content" source="./media/prometheus-api-promql/access-control.png" lightbox="./media/prometheus-api-promql/access-control.png" alt-text="A screenshot showing the Azure Monitor workspace overview page.":::
52+
53+
1. On the **Add role Assignment page**, search for *Monitoring*.
54+
55+
1. Select **Monitoring Data Reader**, then select the Members tab.
56+
57+
:::image type="content" source="./media/prometheus-api-promql/add-role-assignment.png" lightbox="./media/prometheus-api-promql/add-role-assignment.png" alt-text="A screenshot showing the Add role assignment page.":::
58+
59+
1. Select **Select members**.
60+
61+
1. Search for the app that you registered and select it.
62+
63+
1. Choose **Select**.
64+
65+
1. Select **Review + assign**.
66+
67+
:::image type="content" source="./media/prometheus-api-promql/select-members.png" lightbox="./media/prometheus-api-promql/select-members.png" alt-text="A screenshot showing the Add role assignment, select members page.":::
68+
69+
You've created your App registration and have assigned it access to query data from your Azure Monitor workspace. You can now generate a token and use it in a query.
70+
71+
72+
### Request a token
73+
Send the following request in the command prompt or by using a client like Postman.
74+
75+
```shell
76+
curl -X POST 'https://login.microsoftonline.com/<tennant ID>/oauth2/token' \
77+
-H 'Content-Type: application/x-www-form-urlencoded' \
78+
--data-urlencode 'grant_type=client_credentials' \
79+
--data-urlencode 'client_id=<your apps client ID>' \
80+
--data-urlencode 'client_secret=<your apps client secret' \
81+
--data-urlencode 'resource= https://prometheus.monitor.azure.com'
82+
```
83+
84+
Sample response body:
85+
86+
```JSON
87+
{
88+
"token_type": "Bearer",
89+
"expires_in": "86399",
90+
"ext_expires_in": "86399",
91+
"expires_on": "1672826207",
92+
"not_before": "1672739507",
93+
"resource": "https:/prometheus.monitor.azure.com",
94+
"access_token": "eyJ0eXAiOiJKV1Qi....gpHWoRzeDdVQd2OE3dNsLIvUIxQ"
95+
}
96+
```
97+
98+
Save the access token from the response for use in the following HTTP requests.
99+
100+
## Query endpoint
101+
102+
Find your workspace's query endpoint on the Azure Monitor workspace overview page.
103+
104+
:::image type="content" source="./media/prometheus-api-promql/find-query-endpoint.png" lightbox="./media/prometheus-api-promql/find-query-endpoint.png" alt-text="A screenshot sowing the query endpoint on the Azure Monitor workspace overview page.":::
105+
106+
## Supported APIs
107+
The following queries are supported:
108+
109+
### Instant queries
110+
For more information, see [Instant queries](https://prometheus.io/docs/prometheus/latest/querying/api/#instant-queries)
111+
112+
Path: `/api/v1/query`
113+
Examples:
114+
```
115+
POST https://k8s-02-workspace-abcd.eastus.prometheus.monitor.azure.com/api/v1/query
116+
--header 'Authorization: Bearer <access token>'
117+
--header 'Content-Type: application/x-www-form-urlencoded'
118+
--data-urlencode 'query=sum( \
119+
container_memory_working_set_bytes \
120+
* on(namespace,pod) \
121+
group_left(workload, workload_type) \
122+
namespace_workload_pod:kube_pod_owner:relabel{ workload_type="deployment"}) by (pod)'
123+
124+
```
125+
```
126+
GET 'https://k8s02-workspace-abcd.eastus.prometheus.monitor.azure.com/api/v1/query?query=container_memory_working_set_bytes'
127+
--header 'Authorization: Bearer <access token>'
128+
```
129+
### Range queries
130+
For more information, see [Range queries](https://prometheus.io/docs/prometheus/latest/querying/api/#range-queries)
131+
Path: `/api/v1/query_range`
132+
Examples:
133+
```
134+
GET 'https://k8s02-workspace-abcd.eastus.prometheus.monitor.azure.com/api/v1/query_range?query=container_memory_working_set_bytes&start=2023-03-01T00:00:00.000Z&end=2023-03-20T00:00:00.000Z&step=6h'
135+
--header 'Authorization: Bearer <access token>
136+
```
137+
138+
```
139+
POST 'https://k8s02-workspace-abcd.eastus.prometheus.monitor.azure.com/api/v1/query_range'
140+
--header 'Authorization: Bearer <access token>'
141+
--header 'Content-Type: application/x-www-form-urlencoded'
142+
--data-urlencode 'query=up'
143+
--data-urlencode 'start=2023-03-01T20:10:30.781Z'
144+
--data-urlencode 'end=2023-03-20T20:10:30.781Z'
145+
--data-urlencode 'step=6h'
146+
```
147+
### Series
148+
For more information, see [Series](https://prometheus.io/docs/prometheus/latest/querying/api/#finding-series-by-label-matchers)
149+
150+
Path: `/api/v1/series`
151+
Examples:
152+
```
153+
POST 'https://k8s02-workspace-abcd.eastus.prometheus.monitor.azure.com/api/v1/series'
154+
--header 'Authorization: Bearer <access token>
155+
--header 'Content-Type: application/x-www-form-urlencoded'
156+
--data-urlencode 'match[]=kube_pod_info{pod="bestapp-123abc456d-4nmfm"}'
157+
158+
```
159+
```
160+
GET 'https://k8s02-workspace-abcd.eastus.prometheus.monitor.azure.com/api/v1/series?match[]=container_network_receive_bytes_total{namespace="default-1669648428598"}'
161+
```
162+
163+
### Labels
164+
165+
For more information, see [Labels](https://prometheus.io/docs/prometheus/latest/querying/api/#getting-label-names)
166+
Path: `/api/v1/labels`
167+
Examples:
168+
```
169+
GET 'https://k8s02-workspace-abcd.eastus.prometheus.monitor.azure.com/api/v1/labels'
170+
171+
```
172+
```
173+
POST 'https://k8s02-workspace-abcd.eastus.prometheus.monitor.azure.com/api/v1/labels'
174+
```
175+
176+
### Label values
177+
For more information, see [Label values](https://prometheus.io/docs/prometheus/latest/querying/api/#query.ing-label-values)
178+
Path: `/api/v1/label/__name__/values.`
179+
180+
181+
> [!NOTE]
182+
> `__name__` is the only supported version of this API and returns all metric names. No other /api/v1/label/<label_name>/values are supported.
183+
184+
Example:
185+
```
186+
GET 'https://k8s02-workspace-abcd.eastus.prometheus.monitor.azure.com/api/v1/label/__name__/values'
187+
```
188+
189+
For the full specification of OSS prom APIs, see [Prometheus HTTP API](https://prometheus.io/docs/prometheus/latest/querying/api/#http-api )
190+
191+
## API limitations
192+
The following limitations are in addition to those detailed in the Prometheus specification.
193+
194+
+ Query must be scoped to metric
195+
Any time series fetch queries (/series or /query or /query_range) must contain a \_\_name\_\_ label matcher. That is, each query must be scoped to a metric. There can only be one \_\_name\_\_ label matcher in a query.
196+
+ Supported time range
197+
+ /query_range API supports a time range of 32 days. This is the maximum time range allowed including range selectors specified in the query itself.
198+
For example, the query `rate(http_requests_total[1h]` for last 24 hours would actually mean data is being queried for 25 hours. A 24 hours range + 1 hour specified in query itself.
199+
+ /series API fetches data for a maximum 12-hour time range. If `endTime` isn't provided, endTime = time.now(). If the time rage is greater than 12 hours, the `startTime` is set to `endTime – 12h`
200+
+ Ignored time range
201+
Start time and end time provided with `/labels` and `/label/__name__/values` are ignored, and all retained data in the Azure Monitor Workspace is queried.
202+
+ Experimental features
203+
Experimental features such as exemplars aren't supported.
204+
205+
For more information on Prometheus metrics limits, see [Prometheus metrics](../../azure-monitor/service-limits.md#prometheus-metrics)
206+
207+
## Next steps
208+
209+
[Azure Monitor workspace overview (preview)](./azure-monitor-workspace-overview.md)
210+
[Manage an Azure Monitor workspace (preview)](./azure-monitor-workspace-manage.md)
211+
[Overview of Azure Monitor Managed Service for Prometheus (preview)](./prometheus-metrics-overview.md)
212+
[Query Prometheus metrics using Azure workbooks (preview)](./prometheus-workbooks.md)

articles/azure-monitor/toc.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,9 @@ items:
407407
- name: Query Prometheus metrics in workbooks
408408
displayName: Prometheus
409409
href: essentials/prometheus-workbooks.md
410+
- name: Query Prometheus using the API and PromQL
411+
displayName: Prometheus
412+
href: essentials/prometheus-api-promql.md
410413
- name: Remote-write
411414
items:
412415
- name: Overview

0 commit comments

Comments
 (0)