Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Azure resource monitoring with Azure Functions v2
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AzureMonitoring
public class MonitorMetrics
private static string key = TelemetryConfiguration.Active.InstrumentationKey = Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", EnvironmentVariableTarget.Process);
private static TelemetryClient telemetry = new TelemetryClient() { InstrumentationKey = key };
public static async Task MonitorQueueLength([TimerTrigger("*/10 * * * * *")]TimerInfo myTimer,ILogger log)
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
// connect to Azure Storage
var connectionString = Environment.GetEnvironmentVariable("queue_storage_connection_string");
var account = CloudStorageAccount.Parse(connectionString);
var queueClient = account.CreateCloudQueueClient();
var queueNames = new [] {"myqueue", "myqueue-poison"};
var tasks = queueNames.Select(async x =>
// get a reference to the queue and retrieve the queue length
var queue = queueClient.GetQueueReference(x);
await queue.FetchAttributesAsync();
var length = queue.ApproximateMessageCount;
// log the length
log.LogInformation($"{x}: {length}");
await Task.WhenAll(tasks);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment