forked from App-vNext/Polly
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce ResilienceTelemetryFactory (App-vNext#1073)
- Loading branch information
Showing
12 changed files
with
285 additions
and
21 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
src/Polly.Core.Tests/Builder/ResilienceStrategyBuilderContextTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using FluentAssertions; | ||
using Polly.Builder; | ||
using Polly.Telemetry; | ||
using Xunit; | ||
|
||
namespace Polly.Core.Tests.Builder; | ||
|
||
public class ResilienceStrategyBuilderContextTests | ||
{ | ||
[Fact] | ||
public void Ctor_EnsureDefaults() | ||
{ | ||
var context = new ResilienceStrategyBuilderContext(); | ||
|
||
context.BuilderName.Should().Be(""); | ||
context.BuilderProperties.Should().NotBeNull(); | ||
context.StrategyName.Should().Be(""); | ||
context.StrategyType.Should().Be(""); | ||
context.Telemetry.Should().Be(NullResilienceTelemetry.Instance); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/Polly.Core.Tests/Telemetry/NullResilienceTelemetryFactoryTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using FluentAssertions; | ||
using Polly.Telemetry; | ||
using Xunit; | ||
|
||
namespace Polly.Core.Tests.Telemetry; | ||
|
||
public class NullResilienceTelemetryFactoryTests | ||
{ | ||
[Fact] | ||
public void Instance_NotNull() | ||
{ | ||
NullResilienceTelemetry.Instance.Should().NotBeNull(); | ||
} | ||
|
||
[Fact] | ||
public void Default_Ok() | ||
{ | ||
var context = new ResilienceTelemetryFactoryContext(); | ||
|
||
context.BuilderName.Should().BeEmpty(); | ||
context.StrategyType.Should().BeEmpty(); | ||
context.StrategyName.Should().BeEmpty(); | ||
context.BuilderProperties.Should().NotBeNull(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Polly.Core.Tests/Telemetry/NullResilienceTelemetryTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
using FluentAssertions; | ||
using Polly.Telemetry; | ||
using Xunit; | ||
|
||
namespace Polly.Core.Tests.Telemetry; | ||
|
||
public class NullResilienceTelemetryTests | ||
{ | ||
[Fact] | ||
public void Instance_NotNull() | ||
{ | ||
NullResilienceTelemetry.Instance.Should().NotBeNull(); | ||
} | ||
|
||
[Fact] | ||
public void Report_ShouldNotThrow() | ||
{ | ||
NullResilienceTelemetry.Instance | ||
.Invoking(v => | ||
{ | ||
NullResilienceTelemetry.Instance.Report("dummy", ResilienceContext.Get()); | ||
NullResilienceTelemetry.Instance.Report("dummy", 10, ResilienceContext.Get()); | ||
NullResilienceTelemetry.Instance.ReportException("dummy", new InvalidOperationException(), ResilienceContext.Get()); | ||
}) | ||
.Should() | ||
.NotThrow(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 11 additions & 16 deletions
27
src/Polly.Core/Builder/ResilienceStrategyBuilderContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,34 @@ | ||
using Polly.Telemetry; | ||
|
||
namespace Polly.Builder; | ||
|
||
/// <summary> | ||
/// The context used for building an individual resilience strategy. | ||
/// </summary> | ||
public class ResilienceStrategyBuilderContext | ||
{ | ||
internal ResilienceStrategyBuilderContext( | ||
string builderName, | ||
ResilienceProperties builderProperties, | ||
string strategyName, | ||
string strategyType) | ||
{ | ||
BuilderName = Guard.NotNull(builderName); | ||
BuilderProperties = Guard.NotNull(builderProperties); | ||
StrategyName = Guard.NotNull(strategyName); | ||
StrategyType = Guard.NotNull(strategyType); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the name of the builder. | ||
/// </summary> | ||
public string BuilderName { get; } | ||
public string BuilderName { get; internal set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// Gets the custom properties attached to the builder. | ||
/// </summary> | ||
public ResilienceProperties BuilderProperties { get; } | ||
public ResilienceProperties BuilderProperties { get; internal set; } = new(); | ||
|
||
/// <summary> | ||
/// Gets the name of the strategy. | ||
/// </summary> | ||
public string StrategyName { get; } | ||
public string StrategyName { get; internal set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// Gets the type of the strategy. | ||
/// </summary> | ||
public string StrategyType { get; } | ||
public string StrategyType { get; internal set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// Gets the resilience telemetry used to report important events. | ||
/// </summary> | ||
public ResilienceTelemetry Telemetry { get; internal set; } = NullResilienceTelemetry.Instance; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
namespace Polly.Telemetry; | ||
|
||
/// <summary> | ||
/// A implementation of <see cref="ResilienceTelemetryFactory"/> that does nothing. | ||
/// </summary> | ||
public sealed class NullResilienceTelemetry : ResilienceTelemetry | ||
{ | ||
private NullResilienceTelemetry() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Gets an instance of <see cref="NullResilienceTelemetry"/>. | ||
/// </summary> | ||
public static readonly NullResilienceTelemetry Instance = new(); | ||
|
||
/// <inheritdoc/> | ||
public override void Report(string eventName, ResilienceContext context) | ||
{ | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override void Report<TResult>(string eventName, TResult result, ResilienceContext context) | ||
{ | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override void ReportException(string eventName, Exception exception, ResilienceContext context) | ||
{ | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/Polly.Core/Telemetry/NullResilienceTelemetryFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace Polly.Telemetry; | ||
|
||
/// <summary> | ||
/// Factory that returns <see cref="NullResilienceTelemetry"/> instances. | ||
/// </summary> | ||
public sealed class NullResilienceTelemetryFactory : ResilienceTelemetryFactory | ||
{ | ||
/// <summary> | ||
/// Gets the singleton instance of the factory. | ||
/// </summary> | ||
public static readonly NullResilienceTelemetryFactory Instance = new(); | ||
|
||
private NullResilienceTelemetryFactory() | ||
{ | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override ResilienceTelemetry Create(ResilienceTelemetryFactoryContext context) => NullResilienceTelemetry.Instance; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
namespace Polly.Telemetry; | ||
|
||
#pragma warning disable S1694 // An abstract class should have both abstract and concrete methods | ||
|
||
/// <summary> | ||
/// Resilience telemetry is used by individual resilience strategies to report some important events. | ||
/// </summary> | ||
/// <remarks> | ||
/// For example, the timeout strategy reports "OnTimeout" event when the timeout is reached or "OnRetry" for retry strategy. | ||
/// </remarks> | ||
public abstract class ResilienceTelemetry | ||
{ | ||
/// <summary> | ||
/// Reports an event that occurred in the resilience strategy. | ||
/// </summary> | ||
/// <param name="eventName">The event name.</param> | ||
/// <param name="context">The context associated with the event.</param> | ||
public abstract void Report(string eventName, ResilienceContext context); | ||
|
||
/// <summary> | ||
/// Reports an event that occurred in the resilience strategy. | ||
/// </summary> | ||
/// <typeparam name="TResult">The type of the result.</typeparam> | ||
/// <param name="eventName">The event name.</param> | ||
/// <param name="result">The result associated with the event.</param> | ||
/// <param name="context">The context associated with the event.</param> | ||
public abstract void Report<TResult>(string eventName, TResult result, ResilienceContext context); | ||
|
||
/// <summary> | ||
/// Reports an event that occurred in the resilience strategy. | ||
/// </summary> | ||
/// <param name="eventName">The event name.</param> | ||
/// <param name="exception">The exception associated with the event.</param> | ||
/// <param name="context">The context associated with the event.</param> | ||
public abstract void ReportException(string eventName, Exception exception, ResilienceContext context); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace Polly.Telemetry; | ||
|
||
#pragma warning disable S1694 // An abstract class should have both abstract and concrete methods | ||
|
||
/// <summary> | ||
/// Factory used to created instances of <see cref="ResilienceTelemetry"/>. | ||
/// </summary> | ||
public abstract class ResilienceTelemetryFactory | ||
{ | ||
/// <summary> | ||
/// Creates a new instance of <see cref="ResilienceTelemetry"/>. | ||
/// </summary> | ||
/// <param name="context">The context associated with the creation of <see cref="ResilienceTelemetry"/>.</param> | ||
/// <returns>An instance of <see cref="ResilienceTelemetry"/>.</returns> | ||
public abstract ResilienceTelemetry Create(ResilienceTelemetryFactoryContext context); | ||
} |
27 changes: 27 additions & 0 deletions
27
src/Polly.Core/Telemetry/ResilienceTelemetryFactoryContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
namespace Polly.Telemetry; | ||
|
||
/// <summary> | ||
/// The context used for building an instance of <see cref="ResilienceTelemetry"/>. | ||
/// </summary> | ||
public class ResilienceTelemetryFactoryContext | ||
{ | ||
/// <summary> | ||
/// Gets the name of the builder. | ||
/// </summary> | ||
public string BuilderName { get; internal set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// Gets the name of the strategy. | ||
/// </summary> | ||
public string StrategyName { get; internal set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// Gets the type of the strategy. | ||
/// </summary> | ||
public string StrategyType { get; internal set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// Gets the custom properties attached to the builder. | ||
/// </summary> | ||
public ResilienceProperties BuilderProperties { get; internal set; } = new(); | ||
} |