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

Use LoggerMessageGenerator for core logging messages #9274

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
71 changes: 27 additions & 44 deletions src/Orleans.Core/Diagnostics/MessagingTrace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,42 +25,26 @@ internal class MessagingTrace : DiagnosticListener, ILogger
public const string InvokeMessageEventName = Category + ".Invoke";
public const string RejectSendMessageToDeadSiloEventName = Category + ".Reject.TargetDead";

private static readonly Action<ILogger, Message, MessagingInstruments.Phase, Exception> LogDropExpiredMessage
= LoggerMessage.Define<Message, MessagingInstruments.Phase>(
LogLevel.Warning,
new EventId((int)ErrorCode.Messaging_DroppingExpiredMessage, DropExpiredMessageEventName),
"Dropping expired message {Message} at phase {Phase}");

private static readonly Action<ILogger, Message, Exception> LogDropBlockedApplicationMessage
= LoggerMessage.Define<Message>(
LogLevel.Warning,
new EventId((int)ErrorCode.Messaging_DroppingBlockedMessage, DropBlockedApplicationMessageEventName),
"Dropping message {Message} since this silo is blocking application messages");

private static readonly Action<ILogger, Message, Exception> LogEnqueueInboundMessage
= LoggerMessage.Define<Message>(
LogLevel.Trace,
new EventId((int)ErrorCode.Messaging_Inbound_Enqueue, EnqueueInboundMessageEventName),
"Enqueueing inbound message {Message}");

private static readonly Action<ILogger, Message, Exception> LogDequeueInboundMessage
= LoggerMessage.Define<Message>(
LogLevel.Trace,
new EventId((int)ErrorCode.Messaging_Inbound_Dequeue, DequeueInboundMessageEventName),
"Dequeueing inbound message {Message}");

private static readonly Action<ILogger, SiloAddress, Message, string, Exception> LogSiloDropSendingMessage
= LoggerMessage.Define<SiloAddress, Message, string>(
LogLevel.Warning,
new EventId((int)ErrorCode.Messaging_OutgoingMS_DroppingMessage, DropSendingMessageEventName),
"Silo {SiloAddress} is dropping message {Message}. Reason: {Reason}");

private static readonly Action<ILogger, SiloAddress, SiloAddress, Message, Exception> LogRejectSendMessageToDeadSilo
= LoggerMessage.Define<SiloAddress, SiloAddress, Message>(
LogLevel.Information,
new EventId((int)ErrorCode.MessagingSendingRejection, RejectSendMessageToDeadSiloEventName),
"Silo {SiloAddress} is rejecting message to known-dead silo {DeadSilo}: {Message}");
private static partial class Log
{
[LoggerMessage(1, LogLevel.Warning, "Dropping expired message {Message} at phase {Phase}")]
public static partial void DropExpiredMessage(ILogger logger, Message message, MessagingInstruments.Phase phase);

[LoggerMessage(2, LogLevel.Warning, "Dropping message {Message} since this silo is blocking application messages")]
public static partial void DropBlockedApplicationMessage(ILogger logger, Message message);

[LoggerMessage(3, LogLevel.Trace, "Enqueueing inbound message {Message}")]
public static partial void EnqueueInboundMessage(ILogger logger, Message message);

[LoggerMessage(4, LogLevel.Trace, "Dequeueing inbound message {Message}")]
public static partial void DequeueInboundMessage(ILogger logger, Message message);

[LoggerMessage(5, LogLevel.Warning, "Silo {SiloAddress} is dropping message {Message}. Reason: {Reason}")]
public static partial void SiloDropSendingMessage(ILogger logger, SiloAddress localSiloAddress, Message message, string reason);

[LoggerMessage(6, LogLevel.Information, "Silo {SiloAddress} is rejecting message to known-dead silo {DeadSilo}: {Message}")]
public static partial void RejectSendMessageToDeadSilo(ILogger logger, SiloAddress localSilo, SiloAddress deadSilo, Message message);
}

private readonly ILogger log;

Expand Down Expand Up @@ -107,7 +91,7 @@ internal void OnDropExpiredMessage(Message message, MessagingInstruments.Phase p
}

