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

Improve logging messages #1436

Merged
merged 3 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 4 additions & 4 deletions src/Polly.Extensions/Telemetry/Log.cs
martincostello marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ 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")]
Expand All @@ -31,7 +31,7 @@ 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")]
Expand All @@ -45,7 +45,7 @@ public static partial void ExecutingStrategy(
[LoggerMessage(
EventId = 2,
Message = "Resilience strategy executed. " +
"Source: '{BuilderName}[{BuilderInstance}]', " +
"Source: '{BuilderName}/{BuilderInstance}', " +
"Operation Key: '{OperationKey}', " +
"Result Type: '{ResultType}', " +
"Result: '{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 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)");
}
}
Loading