Skip to content

Implement RabbitMQ logging - #662

Merged
Eric Erhardt (eerhardt) merged 2 commits into
mainfrom
RabbitMQLogging
Nov 2, 2023
Merged

Eric Erhardt (eerhardt) merged 2 commits into
mainfrom
RabbitMQLogging

Conversation

@eerhardt

Copy link
Copy Markdown
Member

Add an EventSource listener to listen to the RabbitMQ event source which logs info, warn, and error messages. Forward these messages to an ILogger.

Fix #564

Add an EventSource listener to listen to the RabbitMQ event source which logs info, warn, and error messages. Forward these messages to an ILogger.

Fix #564

namespace Aspire.RabbitMQ.Client;

internal sealed class RabbitMQEventSourceLogForwarder : IDisposable

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This commit message made my day 🤣

/// </summary>
private sealed class RabbitMQEventSourceListener : EventListener
{
private readonly List<EventSource> _eventSources = new List<EventSource>();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thread safety?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't think thread safety is a concern here since this List is only touched in between the base ctor running and this class's ctor running. Between that time, I don't think another thread can touch this object.

Note that this implementation was copied from the Azure SDK, and it hasn't changed in years. So I semi-trust it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure there's a bug here with _eventSources, but I'm also not sure what this accomplishes either. If I recall correctly, you're right that OnEventSourceCreated will be called from the base constructor, and so you do have to handle the case where the EventListener might not be fully initialized when events start to show up in OnEventWritten. That said, you already handle this on line 225 where you ensure that _log is non-null before calling Invoke. I think you can get rid of all of this _eventSources machinery.

I realize I'm late to the party here, but would be worth removing for simplicity, or if it doesn't work without this, identifying an issue that needs to be documented/fixed.

Comment on lines +194 to +197
if (_log == null)
{
_eventSources.Add(eventSource);
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why store all event sources?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Note that this implementation was copied from the Azure SDK.

Only the event sources that are added before the ctor fully finishes (i.e. _log == null) get added to the list. I think this is to handle the case where the base EventListener starts calling virtual methods on this object before the ctor has fully completed. This ensures those event sources are listened to.

configurationOptionsSection.Bind(factory);

// the connection string from settings should win over the one from the ConnectionFactory section
var connectionString = settings.ConnectionString;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

was this a bug before?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

no, this was just me being nit-picky. This variable isn't used until here, so I moved it since it seemed out of place.

@eerhardt
Eric Erhardt (eerhardt) merged commit 0282715 into main Nov 2, 2023
@eerhardt
Eric Erhardt (eerhardt) deleted the RabbitMQLogging branch November 2, 2023 19:16
Eric Erhardt (eerhardt) added a commit that referenced this pull request Nov 2, 2023
* Implement RabbitMQ logging

Add an EventSource listener to listen to the RabbitMQ event source which logs info, warn, and error messages. Forward these messages to an ILogger.

Fix #564

* Stop coding like a dinosaur.
David Fowler (davidfowl) pushed a commit that referenced this pull request Nov 2, 2023
* Implement RabbitMQ logging

Add an EventSource listener to listen to the RabbitMQ event source which logs info, warn, and error messages. Forward these messages to an ILogger.

Fix #564

* Stop coding like a dinosaur.
Jose Perez Rodriguez (joperezr) added a commit that referenced this pull request Nov 14, 2023
@danmoseley Dan Moseley (danmoseley) added the area-integrations Issues pertaining to Aspire Integrations packages label Nov 16, 2023
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 30, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-integrations Issues pertaining to Aspire Integrations packages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enable Logging for RabbitMQ Component

5 participants