|
| 1 | +--- |
| 2 | +title: Query metrics in an Azure Monitor workspace using PromQL |
| 3 | +description: Describes how 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 from an Azure Monitor Workspace using 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 a Azure Monitor workspace. |
| 14 | + |
| 15 | +This article describes how to query an Azure Monitor workspace using PromQL via REST API . |
| 16 | +For more information on ProQL, see [QUERYING PROMETHEUS](https://prometheus.io/docs/prometheus/latest/querying/basics/). |
| 17 | + |
| 18 | +## Prerequisites |
| 19 | +To query a n Azure montior workspace using PromQL you need the following: |
| 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 asr being stored. |
| 23 | + |
| 24 | +## Authentication |
| 25 | + |
| 26 | +To query your Azure Monitor workspace, you must use 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 | +1. [Register an app in Azure Active Directory](https://learn.microsoft.com/en-us/azure/azure-monitor/logs/api/register-app-for-token) |
| 30 | + |
| 31 | +On the app's overview page, select API permissions. |
| 32 | + |
| 33 | +Select Add a permission. |
| 34 | + |
| 35 | +On the APIs my organization uses tab, search for Log Analytics and select Log Analytics API from the list. |
| 36 | + |
| 37 | +To set up Azure Active Directory authentication, follow the steps below: |
| 38 | + |
| 39 | +1. Register an app with Azure Active Directory. |
| 40 | +1. Grant access for the app to your Azure Monitor workspace. |
| 41 | +1. Configure your self-hosted Grafana with the app's credentials. |
| 42 | + |
| 43 | +### Register an app with Azure Active Directory |
| 44 | + |
| 45 | +1. To register an app, open the Active Directory Overview page in the Azure portal. |
| 46 | + |
| 47 | +1. Select **Add** from the tool bar and **App registration** from the dropdown. |
| 48 | + |
| 49 | +1. On the **Register an application page**, enter a Name for the application. |
| 50 | + |
| 51 | +1. Select Register. |
| 52 | + |
| 53 | +1. Note the** Application (client) ID** and **Directory(Tenant) ID**. They're used in the body of the authentication request. |
| 54 | + :::image type="content" source="./media/query-azure-monitor-workspaces/app-registration-overview.png" lightbox="./media/query-azure-monitor-workspaces/app-registration-overview.png" alt-text="A screen shot showing an app registration overview page."::: |
| 55 | + |
| 56 | +1. On the app's overview page, select **Certificates and Secrets**. |
| 57 | + |
| 58 | +1. In the Client secrets tab, select New client secret. |
| 59 | + |
| 60 | +1. Enter a Description. |
| 61 | + |
| 62 | +1. Select an expiry period from the dropdown and select Add. |
| 63 | + |
| 64 | + >[!Note] |
| 65 | + > Create a process to renew the secret and update your API REST calls before the secret expires. Once the secret expires you won't able to authenticate with this client ID and won't be able to query data from your Azure Monitor workspace using the API. |
| 66 | +
|
| 67 | + :::image type="content" source="./media/query-azure-monitor-workspaces/add-a-client-secret.png" lightbox="./media/query-azure-monitor-workspaces/add-a-client-secret.png" alt-text="A screenshot showing the Add client secret page."::: |
| 68 | + |
| 69 | +1. Copy and save the client secret Value. |
| 70 | + |
| 71 | +>[!Note] |
| 72 | +> Client secret values can only be viewed immediately after creation. Be sure to save the secret value before leaving the page. |
| 73 | +
|
| 74 | + :::image type="content" source="./media/query-azure-monitor-workspaces/client-secret.png" lightbox="./media/query-azure-monitor-workspaces/client-secret.png" alt-text="A screenshot showing the client secret page with generated secret value."::: |
| 75 | + |
| 76 | +### Allow your app access to your workspace |
| 77 | +Allow your app to query data from your Azure Monitor workspace. |
| 78 | + |
| 79 | +1. Open your Azure Monitor workspace in the Azure portal. |
| 80 | + |
| 81 | +1. On the Overview page, take note of your Query endpoint. The query endpoint is used when setting up your Grafana data source. |
| 82 | + |
| 83 | +1. Select Access control (IAM). A screenshot showing the Azure Monitor workspace overview page |
| 84 | + |
| 85 | +1. Select **Add**, then **Add role assignment** from the Access Control (IAM) page. |
| 86 | + |
| 87 | +1. On the Add role Assignment page, search for *Monitoring*. |
| 88 | + |
| 89 | +1. Select **Monitoring Data Reader**, then select the Members tab. |
| 90 | + |
| 91 | + :::image type="content" source="./media/query-azure-monitor-workspaces/add-role-assignment.png" lightbox="./media/query-azure-monitor-workspaces/add-role-assignment.png" alt-text="A screenshot showing the Add role assignment page"::: |
| 92 | + |
| 93 | +1. Select **Select members**. |
| 94 | + |
| 95 | +1. Search for the app that you registered in the Register an app with Azure Active Directory section and select it. |
| 96 | + |
| 97 | +1. Click **Select**. |
| 98 | + |
| 99 | +1. Select **Review + assign**. |
| 100 | + |
| 101 | + :::image type="content" source="./media/query-azure-monitor-workspaces/select-members.png" lightbox="./media/query-azure-monitor-workspaces/select-members.png.png" alt-text="A screenshot showing the Add role assignment, select members page."::: |
| 102 | + |
| 103 | +You've created your App registration and have assigned it access to query data from your Azure Monitor workspace. |
| 104 | + |
| 105 | + |
| 106 | +## Request a Token |
| 107 | +Send the following request in the command prompt or by using a client like Postman. |
| 108 | + |
| 109 | +```shell |
| 110 | +curl -X POST 'https://login.microsoftonline.com/<tennant ID>/oauth2/token' \ |
| 111 | +-H 'Content-Type: application/x-www-form-urlencoded' \ |
| 112 | +--data-urlencode 'grant_type=client_credentials' \ |
| 113 | +--data-urlencode 'client_id=<your apps client ID>' \ |
| 114 | +--data-urlencode 'client_secret=<your apps client secret' \ |
| 115 | +--data-urlencode 'resource= https://prometheus.monitor.azure.com' |
| 116 | +``` |
| 117 | + |
| 118 | +Sample response body: |
| 119 | + |
| 120 | +```JSON |
| 121 | +{ |
| 122 | + "token_type": "Bearer", |
| 123 | + "expires_in": "86399", |
| 124 | + "ext_expires_in": "86399", |
| 125 | + "expires_on": "1672826207", |
| 126 | + "not_before": "1672739507", |
| 127 | + "resource": "https:/prometheus.monitor.azure.com", |
| 128 | + "access_token": "eyJ0eXAiOiJKV1Qi....gpHWoRzeDdVQd2OE3dNsLIvUIxQ" |
| 129 | +} |
| 130 | +``` |
| 131 | + |
| 132 | +## Endpoints |
| 133 | + |
| 134 | +Two endpoints are supported for wuering Azure Monitor workspaces: |
| 135 | ++ Azure monitor workspace query endpoint |
| 136 | + For example: |
| 137 | + POST: Query endpoint from the over view page |
| 138 | + https://k8s02-workspace-abcd.eastus.prometheus.monitor.azure.com/api/v1/query |
| 139 | + |
| 140 | ++ https://management.azure.com resource endpoint |
| 141 | + For example: |
| 142 | + GET: https://management.azure.com/subscriptions/<subscriptionId>/resourcegroups/<resourceGroupName>/providers/microsoft.monitor/accounts/<amwName>?api-version=2021-06-01-preview |
| 143 | +When using the management end point, request a token using `--data-urlencode 'resource= https://prometheus.monitor.azure.com'` instead of `prometheus.monitor.azure.com` |
| 144 | + |
| 145 | + |
| 146 | +Save the access token from the response for use in the following HTTP requests. |
0 commit comments