You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: Send metrics to the Azure Monitor metric database using REST API
3
3
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
5
5
services: azure-monitor
6
6
ms.reviewer: priyamishra
7
7
ms.topic: conceptual
8
-
ms.date: 09/24/2018
9
-
ms.author: ancav
8
+
ms.date: 01/04/2023
9
+
ms.author: edbaynash
10
10
---
11
11
# Send custom metrics for an Azure resource to the Azure Monitor metric store by using a REST API
12
12
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.
14
14
15
15
>[!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).
17
18
19
+
## Create and authorize a service principal to emit metrics
18
20
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.
20
22
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.
22
24
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.
24
26
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.":::
30
42
31
43
## 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.
33
46
34
47
```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' \
Save the access token from the response for use in the following HTTP requests.
40
71
41
-
## Emit the metric via the REST API
72
+
## Send a metric via the REST API
42
73
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.
44
75
45
-
```json
76
+
```JSON
46
77
{
47
-
"time": "2018-09-13T16:34:20",
78
+
"time": "2023-01-03T11:00:20",
48
79
"data": {
49
80
"baseData": {
50
81
"metric": "QueueDepth",
@@ -68,48 +99,49 @@ Save the access token from the response.
68
99
}
69
100
}
70
101
}
71
-
```
102
+
```
72
103
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' \
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.
86
118
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
91
120
121
+
If you receive an error message with some part of the process, consider the following troubleshooting information:
92
122
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).
Copy file name to clipboardExpand all lines: articles/azure-monitor/logs/api/access-api.md
+29Lines changed: 29 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -62,6 +62,35 @@ For example,
62
62
63
63
To access the API, you need to register a client app with Azure Active Directory and request a token.
64
64
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
+
65
94
1. After completing the Active Directory setup and workspace permissions, request an authorization token.
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
4
4
author: EdB-MSFT
5
5
ms.author: edbaynash
6
-
ms.date: 11/18/2021
6
+
ms.date: 01/04/2023
7
7
ms.topic: article
8
8
---
9
9
10
-
# Register an App to work with Log Analytics APIs
10
+
# Register an App to request authorization tokens and work with APIs
11
11
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.
13
13
14
14
## Register an App
15
15
@@ -21,15 +21,6 @@ To access the log analytics API, you can generate a token based on a client ID a
21
21
1. Select **New registration**
22
22
1. On the Register an application page, enter a **Name** for the application.
23
23
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.":::
33
24
34
25
1. On the app's overview page, select **Certificates and Secrets**
35
26
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
> Client secret values can only be viewed immediately after creation. Be sure to save the secret before leaving the page.
46
37
47
38
:::image type="content" source="../media/api-register-app/client-secret.png" alt-text="A screenshot showing the client secrets page.":::
48
39
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**.
53
40
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
55
42
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)
59
47
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)
66
49
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)
68
51
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.
70
53
71
54
> [!NOTE]
72
55
> 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