Get URL to an Azure Alert Detail Page
Asked Answered
C

3

6

I am using a logic app to create a webhook (used in a log search alert), which then pushes the alert payload to slack. I am trying to send along with the alert payload data (to slack) a url to the actual alert detail page and not use the built in field linkToSearchResults as that url is huge because my query is long. I essentially want a friendly url, something like the url that is provided in the email template that azure uses for View the alert in Azure Monitor. I have not been able to find a way of putting this link together, I know I can use a custom json payload on the alert for my webhook, but how would I generate this friendly url?

Crossbreed answered 1/7, 2019 at 19:26 Comment(1)
The provided solution works great, but the URL format could change and break. I've added a feature request to provide the URL in the common schema for alerts, so vote it up! feedback.azure.com/forums/913690-azure-monitor/suggestions/…Magnificent
U
11

I believe the question is more around how to get the link to actual alert, like which shows up in email like this.

enter image description here

Whereas the linkToSearchResults takes to the page where we can execute the search query.

Looking more into the link for the alert, it seems to be formatted as

https://ms.portal.azure.com/#blade/Microsoft_Azure_Monitoring/AlertDetailsTemplateBlade/alertId/%2fsubscriptions%2f<subscription_id>%2fproviders%2fMicrosoft.AlertsManagement%2falerts%2f<alert_id>/invokedFrom/emailcommonschema

Now if we look at the json we receive as part of the alert when common schema is enabled, it has these info.

{
  "essentials": {
    "alertId": "/subscriptions/<subscription ID>/providers/Microsoft.AlertsManagement/alerts/b9569717-bc32-442f-add5-83a997729330",
    "alertRule": "Contoso IT Metric Alert",
    "severity": "Sev3",
    "signalType": "Metric",
    "monitorCondition": "Fired",
    "monitoringService": "Platform",
    "alertTargetIDs": [
      "/subscriptions/<subscription ID>/resourceGroups/aimon-rg/providers/Microsoft.Insights/components/ai-orion-int-fe"
    ],
    "originAlertId": "74ff8faa0c79db6084969cf7c72b0710e51aec70b4f332c719ab5307227a984f",
    "firedDateTime": "2019-03-26T05:25:50.4994863Z",
    "description": "Test Metric alert",
    "essentialsVersion": "1.0",
    "alertContextVersion": "1.0"
  }
}

Reference from msdoc.

Lets see, this schema has essentials.alertId which looks familiar to what is being used in the url above (but in url encoded form).

So the final code to generate the friendly url to alert becomes something like this

string.Format("https://ms.portal.azure.com/#blade/Microsoft_Azure_Monitoring/AlertDetailsTemplateBlade/alertId/{0}",
                                HttpUtility.UrlEncode(alertEssentials.AlertId)),

Hope this helps!

Unwish answered 8/11, 2019 at 6:36 Comment(0)
T
1

@akashperfect's answer specifies the URI generation protocol, namely that the alertId value must be URL encoded.

If you want to use an alert rule to publish an alert to Microsoft Teams via a Logic App you can construct the alert page URL with the following expression value:

concat(
  '<a href="',
  'https://portal.azure.com/#blade/Microsoft_Azure_Monitoring/AlertDetailsTemplateBlade/alertId/',
  encodeUriComponent(triggerBody()['data']['essentials']['alertId']),
  '">Open in Azure Monitor</a>')
Towers answered 14/10, 2022 at 11:47 Comment(0)
V
0

If I understand correctly, you are looking for a URL Shortener. For Logic Apps, there is a Bitly connector that you could use to generate the smaller URLs.

If you'd like to use own URL Shortener, you could build simple function app that does this. There is great blog by Jeremy Likness (source) that covers how one could achieve this.

With your own shortener, you would have to use the HTTP Action to get the shortened URL.

Vicennial answered 29/7, 2019 at 8:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.