Skip to content

Commit 829516e

Browse files
authored
Merge pull request #222861 from EdB-MSFT/update-send-custom-metrics
Updated and refeshed how to send custom metrics
2 parents 15d61b9 + 901030b commit 829516e

File tree

7 files changed

+123
-79
lines changed

7 files changed

+123
-79
lines changed
21.9 KB
Loading
41 KB
Loading
35.9 KB
Loading
56.6 KB
Loading
Lines changed: 80 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,81 @@
11
---
22
title: Send metrics to the Azure Monitor metric database using REST API
33
description: Send custom metrics for an Azure resource to the Azure Monitor metric store by using a REST API
4-
author: anirudhcavale
4+
author: EdB-MSFT
55
services: azure-monitor
66
ms.reviewer: priyamishra
77
ms.topic: conceptual
8-
ms.date: 09/24/2018
9-
ms.author: ancav
8+
ms.date: 01/04/2023
9+
ms.author: edbaynash
1010
---
1111
# Send custom metrics for an Azure resource to the Azure Monitor metric store by using a REST API
1212

13-
This article shows you how to send custom metrics for Azure resources to the Azure Monitor metrics store via a REST API. After the metrics are in Azure Monitor, you can do all the things with them that you do with standard metrics. Examples are charting, alerting, and routing them to other external tools.
13+
This article shows you how to send custom metrics for Azure resources to the Azure Monitor metrics store via a REST API. When the metrics are in Azure Monitor, you can do all the things with them that you do with standard metrics. For example, charting, alerting, and routing them to other external tools.
1414

1515
>[!NOTE]
16-
>The REST API only permits sending custom metrics for Azure resources. To send metrics for resources in different environments or on-premises, you can use [Application Insights](../app/api-custom-events-metrics.md).
16+
>The REST API only permits sending custom metrics for Azure resources.
17+
To send metrics for resources in other environments or on-premises, use [Application Insights](../app/api-custom-events-metrics.md).
1718

19+
## Create and authorize a service principal to emit metrics
1820

19-
## Create and authorize a service principal to emit metrics
21+
A service principal is an application whose tokens can be used to authenticate and grant access to specific Azure resources using Azure Active Directory. This includes user-apps, services or automation tools.
2022

21-
Create a service principal in your Azure Active Directory tenant by using the instructions found at [Create a service principal](../../active-directory/develop/howto-create-service-principal-portal.md).
23+
1. [Register an application with Azure Active Directory](../logs/api/register-app-for-token.md) to create a service principal.
2224

23-
Note the following while you go through this process:
25+
1. Save the tenant ID, new client ID, and client secret value for your app to use when requesting a token.
2426

25-
- You can enter any URL for the sign-in URL.
26-
- Create a new client secret for this app.
27-
- Save the key and the client ID for use in later steps.
28-
29-
Give the app created as part of step 1, Monitoring Metrics Publisher, permissions to the resource you wish to emit metrics against. If you plan to use the app to emit custom metrics against many resources, you can grant these permissions at the resource group or subscription level.
27+
1. Give the app created as part of the previous step **Monitoring Metrics Publisher** permissions to the resource you wish to emit metrics against. If you plan to use the app to emit custom metrics against many resources, you can grant these permissions at the resource group or subscription level.
28+
29+
On your resource's overview page, select **Access Control (IAM)**
30+
1. Select **Add**, then **Add role assignment** from the dropdown.
31+
:::image type="content" source="./media/metrics-store-custom-rest-api/access-contol-add-role-assignment.png" alt-text="A screenshot showing the Access control(IAM) page for a virtual machine.":::
32+
1. Search for *Monitoring Metrics* in the search field.
33+
1. Select **Monitoring Metrics Publisher** from the list.
34+
1. Select **Members**.
35+
:::image type="content" source="./media/metrics-store-custom-rest-api/add-role-assignment.png" alt-text="A screenshot showing the add role assignment page.":::
36+
37+
1. Search for your app in the **Select** field.
38+
1. Select your app from the list.
39+
1. Click **Select**.
40+
1. Select **Review + assign**.
41+
:::image type="content" source="./media/metrics-store-custom-rest-api/select-members.png" alt-text="A screenshot showing the members tab of the role assignment page.":::
3042

3143
## Get an authorization token
32-
Open a command prompt and run the following command:
44+
45+
Send the following request in the command prompt or using a client like Postman.
3346

