Skip to content
Daniel Berger edited this page May 4, 2016 · 30 revisions

Metrics in Azure

You will probably want to collect metrics on your various resources within Azure at some point. This page explains how to do that.

Available Metrics

To get metadata about the metrics information itself, you can use the Azure::Insights::MetricsService class. You must know the namespace provider and type of resource that you're using, as well as the resource name and resource group.

If you are unfamiliar with the available namespace providers and resource types, please see the ArmrestService#providers or ArmrestService#provider_info methods.

Below is a sample program that looks for metrics for available memory on a particular virtual machine.

require 'azure-armrest'

conf = Azure::Armrest::ArmrestService.configure(<credentials>)
mets = Azure::Armrest::Insights::MetricsService.new(conf)

rgroup  = 'your_group'
vm_name = 'your_vm'

metrics = mets.list('Microsoft.Compute', 'virtualMachines', vm_name, rgroup)

time_grains = {
  "PT1M"  => "1 Minute",
  "PT5M"  => "5 Minutes",
  "PT1H"  => "1 Hour",
  "PT12H" => "12 Hours"
}

metrics.each do |metric|
  # Comment this out to see everything
  next unless metric.name.localized_value == "Memory available"

  puts "Metric Name: " + metric.name.localized_value
  puts "Start: " + Time.parse(metric.start_time).to_s
  puts "End: " + Time.parse(metric.end_time).to_s
  puts "Unit: " + metric.unit
  puts "Aggregation Type: " + metric.primary_aggregation_type

  metric.metric_availabilities.each do |ma|
    puts "=" * 40
    puts "Time Grain: " + time_grains[ma.time_grain]
    puts "Endpoint: " + ma.location.table_endpoint

    ma.location.table_info.each do |ti|
      puts "-" * 40
      puts "Table Name: " + ti.table_name
      puts "Start Time: " + Time.parse(ti.start_time).to_s
      puts "End Time: " + Time.parse(ti.end_time).to_s
    end
  end

  puts
end

# Sample output, edited for clarity

Metric Name: Memory available
Start: 0001-01-01 00:00:00 UTC
End: 0001-01-01 00:00:00 UTC
Unit: Bytes
Aggregation Type: Average

==>
Time Grain: 1 Hour
Endpoint: https://your_storage1.table.core.windows.net/

Table Name: WADMetricsPT1HP10DV2S20160316
Start Time: 2016-03-16 00:00:00 UTC
End Time: 2016-03-26 00:00:00 UTC
----------------------------------------
Table Name: WADMetricsPT1HP10DV2S20160326
Start Time: 2016-03-26 00:00:00 UTC
End Time: 2016-04-05 00:00:00 UTC
<==

==>
Time Grain: 1 Minute
Endpoint: https://your_storage1.table.core.windows.net/

Table Name: WADMetricsPT1MP10DV2S20160316
Start Time: 2016-03-16 00:00:00 UTC
End Time: 2016-03-26 00:00:00 UTC
----------------------------------------
Table Name: WADMetricsPT1MP10DV2S20160326
Start Time: 2016-03-26 00:00:00 UTC
End Time: 2016-04-05 00:00:00 UTC
<==

This example shows 4 different tables, 2 of which store data in an hourly fashion, the other two minute by minute. Each table stores up to 10 days worth of data.

Notice that you can generally tell what kind of format the data is in by looking at the table name, e.g. the "PT1H" in the table name indicates hourly, while the "PT1M" indicates minutes. You can also derive the name of the storage account via the endpoint.

Looking at the endpoint, you can see that these tables are contained within the storage account "your_storage1", and you can see them in the new portal if you want. Just to keep things interesting, multiple VM's can use the same storage account, so you can't assume that all of the data retrieved from those tables is for a single VM, or whatever resource type you're getting metrics on.