Компоненты мониторинга Azure
Azure Functions → Application Insights → Azure Monitor → Alerts
↓ ↓
Логи/Метрики Log Analytics
↓
KQL Queries → Dashboards
Application Insights
Application Insights автоматически собирает телеметрию из Azure Functions: запросы, зависимости, ошибки, производительность.
# Создание Application Insights
az monitor app-insights component create \
--app crc-insights \
--resource-group crc-resources \
--location westeurope
# Привязка к Function App
INSTRUMENTATION_KEY=$(az monitor app-insights component show \
--app crc-insights \
--resource-group crc-resources \
--query instrumentationKey --output tsv)
az functionapp config appsettings set \
--name crc-visitor-counter \
--resource-group crc-resources \
--settings "APPINSIGHTS_INSTRUMENTATIONKEY=$INSTRUMENTATION_KEY"
KQL-запросы (Kusto Query Language)
KQL -- язык запросов Azure Monitor. Используется для анализа логов и метрик.
Основные запросы для CRC
// Количество запросов за последний час
requests
| where timestamp > ago(1h)
| summarize count() by bin(timestamp, 5m)
| render timechart
// Среднее время ответа
requests
| where timestamp > ago(24h)
| summarize avg(duration), percentile(duration, 95) by bin(timestamp, 1h)
| render timechart
// Ошибки
requests
| where timestamp > ago(24h)
| where success == false
| summarize count() by resultCode
// Топ ошибок
exceptions
| where timestamp > ago(24h)
| summarize count() by type, outerMessage
| order by count_ desc
// Зависимости (Cosmos DB вызовы)
dependencies
| where timestamp > ago(1h)
| where target contains "cosmos"
| summarize avg(duration), count() by bin(timestamp, 5m)
| render timechart
Алерты
# Алерт: ошибки > 5 за 5 минут
az monitor metrics alert create \
--name "crc-errors" \
--resource-group crc-resources \
--scopes "/subscriptions/.../resourceGroups/crc-resources/providers/Microsoft.Web/sites/crc-visitor-counter" \
--condition "total requests/count > 5 where statusCode includes 5" \
--window-size 5m \
--evaluation-frequency 1m \
--action "/subscriptions/.../providers/Microsoft.Insights/actionGroups/crc-alerts"
# Алерт: время ответа > 3 секунд
az monitor metrics alert create \
--name "crc-slow-response" \
--resource-group crc-resources \
--scopes "..." \
--condition "avg HttpResponseTime > 3000" \
--window-size 5m
Dashboard
# Создание dashboard через Azure Portal:
# 1. Azure Portal → Dashboard → New Dashboard
# 2. Add tile: Application Insights → Requests
# 3. Add tile: Application Insights → Response time
# 4. Add tile: Application Insights → Failed requests
# 5. Add tile: Cosmos DB → Request Units consumed
Cost Management
# Расходы по ресурсной группе
az consumption usage list \
--start-date $(date -d '30 days ago' +%Y-%m-%d) \
--end-date $(date +%Y-%m-%d) \
--query "[?contains(instanceName, 'crc')].{Service:meterCategory, Cost:pretaxCost}" \
--output table
Live Metrics
Application Insights Live Metrics показывает real-time данные: запросы, ошибки, CPU, память. Доступно в Azure Portal → Application Insights → Live Metrics.