Skip to main content

Step 8: Add monitoring and alerts

This is step 8 of the enterprise web app learning path. Zava Widgets releases safely now, but once a release is live you have little insight into how it behaves - how fast requests are, how often they fail, or what broke when something does. In this step you turn on Application Insights so the platform collects request, dependency, and exception telemetry automatically, then you add an alert so you find out about problems before your users report them.

Application Insights stores telemetry in a Log Analytics workspace. Your app sends data to it through the APPLICATIONINSIGHTS_CONNECTION_STRING app setting, and a second app setting turns on the App Service-managed agent that instruments Node.js with no code change.

In this step you will:

  • Create a Log Analytics workspace and a workspace-based Application Insights resource.
  • Connect the app with the connection string and enable auto-instrumentation.
  • Generate a little traffic and confirm telemetry is flowing.
  • Create a metric alert that fires on server errors.

Estimated time: 30 to 40 minutes.

Objectives

By the end of this step you will be able to:

  • Explain how Application Insights, the connection string, and the managed agent fit together.
  • Create a workspace-based Application Insights resource.
  • Instrument a Node.js app on App Service without changing code.
  • Create and confirm a metric alert rule.

Before you start

You need the resource group and web app from the earlier steps, plus a name for the new monitoring resources:

RESOURCE_GROUP="rg-zava-widgets"
APP_NAME="<your-app-name>"
LOCATION="eastus"
LAW_NAME="law-zava-widgets"
AI_NAME="appi-zava-widgets"

How Application Insights instruments your app

Application Insights is the application performance monitoring service in Azure Monitor. On App Service you do not add an SDK to instrument a Node.js app - you set two app settings and the platform attaches an agent that captures requests, dependencies, and exceptions for you:

  • APPLICATIONINSIGHTS_CONNECTION_STRING tells the app where to send telemetry.
  • ApplicationInsightsAgent_EXTENSION_VERSION turns on the managed agent (codeless attach).
Choose your tooling
Configure with

Create Application Insights and connect the app

Create the Log Analytics workspace, then a workspace-based Application Insights resource:

az monitor log-analytics workspace create \
--resource-group "$RESOURCE_GROUP" \
--workspace-name "$LAW_NAME" \
--location "$LOCATION"

LAW_ID=$(az monitor log-analytics workspace show \
--resource-group "$RESOURCE_GROUP" --workspace-name "$LAW_NAME" \
--query id -o tsv)

az monitor app-insights component create \
--app "$AI_NAME" \
--location "$LOCATION" \
--resource-group "$RESOURCE_GROUP" \
--workspace "$LAW_ID" \
--application-type web

Read the connection string and set the app settings that connect and instrument the app, then restart it:

AI_CONN=$(az monitor app-insights component show \
--app "$AI_NAME" --resource-group "$RESOURCE_GROUP" \
--query connectionString -o tsv)

az webapp config appsettings set \
--name "$APP_NAME" --resource-group "$RESOURCE_GROUP" \
--settings \
APPLICATIONINSIGHTS_CONNECTION_STRING="$AI_CONN" \
ApplicationInsightsAgent_EXTENSION_VERSION="~3" \
XDT_MicrosoftApplicationInsights_Mode="recommended"

az webapp restart --name "$APP_NAME" --resource-group "$RESOURCE_GROUP"

Generate traffic and confirm telemetry

Send a few requests so there is something to see:

APP_URL="https://$(az webapp show --name "$APP_NAME" --resource-group "$RESOURCE_GROUP" --query defaultHostName -o tsv)"
for i in $(seq 1 20); do curl -s -o /dev/null "$APP_URL/"; curl -s -o /dev/null "$APP_URL/api/products"; done

Confirm the connection string is set on the app:

az webapp config appsettings list \
--name "$APP_NAME" --resource-group "$RESOURCE_GROUP" \
--query "[?name=='APPLICATIONINSIGHTS_CONNECTION_STRING'].name" -o tsv

It prints APPLICATIONINSIGHTS_CONNECTION_STRING, confirming the app is wired up. Telemetry takes a couple of minutes to appear. In the portal, open your Application Insights resource and select Live metrics to watch requests arrive in real time, or Application map and Failures for the aggregated view.

Create an alert on server errors

Create a metric alert on the App Service Http5xx metric. It fires when there are more than 10 server errors in 5 minutes:

APP_ID=$(az webapp show --name "$APP_NAME" --resource-group "$RESOURCE_GROUP" --query id -o tsv)

az monitor metrics alert create \
--name "alert-http5xx-$APP_NAME" \
--resource-group "$RESOURCE_GROUP" \
--scopes "$APP_ID" \
--condition "total Http5xx > 10" \
--window-size 5m \
--evaluation-frequency 1m \
--severity 2 \
--description "More than 10 HTTP 5xx responses in 5 minutes."

To actually be notified, attach an action group with --action <action-group-id> so the alert can email or message you.

Verify

Confirm the alert rule exists and is enabled:

az monitor metrics alert list --resource-group "$RESOURCE_GROUP" \
--query "[].{name:name, enabled:enabled, severity:severity}" -o table

You should see your rule with enabled set to True:

Name Enabled Severity
-------------------------------- --------- ----------
alert-http5xx-<your-app-name> True 2
Alert on symptoms your users feel

Good first alerts track things users actually notice: server errors (Http5xx), slow responses (server response time), and availability. Start with a couple of high-signal rules and add more as you learn the app's normal behavior.

Troubleshooting

  • No telemetry after a few minutes. Confirm APPLICATIONINSIGHTS_CONNECTION_STRING and ApplicationInsightsAgent_EXTENSION_VERSION are both set, then restart the app. Auto-instrumentation attaches at startup.
  • az monitor app-insights is not found. The CLI installs the application-insights extension automatically the first time you run the command; accept the prompt, or run az extension add --name application-insights.
  • The alert never fires. Http5xx only crosses the threshold when the app actually returns server errors. That is expected for a healthy app - the rule is still active and will fire when errors occur.

Summary

Zava Widgets is now observable: Application Insights captures requests, dependencies, and exceptions with no code change, stores them in a Log Analytics workspace, and an alert watches for a spike in server errors. You can see what the app is doing in production and get told when it misbehaves. Next you put a front door on it - requiring users to sign in with Entra ID before they can reach the app at all.

Learn more