This document explains how Application Insights is configured in the SSW.Website project for optimal cost management and monitoring.
Application Insights is used for monitoring the website's performance and tracking errors. The implementation uses both server-side (Node.js) and client-side (browser) tracking with configurable sampling to optimize Azure Log Analytics costs.
The default configuration implements 20% sampling for both server and client-side telemetry, which reduces data ingestion by approximately 75-80% while maintaining sufficient visibility into application behavior and errors.
- Before Optimization: ~$55/month (28GB data: 19GB requests + 9GB dependencies)
- After Optimization: ~$12-15/month (7GB data with 20% sampling)
- Cost Reduction: 75-80%
All Application Insights features can be controlled via environment variables without requiring code changes or redeployment.
| Variable | Default | Description |
|---|---|---|
APPINSIGHTS_SAMPLING_PERCENTAGE |
20 |
Percentage of requests/dependencies to track (1-100). Lower = less cost. |
APPINSIGHTS_ENABLE_CONSOLE_COLLECTION |
false |
When true, collects console output from third-party loggers (Winston, Bunyan). When false, disables all console collection. |
APPINSIGHTS_COLLECT_NATIVE_CONSOLE |
false |
Controls whether native console methods (console.log, console.error, etc.) are included when console collection is enabled via APPINSIGHTS_ENABLE_CONSOLE_COLLECTION. When true, native console methods are also collected; when false, native console methods are not collected. |
APPINSIGHTS_ENABLE_LIVE_METRICS |
false |
Enable real-time metrics streaming. Expensive feature - only enable when actively debugging. |
| Variable | Default | Description |
|---|---|---|
NEXT_PUBLIC_APPINSIGHTS_CLIENT_SAMPLING_PERCENTAGE |
20 |
Percentage of client-side events to track (1-100). |
NEXT_PUBLIC_APPINSIGHTS_TRACK_WEB_VITALS |
true |
Track Core Web Vitals metrics (TTFB, FCP, LCP, FID, CLS, INP). |
| Variable | Required | Description |
|---|---|---|
NEXT_PUBLIC_APP_INSIGHT_CONNECTION_STRING |
Yes | Azure Application Insights connection string. Set in Azure App Service configuration. |
- File:
appInsight-api.js - Loaded by:
instrumentation.ts(Next.js instrumentation hook) - Tracks:
- HTTP requests (with sampling)
- Dependencies (with sampling)
- Exceptions (tracked with sampling)
- Console errors/logs (configurable)
- User-agent information
- File:
context/app-insight-client.tsx - Type: React Context Provider
- Tracks:
- Page views (with sampling)
- AJAX calls (with sampling)
- Browser exceptions (tracked with sampling)
- Page visit time
- Web Vitals metrics (configurable)
APPINSIGHTS_SAMPLING_PERCENTAGE=20
APPINSIGHTS_ENABLE_CONSOLE_COLLECTION=false
APPINSIGHTS_COLLECT_NATIVE_CONSOLE=false
APPINSIGHTS_ENABLE_LIVE_METRICS=false
NEXT_PUBLIC_APPINSIGHTS_CLIENT_SAMPLING_PERCENTAGE=20
NEXT_PUBLIC_APPINSIGHTS_TRACK_WEB_VITALS=trueAPPINSIGHTS_SAMPLING_PERCENTAGE=100
APPINSIGHTS_ENABLE_CONSOLE_COLLECTION=true
APPINSIGHTS_COLLECT_NATIVE_CONSOLE=true
APPINSIGHTS_ENABLE_LIVE_METRICS=true
NEXT_PUBLIC_APPINSIGHTS_CLIENT_SAMPLING_PERCENTAGE=100
NEXT_PUBLIC_APPINSIGHTS_TRACK_WEB_VITALS=trueAPPINSIGHTS_SAMPLING_PERCENTAGE=100
APPINSIGHTS_ENABLE_CONSOLE_COLLECTION=true
APPINSIGHTS_COLLECT_NATIVE_CONSOLE=true
APPINSIGHTS_ENABLE_LIVE_METRICS=true
NEXT_PUBLIC_APPINSIGHTS_CLIENT_SAMPLING_PERCENTAGE=100
NEXT_PUBLIC_APPINSIGHTS_TRACK_WEB_VITALS=trueAPPINSIGHTS_SAMPLING_PERCENTAGE=10
APPINSIGHTS_ENABLE_CONSOLE_COLLECTION=false
APPINSIGHTS_COLLECT_NATIVE_CONSOLE=false
APPINSIGHTS_ENABLE_LIVE_METRICS=false
NEXT_PUBLIC_APPINSIGHTS_CLIENT_SAMPLING_PERCENTAGE=10
NEXT_PUBLIC_APPINSIGHTS_TRACK_WEB_VITALS=false- Navigate to your App Service in Azure Portal
- Go to Settings → Configuration
- Click New application setting for each variable
- Add/update the variable name and value
- Click Save (application will restart automatically)
# Set server-side sampling to 30%
az webapp config appsettings set \
--name <app-name> \
--resource-group <resource-group> \
--settings APPINSIGHTS_SAMPLING_PERCENTAGE=30
# Enable live metrics
az webapp config appsettings set \
--name <app-name> \
--resource-group <resource-group> \
--settings APPINSIGHTS_ENABLE_LIVE_METRICS=true
# Set multiple variables at once
az webapp config appsettings set \
--name <app-name> \
--resource-group <resource-group> \
--settings \
APPINSIGHTS_SAMPLING_PERCENTAGE=20 \
APPINSIGHTS_ENABLE_CONSOLE_COLLECTION=false \
APPINSIGHTS_COLLECT_NATIVE_CONSOLE=false \
APPINSIGHTS_ENABLE_LIVE_METRICS=false \
NEXT_PUBLIC_APPINSIGHTS_CLIENT_SAMPLING_PERCENTAGE=20 \
NEXT_PUBLIC_APPINSIGHTS_TRACK_WEB_VITALS=true- ✅ Exceptions: Tracked with exception auto-collection enabled (subject to sampling)
- ✅ Failed Requests: Captured to identify issues (subject to sampling)
- ✅ Console Output: Only tracked when console collection is enabled via
APPINSIGHTS_ENABLE_CONSOLE_COLLECTION=true
Note: While exception tracking is enabled, the configured sampling percentage applies to most telemetry types including exceptions. The sampling is designed to give a representative view of application behavior while reducing costs.
- 📊 Successful Requests: Only a percentage is tracked (default 20%)
- 📊 Dependencies: Database and API calls sampled (default 20%)
- 📊 Page Views: Client-side navigation sampled (default 20%)
- 📊 Web Vitals: Performance metrics sampled (default 20%)
- Sampling is random but consistent - all telemetry for a given request is either included or excluded together
- 20% sampling means you still get a representative view of traffic patterns and performance
- Exception tracking is enabled and will be sampled along with other telemetry
- For a mostly static site, 20% sampling provides sufficient visibility while significantly reducing costs
Solution: Verify environment variables are set correctly in Azure App Service. Check that sampling is enabled (default 20%).
Problem: With sampling enabled, some errors might not be captured. Solution: If you need to capture all errors temporarily, increase sampling to 100%:
az webapp config appsettings set \
--name <app-name> \
--resource-group <resource-group> \
--settings APPINSIGHTS_SAMPLING_PERCENTAGE=100Problem: Connection string might be missing or invalid. Verification:
- Check that
NEXT_PUBLIC_APP_INSIGHT_CONNECTION_STRINGis set in Azure - Check browser console for "Client side logging is turned on!" message
- Check server logs for "App Insights - Server Side logging is turned on!" message
Solution: Increase sampling percentage temporarily:
az webapp config appsettings set \
--name <app-name> \
--resource-group <resource-group> \
--settings APPINSIGHTS_SAMPLING_PERCENTAGE=100appInsight-api.js- Server-side configuration with samplingcontext/app-insight-client.tsx- Client-side configuration with samplingapp/components/web-vitals.tsx- Configurable Web Vitals trackingcomponents/layout/layout.tsx- Removed duplicate Web Vitals tracking.env.example- Added all configuration variables
- GitHub Issue #4187 - Original cost optimization request
For questions or issues related to Application Insights configuration, please open a GitHub issue or contact the SSW.Website team.