MessagingInstruments.OnMessageExpired(phase);
LogDropExpiredMessage(this, message, phase, null);
Log.DropExpiredMessage(this.log, message, phase);
}

internal void OnDropBlockedApplicationMessage(Message message)
Expand All @@ -117,13 +101,13 @@ internal void OnDropBlockedApplicationMessage(Message message)
this.Write(DropBlockedApplicationMessageEventName, message);
}

LogDropBlockedApplicationMessage(this, message, null);
Log.DropBlockedApplicationMessage(this.log, message);
}

internal void OnSiloDropSendingMessage(SiloAddress localSiloAddress, Message message, string reason)
{
MessagingInstruments.OnDroppedSentMessage(message);
LogSiloDropSendingMessage(this, localSiloAddress, message, reason, null);
Log.SiloDropSendingMessage(this.log, localSiloAddress, message, reason);
}

public void OnEnqueueInboundMessage(Message message)
Expand All @@ -133,7 +117,7 @@ public void OnEnqueueInboundMessage(Message message)
this.Write(EnqueueInboundMessageEventName, message);
}

LogEnqueueInboundMessage(this, message, null);
Log.EnqueueInboundMessage(this.log, message);
}

public void OnDequeueInboundMessage(Message message)
Expand All @@ -143,7 +127,7 @@ public void OnDequeueInboundMessage(Message message)
this.Write(DequeueInboundMessageEventName, message);
}

LogDequeueInboundMessage(this, message, null);
Log.DequeueInboundMessage(this.log, message);
}

internal void OnCreateMessage(Message message)
Expand Down Expand Up @@ -189,12 +173,11 @@ public void OnRejectSendMessageToDeadSilo(SiloAddress localSilo, Message message
this.Write(RejectSendMessageToDeadSiloEventName, message);
}

LogRejectSendMessageToDeadSilo(
this,
Log.RejectSendMessageToDeadSilo(
this.log,
localSilo,
message.TargetSilo,
message,
null);
message);
}

internal void OnSendRequest(Message message)
Expand Down
39 changes: 27 additions & 12 deletions src/Orleans.Core/Manifest/ClientClusterManifestProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ namespace Orleans.Runtime
/// </summary>
internal class ClientClusterManifestProvider : IClusterManifestProvider, IAsyncDisposable, IDisposable
{
private static partial class Log
{
[LoggerMessage(1, LogLevel.Debug, "Refreshed cluster manifest")]
public static partial void RefreshedClusterManifest(ILogger logger);

[LoggerMessage(2, LogLevel.Debug, "Stopped refreshing cluster manifest")]
public static partial void StoppedRefreshingClusterManifest(ILogger logger);

[LoggerMessage(3, LogLevel.Information, "Graceful shutdown of cluster manifest provider was canceled.")]
public static partial void GracefulShutdownCanceled(ILogger logger);

[LoggerMessage(4, LogLevel.Error, "Error stopping cluster manifest provider.")]
public static partial void ErrorStoppingClusterManifestProvider(ILogger logger, Exception exception);

[LoggerMessage(5, LogLevel.Warning, "Error trying to get cluster manifest from gateway {Gateway}")]
public static partial void ErrorGettingClusterManifest(ILogger logger, Exception exception, SiloAddress gateway);

[LoggerMessage(6, LogLevel.Warning, "Failed to fetch cluster manifest update from {Provider}.")]
public static partial void FailedToFetchClusterManifestUpdate(ILogger logger, Exception exception, IClusterManifestSystemTarget provider);
}

private readonly TaskCompletionSource<bool> _initialized = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
private readonly ILogger<ClientClusterManifestProvider> _logger;
private readonly TypeManagementOptions _typeManagementOptions;
Expand Down Expand Up @@ -90,11 +111,11 @@ public async Task StopAsync(CancellationToken cancellationToken)
}
catch (OperationCanceledException)
{
_logger.LogInformation("Graceful shutdown of cluster manifest provider was canceled.");
Log.GracefulShutdownCanceled(_logger);
}
catch (Exception exception)
{
_logger.LogError(exception, "Error stopping cluster manifest provider.");
Log.ErrorStoppingClusterManifestProvider(_logger, exception);
}
}

