Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ public void GlobalSetup()
{
_listener = MetricsUtil.ListenPollyMetrics();
_pipeline = CreateResiliencePipeline(_ => { });
_pipelineEnriched = CreateResiliencePipeline(services =>
{
services.AddResilienceEnricher();
services.ConfigureFailureResultContext<string>(res => FailureResultContext.Create("dummy", "dummy", "dummy"));
});
_pipelineEnriched = CreateResiliencePipeline(services => services.AddResilienceEnricher());
}

[GlobalCleanup]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<PropertyGroup>
<UseLoggingGenerator>true</UseLoggingGenerator>
<UseMetricsGenerator>true</UseMetricsGenerator>
<UseEnumStringsGenerator>true</UseEnumStringsGenerator>
<InjectSharedNumericExtensions>true</InjectSharedNumericExtensions>
<InjectSharedThrow>true</InjectSharedThrow>
<InjectDiagnosticAttributesOnLegacy>true</InjectDiagnosticAttributesOnLegacy>
Expand All @@ -20,7 +19,7 @@

<PropertyGroup>
<Stage>normal</Stage>
<MinCodeCoverage>98</MinCodeCoverage>
<MinCodeCoverage>97</MinCodeCoverage>
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be at 100, but code coverage is reporting lower number :/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's been reported as
image

Because of this:
image

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...though the validation script reports it as 97.38%...
image

<MinMutationScore>100</MinMutationScore>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Net.Http;
using Microsoft.Shared.Text;
using Polly.Telemetry;

namespace Microsoft.Extensions.Http.Resilience.Internal;