3447
```shell
35-
curl -X POST https://login.microsoftonline.com/<yourtenantid>/oauth2/token -F "grant_type=client_credentials" -F "client_id=<insert clientId from earlier step>" -F "client_secret=<insert client secret from earlier step>" -F "resource=https://monitoring.azure.com/"
48+
curl -X POST 'https://login.microsoftonline.com/<tennant ID>/oauth2/token' \
49+
-H 'Content-Type: application/x-www-form-urlencoded' \
50+
--data-urlencode 'grant_type=client_credentials' \
51+
--data-urlencode 'client_id=<your apps client ID>' \
52+
--data-urlencode 'client_secret=<your apps client secret' \
53+
--data-urlencode 'resource=https://management.azure.com'
54+
```
55+
56+
The response body appears as follows:
57+
58+
```JSON
59+
{
60+
"token_type": "Bearer",
61+
"expires_in": "86399",
62+
"ext_expires_in": "86399",
63+
"expires_on": "1672826207",
64+
"not_before": "1672739507",
65+
"resource": "https://monitoring.azure.com",
66+
"access_token": "eyJ0eXAiOiJKV1Qi....gpHWoRzeDdVQd2OE3dNsLIvUIxQ"
67+
}
3668
```
37-
Save the access token from the response.
3869

39-
![Access token](./media/metrics-store-custom-rest-api/accesstoken.png)
70+
Save the access token from the response for use in the following HTTP requests.
4071

41-
## Emit the metric via the REST API
72+
## Send a metric via the REST API
4273

43-
1. Paste the following JSON into a file, and save it as **custommetric.json** on your local computer. Update the time parameter in the JSON file:
74+
1. Paste the following JSON into a file, and save it as **custommetric.json** on your local computer. Update the time parameter so that it is within the last 20 minutes. You can't put a metric into the store that's over 20 minutes old. The metric store is optimized for alerting and real-time charting.
4475

45-
```json
76+
```JSON
4677
{
47-
"time": "2018-09-13T16:34:20",
78+
"time": "2023-01-03T11:00:20",
4879
"data": {
4980
"baseData": {
5081
"metric": "QueueDepth",
@@ -68,48 +99,49 @@ Save the access token from the response.
6899
}
69100
}
70101
}
71-
```
102+
```
72103

73-
1. In your command prompt window, post the metric data:
74-
- **azureRegion**. Must match the deployment region of the resource you're emitting metrics for.
75-
- **resourceID**. Resource ID of the Azure resource you're tracking the metric against.
76-
- **AccessToken**. Paste the token that you acquired previously.
104+
1. Submit the following HTTP POST request using the following variables:
105+
- **location**: Deployment region of the resource you're emitting metrics for.
106+
- **resourceId**: Resource ID of the Azure resource you're tracking the metric against.
107+
- **accessToken**: The authorization token acquired from the previous step.
108+
109+
```Shell
110+
curl -X POST 'https://<location>.monitoring.azure.com/<resourceId>/metrics' \
111+
-H 'Content-Type: application/json' \
112+
-H 'Authorization: Bearer <accessToken>' \
113+
-d @custommetric.json
114+
```
77115

78-
```Shell
79-
curl -X POST https://<azureRegion>.monitoring.azure.com/<resourceId>/metrics -H "Content-Type: application/json" -H "Authorization: Bearer <AccessToken>" -d @custommetric.json
80-
```
81116
1. Change the timestamp and values in the JSON file.
82-
1. Repeat the previous two steps a few times, so you have data for several minutes.
83-
84-
## Troubleshooting
85-
If you receive an error message with some part of the process, consider the following troubleshooting information:
117+
1. Repeat the previous two steps a number of times, to create data for several minutes.
86118

87-
1. You can't issue metrics against a subscription or resource group as your Azure resource.
88-
1. You can't put a metric into the store that's over 20 minutes old. The metric store is optimized for alerting and real-time charting.
89-
2. The number of dimension names should match the values and vice versa. Check the values.
90-
2. You might be emitting metrics against a region that doesn’t support custom metrics. See [supported regions](./metrics-custom-overview.md#supported-regions).
119+
## Troubleshooting
91120

121+
If you receive an error message with some part of the process, consider the following troubleshooting information:
92122

123+
- If you can't issue metrics against a subscription or resource group, or resource, check that your application or Service Principal has the **Monitoring Metrics Publisher** role assigned in Access control (IAM).
124+
- Check that the number of dimension names matches the number values.
125+
- Check that you are not emitting metrics against a region that doesn’t support custom metrics. See [supported regions](./metrics-custom-overview.md#supported-regions).
93126

94-
## View your metrics
127+
## View your metrics
95128

96-
1. Sign in to the Azure portal.
129+
1. Sign in to the Azure portal.
97130

98-
1. In the left-hand menu, select **Monitor**.
131+
1. In the left-hand menu, select **Monitor**.
99132

100-
1. On the **Monitor** page, select **Metrics**.
133+
1. On the **Monitor** page, select **Metrics**.
101134

102-
![Select Metrics](./media/metrics-store-custom-rest-api/metrics.png)
135+
![Select Metrics](./media/metrics-store-custom-rest-api/metrics.png)
103136

104-
1. Change the aggregation period to **Last 30 minutes**.
137+
1. Change the aggregation period to **Last hour**.
105138

106-
1. In the **resource** drop-down menu, select the resource you emitted the metric against.
139+
1. In the **Scope** drop-down menu, select the resource you send the metric for.
107140

108-
1. In the **namespaces** drop-down menu, select **QueueProcessing**.
141+
1. In the **Metric namespace** drop-down menu, select **QueueProcessing**.
109142

110-
1. In the **metrics** drop-down menu, select **QueueDepth**.
143+
1. In the **Metric** drop-down menu, select **QueueDepth**.
111144

112-
113145
## Next steps
114-
- Learn more about [custom metrics](./metrics-custom-overview.md).
115146

147+
- Learn more about [custom metrics](./metrics-custom-overview.md).

articles/azure-monitor/logs/api/access-api.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,35 @@ For example,
6262

6363
To access the API, you need to register a client app with Azure Active Directory and request a token.
6464
1. [Register an app in Azure Active Directory](./register-app-for-token.md).
65+
66+
1. On the app's overview page, select **API permissions**
67+
1. Select **Add a permission**
68+
1. In the **APIs my organization uses** tab search for *log analytics* and select **Log Analytics API** from the list.
69+
:::image type="content" source="../media/api-register-app/request-api-permissions.png" alt-text="A screenshot showing the Request API permissions page.":::
70+
71+
1. Select **Delegated permissions**
72+
1. Check the checkbox for **Data.Read**
73+
1. Select **Add permissions**
74+
:::image type="content" source="../media/api-register-app/add-requested-permissions.png" alt-text="A screenshot showing the continuation of the Request API permissions page.":::
75+
76+
Now that your app is registered and has permissions to use the API, grant your app access to your Log Analytics Workspace.
77+
78+
1. From your Log analytics Workspace overview page, select **Access control (IAM)**.
79+
1. Select **Add role assignment**.
80+
81+
:::image type="content" source="../media/api-register-app/workspace-access-control.png" alt-text="A screenshot showing the access control page for a log analytics workspace.":::
82+
83+
1. Select the **Reader** role then select **Members**
84+
85+
:::image type="content" source="../media/api-register-app/add-role-assignment.png" alt-text="A screenshot showing the add role assignment page for a log analytics workspace.":::
86+
87+
1. In the Members tab, select **Select members**
88+
1. Enter the name of your app in the **Select** field.
89+
1. Choose your app and select **Select**
90+
1. Select **Review and assign**
91+
92+
:::image type="content" source="../media/api-register-app/select-members.png" alt-text="A screenshot showing the select members blade on the role assignment page for a log analytics workspace.":::
93+
6594
1. After completing the Active Directory setup and workspace permissions, request an authorization token.
6695

6796
## Request an Authorization Token
Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
---
2-
title: Register an App for API Access
3-
description: How to register an app and assign a role so it can access a log analytics workspace using the API
2+
title: Register an App to request authorization tokens and work with APIs
3+
description: How to register an app and assign a role so it can access request a token and work with APIs
44
author: EdB-MSFT
55
ms.author: edbaynash
6-
ms.date: 11/18/2021
6+
ms.date: 01/04/2023
77
ms.topic: article
88
---
99

10-
# Register an App to work with Log Analytics APIs
10+
# Register an App to request authorization tokens and work with APIs
1111

12-
To access the log analytics API, you can generate a token based on a client ID and secret. This article shows you how to register a client app and assign permissions to access a Log Analytics Workspace.
12+
To access Azure REST APIs such as the Log analytics API, or to send custom metrics, you can generate an authorization token based on a client ID and secret. The token is then passed in your REST API request. This article shows you how to register a client app and create a client secret so that you can generate a token.
1313

1414
## Register an App
1515

@@ -21,15 +21,6 @@ To access the log analytics API, you can generate a token based on a client ID a
2121
1. Select **New registration**
2222
1. On the Register an application page, enter a **Name** for the application.
2323
1. Select **Register**
24-
1. On the app's overview page, select **API permissions**
25-
1. Select **Add a permission**
26-
1. In the **APIs my organization uses** tab search for *log analytics* and select **Log Analytics API** from the list.
27-
:::image type="content" source="../media/api-register-app/request-api-permissions.png" alt-text="A screenshot showing the Request API permissions page.":::
28-
29-
1. Select **Delegated permissions**
30-
1. Check the checkbox for **Data.Read**
31-
1. Select **Add permissions**
32-
:::image type="content" source="../media/api-register-app/add-requested-permissions.png" alt-text="A screenshot showing the continuation of the Request API permissions page.":::
3324

3425
1. On the app's overview page, select **Certificates and Secrets**
3526
1. Note the **Application (client) ID**. It's used in the HTTP request for a token.
@@ -39,34 +30,26 @@ To access the log analytics API, you can generate a token based on a client ID a
3930
1. Enter a **Description** and select **Add**
4031
:::image type="content" source="../media/api-register-app/add-a-client-secret.png" alt-text="A screenshot showing the Add client secret page.":::
4132

42-
1. Copy and save the client secret **Value**.
33+
1. Copy and save the client secret **Value**.
4334

4435
> [!NOTE]
4536
> Client secret values can only be viewed immediately after creation. Be sure to save the secret before leaving the page.
4637
4738
:::image type="content" source="../media/api-register-app/client-secret.png" alt-text="A screenshot showing the client secrets page.":::
4839

49-
## Grant your app access to a Log Analytics Workspace
50-
51-
1. From your Log analytics Workspace overview page, select **Access control (IAM)**.
52-
1. Select **Add role assignment**.
5340

54-
:::image type="content" source="../media/api-register-app/workspace-access-control.png" alt-text="A screenshot showing the access control page for a log analytics workspace.":::
41+
## Next steps
5542

56-
1. Select the **Reader** role then select **Members**
57-
58-
:::image type="content" source="../media/api-register-app/add-role-assignment.png" alt-text="A screenshot showing the add role assignment page for a log analytics workspace.":::
43+
Before you can generate a token using your app, client ID, and secret, assign the app to a role using Access control (IAM) for resource that you want to access.
44+
The role will depend on the resource type and the API that you want to use.
45+
For example,
46+
- To grant your app read from a Log Analytics Workspace, add your app as a member to the **Reader** role using Access control (IAM) for your Log Analytics Workspace. For more information, see [Access the API](./access-api.md)
5947

60-
1. In the Members tab, select **Select members**
61-
1. Enter the name of your app in the **Select** field.
62-
1. Choose your app and select **Select**
63-
1. Select **Review and assign**
64-
65-
:::image type="content" source="../media/api-register-app/select-members.png" alt-text="A screenshot showing the select members blade on the role assignment page for a log analytics workspace.":::
48+
- To grant access to send custom metrics for a resource, add your app as a member to the **Monitoring Metrics Publisher** role using Access control (IAM) for your resource. For more information, see [ Send metrics to the Azure Monitor metric database using REST API](../../essentials/metrics-store-custom-rest-api.md)
6649

67-
## Next steps
50+
For more information see [Assign Azure roles using the Azure portal](https://learn.microsoft.com/azure/role-based-access-control/role-assignments-portal)
6851

69-
You can use your client ID and client secret to generate a bearer token to access the Log Analytics API. For more information, see [Access the API](./access-api.md)
52+
Once you have assigned a role you can use your app, client ID, and client secret to generate a bearer token to access the REST API.
7053

7154
> [!NOTE]
7255
> When using Azure AD authentication, it may take up to 60 minutes for the Azure Application Insights REST API to recognize new role-based access control (RBAC) permissions. While permissions are propagating, REST API calls may fail with error code 403.

0 commit comments

Comments
 (0)