Skip to content

Commit

Permalink
Merge branch 'main' into logs-scope-buffering-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
cijothomas authored Oct 5, 2022
2 parents 88cd258 + d42f171 commit 0ab4d51
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// </copyright>
#if !NETFRAMEWORK
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
Expand Down Expand Up @@ -300,30 +301,36 @@ public async Task HttpClientInstrumentation_AddViaFactory_HttpInstrumentation_Co
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]
Expand Down
52 changes: 52 additions & 0 deletions test/OpenTelemetry.Instrumentation.Http.Tests/RetryHandler.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

0 comments on commit 0ab4d51

Please sign in to comment.