-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Description
If a user chooses to set a InstanceName on a strategy/pipeline, its value is never passed through to telemetry and is always null.
This is because by default the InstanceNameFormatter is null, so any value is always formatted to be null:
Polly/src/Polly.Core/Registry/ResiliencePipelineRegistryOptions.cs
Lines 38 to 49 in 445c582
| /// <summary> | |
| /// Gets or sets the formatter that is used by the registry to format the <typeparamref name="TKey"/> to a string that | |
| /// represents the instance name of the builder. | |
| /// </summary> | |
| /// <remarks> | |
| /// Use custom formatter for composite keys in case you want to have different metric values for a builder and instance key. | |
| /// In general, pipelines can have the same builder name and different instance names. | |
| /// </remarks> | |
| /// <value> | |
| /// The default value is <see langword="null"/>. | |
| /// </value> | |
| public Func<TKey, string>? InstanceNameFormatter { get; set; } |
To get the desired behaviour, instead the user has to explicitly configure the internally-registered-by-default ResiliencePipelineRegistryOptions<TKey> type to provide a formatter for each type they generate a registry for:
services.AddOptions<ResiliencePipelineRegistryOptions<string>>()
.Configure((p) => p.InstanceNameFormatter = (key) => key?.ToString() ?? string.Empty);This behaviour is not intuitive (I had to work it out by debugging through the Polly sources from my app).
It would be much easier for users to instead just apply a default formatter to the instance the same as we do for BuilderNameFormatter:
Polly/src/Polly.Core/Registry/ResiliencePipelineRegistryOptions.cs
Lines 51 to 63 in 445c582
| /// <summary> | |
| /// Gets or sets the formatter that is used by the registry to format the <typeparamref name="TKey"/> to a string that | |
| /// represents the builder name. | |
| /// </summary> | |
| /// <remarks> | |
| /// Use custom formatter for composite keys in case you want to have different metric values for a builder and strategy key. | |
| /// In general, pipelines can have the same builder name and different pipeline keys. | |
| /// </remarks> | |
| /// <value> | |
| /// The default value is a formatter that formats the keys using the <see cref="object.ToString"/> method. | |
| /// </value> | |
| [Required] | |
| public Func<TKey, string> BuilderNameFormatter { get; set; } = (key) => key?.ToString() ?? string.Empty; |
We could always pre-allocate the default formatter delegates as a private static field if allocations are a concern.