Skip to content

Commit

Permalink
Improve logging messages (#1436)
Browse files Browse the repository at this point in the history
  • Loading branch information
martintmk authored Jul 26, 2023
1 parent 1b2c070 commit 7dc06c0
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 35 deletions.
26 changes: 13 additions & 13 deletions src/Polly.Extensions/Telemetry/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ internal static partial class Log
EventId = 0,
Message = "Resilience event occurred. " +
"EventName: '{EventName}', " +
"Source: '{BuilderName}[{BuilderInstance}]/{StrategyName}', " +
"Source: '{BuilderName}/{BuilderInstance}/{StrategyName}', " +
"Operation Key: '{OperationKey}', " +
"Result: '{Result}'",
EventName = "ResilienceEvent")]
public static partial void ResilienceEvent(
this ILogger logger,
LogLevel logLevel,
string eventName,
string? builderName,
string? builderInstance,
string builderName,
string builderInstance,
string? strategyName,
string? operationKey,
object? result,
Expand All @@ -31,21 +31,21 @@ public static partial void ResilienceEvent(
1,
LogLevel.Debug,
"Resilience strategy executing. " +
"Source: '{BuilderName}[{BuilderInstance}]', " +
"Source: '{BuilderName}/{BuilderInstance}', " +
"Operation Key: '{OperationKey}', " +
"Result Type: '{ResultType}'",
EventName = "StrategyExecuting")]
public static partial void ExecutingStrategy(
this ILogger logger,
string? builderName,
string? builderInstance,
string builderName,
string builderInstance,
string? operationKey,
string resultType);

