From 2ec661621d0b12fb0e5e400dbc404556ffb450bb Mon Sep 17 00:00:00 2001 From: woksin Date: Fri, 10 Jan 2025 09:21:30 +0100 Subject: [PATCH 1/3] Make it possible to configure Polly resiilience OnRetry event severity. Defaults to Debug --- Source/Kernel/Setup/ResilientStorageOptions.cs | 6 ++++++ Source/Kernel/Setup/StorageProviderExtensions.cs | 14 ++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/Source/Kernel/Setup/ResilientStorageOptions.cs b/Source/Kernel/Setup/ResilientStorageOptions.cs index 8052117dd..24c8eca72 100644 --- a/Source/Kernel/Setup/ResilientStorageOptions.cs +++ b/Source/Kernel/Setup/ResilientStorageOptions.cs @@ -3,6 +3,7 @@ using System.ComponentModel.DataAnnotations; using Polly.Retry; +using Polly.Telemetry; using Polly.Timeout; namespace Cratis.Chronicle.Setup; @@ -22,4 +23,9 @@ public class ResilientStorageOptions /// [Required] public TimeoutStrategyOptions Timeout { get; set; } = new(); + + /// + /// Gets or sets the value indicating whether to enable Polly recording telemetry for each "OnRetry" event. + /// + public ResilienceEventSeverity OnRetryEventSeverity { get; set; } = ResilienceEventSeverity.Debug; } diff --git a/Source/Kernel/Setup/StorageProviderExtensions.cs b/Source/Kernel/Setup/StorageProviderExtensions.cs index 636817455..59dc58858 100644 --- a/Source/Kernel/Setup/StorageProviderExtensions.cs +++ b/Source/Kernel/Setup/StorageProviderExtensions.cs @@ -8,6 +8,7 @@ using Orleans.Storage; using Polly; using Polly.Registry; +using Polly.Telemetry; namespace Orleans.Hosting; @@ -33,6 +34,19 @@ public static ISiloBuilder AddStorageProviders(this ISiloBuilder builder) var options = context.GetOptions(); builder.AddRetry(options.Retry); builder.AddTimeout(options.Timeout); + + var telemetryOptions = new TelemetryOptions(context.GetOptions()); + telemetryOptions.SeverityProvider = ev => + { + // Docs https://github.com/App-vNext/Polly/blob/main/docs/advanced/telemetry.md/ + if (ev.Event.EventName == "OnRetry") + { + return options.OnRetryEventSeverity; + } + + return ev.Event.Severity; + }; + builder.ConfigureTelemetry(telemetryOptions); }); builder.ConfigureServices(services => From 46a7daea0f085e2e81576cb477d47531cdcd4a0e Mon Sep 17 00:00:00 2001 From: woksin Date: Fri, 10 Jan 2025 09:32:04 +0100 Subject: [PATCH 2/3] Make it a bite more flexible --- Source/Kernel/Setup/ResilientStorageOptions.cs | 5 ++++- Source/Kernel/Setup/StorageProviderExtensions.cs | 5 ++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Source/Kernel/Setup/ResilientStorageOptions.cs b/Source/Kernel/Setup/ResilientStorageOptions.cs index 24c8eca72..14b8393fc 100644 --- a/Source/Kernel/Setup/ResilientStorageOptions.cs +++ b/Source/Kernel/Setup/ResilientStorageOptions.cs @@ -27,5 +27,8 @@ public class ResilientStorageOptions /// /// Gets or sets the value indicating whether to enable Polly recording telemetry for each "OnRetry" event. /// - public ResilienceEventSeverity OnRetryEventSeverity { get; set; } = ResilienceEventSeverity.Debug; + public Dictionary ResilienceEventSeverities { get; set; } = new(StringComparer.InvariantCulture) + { + { "OnRetry", ResilienceEventSeverity.Debug } + }; } diff --git a/Source/Kernel/Setup/StorageProviderExtensions.cs b/Source/Kernel/Setup/StorageProviderExtensions.cs index 59dc58858..bc61b99c2 100644 --- a/Source/Kernel/Setup/StorageProviderExtensions.cs +++ b/Source/Kernel/Setup/StorageProviderExtensions.cs @@ -38,10 +38,9 @@ public static ISiloBuilder AddStorageProviders(this ISiloBuilder builder) var telemetryOptions = new TelemetryOptions(context.GetOptions()); telemetryOptions.SeverityProvider = ev => { - // Docs https://github.com/App-vNext/Polly/blob/main/docs/advanced/telemetry.md/ - if (ev.Event.EventName == "OnRetry") + if (options.ResilienceEventSeverities.TryGetValue(ev.Event.EventName, out var severity)) { - return options.OnRetryEventSeverity; + return severity; } return ev.Event.Severity; From d5a66fbfdfb8d0e5f60ffa09f76c59e645a59aae Mon Sep 17 00:00:00 2001 From: woksin Date: Fri, 10 Jan 2025 09:44:31 +0100 Subject: [PATCH 3/3] Fix build --- Source/Kernel/Setup/ResilientStorageOptions.cs | 2 ++ Source/Kernel/Setup/StorageProviderExtensions.cs | 14 ++++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/Source/Kernel/Setup/ResilientStorageOptions.cs b/Source/Kernel/Setup/ResilientStorageOptions.cs index 14b8393fc..9d6076cfc 100644 --- a/Source/Kernel/Setup/ResilientStorageOptions.cs +++ b/Source/Kernel/Setup/ResilientStorageOptions.cs @@ -27,7 +27,9 @@ public class ResilientStorageOptions /// /// Gets or sets the value indicating whether to enable Polly recording telemetry for each "OnRetry" event. /// +#pragma warning disable MA0016 public Dictionary ResilienceEventSeverities { get; set; } = new(StringComparer.InvariantCulture) +#pragma warning restore MA0016 { { "OnRetry", ResilienceEventSeverity.Debug } }; diff --git a/Source/Kernel/Setup/StorageProviderExtensions.cs b/Source/Kernel/Setup/StorageProviderExtensions.cs index bc61b99c2..b84a5189a 100644 --- a/Source/Kernel/Setup/StorageProviderExtensions.cs +++ b/Source/Kernel/Setup/StorageProviderExtensions.cs @@ -35,15 +35,17 @@ public static ISiloBuilder AddStorageProviders(this ISiloBuilder builder) builder.AddRetry(options.Retry); builder.AddTimeout(options.Timeout); - var telemetryOptions = new TelemetryOptions(context.GetOptions()); - telemetryOptions.SeverityProvider = ev => + var telemetryOptions = new TelemetryOptions(context.GetOptions()) { - if (options.ResilienceEventSeverities.TryGetValue(ev.Event.EventName, out var severity)) + SeverityProvider = ev => { - return severity; - } + if (options.ResilienceEventSeverities.TryGetValue(ev.Event.EventName, out var severity)) + { + return severity; + } - return ev.Event.Severity; + return ev.Event.Severity; + } }; builder.ConfigureTelemetry(telemetryOptions); });