From 33eafccadb327330fad118c115ada06fe56ee834 Mon Sep 17 00:00:00 2001 From: Alexey Rodionov Date: Mon, 17 Nov 2025 17:23:03 -0800 Subject: [PATCH] [ServiceBus] Replacing scaling logs to WebJobs extension methods --- NuGet.Config | 1 + eng/Packages.Data.props | 4 +-- .../Listeners/ServiceBusMetricsProvider.cs | 33 +++++++++++-------- .../src/Listeners/ServiceBusScaleMonitor.cs | 2 +- .../src/Listeners/ServiceBusTargetScaler.cs | 13 ++++---- 5 files changed, 30 insertions(+), 23 deletions(-) diff --git a/NuGet.Config b/NuGet.Config index c679a5601f0e..556c1989896a 100644 --- a/NuGet.Config +++ b/NuGet.Config @@ -4,6 +4,7 @@ + diff --git a/eng/Packages.Data.props b/eng/Packages.Data.props index cd799d626eec..d22caf329547 100644 --- a/eng/Packages.Data.props +++ b/eng/Packages.Data.props @@ -249,8 +249,8 @@ - - + + diff --git a/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Listeners/ServiceBusMetricsProvider.cs b/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Listeners/ServiceBusMetricsProvider.cs index fb1697e196dc..f8581db1955f 100644 --- a/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Listeners/ServiceBusMetricsProvider.cs +++ b/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Listeners/ServiceBusMetricsProvider.cs @@ -1,14 +1,14 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. -using Microsoft.Azure.WebJobs.ServiceBus.Listeners; -using Microsoft.Azure.WebJobs.ServiceBus; using System; using System.Threading.Tasks; -using Microsoft.Extensions.Logging; using Azure.Messaging.ServiceBus; using Azure.Messaging.ServiceBus.Administration; -using Microsoft.Azure.WebJobs.Extensions.ServiceBus.Config; +using Microsoft.Azure.WebJobs.Host.Scale; +using Microsoft.Azure.WebJobs.ServiceBus; +using Microsoft.Azure.WebJobs.ServiceBus.Listeners; +using Microsoft.Extensions.Logging; namespace Microsoft.Azure.WebJobs.Extensions.ServiceBus.Listeners { @@ -17,6 +17,7 @@ internal class ServiceBusMetricsProvider internal const string DeadLetterQueuePath = @"/$DeadLetterQueue"; private readonly ILogger _logger; + private readonly string _functionId; private readonly string _entityPath; private readonly ServiceBusEntityType _serviceBusEntityType; private readonly Lazy _receiver; @@ -30,12 +31,14 @@ internal class ServiceBusMetricsProvider private DateTime _nextWarningTime; public ServiceBusMetricsProvider( + string functionId, string entityPath, ServiceBusEntityType serviceBusEntityType, Lazy receiver, Lazy administrationClient, ILoggerFactory loggerFactory) { + _functionId = functionId; _serviceBusEntityType = serviceBusEntityType; _receiver = receiver; _entityPath = entityPath; @@ -90,19 +93,20 @@ public async Task GetMessageCountAsync() catch (ServiceBusException ex) when (ex.Reason == ServiceBusFailureReason.MessagingEntityNotFound) { - _logger.LogWarning($"ServiceBus {entityName} '{_entityPath}' was not found."); + _logger.LogFunctionScaleWarning($"ServiceBus {entityName} '{_entityPath}' was not found.", _functionId, ex); } catch (UnauthorizedAccessException ex) { if (TimeToLogWarning()) { - _logger.LogWarning(ex, $"Connection string does not have 'Manage Claim' for {entityName} '{_entityPath}'. Unable to determine active message count."); + _logger.LogFunctionScaleWarning($"Connection string does not have 'Manage Claim' for {entityName} '{_entityPath}'. Unable to determine active message count.", + _functionId, ex); } throw; } - catch (Exception e) + catch (Exception ex) { - _logger.LogWarning(e, $"Error querying for Service Bus {entityName} scale"); + _logger.LogFunctionScaleWarning($"Error querying for Service Bus {entityName} scale", _functionId, ex); } long totalNewMessageCount = 0; @@ -163,19 +167,20 @@ public async Task GetMetricsAsync() catch (ServiceBusException ex) when (ex.Reason == ServiceBusFailureReason.MessagingEntityNotFound) { - _logger.LogWarning($"ServiceBus {entityName} '{_entityPath}' was not found."); + _logger.LogFunctionScaleWarning($"ServiceBus {entityName} '{_entityPath}' was not found.", _functionId, ex); } - catch (UnauthorizedAccessException) // When manage claim is not used on Service Bus connection string + catch (UnauthorizedAccessException ex) // When manage claim is not used on Service Bus connection string { if (TimeToLogWarning()) { - _logger.LogWarning($"Connection string does not have Manage claim for {entityName} '{_entityPath}'. Failed to get {entityName} description to " + - $"derive {entityName} length metrics. Falling back to using first message enqueued time."); + _logger.LogFunctionScaleWarning($"Connection string does not have Manage claim for {entityName} '{_entityPath}'. Failed to get {entityName} description to " + + $"derive {entityName} length metrics. Falling back to using first message enqueued time.", + _functionId, ex); } } - catch (Exception e) + catch (Exception ex) { - _logger.LogWarning($"Error querying for Service Bus {entityName} scale status: {e.Message}"); + _logger.LogFunctionScaleWarning($"Error querying for Service Bus {entityName} scale status", _functionId, ex); } // Path for connection strings with no manage claim diff --git a/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Listeners/ServiceBusScaleMonitor.cs b/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Listeners/ServiceBusScaleMonitor.cs index 3f0f754e2924..b0c4d7276cf5 100644 --- a/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Listeners/ServiceBusScaleMonitor.cs +++ b/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Listeners/ServiceBusScaleMonitor.cs @@ -37,7 +37,7 @@ ILoggerFactory loggerFactory { _functionId = functionId; _entityPath = entityPath; - _serviceBusMetricsProvider = new ServiceBusMetricsProvider(entityPath, entityType, receiver, administrationClient, loggerFactory); + _serviceBusMetricsProvider = new ServiceBusMetricsProvider(_functionId, entityPath, entityType, receiver, administrationClient, loggerFactory); _scaleMonitorDescriptor = new ScaleMonitorDescriptor($"{_functionId}-ServiceBusTrigger-{_entityPath}".ToLower(CultureInfo.InvariantCulture), functionId); _logger = loggerFactory.CreateLogger(); } diff --git a/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Listeners/ServiceBusTargetScaler.cs b/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Listeners/ServiceBusTargetScaler.cs index ea00cb6e9231..aa5157abf211 100644 --- a/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Listeners/ServiceBusTargetScaler.cs +++ b/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Listeners/ServiceBusTargetScaler.cs @@ -16,7 +16,7 @@ namespace Microsoft.Azure.WebJobs.Extensions.ServiceBus.Listeners { internal class ServiceBusTargetScaler : ITargetScaler { - private readonly string _functionId; + private readonly string _functionName; private readonly ServiceBusMetricsProvider _serviceBusMetricsProvider; private readonly ServiceBusOptions _options; private readonly bool _isSessionsEnabled; @@ -26,7 +26,7 @@ internal class ServiceBusTargetScaler : ITargetScaler private readonly ILogger _logger; public ServiceBusTargetScaler( - string functionId, + string functionName, string entityPath, ServiceBusEntityType entityType, Lazy receiver, @@ -37,10 +37,10 @@ public ServiceBusTargetScaler( ILoggerFactory loggerFactory ) { - _functionId = functionId; - _serviceBusMetricsProvider = new ServiceBusMetricsProvider(entityPath, entityType, receiver, administrationClient, loggerFactory); + _functionName = functionName; + _serviceBusMetricsProvider = new ServiceBusMetricsProvider(_functionName, entityPath, entityType, receiver, administrationClient, loggerFactory); _entityPath = entityPath; - _targetScalerDescriptor = new TargetScalerDescriptor(functionId); + _targetScalerDescriptor = new TargetScalerDescriptor(functionName); _logger = loggerFactory.CreateLogger(); _options = options; _singleDispatch = singleDispatch; @@ -108,7 +108,8 @@ internal TargetScalerResult GetScaleResultInternal(TargetScalerContext context, targetWorkerCount = int.MaxValue; } - _logger.LogInformation($"Target worker count for function '{_functionId}' is '{targetWorkerCount}' (EntityPath='{_entityPath}', MessageCount ='{messageCount}', Concurrency='{concurrency}')."); + _logger.LogFunctionScaleVote(_functionName, targetWorkerCount, (int)messageCount, concurrency, + $"Target worker count for function '{_functionName}' is '{targetWorkerCount}' (EntityPath='{_entityPath}', MessageCount ='{messageCount}', Concurrency='{concurrency}')."); return new TargetScalerResult {