Skip to content

Commit f938ec7

Browse files
author
Scott Bommarito
authored
Log search service exceptions encountered during retry (#6232)
1 parent 418bb65 commit f938ec7

5 files changed

Lines changed: 35 additions & 15 deletions

File tree

src/NuGet.Services.Search.Client/Client/RetryingHttpClientWrapper.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public sealed class RetryingHttpClientWrapper
1515
{
1616
private readonly HttpClient _httpClient;
1717
private readonly IEndpointHealthIndicatorStore _endpointHealthIndicatorStore;
18+
private readonly Action<Exception> _onException;
1819

1920
private static readonly int PeriodToDelayAlternateRequest = 3000;
2021
private static readonly IComparer<int> HealthComparer;
@@ -24,16 +25,16 @@ static RetryingHttpClientWrapper()
2425
HealthComparer = new WeightedRandomComparer();
2526
}
2627

27-
public RetryingHttpClientWrapper(HttpClient httpClient)
28-
: this (httpClient, new BaseUrlHealthIndicatorStore(new NullHealthIndicatorLogger()))
28+
public RetryingHttpClientWrapper(HttpClient httpClient, Action<Exception> onException)
29+
: this (httpClient, new BaseUrlHealthIndicatorStore(new NullHealthIndicatorLogger()), onException)
2930
{
30-
_httpClient = httpClient;
3131
}
3232

33-
public RetryingHttpClientWrapper(HttpClient httpClient, IEndpointHealthIndicatorStore endpointHealthIndicatorStore)
33+
public RetryingHttpClientWrapper(HttpClient httpClient, IEndpointHealthIndicatorStore endpointHealthIndicatorStore, Action<Exception> onException)
3434
{
35-
_httpClient = httpClient;
36-
_endpointHealthIndicatorStore = endpointHealthIndicatorStore;
35+
_httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
36+
_endpointHealthIndicatorStore = endpointHealthIndicatorStore ?? throw new ArgumentNullException(nameof(endpointHealthIndicatorStore));
37+
_onException = onException ?? throw new ArgumentNullException(nameof(onException));
3738
}
3839

3940
public async Task<string> GetStringAsync(IEnumerable<Uri> endpoints)
@@ -91,6 +92,11 @@ private async Task<HttpResponseMessage> GetWithRetry(IEnumerable<Uri> endpoints,
9192
cancellationTokenSource.Cancel(false);
9293
}
9394

95+
foreach (var exception in exceptions)
96+
{
97+
_onException(exception);
98+
}
99+
94100
if (completedTask.IsFaulted || completedTask.IsCanceled)
95101
{
96102
var exceptionsToThrow = exceptions.Where(e => !(e is TaskCanceledException || e.InnerException is TaskCanceledException)).ToList();

src/NuGet.Services.Search.Client/Client/SearchClient.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public class SearchClient
2424
/// </summary>
2525
/// <param name="baseUri">The URL to the root of the service</param>
2626
/// <param name="handlers">Handlers to apply to the request in order from first to last</param>
27-
public SearchClient(Uri baseUri, params DelegatingHandler[] handlers)
28-
: this(baseUri, "SearchGalleryQueryService/3.0.0-rc", null, new BaseUrlHealthIndicatorStore(new NullHealthIndicatorLogger()), handlers)
27+
public SearchClient(Uri baseUri, Action<Exception> onException, params DelegatingHandler[] handlers)
28+
: this(baseUri, "SearchGalleryQueryService/3.0.0-rc", null, new BaseUrlHealthIndicatorStore(new NullHealthIndicatorLogger()), onException, handlers)
2929
{
3030
}
3131

@@ -37,7 +37,7 @@ public SearchClient(Uri baseUri, params DelegatingHandler[] handlers)
3737
/// <param name="credentials">The credentials to connect to the service with</param>
3838
/// <param name="healthIndicatorStore">Health indicator store</param>
3939
/// <param name="handlers">Handlers to apply to the request in order from first to last</param>
40-
public SearchClient(Uri baseUri, string resourceType, ICredentials credentials, IEndpointHealthIndicatorStore healthIndicatorStore, params DelegatingHandler[] handlers)
40+
public SearchClient(Uri baseUri, string resourceType, ICredentials credentials, IEndpointHealthIndicatorStore healthIndicatorStore, Action<Exception> onException, params DelegatingHandler[] handlers)
4141
{
4242
_resourceType = resourceType;
4343

@@ -57,7 +57,7 @@ public SearchClient(Uri baseUri, string resourceType, ICredentials credentials,
5757

5858
_httpClient = new HttpClient(handler, disposeHandler: true);
5959

60-
_retryingHttpClientWrapper = new RetryingHttpClientWrapper(_httpClient, healthIndicatorStore);
60+
_retryingHttpClientWrapper = new RetryingHttpClientWrapper(_httpClient, healthIndicatorStore, onException);
6161
_discoveryClient = new ServiceDiscoveryClient(_httpClient, baseUri);
6262
}
6363

src/NuGetGallery/Infrastructure/Lucene/ExternalSearchService.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,14 @@ public ExternalSearchService()
5252

5353
if (_client == null)
5454
{
55-
_client = new SearchClient(ServiceUri, "SearchGalleryQueryService/3.0.0-rc", null, _healthIndicatorStore, new TracingHttpHandler(Trace), new CorrelatingHttpClientHandler());
55+
_client = new SearchClient(
56+
ServiceUri,
57+
"SearchGalleryQueryService/3.0.0-rc",
58+
null,
59+
_healthIndicatorStore,
60+
QuietLog.LogHandledException,
61+
new TracingHttpHandler(Trace),
62+
new CorrelatingHttpClientHandler());
5663
}
5764
}
5865

@@ -90,7 +97,14 @@ public ExternalSearchService(IAppConfiguration config, IDiagnosticsService diagn
9097

9198
if (_client == null)
9299
{
93-
_client = new SearchClient(ServiceUri, config.SearchServiceResourceType, credentials, _healthIndicatorStore, new TracingHttpHandler(Trace), new CorrelatingHttpClientHandler());
100+
_client = new SearchClient(
101+
ServiceUri,
102+
config.SearchServiceResourceType,
103+
credentials,
104+
_healthIndicatorStore,
105+
QuietLog.LogHandledException,
106+
new TracingHttpHandler(Trace),
107+
new CorrelatingHttpClientHandler());
94108
}
95109
}
96110

src/NuGetGallery/Queries/AutoCompleteServiceQuery.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public AutoCompleteServiceQuery(IAppConfiguration configuration)
2828

2929
_serviceDiscoveryClient = new ServiceDiscoveryClient(configuration.ServiceDiscoveryUri);
3030
_autocompleteServiceResourceType = configuration.AutocompleteServiceResourceType;
31-
_httpClient = new RetryingHttpClientWrapper(new HttpClient());
31+
_httpClient = new RetryingHttpClientWrapper(new HttpClient(), QuietLog.LogHandledException);
3232
}
3333

3434
public async Task<IEnumerable<string>> RunServiceQuery(

tests/NuGetGallery.Facts/SearchClient/RetryingHttpClientWrapperFacts.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ public class RetryingHttpClientWrapperFacts
2323

2424
private RetryingHttpClientWrapper CreateWrapperClient(HttpMessageHandler handler)
2525
{
26-
return new RetryingHttpClientWrapper(new HttpClient(handler));
26+
return new RetryingHttpClientWrapper(new HttpClient(handler), (exception) => { });
2727
}
2828

2929
private RetryingHttpClientWrapper CreateWrapperClient()
3030
{
31-
return new RetryingHttpClientWrapper(new HttpClient());
31+
return new RetryingHttpClientWrapper(new HttpClient(), (exception) => { });
3232
}
3333

3434
[Fact]

0 commit comments

Comments
 (0)