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

[Docs] Add docs for metering enrichment #1724

Merged
merged 3 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .github/wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,4 @@ uwp
waitandretry
wpf
xunit
enricher
40 changes: 40 additions & 0 deletions docs/advanced/telemetry.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <xref:Polly.Telemetry.MeteringEnricher> class and add your custom enricher to the <xref:Polly.Telemetry.TelemetryOptions.MeteringEnrichers> list.

The custom enricher:

<!-- snippet: metering-enricher -->
```cs
internal sealed class CustomMeteringEnricher : MeteringEnricher
{
public override void Enrich<TResult, TArgs>(in EnrichmentContext<TResult, TArgs> context)
{
// You can read additional details from any resilience event and use it to enrich the telemetry
if (context.TelemetryEvent.Arguments is OnRetryArguments<TResult> 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));
}
}
}
```
<!-- endSnippet -->

Registering the custom enricher:

<!-- snippet: metering-enricher-registration -->
```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();
```
<!-- endSnippet -->

## Logs

Logs are registered under the `Polly` logger name. Here are some examples of the logs:
Expand Down
35 changes: 35 additions & 0 deletions src/Snippets/Docs/Telemetry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,39 @@ public override void Enrich<TResult, TArgs>(in EnrichmentContext<TResult, TArgs>
}

#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<TResult, TArgs>(in EnrichmentContext<TResult, TArgs> context)
{
// You can read additional details from any resilience event and use it to enrich the telemetry
if (context.TelemetryEvent.Arguments is OnRetryArguments<TResult> 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
}