June 7, 2012 update: The Microsoft Windows Azure team has released a new Windows Azure SDK for PHP. This release is part of an effort to keep PHP client libraries up to date with new Windows Azure features and to make PHP a first-class citizen in Windows Azure. The latest client libraries are on GitHub: https://github.com/WindowsAzure/azure-sdk-for-php. While the SDK hosted on CodePlex will continue to work for the foreseeable future, it is strongly recommended that new PHP/Windows Azure application use the SDK hosted on GitHub.
The work done by Maarten Balliauw and other contributors in building the SDK hosted on CodePlex was critical in unifying the PHP developer experience for Windows Azure. The Windows Azure team is grateful to these contributors for their pioneering work and looks forward to their continued support (and yours!) in adding to the new SDK on GitHub.
The Windows Azure Team
In part 1 of this two part series, I showed how you can use a configuration file (the diagnostics.wadcfg file) to configure Azure diagnostics. However, that approach requires that you know all the diagnostic information that you want to collect before you deploy your application. In this post, I’ll show how you can use the Windows Azure SDK for PHP API to configure diagnostic information after an application has been deployed to Windows Azure.
Recall from part 1 that the Windows Azure Diagnostics Monitor persists your diagnostics configuration in your Azure Blob storage (in a container called wad-control-container). One configuration file is stored for each role instance your are running (the name of each file is of the format <deployment_id>/<role_name>/<role_instance_name>). You could, of course, download the configuration files (they are XML files), edit them, and upload them to your Blob storage. The Diagnostics Monitor would, on it’s configured schedule, check for updates and apply them. However, it would be much more fun to write code to do this for us. So, from here, I’ll walk you through a PHP script that turns on the collection of 3 performance counters. I’ll provide the complete script at the end, and provide some guidance as to how you can turn on other diagnostics. (I’ll assume that you have read part 1 of this series.)
First, I’ll include the classes I’ll use:
Next, I’ll define several constants I’ll use in my script:
A couple notes about these constants:
- ROLE_NAME: This is specified in your ServiceConfiguration.cscfg file. If you used the default scaffold in the Windows Azure SDK for PHP to create your project skeleton, this value will be PhpOnAzure.Web.
- CERTIFICATE: See Overview of Command Line Deployment and Management with the Windows Azure SDK for PHP for information about creating a .pem certificate.
Now I’ll use the Client, Blob, and Manager classes to get deployment information and current diagnostic configuration (from my blob storage). I’ll also define an array that contains the performance counters I want to collect:
And finally, I’ll loop through the role instances for my deployment and turn on the performance counters I defined above.
A couple of notes about the code above:
- This code assumes that you might have multiple roles for a deployment (e.g. a worker role). It will only turn on performance counters for the specified ROLE_NAME.
- The getConfigurationForRoleInstance method on the Manager class requires a “role Id” parameter. The role Id is of this format: <deployment id>/<role name>/<role name>_IN_<instance index>.
- The DataSources property on the Configuration class has several properties that you can set: overallquotainmb, logs, diagnosticinfrastructurelogs, performancecounters, windowseventlog, and directories.
That’s it. Within a few minutes after executing that script you should begin seeing performance counter data written to the WADPerformanceCountersTable table in your Windows Azure Table storage.
Collecting other diagnostic information is similar. However, you’ll need to look more closely at these classes in the SDK to figure out the details:
I’d be happy to dive into the details of using any of those classes…just post a comment!
Oh yes, and here’s the complete script, as promised: