前面設定完了Application Block,接下來,我們必須要讓我們想要Auto Scale的目標能夠收集Performance資料,這樣Auto Scaling AB才有辦法判斷要不要scale。
作法是透過Diagnostic Monitor來收集相關數據。關於Diagnostic Monitor的相關說明可以參考這一整節,另外這裡有介紹了Diagnostic Monitor Configuration File。
我們關心的是WebRole執行時的效能數據,這邊我使用比較簡單的scenario,也是MSDN Sample Code上的Scenario,就是當WebRole的五分鐘平均CPU usage高於60%時,就增加一個Instance,如果五分鐘平均小於60%時,則減少一個Instance。這些規則已經在上一篇的rule.xml裡了,晚點我們再來review她一下。
在這邊打開我們的WebRole Project,在WebRole的OnStart()中加入下面這一段code:
public override bool OnStart()
{
// For information on handling configuration changes
// see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.
var config = DiagnosticMonitor.GetDefaultInitialConfiguration();
//Enabled by default, logged Infra. info. Remote Forwarder, Remote Access Module
config.DiagnosticInfrastructureLogs.ScheduledTransferPeriod = TimeSpan.FromMinutes(2);
config.DiagnosticInfrastructureLogs.ScheduledTransferLogLevelFilter = LogLevel.Verbose;
//Custom Log files
var dirConfig = new DirectoryConfiguration();
dirConfig.Container = "azurelog";
dirConfig.DirectoryQuotaInMB = 300;
dirConfig.Path = RoleEnvironment.GetLocalResource("MyLog").RootPath;
config.Directories.DataSources.Add(dirConfig);
config.Directories.ScheduledTransferPeriod = TimeSpan.FromMinutes(1);
//Basic Azure Log, write thru Listener
config.Logs.ScheduledTransferPeriod = TimeSpan.FromMinutes(1);
config.Logs.ScheduledTransferLogLevelFilter = LogLevel.Verbose;
config.Logs.ScheduledTransferPeriod = TimeSpan.FromMinutes(1);
//Windows Event Log
config.WindowsEventLog.DataSources.Add("Application!*");
config.WindowsEventLog.ScheduledTransferLogLevelFilter = LogLevel.Verbose;
config.WindowsEventLog.ScheduledTransferPeriod = TimeSpan.FromMinutes(1);
//preformance counter, 在我們的case裡,只需要注意下面這一段
config.PerformanceCounters.ScheduledTransferPeriod = TimeSpan.FromMinutes(1);
config.PerformanceCounters.BufferQuotaInMB = 300;
var cpuCounter = new PerformanceCounterConfiguration()
{
CounterSpecifier = @"\Processor(_Total)\% Processor Time",
SampleRate = TimeSpan.FromSeconds(30)
};
config.PerformanceCounters.DataSources.Add(
cpuCounter
);
if (!config.PerformanceCounters.DataSources.Contains(cpuCounter))
{
config.PerformanceCounters.DataSources.Add(cpuCounter);
}
//
config.OverallQuotaInMB = 4096;
DiagnosticMonitor.Start("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString",
config);
Helpers.Logger.Log("Started!Logged by Michael's Logger");
return base.OnStart();
}
上面這一大段code是我自己測試diagnostic monitor時做的測試,以Auto scaling的scenario來說,只需要performance counter這一段就可以了。
寫完Code,記得到Role->WorkerRole->Property中,把Storage的Connection String設定到上一篇中我們預定要存放Performance資料的Storage。接著就可以dpeloy到Azure上看看會發生甚麼事了。
沒有留言:
張貼留言