[HttpClient] Unit test for retries (#3730)

This commit is contained in:
Vishwesh Bankwar 2022-10-05 12:49:00 -07:00 committed by GitHub
parent e826f9d233
commit d42f171d13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 74 additions and 15 deletions

View File

@ -15,6 +15,7 @@
// </copyright>
#if !NETFRAMEWORK
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
@ -300,30 +301,36 @@ namespace OpenTelemetry.Instrumentation.Http.Tests
Assert.IsType<Activity>(processor.Invocations[1].Arguments[0]);
}
[Fact]
public async Task HttpClientInstrumentationBacksOffIfAlreadyInstrumented()
[Fact(Skip = "https://github.com/open-telemetry/opentelemetry-dotnet/issues/3729")]
public async Task HttpClientInstrumentationExportsSpansCreatedForRetries()
{
// TODO: Investigate why this feature is required.
var processor = new Mock<BaseProcessor<Activity>>();
var exportedItems = new List<Activity>();
var request = new HttpRequestMessage
{
RequestUri = new Uri(this.url),
Method = new HttpMethod("GET"),
};
request.Headers.Add("traceparent", "00-0123456789abcdef0123456789abcdef-0123456789abcdef-01");
using (Sdk.CreateTracerProviderBuilder()
using var traceprovider = Sdk.CreateTracerProviderBuilder()
.AddHttpClientInstrumentation()
.AddProcessor(processor.Object)
.Build())
{
using var c = new HttpClient();
await c.SendAsync(request);
}
.AddInMemoryExporter(exportedItems)
.Build();
Assert.Equal(4, processor.Invocations.Count); // SetParentProvider/OnShutdown/Dispose/OnStart called.
int maxRetries = 3;
using var c = new HttpClient(new RetryHandler(new HttpClientHandler(), maxRetries));
await c.SendAsync(request);
// number of exported spans should be 3(maxRetries)
Assert.Equal(3, exportedItems.Count());
var spanid1 = exportedItems[0].SpanId;
var spanid2 = exportedItems[1].SpanId;
var spanid3 = exportedItems[2].SpanId;
// Validate span ids are different
Assert.NotEqual(spanid1, spanid2);
Assert.NotEqual(spanid3, spanid1);
Assert.NotEqual(spanid2, spanid3);
}
[Fact]

View File

@ -0,0 +1,52 @@
// <copyright file="RetryHandler.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace OpenTelemetry.Tests
{
public class RetryHandler : DelegatingHandler
{
private int maxRetries;
public RetryHandler(HttpMessageHandler innerHandler, int maxRetries)
: base(innerHandler)
{
this.maxRetries = maxRetries;
}
protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken)
{
HttpResponseMessage response = null;
for (int i = 0; i < this.maxRetries; i++)
{
try
{
response = await base.SendAsync(request, cancellationToken);
}
catch
{
}
}
return response;
}
}
}