Share via


How to use Windows Azure Monitoring Services Management Library to create an Autoscale Rule

 

Recently we released Windows Azure Monitoring Services Management Library 0.9.2-preview which is a unified API to retrieve and configure monitoring metrics, alerts, and autoscale rules for your Windows Azure services. Find MSDN documentation here and an excellent presentation at https://channel9.msdn.com/Shows/Cloud+Cover/Episode-131-Developing-against-the-Monitoring-Alerts-Autoscale-and-Metrics-API .

 

Here is a quick walkthrough on how to set an Autoscale rule for your cloud service.

 

1. Create a sample application in Visual Studio. I chose to create MVC Webrole (just out of habit).

2. Install Nuget Package in your solution

 

image

 

3. Here is the code I used in HomeController.cs for setting up an Autoscale rule for my cloud service triggering on queue message count. Please ensure to replace the values in highlighted portion with your own.

 

  
  
  using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Web.Mvc;
 using System.Text;
 using Microsoft.WindowsAzure;
 using Microsoft.WindowsAzure.Management.Monitoring.Autoscale;
 using Microsoft.WindowsAzure.Management.Monitoring.Autoscale.Models;
 using Microsoft.WindowsAzure.Management.Monitoring.Utilities;
 using System.Security.Cryptography.X509Certificates;
 
 
 namespace MvcWebRole1.Controllers
 {
 public class HomeController : Controller
 {
 public ActionResult Index()
 {
 ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
 
 // The Windows Azure subscription ID.
 string subscriptionId = "<subscription id>";
 
 // The thumbprint of the certificate.
 string thumbprint = "<management certificate thumbprint>";
 
 // Get the certificate from the local store.
 X509Certificate2 cert =GetCertificate(StoreName.My, StoreLocation.CurrentUser, thumbprint);
 
 // Create the autoscale client. .. this can fail if there is no autoscale setting already
 AutoscaleClient autoscaleClient = 
 new AutoscaleClient(new CertificateCloudCredentials(subscriptionId, cert));
 
 AutoscaleSettingGetResponse get = 
 autoscaleClient.Settings.Get(AutoscaleResourceIdBuilder.BuildCloudServiceResourceId("<cloud service name>","<role name>",true) );
 AutoscaleSetting setting = get.Setting;
 
 AutoscaleSettingCreateOrUpdateParameters createParams = new AutoscaleSettingCreateOrUpdateParameters()
 {
 Setting = new AutoscaleSetting()
 {
 Enabled = true,
 Profiles = new List<AutoscaleProfile>
 {
 new AutoscaleProfile 
 {
 Capacity = new ScaleCapacity 
 { 
 Default ="1", 
 Maximum="10", 
 Minimum="1"
 },
 Name = "anurag",
 Recurrence= new Recurrence 
 { 
 Frequency=RecurrenceFrequency.Week,
 Schedule = new RecurrentSchedule
 { 
 Days = new List<String>{"Monday", "Thursday", "Friday"},
 Hours = {7, 19},
 Minutes=new List<int>{0},
 TimeZone = "Pacific Standard Time"
 }
 },
 Rules=new List<ScaleRule>
 {
 new ScaleRule
 { 
 MetricTrigger =new MetricTrigger
 {
 MetricName="ApproximateMessageCount",
 MetricNamespace="", 
 MetricSource=
 AutoscaleMetricSourceBuilder.BuildStorageQueueMetricSource("<storage account name>", "<queue name>"),
 Operator=ComparisonOperationType.GreaterThan,
 Threshold=2000,
 Statistic=MetricStatisticType.Average,
 TimeGrain=TimeSpan.FromMinutes(5),
 TimeAggregation=TimeAggregationType.Average,
 TimeWindow=TimeSpan.FromMinutes(30)
 },
 ScaleAction = new ScaleAction 
 {
 Direction = ScaleDirection.Increase,
 Cooldown = TimeSpan.FromMinutes(20),
 Type=ScaleType.ChangeCount,
 Value = "4"
 }
 }
 }
 }
 
 
 
 
 }
 }
 };
 
 
 OperationResponse autoscaleResponse =
 autoscaleClient.Settings.CreateOrUpdate (AutoscaleResourceIdBuilder.BuildCloudServiceResourceId("<cloud service name>", "<role name>",
 true), createParams);
 
 Response.Write(autoscaleResponse.StatusCode.ToString());
 
 
 return View();
 }
 
 public ActionResult About()
 {
 ViewBag.Message = "Your app description page.";
 
 return View();
 }
 
 public ActionResult Contact()
 {
 ViewBag.Message = "Your contact page.";
 
 return View();
 }
 
 public static X509Certificate2 GetCertificate(StoreName storeName, StoreLocation storeLocation, string thumbprint)
 {
 var store = new X509Store(storeName, storeLocation);
 store.Open(OpenFlags.ReadOnly);
 
 try
 {
 StringBuilder builder = new StringBuilder(thumbprint.Length);
 foreach (char c in thumbprint)
 {
 if (char.IsLetterOrDigit(c))
 {
 builder.Append(c);
 }
 }
 
 string cleanThumbprint = builder.ToString();
 X509Certificate2Collection list = store.Certificates.Find(
 X509FindType.FindByThumbprint, cleanThumbprint, false);
 
 X509Certificate2 cert;
 if (list == null || list.Count != 1)
 {
 cert = null;
 }
 else
 {
 cert = list[0];
 }
 
 return cert;
 }
 finally
 {
 store.Close();
 }
 }
 
 }
 }
 

In case you want to scale your Cloud Service by CPU then you can use below code for Metrictrigger

  .
 .
 MetricTrigger =new MetricTrigger
 {
 MetricName="Percentage CPU",
 MetricNamespace="", 
 MetricSource=
 AutoscaleMetricSourceBuilder.BuildCloudServiceMetricSource("<servicename>","<rolename>",true),
 Operator=ComparisonOperationType.GreaterThan,
 Threshold=80,
 Statistic=MetricStatisticType.Average, 

Hope this helps !!

Comments

  • Anonymous
    September 25, 2014
    Hi Anurag,Thanks for the post. Can you explain how we can connect to a custom metrics source. Even connecting to Azure diagnostics account for reading metrics would also work. There is no example given in the msdn documentation, so any information would be helpful.Thanks