-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueueAgent.cs
92 lines (77 loc) · 3.18 KB
/
QueueAgent.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Queue;
using NewRelic.Platform.Sdk;
namespace ScalableBytes.NewRelic.AzureStorageQueueSize.Plugin
{
public class QueueAgent : Agent
{
public override string Version { get { return "1.0.0"; } }
public string SystemName;
public List<Dictionary<string, string>> StorageAccounts;
public QueueAgent(string systemName, List<Dictionary<string, string>> accounts)
{
SystemName = systemName;
StorageAccounts = accounts;
}
/// <summary>
/// Returns a human-readable string to differentiate different hosts/entities in the site UI
/// </summary>
/// <returns></returns>
public override string GetAgentName()
{
return SystemName;
}
public override string Guid
{
get
{
return "com.scalablebytes.newrelic.azurestoragequeuesize";
}
}
/// <summary>
// This is where logic for fetching and reporting metrics should exist.
// Call off to a REST head, SQL DB, virtually anything you can programmatically
// get metrics from and then call ReportMetric.
/// </summary>
public override void PollCycle()
{
foreach (var storageAccountInfo in StorageAccounts)
{
var accountName = storageAccountInfo["accountName"];
var connectionString = storageAccountInfo["connectionString"];
var storageAccount = CloudStorageAccount.Parse(connectionString);
var queueClient = storageAccount.CreateCloudQueueClient();
var continuationToken = new QueueContinuationToken();
while (continuationToken != null)
{
var listResponse = queueClient.ListQueuesSegmented(continuationToken);
// We must ask Azure for the size of each queue individually.
// This can be done in parallel.
Parallel.ForEach(listResponse.Results, queue =>
{
try
{
queue.FetchAttributes();
}
catch (Exception)
{
// Failed to communicate with Azure Storage, or queue is gone.
}
});
// ReportMetric is not thread-safe, so we can't call it in the parallel
foreach (var queue in listResponse.Results)
{
int count = queue.ApproximateMessageCount.HasValue ? queue.ApproximateMessageCount.Value : 0;
string metricName = string.Format("Queues/{0}/{1}/size", accountName, queue.Name);
ReportMetric(metricName, "messages", count);
}
continuationToken = listResponse.ContinuationToken;
}
}
}
}
}