Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify that storage queue to monitor exists #417

Merged
merged 3 commits into from
Mar 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions samples/promitor-sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ metrics:
# Optionally override the default aggregation interval (metricDefaults.aggregation.interval)
interval: 00:15:00
- name: demo_azurestoragequeue_queue_size
description: "Approximate amount of messages in 'testqueue' queue (determined with AzureStorageQueue provider)"
resourceType: AzureStorageQueue
description: "Approximate amount of messages in 'testqueue' queue (determined with StorageQueue provider)"
resourceType: StorageQueue
accountName: testaccount
queueName: testqueue
sasToken: "?sv=2015-04-05&si=read&sig=zs87c3nUp1uiF4UAMMstXrLKhIpGOirFHRcQ4XYxxpY%3D"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Promitor.Core.Scraping.ResourceTypes
{
public class StorageQueueScraper: Scraper<StorageQueueMetricDefinition>
public class StorageQueueScraper : Scraper<StorageQueueMetricDefinition>
{
private readonly AzureStorageQueueClient _azureStorageQueueClient;
public StorageQueueScraper(AzureMetadata azureMetadata, MetricDefaults metricDefaults, AzureMonitorClient azureMonitorClient, ILogger logger, IExceptionTracker exceptionTracker)
Expand Down
25 changes: 25 additions & 0 deletions src/Promitor.Integrations.AzureStorage/AzureStorageClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using GuardNet;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;

namespace Promitor.Integrations.AzureStorage
{
public class AzureStorageClient
{
/// <summary>
/// Authenticate with Azure Storage
/// </summary>
/// <param name="accountName">Name of the storage account</param>
/// <param name="sasToken">SAS token to authenticate with</param>
/// <returns>Authenticated storage account</returns>
protected CloudStorageAccount AuthenticateWithSasToken(string accountName, string sasToken)
{
Guard.NotNullOrEmpty(accountName, nameof(accountName));
Guard.NotNullOrEmpty(sasToken, nameof(sasToken));

var storageCredentials = new StorageCredentials(sasToken);

return new CloudStorageAccount(storageCredentials, accountName, endpointSuffix: null, useHttps: true);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Queue;
using Promitor.Integrations.AzureStorage.Exceptions;

namespace Promitor.Integrations.AzureStorage
{
public class AzureStorageQueueClient
public class AzureStorageQueueClient : AzureStorageClient
{
private readonly ILogger _logger;

Expand All @@ -29,15 +28,21 @@ public AzureStorageQueueClient(ILogger logger)
public async Task<int> GetQueueMessageCountAsync(string accountName, string queueName, string sasToken)
{
var queue = GetQueueReference(accountName, queueName, sasToken);
var doesQueueExist = await queue.ExistsAsync();
if (doesQueueExist == false)
{
throw new QueueNotFoundException(queueName);
}

await queue.FetchAttributesAsync();
var messageCount = queue.ApproximateMessageCount ?? 0;
_logger.LogInformation("Current size of queue {0} is {1}", queueName, messageCount);
return messageCount;
}

private static CloudQueue GetQueueReference(string accountName, string queueName, string sasToken)
private CloudQueue GetQueueReference(string accountName, string queueName, string sasToken)
{
var account = new CloudStorageAccount(new StorageCredentials(sasToken), accountName, null, true);
var account = AuthenticateWithSasToken(accountName, sasToken);
var queueClient = account.CreateCloudQueueClient();
return queueClient.GetQueueReference(queueName);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using GuardNet;

namespace Promitor.Integrations.AzureStorage.Exceptions
{
public class QueueNotFoundException : Exception
{
/// <summary>
/// Constructor
/// </summary>
/// <param name="queueName">Name of the queue that was not found</param>
public QueueNotFoundException(string queueName) : base($"Azure Storage Queue '{queueName}' was not found")
{
Guard.NotNullOrEmpty(queueName, nameof(queueName));

QueueName = queueName;
}

public string QueueName { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Guard.Net" Version="1.2.0" />
<PackageReference Include="Microsoft.Azure.Storage.Queue" Version="9.4.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.2.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public async Task ExecuteAsync(CancellationToken cancellationToken)
var scrapingTask = ScrapeMetric(scrapeConfiguration.AzureMetadata, scrapeConfiguration.MetricDefaults, metricDefinition);
scrapingTasks.Add(scrapingTask);
}

await Task.WhenAll(scrapingTasks);
}
catch (Exception exception)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public List<string> Validate(StorageQueueMetricDefinition metricDefinition)

if (string.IsNullOrWhiteSpace(metricDefinition.SasToken))
{
errorMessages.Add("No Azure Storage Queue SAS Token is configured");
errorMessages.Add("No Azure Storage SAS Token is configured");
}

if (metricDefinition.AzureMetricConfiguration.MetricName != "MessageCount")
Expand Down