[LoggerMessage(
EventId = 2,
Message = "Resilience strategy executed. " +
"Source: '{BuilderName}[{BuilderInstance}]', " +
"Source: '{BuilderName}/{BuilderInstance}', " +
"Operation Key: '{OperationKey}', " +
"Result Type: '{ResultType}', " +
"Result: '{Result}', " +
Expand All @@ -55,8 +55,8 @@ public static partial void ExecutingStrategy(
public static partial void StrategyExecuted(
this ILogger logger,
LogLevel logLevel,
string? builderName,
string? builderInstance,
string builderName,
string builderInstance,
string? operationKey,
string resultType,
object? result,
Expand All @@ -67,7 +67,7 @@ public static partial void StrategyExecuted(
[LoggerMessage(
EventId = 3,
Message = "Execution attempt. " +
"Source: '{BuilderName}[{BuilderInstance}]/{StrategyName}', " +
"Source: '{BuilderName}/{BuilderInstance}/{StrategyName}', " +
"Operation Key: '{OperationKey}', " +
"Result: '{Result}', " +
"Handled: '{Handled}', " +
Expand All @@ -78,9 +78,9 @@ public static partial void StrategyExecuted(
public static partial void ExecutionAttempt(
this ILogger logger,
LogLevel level,
string? builderName,
string? builderInstance,
string? strategyName,
string builderName,
string builderInstance,
string strategyName,
string? operationKey,
object? result,
bool handled,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ private void LogEvent(TelemetryEventArguments args)
Log.ExecutionAttempt(
_logger,
level,
args.Source.BuilderName,
args.Source.BuilderInstanceName,
args.Source.StrategyName,
args.Source.BuilderName.GetValueOrPlaceholder(),
args.Source.BuilderInstanceName.GetValueOrPlaceholder(),
args.Source.StrategyName.GetValueOrPlaceholder(),
args.Context.OperationKey,
result,
executionAttempt.Handled,
Expand All @@ -132,9 +132,9 @@ private void LogEvent(TelemetryEventArguments args)
_logger,
level,
args.Event.EventName,
args.Source.BuilderName,
args.Source.BuilderInstanceName,
args.Source.StrategyName,
args.Source.BuilderName.GetValueOrPlaceholder(),
args.Source.BuilderInstanceName.GetValueOrPlaceholder(),
args.Source.StrategyName.GetValueOrPlaceholder(),
args.Context.OperationKey,
result,
args.Outcome?.Exception);
Expand Down
8 changes: 4 additions & 4 deletions src/Polly.Extensions/Telemetry/TelemetryResilienceStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace Polly.Extensions.Telemetry;
internal sealed class TelemetryResilienceStrategy : ResilienceStrategy
{
private readonly TimeProvider _timeProvider;
private readonly string? _builderName;
private readonly string? _builderInstance;
private readonly string _builderName;
private readonly string _builderInstance;
private readonly List<Action<EnrichmentContext>> _enrichers;
private readonly ILogger _logger;
private readonly Func<ResilienceContext, object?, object?> _resultFormatter;
Expand All @@ -32,8 +32,8 @@ public TelemetryResilienceStrategy(
List<Action<EnrichmentContext>> enrichers)
{
_timeProvider = timeProvider;
_builderName = builderName;
_builderInstance = builderInstance;
_builderName = builderName.GetValueOrPlaceholder();
_builderInstance = builderInstance.GetValueOrPlaceholder();
_resultFormatter = resultFormatter;
_enrichers = enrichers;
_logger = loggerFactory.CreateLogger(TelemetryUtil.PollyDiagnosticSource);
Expand Down
2 changes: 2 additions & 0 deletions src/Polly.Extensions/Telemetry/TelemetryUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ internal static class TelemetryUtil

internal const string PollyDiagnosticSource = "Polly";

public static string GetValueOrPlaceholder(this string? value) => value ?? "(null)";

public static object AsBoxedBool(this bool value) => value switch
{
true => True,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ public void WriteEvent_LoggingWithOutcome_Ok(bool noOutcome)

if (noOutcome)
{
messages[0].Message.Should().Be("Resilience event occurred. EventName: 'my-event', Source: 'my-builder[builder-instance]/my-strategy', Operation Key: 'op-key', Result: ''");
messages[0].Message.Should().Be("Resilience event occurred. EventName: 'my-event', Source: 'my-builder/builder-instance/my-strategy', Operation Key: 'op-key', Result: ''");
}
else
{
messages[0].Message.Should().Be("Resilience event occurred. EventName: 'my-event', Source: 'my-builder[builder-instance]/my-strategy', Operation Key: 'op-key', Result: '200'");
messages[0].Message.Should().Be("Resilience event occurred. EventName: 'my-event', Source: 'my-builder/builder-instance/my-strategy', Operation Key: 'op-key', Result: '200'");
}
}

Expand All @@ -99,11 +99,11 @@ public void WriteEvent_LoggingWithException_Ok(bool noOutcome)

if (noOutcome)
{
messages[0].Message.Should().Be("Resilience event occurred. EventName: 'my-event', Source: 'my-builder[builder-instance]/my-strategy', Operation Key: 'op-key', Result: ''");
messages[0].Message.Should().Be("Resilience event occurred. EventName: 'my-event', Source: 'my-builder/builder-instance/my-strategy', Operation Key: 'op-key', Result: ''");
}
else
{
messages[0].Message.Should().Be("Resilience event occurred. EventName: 'my-event', Source: 'my-builder[builder-instance]/my-strategy', Operation Key: 'op-key', Result: 'Dummy message.'");
messages[0].Message.Should().Be("Resilience event occurred. EventName: 'my-event', Source: 'my-builder/builder-instance/my-strategy', Operation Key: 'op-key', Result: 'Dummy message.'");
}
}

Expand All @@ -115,7 +115,7 @@ public void WriteEvent_LoggingWithoutInstanceName_Ok()

var messages = _logger.GetRecords(new EventId(0, "ResilienceEvent")).ToList();

messages[0].Message.Should().Be("Resilience event occurred. EventName: 'my-event', Source: 'my-builder[]/my-strategy', Operation Key: 'op-key', Result: ''");
messages[0].Message.Should().Be("Resilience event occurred. EventName: 'my-event', Source: 'my-builder/(null)/my-strategy', Operation Key: 'op-key', Result: ''");
}

[InlineData(ResilienceEventSeverity.Error, LogLevel.Error)]
Expand Down Expand Up @@ -150,7 +150,7 @@ public void WriteExecutionAttempt_LoggingWithException_Ok()
var messages = _logger.GetRecords(new EventId(3, "ExecutionAttempt")).ToList();
messages.Should().HaveCount(1);

messages[0].Message.Should().Be("Execution attempt. Source: 'my-builder[builder-instance]/my-strategy', Operation Key: 'op-key', Result: 'Dummy message.', Handled: 'True', Attempt: '4', Execution Time: '123'");
messages[0].Message.Should().Be("Execution attempt. Source: 'my-builder/builder-instance/my-strategy', Operation Key: 'op-key', Result: 'Dummy message.', Handled: 'True', Attempt: '4', Execution Time: '123'");
}

[InlineData(true, true)]
Expand All @@ -170,11 +170,11 @@ public void WriteExecutionAttempt_LoggingWithOutcome_Ok(bool noOutcome, bool han
if (noOutcome)
{
string resultString = string.Empty;
messages[0].Message.Should().Be($"Execution attempt. Source: 'my-builder[builder-instance]/my-strategy', Operation Key: 'op-key', Result: '{resultString}', Handled: '{handled}', Attempt: '4', Execution Time: '123'");
messages[0].Message.Should().Be($"Execution attempt. Source: 'my-builder/builder-instance/my-strategy', Operation Key: 'op-key', Result: '{resultString}', Handled: '{handled}', Attempt: '4', Execution Time: '123'");
}
else
{
messages[0].Message.Should().Be($"Execution attempt. Source: 'my-builder[builder-instance]/my-strategy', Operation Key: 'op-key', Result: '200', Handled: '{handled}', Attempt: '4', Execution Time: '123'");
messages[0].Message.Should().Be($"Execution attempt. Source: 'my-builder/builder-instance/my-strategy', Operation Key: 'op-key', Result: '200', Handled: '{handled}', Attempt: '4', Execution Time: '123'");
}

messages[0].LogLevel.Should().Be(LogLevel.Warning);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ public void Execute_EnsureLogged(bool healthy)

var messages = _logger.GetRecords(new EventId(1, "StrategyExecuting")).ToList();
messages.Should().HaveCount(1);
messages[0].Message.Should().Be("Resilience strategy executing. Source: 'my-builder[my-instance]', Operation Key: 'op-key', Result Type: 'void'");
messages[0].Message.Should().Be("Resilience strategy executing. Source: 'my-builder/my-instance', Operation Key: 'op-key', Result Type: 'void'");
messages = _logger.GetRecords(new EventId(2, "StrategyExecuted")).ToList();
messages.Should().HaveCount(1);
messages[0].Message.Should().Match($"Resilience strategy executed. Source: 'my-builder[my-instance]', Operation Key: 'op-key', Result Type: 'void', Result: 'void', Execution Health: '{healthString}', Execution Time: *ms");
messages[0].Message.Should().Match($"Resilience strategy executed. Source: 'my-builder/my-instance', Operation Key: 'op-key', Result Type: 'void', Result: 'void', Execution Health: '{healthString}', Execution Time: *ms");
messages[0].LogLevel.Should().Be(healthy ? LogLevel.Debug : LogLevel.Warning);

// verify reported state
Expand All @@ -78,11 +78,11 @@ public void Execute_WithException_EnsureLogged()

var messages = _logger.GetRecords(new EventId(1, "StrategyExecuting")).ToList();
messages.Should().HaveCount(1);
messages[0].Message.Should().Be("Resilience strategy executing. Source: 'my-builder[my-instance]', Operation Key: 'op-key', Result Type: 'void'");
messages[0].Message.Should().Be("Resilience strategy executing. Source: 'my-builder/my-instance', Operation Key: 'op-key', Result Type: 'void'");

messages = _logger.GetRecords(new EventId(2, "StrategyExecuted")).ToList();
messages.Should().HaveCount(1);
messages[0].Message.Should().Match($"Resilience strategy executed. Source: 'my-builder[my-instance]', Operation Key: 'op-key', Result Type: 'void', Result: 'Dummy message.', Execution Health: 'Healthy', Execution Time: *ms");
messages[0].Message.Should().Match($"Resilience strategy executed. Source: 'my-builder/my-instance', Operation Key: 'op-key', Result Type: 'void', Result: 'Dummy message.', Execution Health: 'Healthy', Execution Time: *ms");
messages[0].Exception.Should().BeOfType<InvalidOperationException>();
}

Expand Down
7 changes: 7 additions & 0 deletions test/Polly.Extensions.Tests/Utils/TelemetryUtilTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,11 @@ public void AsBoxedInt_Ok()
(-1).AsBoxedInt().Should().NotBeSameAs((-1).AsBoxedInt());
100.AsBoxedInt().Should().NotBeSameAs(100.AsBoxedInt());
}

[Fact]
public void GetValueOrPlaceholder_Ok()
{
TelemetryUtil.GetValueOrPlaceholder("dummy").Should().Be("dummy");
TelemetryUtil.GetValueOrPlaceholder(null).Should().Be("(null)");
}
}

0 comments on commit 7dc06c0

Please sign in to comment.