Skip to content

Default logging behaviour when using InstanceName is unintuitive #1547

@martincostello

Description

@martincostello

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:

/// <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:

/// <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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugv8Issues related to the new version 8 of the Polly library.

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions