-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2594 from Particular/legacy-queue-length-exception
Prevent exception with legacy queue length reporting
- Loading branch information
Showing
5 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/ServiceControl.Monitoring.AcceptanceTests/TestSupport/ContextLoggerExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
namespace ServiceControl.Monitoring.AcceptanceTests.TestSupport | ||
{ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using Microsoft.Extensions.Logging; | ||
using NServiceBus.AcceptanceTesting; | ||
|
||
static class ContextLoggerExtensions | ||
{ | ||
public static ILoggingBuilder AddScenarioContextLogging(this ILoggingBuilder builder) | ||
{ | ||
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, ContextLoggerProvider>()); | ||
return builder; | ||
} | ||
|
||
class ContextLoggerProvider : ILoggerProvider | ||
{ | ||
ConcurrentDictionary<string, ILogger> loggers = new ConcurrentDictionary<string, ILogger>(); | ||
ScenarioContext context; | ||
|
||
public ContextLoggerProvider(ScenarioContext context) | ||
{ | ||
this.context = context; | ||
} | ||
|
||
public void Dispose() => loggers.Clear(); | ||
|
||
public ILogger CreateLogger(string categoryName) => | ||
loggers.GetOrAdd(categoryName, name => new ContextLogger(name, context)); | ||
} | ||
|
||
class ContextLogger : ILogger | ||
{ | ||
string categoryName; | ||
ScenarioContext context; | ||
|
||
public ContextLogger(string categoryName, ScenarioContext context) | ||
{ | ||
this.categoryName = categoryName; | ||
this.context = context; | ||
} | ||
|
||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, | ||
Exception exception, Func<TState, Exception, string> formatter) => | ||
context.Logs.Enqueue(new ScenarioContext.LogItem | ||
{ | ||
LoggerName = categoryName, | ||
Message = formatter(state, exception), | ||
Level = ConvertLogLevel(logLevel) | ||
}); | ||
|
||
NServiceBus.Logging.LogLevel ConvertLogLevel(LogLevel level) | ||
=> level switch | ||
{ | ||
LogLevel.Critical => NServiceBus.Logging.LogLevel.Fatal, | ||
LogLevel.Trace => NServiceBus.Logging.LogLevel.Debug, | ||
LogLevel.Debug => NServiceBus.Logging.LogLevel.Debug, | ||
LogLevel.Information => NServiceBus.Logging.LogLevel.Info, | ||
LogLevel.Warning => NServiceBus.Logging.LogLevel.Warn, | ||
LogLevel.Error => NServiceBus.Logging.LogLevel.Error, | ||
LogLevel.None => NServiceBus.Logging.LogLevel.Debug, | ||
_ => throw new ArgumentOutOfRangeException(nameof(level), level, null) | ||
}; | ||
|
||
public bool IsEnabled(LogLevel logLevel) => | ||
ConvertLogLevel(logLevel) <= context.LogLevel; | ||
|
||
public IDisposable BeginScope<TState>(TState state) => NullScope.Instance; | ||
|
||
class NullScope : IDisposable | ||
{ | ||
public void Dispose() { } | ||
|
||
public static NullScope Instance { get; } = new NullScope(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/ServiceControl.Monitoring.AcceptanceTests/Tests/When_sending_legacy_metric_report.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
namespace ServiceControl.Monitoring.AcceptanceTests.Tests | ||
{ | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Infrastructure; | ||
using NServiceBus; | ||
using NServiceBus.AcceptanceTesting; | ||
using NServiceBus.Metrics; | ||
using NUnit.Framework; | ||
using TestSupport.EndpointTemplates; | ||
|
||
class When_sending_legacy_metric_report : AcceptanceTest | ||
{ | ||
[Test] | ||
public async Task Should_report_legacy_queue_length_reporting() | ||
{ | ||
await Define<SomeContext>() | ||
.WithEndpoint<EndpointSendingLegacyMetricReport>(b => | ||
b.When(session => | ||
{ | ||
var sendOptions = new SendOptions(); | ||
sendOptions.SetDestination(Settings.DEFAULT_ENDPOINT_NAME); | ||
sendOptions.SetHeader(MetricHeaders.MetricInstanceId, "MetricInstanceId"); | ||
return session.Send(new MetricReport(), sendOptions); | ||
})) | ||
.Done(ctx => ctx.Logs.Any(x => x.Message == "Legacy queue length report received from MetricInstanceId instance of SendingLegacyMetricReport.EndpointSendingLegacyMetricReport")) | ||
.Run(); | ||
} | ||
|
||
class EndpointSendingLegacyMetricReport : EndpointConfigurationBuilder | ||
{ | ||
public EndpointSendingLegacyMetricReport() | ||
{ | ||
EndpointSetup<DefaultServer>(); | ||
} | ||
} | ||
|
||
class SomeContext : ScenarioContext | ||
{ | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters