1

I'm trying to access metrics for a given cloudService

I have the following code:

var metricsClient = new MetricsClient(new CertificateCloudCredentials(SubscriptionId, Certificate));

var resourceid = ResourceIdBuilder.BuildCloudServiceResourceId(cloudServiceName, deploymentName);

var metricsDefinisions = metricsClient.MetricDefinitions.List(resourceid, null, null);


// timeGrain must be 5, 60 or 720 minutes.
TimeSpan timeGrain = TimeSpan.FromMinutes(60);
DateTime startTime = DateTime.UtcNow.AddDays(-1);
DateTime endTime = DateTime.UtcNow;

var name = metricsDefinisions.MetricDefinitionCollection.Value.First().Name;

var response = metricsClient.MetricValues.List(resourceid, new string[] { name }, String.Empty, timeGrain, startTime, endTime);

The last line returns with an error -

{"Code":"InvalidRequest","Message":"Could not retrieve metrics."}

What could I be doing wrong?

Elad Katz
  • 7,483
  • 5
  • 35
  • 66

1 Answers1

1

from the MSDN ticket i've opened:

The problem is the resourceId being used to query the metrics. While you can request metric definitions at the deployment level (this allows discovery of what metrics are available) you cannot query for metrics at the deployment.

Metrics are available at the role and role instance level.

If you look at each of the metricDefinitions returned by the call :

  var metricsDefinisions = metricsClient.MetricDefinitions.List(resourceId, null, null);

they should have a ResourceIdSuffix property for each metric in the list. This provides the information you need to discover where the metrics are located.

Basically they will contain values like roles\roleName and roles/rolename/roleinstance/roleinstancename If you add this to the resourceId you already have (as a valid uri) it should get you the metric values associated with that definition. In your case you probably just need to add the role/roleinstance for your specific application.

I was able to change my repro where I was able to repro the issue to the following and get it working. From:

var resourceId = ResourceIdBuilder.BuildCloudServiceResourceId("imtiazhclientcerttest", "3d2975a038db48d1bd9f40c3b14de459");

To:

var resourceId = ResourceIdBuilder.BuildCloudServiceResourceId("imtiazhclientcerttest", "3d2975a038db48d1bd9f40c3b14de459","WebRole1", "WebRole1_IN_0");
Elad Katz
  • 7,483
  • 5
  • 35
  • 66