Instantly share code, notes, and snippets.

Embed
What would you like to do?
#r "Newtonsoft.Json"
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using Microsoft.Azure.Services.AppAuthentication;
using Newtonsoft.Json;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Queue;
private static HttpClient HttpClient = new HttpClient();
private static AzureServiceTokenProvider AzureServiceTokenProvider = new AzureServiceTokenProvider();
private static string QueueStorageAccountResourceId = System.Environment.GetEnvironmentVariable("QueueStorageAccountResourceId");
private static string QueueStorageAccountResourceRegionId = System.Environment.GetEnvironmentVariable("QueueStorageAccountResourceRegionId");
private static string QueueStorageAccountConnectionString = System.Environment.GetEnvironmentVariable("QueueStorageAccountConnectionString");
private const string MetricName = "QueueLength";
private const string MetricNamespace = "QueueProcessing";
public static async Task Run(TimerInfo timer, ILogger log)
{
// connect to the storage account
var storageAccount = CloudStorageAccount.Parse(QueueStorageAccountConnectionString);
var queueClient = storageAccount.CreateCloudQueueClient();
// list the queues in the account
var queues = new List<CloudQueue>();
QueueContinuationToken continuationToken = null;
do
{
var segment = await queueClient.ListQueuesSegmentedAsync(continuationToken);
queues.AddRange(segment.Results);
continuationToken = segment.ContinuationToken;
} while (continuationToken != null);
if (!queues.Any())
{
log.LogInformation("There are no queues to process.");
return;
}
log.LogInformation("Found {queueCount} queues.", queues.Count());
// get the message count from each queue
var queueSummaries = new List<(string queueName, int? queueLength)>();
foreach (var queue in queues)
{
await queue.FetchAttributesAsync();
queueSummaries.Add((queue.Name, queue.ApproximateMessageCount));
}
// prepare a metric to publish
var metric = new Metric
{
Time = DateTime.UtcNow,
Data = new MetricData
{
BaseData = new MetricBaseData
{
Metric = MetricName,
Namespace = MetricNamespace,
DimNames = new string[] {
"QueueName"
},
Series = queueSummaries.Select(qs => {
if (qs.queueLength == null)
{
return null;
}
else
{
return new MetricSeries {
DimValues = new[] {
qs.queueName
},
Min = qs.queueLength.Value,
Max = qs.queueLength.Value,
Sum = qs.queueLength.Value,
Count = 1
};
}
}).Where(qs => qs != null)
}
}
};
// publish the metric to Azure Monitor
await PublishMetric(metric);
}
public static async Task PublishMetric(Metric metric)
{
var metricJson = JsonConvert.SerializeObject(metric);
var stringContent = new StringContent(metricJson, System.Text.Encoding.Default, "application/json");
var token = await AzureServiceTokenProvider.GetAccessTokenAsync("https://monitoring.azure.com/");
HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
var monitoringUri = $"https://{QueueStorageAccountResourceRegionId}.monitoring.azure.com{QueueStorageAccountResourceId}/metrics";
var result = await HttpClient.PostAsync(monitoringUri, stringContent);
result.EnsureSuccessStatusCode();
}
public class Metric
{
public DateTime Time { get; set; }
public MetricData Data { get; set; }
}
public class MetricData
{
public MetricBaseData BaseData { get; set; }
}
public class MetricBaseData
{
public string Metric { get; set; }
public string Namespace { get; set; }
public IEnumerable<string> DimNames { get; set; }
public IEnumerable<MetricSeries> Series { get; set; }
}
public class MetricSeries
{
public IEnumerable<string> DimValues { get; set;}
public double Min { get; set; }
public double Max { get; set; }
public double Sum { get; set; }
public int Count { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment