Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,8 @@ public QueueMetricsProvider(QueueClient queue, ILoggerFactory loggerFactory)
/// <returns>The queue length from the associated queue entity.</returns>
public async Task<int> GetQueueLengthAsync()
{
try
{
QueueTriggerMetrics queueMetrics = await GetMetricsAsync().ConfigureAwait(false);
return queueMetrics.QueueLength;
}
catch (RequestFailedException ex)
{
if (ex.IsNotFoundQueueNotFound() ||
ex.IsConflictQueueBeingDeletedOrDisabled() ||
ex.IsServerSideError())
{
// ignore transient errors, and return default metrics
// E.g. if the queue doesn't exist, we'll return a zero queue length
// and scale in
_logger.LogWarning($"Error querying for queue scale status: {ex.ToString()}");
}
}
catch (Exception ex)
{
_logger.LogWarning($"Fatal error querying for queue scale status: {ex.ToString()}");
}

return 0;
QueueTriggerMetrics queueMetrics = await GetMetricsAsync().ConfigureAwait(false);
return queueMetrics.QueueLength;
Comment on lines +38 to +39
Copy link

Copilot AI Nov 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing exception handling from GetQueueLengthAsync() and GetMetricsAsync() changes the API contract in a breaking way. Callers like QueueTargetScaler.GetScaleResultAsync() that previously received a default value (0) during transient errors will now experience exceptions. This could cause scaling operations to fail instead of gracefully degrading. Consider whether callers are prepared to handle exceptions for transient storage issues like queue not found, queue being deleted, or server-side errors.

Copilot uses AI. Check for mistakes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do kinda agree with this but at the same time, if I recall correctly, if an exception is thrown in webjobs then it will actually emit errors to application insights actually and not tear down the entire application. @alrod @mathewc Could you help my understanding here? Just want to make sure we don't cause a breaking behavior change with this.

Copy link
Member

@mathewc mathewc Nov 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's not clear to me either whether changing these contracts across all the extensions won't be breaking. As it stands, when any of these monitors/scalers can't provide metrics, they return default/empty metrics but don't throw.

The client of these APIs is not only ScaleManager but also ScaleMonitorService for runtime driven scale. Are there any other callers of these APIs that might not be prepared to handle exceptions? We need to carefully verify.

Yes, this is a risky change to make across all these extensions. I don't understand - why won't these error messages written to the supplied ILogger go to the customer's App Insights or other log streams? The logger should already be configured to do this.

}

/// <summary>
Expand All @@ -69,45 +48,26 @@ public async Task<QueueTriggerMetrics> GetMetricsAsync()
int queueLength = 0;
TimeSpan queueTime = TimeSpan.Zero;

try
{
QueueProperties queueProperties = await _queue.GetPropertiesAsync().ConfigureAwait(false);
queueLength = queueProperties.ApproximateMessagesCount;
QueueProperties queueProperties = await _queue.GetPropertiesAsync().ConfigureAwait(false);
queueLength = queueProperties.ApproximateMessagesCount;

if (queueLength > 0)
if (queueLength > 0)
{
PeekedMessage message = (await _queue.PeekMessagesAsync(1).ConfigureAwait(false)).Value.FirstOrDefault();
if (message != null)
{
PeekedMessage message = (await _queue.PeekMessagesAsync(1).ConfigureAwait(false)).Value.FirstOrDefault();
if (message != null)
{
if (message.InsertedOn.HasValue)
{
queueTime = DateTime.UtcNow.Subtract(message.InsertedOn.Value.DateTime);
}
}
else
if (message.InsertedOn.HasValue)
{
// ApproximateMessageCount often returns a stale value,
// especially when the queue is empty.
queueLength = 0;
queueTime = DateTime.UtcNow.Subtract(message.InsertedOn.Value.DateTime);
}
}
}
catch (RequestFailedException ex)
{
if (ex.IsNotFoundQueueNotFound() ||
ex.IsConflictQueueBeingDeletedOrDisabled() ||
ex.IsServerSideError())
else
{
// ignore transient errors, and return default metrics
// E.g. if the queue doesn't exist, we'll return a zero queue length
// and scale in
_logger.LogWarning($"Error querying for queue scale status: {ex.ToString()}");
// ApproximateMessageCount often returns a stale value,
// especially when the queue is empty.
queueLength = 0;
}
}
catch (Exception ex)
{
_logger.LogWarning($"Fatal error querying for queue scale status: {ex.ToString()}");
}

return new QueueTriggerMetrics
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Threading;
using System.Threading.Tasks;
using Azure;
using Azure.Core.TestFramework;
using Azure.Storage.Queues;
using Microsoft.Azure.WebJobs.Extensions.Storage.Common.Listeners;
using Microsoft.Azure.WebJobs.Extensions.Storage.Common.Tests;
using Microsoft.Azure.WebJobs.Host.Listeners;
Copy link

Copilot AI Nov 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This using statement appears to be unused and should be removed.

Suggested change
using Microsoft.Azure.WebJobs.Host.Listeners;

Copilot uses AI. Check for mistakes.
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -81,7 +82,7 @@ public async Task GetMetrics_ReturnsExpectedResult()
}

[Test]
public async Task GetMetrics_HandlesStorageExceptions()
public void GetMetrics_HandlesStorageExceptions()
{
var exception = new RequestFailedException(
500,
Expand All @@ -91,14 +92,9 @@ public async Task GetMetrics_HandlesStorageExceptions()

_mockQueue.Setup(p => p.GetPropertiesAsync(It.IsAny<CancellationToken>())).Throws(exception);

var metrics = await _metricsProvider.GetMetricsAsync();

Assert.AreEqual(0, metrics.QueueLength);
Assert.AreEqual(TimeSpan.Zero, metrics.QueueTime);
Assert.AreNotEqual(default(DateTime), metrics.Timestamp);

var warning = _loggerProvider.GetAllLogMessages().Single(p => p.Level == Microsoft.Extensions.Logging.LogLevel.Warning);
Assert.AreEqual("Error querying for queue scale status: Things are very wrong.", warning.FormattedMessage);
var ex = Assert.ThrowsAsync<RequestFailedException>(async () =>
await _metricsProvider.GetMetricsAsync());
Assert.AreEqual("Things are very wrong.", ex.Message);
}

public class TestFixture : IDisposable
Expand Down
Loading