Expand Down Expand Up @@ -182,16 +203,13 @@ private async Task RunAsync()

_initialized.TrySetResult(true);

if (_logger.IsEnabled(LogLevel.Debug))
{
_logger.LogDebug("Refreshed cluster manifest");
}
Log.RefreshedClusterManifest(_logger);

await Task.WhenAny(cancellationTask, Task.Delay(_typeManagementOptions.TypeMapRefreshInterval));
}
catch (Exception exception)
{
_logger.LogWarning(exception, "Error trying to get cluster manifest from gateway {Gateway}", gateway);
Log.ErrorGettingClusterManifest(_logger, exception, gateway);
await Task.Delay(StandardExtensions.Min(_typeManagementOptions.TypeMapRefreshInterval, TimeSpan.FromSeconds(5)));

// Reset the gateway so that another will be selected on the next iteration.
Expand All @@ -203,10 +221,7 @@ private async Task RunAsync()
{
_initialized.TrySetResult(false);

if (_logger.IsEnabled(LogLevel.Debug))
{
_logger.LogDebug("Stopped refreshing cluster manifest");
}
Log.StoppedRefreshingClusterManifest(_logger);
}
}

Expand All @@ -220,7 +235,7 @@ private async Task RunAsync()
}
catch (Exception exception)
{
_logger.LogWarning(exception, "Failed to fetch cluster manifest update from {Provider}.", provider);
Log.FailedToFetchClusterManifestUpdate(_logger, exception, provider);

// If the provider does not support the new API, fall back to the old one.
var manifest = await provider.GetClusterManifest();
Expand Down
70 changes: 36 additions & 34 deletions src/Orleans.Core/Messaging/ClientMessageCenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,33 @@ internal class ClientMessageCenter : IMessageCenter, IDisposable
private readonly ConnectionManager connectionManager;
private readonly LocalClientDetails _localClientDetails;

private static partial class Log
{
[LoggerMessage(1, LogLevel.Debug, "Proxy grain client constructed")]
public static partial void ProxyGrainClientConstructed(ILogger logger);

[LoggerMessage(2, LogLevel.Debug, "Proxy grain client started")]
public static partial void ProxyGrainClientStarted(ILogger logger);

[LoggerMessage(3, LogLevel.Error, "Ignoring {Message} because the Client message center is not running")]
public static partial void IgnoringMessage(ILogger logger, Message message);

[LoggerMessage(4, LogLevel.Trace, "Sending message {Message} via gateway {Gateway}")]
public static partial void SendingMessageViaGateway(ILogger logger, Message message, EndPoint gateway);

[LoggerMessage(5, LogLevel.Warning, "Unable to send message {Message}; Gateway manager state is {GatewayManager}")]
public static partial void UnableToSendMessage(ILogger logger, Message message, GatewayManager gatewayManager);

[LoggerMessage(6, LogLevel.Debug, "Dropping message: {Message}. Reason = {Reason}")]
public static partial void DroppingMessage(ILogger logger, Message message, string reason);

[LoggerMessage(7, LogLevel.Debug, "Rejecting message: {Message}. Reason = {Reason}")]
public static partial void RejectingMessage(ILogger logger, Message message, string reason);

[LoggerMessage(8, LogLevel.Warning, "Unable to send message {Message}; Gateway manager state is {GatewayManager}")]
public static partial void UnableToSendMessageNoGateway(ILogger logger, Message message, GatewayManager gatewayManager);
}

public ClientMessageCenter(
IOptions<ClientMessagingOptions> clientMessagingOptions,
LocalClientDetails localClientDetails,
Expand All @@ -85,7 +112,7 @@ public ClientMessageCenter(
numMessages = 0;
this.grainBuckets = new WeakReference<ClientOutboundConnection>[clientMessagingOptions.Value.ClientSenderBuckets];
logger = loggerFactory.CreateLogger<ClientMessageCenter>();
if (logger.IsEnabled(LogLevel.Debug)) logger.LogDebug("Proxy grain client constructed");
Log.ProxyGrainClientConstructed(logger);
ClientInstruments.RegisterConnectedGatewayCountObserve(() => connectionManager.ConnectionCount);
}

Expand All @@ -94,7 +121,7 @@ public async Task StartAsync(CancellationToken cancellationToken)
await EstablishInitialConnection(cancellationToken);

Running = true;
if (logger.IsEnabled(LogLevel.Debug)) logger.LogDebug("Proxy grain client started");
Log.ProxyGrainClientStarted(logger);
}

private async Task EstablishInitialConnection(CancellationToken cancellationToken)
Expand Down Expand Up @@ -170,10 +197,7 @@ public void SendMessage(Message msg)
{
if (!Running)
{
this.logger.LogError(
(int)ErrorCode.ProxyClient_MsgCtrNotRunning,
"Ignoring {Message} because the Client message center is not running",
msg);
Log.IgnoringMessage(logger, msg);
return;
}

Expand All @@ -185,14 +209,7 @@ public void SendMessage(Message msg)

connection.Send(msg);

if (this.logger.IsEnabled(LogLevel.Trace))
{
this.logger.LogTrace(
(int)ErrorCode.ProxyClient_QueueRequest,
"Sending message {Message} via gateway {Gateway}",
msg,
connection.RemoteEndPoint);
}
Log.SendingMessageViaGateway(logger, msg, connection.RemoteEndPoint);
}
else
{
Expand All @@ -209,14 +226,7 @@ async Task SendAsync(ValueTask<Connection> task, Message message)

connection.Send(message);

if (this.logger.IsEnabled(LogLevel.Trace))
{
this.logger.LogTrace(
(int)ErrorCode.ProxyClient_QueueRequest,
"Sending message {Message} via gateway {Gateway}",
message,
connection.RemoteEndPoint);
}
Log.SendingMessageViaGateway(logger, message, connection.RemoteEndPoint);
}
catch (Exception exception)
{
Expand Down Expand Up @@ -267,11 +277,7 @@ private ValueTask<Connection> GetGatewayConnection(Message msg)
if (numGateways == 0)
{
RejectMessage(msg, "No gateways available");
logger.LogWarning(
(int)ErrorCode.ProxyClient_CannotSend,
"Unable to send message {Message}; Gateway manager state is {GatewayManager}",
msg,
gatewayManager);
Log.UnableToSendMessage(logger, msg, gatewayManager);
return new ValueTask<Connection>(default(Connection));
}

Expand Down Expand Up @@ -305,11 +311,7 @@ private ValueTask<Connection> GetGatewayConnection(Message msg)
if (addr == null)
{
RejectMessage(msg, "No gateways available");
logger.LogWarning(
(int)ErrorCode.ProxyClient_CannotSend_NoGateway,
"Unable to send message {Message}; Gateway manager state is {GatewayManager}",
msg,
gatewayManager);
Log.UnableToSendMessageNoGateway(logger, msg, gatewayManager);
return new ValueTask<Connection>(default(Connection));
}

Expand Down Expand Up @@ -392,11 +394,11 @@ public void RejectMessage(Message msg, string reason, Exception exc = null)

if (msg.Direction != Message.Directions.Request)
{
if (logger.IsEnabled(LogLevel.Debug)) logger.LogDebug((int)ErrorCode.ProxyClient_DroppingMsg, "Dropping message: {Message}. Reason = {Reason}", msg, reason);
Log.DroppingMessage(logger, msg, reason);
}
else
{
if (logger.IsEnabled(LogLevel.Debug)) logger.LogDebug((int)ErrorCode.ProxyClient_RejectingMsg, "Rejecting message: {Message}. Reason = {Reason}", msg, reason);
Log.RejectingMessage(logger, msg, reason);
MessagingInstruments.OnRejectedMessage(msg);
var error = this.messageFactory.CreateRejectionResponse(msg, Message.RejectionTypes.Unrecoverable, reason, exc);
DispatchLocalMessage(error);
Expand Down
Loading
Loading