Azure Monitor是Azure上提供的通過監控一系列數據來監測應用程序的可用性和性能的解決方案,監測的數據包括Metrics,Logs,監測的平台可以是一個Azure的訂閱,資源組,也可以是這些資源組下面具體的VM,應用程序,甚至包括本地或者其他平台上的Guest OS/應用都可以支持
在應用方面,Azure Monitor有Application Insights 和 Azure Monitor for containers兩項服務為應用程序提供更深層次的監控和分析,Application Insights 提供Application Map,Live Metrics Stream,Availability,Failures & Performance等十個細化的功能,利用這個工具既可以監測到應用本身的健康狀況,也可以在業務層面更好的了解用戶訪問的相關信息,關於這十個功能模塊的介紹,可以參考我同事趙健寫的一篇博客:借助Application Insights,讓Apps快速擁有APM,目前Application Insights官方支持 .Net / Java / Node.js幾種語言,社區支持Python,PHP等其他語言,下面這篇文章會演示如何用Application Insights監視一個Python(Django框架)的應用。因為篇幅較長,我們把它大體分成兩篇,第一篇驗證Application Insights監控本地應用,第二篇用Application Insights監控一個部署在VM中的應用,除了看應用本身之外,我們還會看一下Azure Monitor在平台級別的監控如何。下面先開始准備工作:
(1)在用Azure VM + Azure Database for MySQL搭建Web服務這篇文章中,我們已經在Azure China Cloud搭好了一個WEB應用,為准備好環境,我們可以用快照跨區域復制的方法把這個VM快照拷貝到Global Auzre賬號下,
這里遇到一個“小坑”,我們還用這篇文章用到的命令(Azure上幾種常見的VM復制操作)來將快照跨區域復制到美東,
az storage blob copy start --destination-blob <name> --destination-container <name> --account-name <name> -account-key <> --source-uri <>
發現會報錯:
You do not have the required permissions needed to perform this operation. Depending on your operation, you may need to be assigned one of the following roles:
'Storage Blob Data Contributor (Preview)'
'Storage Blob Data Reader (Preview)'
'Storage Queue Data Contributor (Preview)'
'Storage Queue Data Reader (Preview)'
如果提示是這樣,表明你沒有權限,這時候需要給自己添加列出來的一個角色(即使你是這個存儲賬號的owner也需要添加),參照https://docs.microsoft.com/zh-cn/azure/storage/common/storage-auth-aad-rbac-portal 操作之后再運行就可以了:
快照復制完之后我們創建好虛擬機,重新調試一下環境沒問題。
(2)創建一個application insights服務,在portal.azure.com的market place 搜索application insights,創建,可以選擇美東,美東2,(其他感興趣的區域可以查看下看支不支持這個功能https://azure.microsoft.com/zh-cn/global-infrastructure/services/),創建完成后在概述中記下Instrumentation Key,這個相當與告訴應用你要把log發到哪個application insights服務終結點(service endpoint)。
(3)參考https://pypi.org/project/applicationinsights/0.11.8/ 選擇一個你想支持的Python版本,
先調試你本地的程序沒問題,然后安裝:
pip install applicationinsights
在你的setting.py 里添加一個中間件:
'applicationinsights.django.ApplicationInsightsMiddleware'
然后添加:
APPLICATION_INSIGHTS = { # (required) Your Application Insights instrumentation key 'ikey': "00000000-0000-0000-0000-000000000000", # (optional) By default, request names are logged as the request method # and relative path of the URL. To log the fully-qualified view names # instead, set this to True. Defaults to False. 'use_view_name': True, # (optional) To log arguments passed into the views as custom properties, # set this to True. Defaults to False. 'record_view_arguments': True, # (optional) Exceptions are logged by default, to disable, set this to False. 'log_exceptions': False, # (optional) Events are submitted to Application Insights asynchronously. # send_interval specifies how often the queue is checked for items to submit. # send_time specifies how long the sender waits for new input before recycling # the background thread. 'send_interval': 1.0, # Check every second 'send_time': 3.0, # Wait up to 3 seconds for an event # (optional, uncommon) If you must send to an endpoint other than the # default endpoint, specify it here: 'endpoint': "https://dc.services.visualstudio.com/v2/track", }
這里的ikey換成你上面記的Instrumentation Key,這樣就能將一些event log,trace發到ApplicationInsights上作分析和監控。另外還可以和Django的logging做集成,把Django的warnings,errors日志拿到ApplicationInsights上統一分析與監控:
LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handlers': { # The application insights handler is here 'appinsights': { 'class': 'applicationinsights.django.LoggingHandler', 'level': 'WARNING' } }, 'loggers': { 'django': { 'handlers': ['appinsights'], 'level': 'WARNING', 'propagate': True, } } }
(4)上你剛創建的Azure Portal上查看:
概述中會將以下指標顯示出來:
看下performance:
應用依賴情況:
session情況:
更多內容其實可以根據這篇操作自己搭一個體驗一下怎么看。
至此一個本地Python應用的監控基本驗證完成了,還是比較簡單的配置,后面的文章會慢慢詳細分析其中這些指標的內容和查詢。