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

Exception logging for EventProcessorHost background exceptions (#1705) #1712

Merged
merged 1 commit into from
May 23, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using System.Reflection;
using System.Text;
using Microsoft.Azure.WebJobs.Host.Config;
using Microsoft.Azure.WebJobs.Logging;
using Microsoft.Extensions.Logging;
using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Messaging;

Expand Down Expand Up @@ -453,7 +455,25 @@ void IExtensionConfigProvider.Initialize(ExtensionConfigContext context)

// register our binding provider
context.AddBindingRule<EventHubAttribute>()
.BindToCollector(BuildFromAttribute);
.BindToCollector(BuildFromAttribute);

// Register an exception handler for background exceptions
// coming from the EventProcessorHost.
//
// EventProcessorOptions is a host level instance that is shared
// across all bindings, so we have to subscribe to it at the
// host level.
_options.ExceptionReceived += (s, e) =>
{
if (!e.Exception.IsWrappedExceptionTransient())
{
string message = $"EventProcessorHost error (Action={e.Action})";
var logger = context.Config.LoggerFactory?.CreateLogger(LogCategories.Executor);
logger?.LogError(0, e.Exception, message);

context.Trace.Error(message, e.Exception);
}
};
}

private IAsyncCollector<EventData> BuildFromAttribute(EventHubAttribute attribute)
Expand Down
3 changes: 2 additions & 1 deletion src/Microsoft.Azure.WebJobs.ServiceBus/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1014:MarkAssembliesWithClsCompliant")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "EventHubClient", Scope = "member", Target = "Microsoft.Azure.WebJobs.ServiceBus.EventHubConfiguration.#GetEndpointFromEventHubClient(Microsoft.ServiceBus.Messaging.EventHubClient)")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "MessagingFactory", Scope = "member", Target = "Microsoft.Azure.WebJobs.ServiceBus.EventHubConfiguration.#GetEndpointFromEventHubClient(Microsoft.ServiceBus.Messaging.EventHubClient)")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "MessageReceiver", Scope = "member", Target = "Microsoft.Azure.WebJobs.ServiceBus.Config.ServiceBusExtensionConfig.#Initialize(Microsoft.Azure.WebJobs.Host.Config.ExtensionConfigContext)")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "MessageReceiver", Scope = "member", Target = "Microsoft.Azure.WebJobs.ServiceBus.Config.ServiceBusExtensionConfig.#Initialize(Microsoft.Azure.WebJobs.Host.Config.ExtensionConfigContext)")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "EventProcessorHost", Scope = "member", Target = "Microsoft.Azure.WebJobs.ServiceBus.EventHubConfiguration.#Microsoft.Azure.WebJobs.Host.Config.IExtensionConfigProvider.Initialize(Microsoft.Azure.WebJobs.Host.Config.ExtensionConfigContext)")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Azure.WebJobs.Host.Bindings;
using Microsoft.Azure.WebJobs.Host.Config;
using Microsoft.Azure.WebJobs.Host.TestCommon;
using Microsoft.Azure.WebJobs.Host.Triggers;
using Microsoft.Extensions.Logging;
using Microsoft.ServiceBus.Messaging;
using Xunit;

namespace Microsoft.Azure.WebJobs.ServiceBus.UnitTests.Config
{
public class EventHubConfigurationTests
{
[Fact]
public void Initialize_PerformsExpectedRegistrations()
{
JobHostConfiguration config = new JobHostConfiguration();
config.AddService<INameResolver>(new RandomNameResolver());

TestLoggerProvider loggerProvider = new TestLoggerProvider();
ILoggerFactory loggerFactory = new LoggerFactory();
loggerFactory.AddProvider(loggerProvider);
config.LoggerFactory = loggerFactory;

EventHubConfiguration eventHubConfiguration = new EventHubConfiguration();

IExtensionRegistry extensions = config.GetService<IExtensionRegistry>();
ITriggerBindingProvider[] triggerBindingProviders = extensions.GetExtensions<ITriggerBindingProvider>().ToArray();
Assert.Equal(0, triggerBindingProviders.Length);
IBindingProvider[] bindingProviders = extensions.GetExtensions<IBindingProvider>().ToArray();
Assert.Equal(0, bindingProviders.Length);

var traceWriter = new TestTraceWriter(TraceLevel.Verbose);
ExtensionConfigContext context = new ExtensionConfigContext
{
Config = config,
Trace = traceWriter
};
((IExtensionConfigProvider)eventHubConfiguration).Initialize(context);

// ensure the EventHubTriggerAttributeBindingProvider was registered
triggerBindingProviders = extensions.GetExtensions<ITriggerBindingProvider>().ToArray();
Assert.Equal(1, triggerBindingProviders.Length);
EventHubTriggerAttributeBindingProvider triggerBindingProvider = (EventHubTriggerAttributeBindingProvider)triggerBindingProviders[0];
Assert.NotNull(triggerBindingProvider);

// ensure the EventProcessorOptions ExceptionReceived event is wired up
var eventProcessorOptions = eventHubConfiguration.GetOptions();
var ex = new Exception("Kaboom!");
var args = new ExceptionReceivedEventArgs(ex, "Testing");
var eventDelegate = (MulticastDelegate)eventProcessorOptions.GetType().GetField("ExceptionReceived", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(eventProcessorOptions);
Assert.Equal(1, eventDelegate.GetInvocationList().Length);
var handler = eventDelegate.GetInvocationList().Single();
handler.Method.Invoke(handler.Target, new object[] { null, args });

string expectedMessage = "EventProcessorHost error (Action=Testing)";
var trace = traceWriter.Traces.Last();
Assert.Equal(expectedMessage, trace.Message);
Assert.Same(ex, trace.Exception);

var logMessage = loggerProvider.GetAllLogMessages().Single();
Assert.Equal(LogLevel.Error, logMessage.Level);
Assert.Equal(expectedMessage, logMessage.FormattedMessage);
Assert.Same(ex, logMessage.Exception);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@
<Compile Include="Bindings\ServiceBusTriggerBindingTests.cs" />
<Compile Include="BrokeredMessageToByteArrayConverterTests.cs" />
<Compile Include="BrokeredMessageToStringConverterTests.cs" />
<Compile Include="Config\EventHubConfigurationTests.cs" />
<Compile Include="Config\ServiceBusConfigurationTests.cs" />
<Compile Include="Config\ServiceBusJobHostConfigurationExtensionsTests.cs" />
<Compile Include="Config\ServiceBusExtensionConfigTests.cs" />
Expand Down