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

Implement RabbitMQ logging #662

Merged
merged 2 commits into from
Nov 2, 2023
Merged

Implement RabbitMQ logging #662

merged 2 commits into from
Nov 2, 2023

Conversation

eerhardt
Copy link
Member

@eerhardt eerhardt commented Nov 2, 2023

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
@eerhardt eerhardt requested a review from davidfowl November 2, 2023 16:33

namespace Aspire.RabbitMQ.Client;

internal sealed class RabbitMQEventSourceLogForwarder : IDisposable
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
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
Member

Choose a reason for hiding this comment

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

Thread safety?

Copy link
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
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
Member

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
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.


var factory = new ConnectionFactory();

var configurationOptionsSection = configSection.GetSection("ConnectionFactory");
configurationOptionsSection.Bind(factory);

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

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
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 eerhardt merged commit 0282715 into main Nov 2, 2023
5 checks passed
@eerhardt eerhardt deleted the RabbitMQLogging branch November 2, 2023 19:16
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.
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.
joperezr added a commit that referenced this pull request Nov 14, 2023
@danmoseley 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