|
1 | 1 | # Telemetry |
2 | 2 |
|
3 | | -🚧 This documentation is being written as part of the Polly v8 release. |
| 3 | +Starting with version 8, Polly provides telemetry for all built-in resilience strategies. |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +To enable telemetry in Polly, add the `Polly.Extensions` package to your project: |
| 8 | + |
| 9 | +```sh |
| 10 | +dotnet add package Polly.Extensions |
| 11 | +``` |
| 12 | + |
| 13 | +Afterwards, you can use the `ConfigureTelemetry(...)` extension method on the `ResiliencePipelineBuilder`: |
| 14 | + |
| 15 | +<!-- snippet: configure-telemetry --> |
| 16 | +```cs |
| 17 | +var telemetryOptions = new TelemetryOptions |
| 18 | +{ |
| 19 | + // Configure logging |
| 20 | + LoggerFactory = LoggerFactory.Create(builder => builder.AddConsole()) |
| 21 | +}; |
| 22 | + |
| 23 | +// Configure enrichers |
| 24 | +telemetryOptions.MeteringEnrichers.Add(new MyMeteringEnricher()); |
| 25 | + |
| 26 | +// Configure telemetry listeners |
| 27 | +telemetryOptions.TelemetryListeners.Add(new MyTelemetryListener()); |
| 28 | + |
| 29 | +var builder = new ResiliencePipelineBuilder() |
| 30 | + .AddTimeout(TimeSpan.FromSeconds(1)) |
| 31 | + .ConfigureTelemetry(telemetryOptions) // This method enables telemetry in the builder |
| 32 | + .Build(); |
| 33 | +``` |
| 34 | +<!-- endSnippet --> |
| 35 | + |
| 36 | +The `MyTelemetryListener` and `MyMeteringEnricher` is implemented as: |
| 37 | + |
| 38 | +<!-- snippet: telemetry-listeners --> |
| 39 | +```cs |
| 40 | +internal class MyTelemetryListener : TelemetryListener |
| 41 | +{ |
| 42 | + public override void Write<TResult, TArgs>(in TelemetryEventArguments<TResult, TArgs> args) |
| 43 | + { |
| 44 | + Console.WriteLine($"Telemetry event occurred: {args.Event.EventName}"); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +internal class MyMeteringEnricher : MeteringEnricher |
| 49 | +{ |
| 50 | + public override void Enrich<TResult, TArgs>(in EnrichmentContext<TResult, TArgs> context) |
| 51 | + { |
| 52 | + context.Tags.Add(new("my-custom-tag", "custom-value")); |
| 53 | + } |
| 54 | +} |
| 55 | +``` |
| 56 | +<!-- endSnippet --> |
| 57 | + |
| 58 | +Alternatively, you can use the `AddResiliencePipeline(...)` extension method which automatically enables telemetry for defined pipeline: |
| 59 | + |
| 60 | +<!-- snippet: add-resilience-pipeline-with-telemetry --> |
| 61 | +```cs |
| 62 | +var serviceCollection = new ServiceCollection() |
| 63 | + .AddLogging(builder => builder.AddConsole()) |
| 64 | + .AddResiliencePipeline("my-strategy", builder => builder.AddTimeout(TimeSpan.FromSeconds(1))) |
| 65 | + .Configure<TelemetryOptions>(options => |
| 66 | + { |
| 67 | + // Configure enrichers |
| 68 | + options.MeteringEnrichers.Add(new MyMeteringEnricher()); |
| 69 | + |
| 70 | + // Configure telemetry listeners |
| 71 | + options.TelemetryListeners.Add(new MyTelemetryListener()); |
| 72 | + }); |
| 73 | +``` |
| 74 | +<!-- endSnippet --> |
| 75 | + |
| 76 | +## Metrics |
| 77 | + |
| 78 | +The emitted metrics are emitted under the `Polly` meter name. The subsequent sections provide insights into the metrics produced by Polly. Please note that any custom enriched dimensions are not depicted in the following tables. |
| 79 | + |
| 80 | +### resilience-events |
| 81 | + |
| 82 | +- Type: *Counter* |
| 83 | +- Description: Emitted upon the occurrence of a resilience event. |
| 84 | + |
| 85 | +Dimensions: |
| 86 | + |
| 87 | +|Name|Description| |
| 88 | +|---| ---| |
| 89 | +|`event-name`| The name of the emitted event.| |
| 90 | +|`event-severity`| The severity of the event (`Debug`, `Information`, `Warning`, `Error`, `Critical`).| |
| 91 | +|`pipeline-name`| The name of the pipeline corresponding to the resilience pipeline.| |
| 92 | +|`pipeline-instance`| The instance name of the pipeline corresponding to the resilience pipeline.| |
| 93 | +|`strategy-name`| The name of the strategy generating this event.| |
| 94 | +|`operation-key`| The operation key associated with the call site. | |
| 95 | +|`exception-name`| The full name of the exception assigned to the execution result (`System.InvalidOperationException`). | |
| 96 | + |
| 97 | +### execution-attempt-duration |
| 98 | + |
| 99 | +- Type: *Histogram* |
| 100 | +- Unit: *milliseconds* |
| 101 | +- Description: Tracks the duration of execution attempts, produced by `Retry` and `Hedging` resilience strategies. |
| 102 | + |
| 103 | +Dimensions: |
| 104 | + |
| 105 | +|Name|Description| |
| 106 | +|---| ---| |
| 107 | +|`event-name`| The name of the emitted event.| |
| 108 | +|`event-severity`| The severity of the event (`Debug`, `Information`, `Warning`, `Error`, `Critical`).| |
| 109 | +|`pipeline-name`| The name of the pipeline corresponding to the resilience pipeline.| |
| 110 | +|`pipeline-instance`| The instance name of the pipeline corresponding to the resilience pipeline.| |
| 111 | +|`strategy-name`| The name of the strategy generating this event.| |
| 112 | +|`operation-key`| The operation key associated with the call site. | |
| 113 | +|`exception-name`| The full name of the exception assigned to the execution result (`System.InvalidOperationException`). | |
| 114 | +|`attempt-number`| The execution attempt number, starting at 0 (0, 1, 2). | |
| 115 | +|`attempt-handled`| Indicates if the execution outcome was handled. A handled outcome indicates execution failure and the need for retry (`true`, `false`). | |
| 116 | + |
| 117 | +### pipeline-execution-duration |
| 118 | + |
| 119 | +- Type: *Histogram* |
| 120 | +- Unit: *milliseconds* |
| 121 | +- Description: Measures the duration and results of resilience pipelines. |
| 122 | + |
| 123 | +Dimensions: |
| 124 | + |
| 125 | +|Name|Description| |
| 126 | +|---| ---| |
| 127 | +|`pipeline-name`| The name of the pipeline corresponding to the resilience pipeline.| |
| 128 | +|`pipeline-instance`| The instance name of the pipeline corresponding to the resilience pipeline.| |
| 129 | +|`operation-key`| The operation key associated with the call site. | |
| 130 | +|`exception-name`| The full name of the exception assigned to the execution result (`System.InvalidOperationException`). | |
| 131 | + |
| 132 | +## Logs |
| 133 | + |
| 134 | +Logs are registered under the `Polly` logger name. Here are some examples of the logs: |
| 135 | + |
| 136 | +``` text |
| 137 | +// This log is recorded whenever a resilience event occurs. EventId = 0 |
| 138 | +Resilience event occurred. EventName: '{EventName}', Source: '{PipelineName}/{PipelineInstance}/{StrategyName}', Operation Key: '{OperationKey}', Result: '{Result}' |
| 139 | +
|
| 140 | +// This log is recorded when a resilience pipeline begins executing. EventId = 1 |
| 141 | +Resilience pipeline executing. Source: '{PipelineName}/{PipelineInstance}', Operation Key: '{OperationKey}' |
| 142 | +
|
| 143 | +// This log is recorded when a resilience pipeline finishes execution. EventId = 2 |
| 144 | +Resilience pipeline executed. Source: '{PipelineName}/{PipelineInstance}', Operation Key: '{OperationKey}', Result: '{Result}', Execution Health: '{ExecutionHealth}', Execution Time: {ExecutionTime}ms |
| 145 | +
|
| 146 | +// This log is recorded upon the completion of every execution attempt. EventId = 3 |
| 147 | +Execution attempt. Source: '{PipelineName}/{PipelineInstance}/{StrategyName}', Operation Key: '{OperationKey}', Result: '{Result}', Handled: '{Handled}', Attempt: '{Attempt}', Execution Time: '{ExecutionTimeMs}' |
| 148 | +``` |
| 149 | + |
| 150 | +## Emitting telemetry events |
| 151 | + |
| 152 | +Each resilience strategy can generate telemetry data through the [`ResilienceStrategyTelemetry`](../src/Polly.Core/Telemetry/ResilienceStrategyTelemetry.cs) API. Polly encapsulates event details as [`TelemetryEventArguments`](../src/Polly.Core/Telemetry/TelemetryEventArguments.cs) and emits them via `TelemetryListener`. |
| 153 | + |
| 154 | +To leverage this telemetry data, users should assign a `TelemetryListener` instance to `ResiliencePipelineBuilder.TelemetryListener` and then consume the `TelemetryEventArguments`. |
| 155 | + |
| 156 | +For common scenarios, it is expected that users would make use of `Polly.Extensions`. This extension enables telemetry configuration through the `ResiliencePipelineBuilder.ConfigureTelemetry(...)` method, which processes `TelemetryEventArguments` to generate logs and metrics. |
0 commit comments