internal sealed class HttpResilienceMetricsEnricher : MeteringEnricher
{
public override void Enrich<TResult, TArgs>(in EnrichmentContext<TResult, TArgs> context)
{
if (typeof(TResult) != typeof(HttpResponseMessage))
{
return;
}

if (context.TelemetryEvent.Outcome.HasValue && context.TelemetryEvent.Outcome.Value.Result is HttpResponseMessage response)
{
context.Tags.Add(new(HttpResilienceTagNames.ErrorType, ((int)response.StatusCode).ToInvariantString()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.Extensions.Http.Resilience.Internal;

internal static class HttpResilienceTagNames
{
public const string ErrorType = "error.type";
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Net;
using System.Net.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.ExceptionSummarization;
using Microsoft.Extensions.EnumStrings;
using Microsoft.Extensions.Http.Resilience;
using Microsoft.Extensions.Http.Resilience.Internal;
using Microsoft.Extensions.Resilience;
using Microsoft.Shared.Diagnostics;
using Microsoft.Shared.Text;
using Polly;
using Polly.Registry;

[assembly: EnumStrings(typeof(HttpStatusCode))]
using Polly.Telemetry;

namespace Microsoft.Extensions.DependencyInjection;

Expand Down Expand Up @@ -44,9 +39,7 @@ public static IHttpResiliencePipelineBuilder AddResilienceHandler(
_ = Throw.IfNullOrEmpty(pipelineName);
_ = Throw.IfNull(configure);

return builder.AddResilienceHandler(pipelineName, ConfigureBuilder);

void ConfigureBuilder(ResiliencePipelineBuilder<HttpResponseMessage> builder, ResilienceHandlerContext context) => configure(builder);
return builder.AddResilienceHandler(pipelineName, (builder, _) => configure(builder));
}

/// <summary>
Expand Down Expand Up @@ -149,17 +142,7 @@ private static void ConfigureHttpServices(IServiceCollection services)

_ = services
.AddExceptionSummarizer(b => b.AddHttpProvider())
.ConfigureFailureResultContext<HttpResponseMessage>((response) =>
{
if (response != null)
{
return FailureResultContext.Create(
failureReason: ((int)response.StatusCode).ToInvariantString(),
additionalInformation: response.StatusCode.ToInvariantString());
}

return FailureResultContext.Create();
});
.Configure<TelemetryOptions>(options => options.MeteringEnrichers.Add(new HttpResilienceMetricsEnricher()));
}

private sealed class Marker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ public OrderedGroupsRoutingStrategy Get()
return strategy;
}

public void Return(OrderedGroupsRoutingStrategy strategy) => _pool.Return(strategy);

OrderedGroupsRoutingStrategy IPooledObjectPolicy<OrderedGroupsRoutingStrategy>.Create() => new(_randomizer, _pool);

bool IPooledObjectPolicy<OrderedGroupsRoutingStrategy>.Return(OrderedGroupsRoutingStrategy obj) => obj.TryReset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ public WeightedGroupsRoutingStrategy Get()
return strategy;
}

public void Return(WeightedGroupsRoutingStrategy strategy) => _pool.Return(strategy);

WeightedGroupsRoutingStrategy IPooledObjectPolicy<WeightedGroupsRoutingStrategy>.Create() => new(_randomizer, _pool);

bool IPooledObjectPolicy<WeightedGroupsRoutingStrategy>.Return(WeightedGroupsRoutingStrategy obj) => obj.TryReset();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,6 @@
{
"Name": "Microsoft.Extensions.Resilience, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35",
"Types": [
{
"Type": "readonly struct Microsoft.Extensions.Resilience.FailureResultContext",
"Stage": "Stable",
"Methods": [
{
"Member": "Microsoft.Extensions.Resilience.FailureResultContext.FailureResultContext();",
"Stage": "Stable"
},
{
"Member": "static Microsoft.Extensions.Resilience.FailureResultContext Microsoft.Extensions.Resilience.FailureResultContext.Create(string failureSource = \"unknown\", string failureReason = \"unknown\", string additionalInformation = \"unknown\");",
"Stage": "Stable"
}
],
"Properties": [
{
"Member": "string Microsoft.Extensions.Resilience.FailureResultContext.AdditionalInformation { get; }",
"Stage": "Stable"
},
{
"Member": "string Microsoft.Extensions.Resilience.FailureResultContext.FailureReason { get; }",
"Stage": "Stable"
},
{
"Member": "string Microsoft.Extensions.Resilience.FailureResultContext.FailureSource { get; }",
"Stage": "Stable"
}
]
},
{
"Type": "static class Microsoft.Extensions.Resilience.ResilienceContextExtensions",
"Stage": "Stable",
Expand All @@ -50,12 +22,8 @@
{
"Member": "static Microsoft.Extensions.DependencyInjection.IServiceCollection Microsoft.Extensions.DependencyInjection.ResilienceServiceCollectionExtensions.AddResilienceEnricher(this Microsoft.Extensions.DependencyInjection.IServiceCollection services);",
"Stage": "Stable"
},
{
"Member": "static Microsoft.Extensions.DependencyInjection.IServiceCollection Microsoft.Extensions.DependencyInjection.ResilienceServiceCollectionExtensions.ConfigureFailureResultContext<TResult>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Func<TResult, Microsoft.Extensions.Resilience.FailureResultContext> configure);",
"Stage": "Stable"
}
]
}
]
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,23 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Diagnostics.ExceptionSummarization;
using Microsoft.Extensions.Http.Diagnostics;
using Microsoft.Extensions.Options;
using Polly.Telemetry;

namespace Microsoft.Extensions.Resilience.Internal;

internal sealed class ResilienceMetricsEnricher : MeteringEnricher
{
private readonly FrozenDictionary<Type, Func<object, FailureResultContext>> _faultFactories;
private readonly IOutgoingRequestContext? _outgoingRequestContext;
private readonly IExceptionSummarizer? _exceptionSummarizer;

public ResilienceMetricsEnricher(
IOptions<FailureEventMetricsOptions> metricsOptions,
IEnumerable<IOutgoingRequestContext> outgoingRequestContext,
IOutgoingRequestContext? outgoingRequestContext = null,
IExceptionSummarizer? exceptionSummarizer = null)
{
_faultFactories = metricsOptions.Value.Factories.ToFrozenDictionary();
_outgoingRequestContext = outgoingRequestContext.FirstOrDefault();
_outgoingRequestContext = outgoingRequestContext;
_exceptionSummarizer = exceptionSummarizer;
}

Expand All @@ -34,12 +28,7 @@ public override void Enrich<TResult, TArgs>(in EnrichmentContext<TResult, TArgs>

if (_exceptionSummarizer is not null && outcome?.Exception is Exception e)
{
context.Tags.Add(new(ResilienceTagNames.ErrorType, _exceptionSummarizer.Summarize(e).ToString()));
}
else if (outcome is not null && outcome.Value.Result is object result && _faultFactories.TryGetValue(result.GetType(), out var factory))
{
var failureContext = factory(result);
context.Tags.Add(new(ResilienceTagNames.ErrorType, failureContext.AdditionalInformation));
context.Tags.Add(new(ResilienceTagNames.ErrorType, _exceptionSummarizer.Summarize(e).Description));
}

if ((context.TelemetryEvent.Context.GetRequestMetadata() ?? _outgoingRequestContext?.RequestMetadata) is RequestMetadata requestMetadata)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Microsoft.Extensions.Diagnostics.ExceptionSummarization;
using Microsoft.Extensions.Http.Diagnostics;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Resilience;
using Microsoft.Extensions.Resilience.Internal;
using Microsoft.Shared.Diagnostics;
using Polly.Telemetry;
Expand All @@ -31,9 +30,6 @@ public static class ResilienceServiceCollectionExtensions
/// Exception enrichment based on <see cref="IExceptionSummarizer"/>.</description>
/// </item>
/// <item><description>
/// Result enrichment based on <see cref="ConfigureFailureResultContext"/> and <see cref="FailureResultContext"/>.</description>
/// </item>
/// <item><description>
/// Request metadata enrichment based on <see cref="RequestMetadata"/>.</description>
/// </item>
/// </list>
Expand All @@ -57,24 +53,4 @@ public static IServiceCollection AddResilienceEnricher(this IServiceCollection s

return services;
}

/// <summary>
/// Configures the failure result dimensions.
/// </summary>
/// <typeparam name="TResult">The type of the policy result.</typeparam>
/// <param name="services">The services.</param>
/// <param name="configure">The configure result dimensions.</param>
/// <returns>The input <paramref name="services"/>.</returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="services"/> or <paramref name="configure"/> is <see langword="null"/>.
/// </exception>
public static IServiceCollection ConfigureFailureResultContext<TResult>(
this IServiceCollection services,
Func<TResult, FailureResultContext> configure)
{
_ = Throw.IfNull(services);
_ = Throw.IfNull(configure);

return services.Configure<FailureEventMetricsOptions>(options => options.ConfigureFailureResultContext(configure));
}
}
Loading