diff --git a/.github/wordlist.txt b/.github/wordlist.txt index 3e816cc7e57..3c447baa572 100644 --- a/.github/wordlist.txt +++ b/.github/wordlist.txt @@ -64,3 +64,4 @@ uwp waitandretry wpf xunit +enricher diff --git a/docs/advanced/telemetry.md b/docs/advanced/telemetry.md index 99859843eee..ef6d2af8b51 100644 --- a/docs/advanced/telemetry.md +++ b/docs/advanced/telemetry.md @@ -186,6 +186,46 @@ Tags: |`operation.key`| The operation key associated with the call site. | |`exception.type`| The full name of the exception assigned to the execution result (`System.InvalidOperationException`). | +### Metering enrichment + +Polly API lets you add extra tags to any resilience event created by resilience strategies. To do this, derive from the class and add your custom enricher to the list. + +The custom enricher: + + +```cs +internal sealed class CustomMeteringEnricher : MeteringEnricher +{ + public override void Enrich(in EnrichmentContext context) + { + // You can read additional details from any resilience event and use it to enrich the telemetry + if (context.TelemetryEvent.Arguments is OnRetryArguments retryArgs) + { + // See https://github.com/open-telemetry/semantic-conventions/blob/main/docs/general/metrics.md for more details + // on how to name the tags. + context.Tags.Add(new("retry.attempt", retryArgs.AttemptNumber)); + } + } +} +``` + + +Registering the custom enricher: + + +```cs +var telemetryOptions = new TelemetryOptions(); + +// Register custom enricher +telemetryOptions.MeteringEnrichers.Add(new CustomMeteringEnricher()); + +var builder = new ResiliencePipelineBuilder() + .AddRetry(new RetryStrategyOptions()) + .ConfigureTelemetry(telemetryOptions) // This method enables telemetry in the builder + .Build(); +``` + + ## Logs Logs are registered under the `Polly` logger name. Here are some examples of the logs: diff --git a/src/Snippets/Docs/Telemetry.cs b/src/Snippets/Docs/Telemetry.cs index 1dc6433b39d..7825e20db60 100644 --- a/src/Snippets/Docs/Telemetry.cs +++ b/src/Snippets/Docs/Telemetry.cs @@ -99,4 +99,39 @@ public override void Enrich(in EnrichmentContext } #endregion + + public static void MeteringEnricherRegistration() + { + #region metering-enricher-registration + + var telemetryOptions = new TelemetryOptions(); + + // Register custom enricher + telemetryOptions.MeteringEnrichers.Add(new CustomMeteringEnricher()); + + var builder = new ResiliencePipelineBuilder() + .AddRetry(new RetryStrategyOptions()) + .ConfigureTelemetry(telemetryOptions) // This method enables telemetry in the builder + .Build(); + + #endregion + } + + #region metering-enricher + + internal sealed class CustomMeteringEnricher : MeteringEnricher + { + public override void Enrich(in EnrichmentContext context) + { + // You can read additional details from any resilience event and use it to enrich the telemetry + if (context.TelemetryEvent.Arguments is OnRetryArguments retryArgs) + { + // See https://github.com/open-telemetry/semantic-conventions/blob/main/docs/general/metrics.md for more details + // on how to name the tags. + context.Tags.Add(new("retry.attempt", retryArgs.AttemptNumber)); + } + } + } + + #endregion }