From 1c5b0d758ef2b1b7fe4cdc5b070f659abcecbcb0 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Wed, 12 Oct 2022 14:48:54 -0700 Subject: [PATCH 01/10] add .net core tests --- .../HttpClientTests.Basic.cs | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.cs b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.cs index 8d7581e49fe..fc96fd78032 100644 --- a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.cs +++ b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.cs @@ -45,6 +45,10 @@ public HttpClientTests() ctx.Response.RedirectLocation = "/"; ctx.Response.StatusCode = 302; } + else if (ctx.Request.Url.PathAndQuery.Contains("500")) + { + ctx.Response.StatusCode = 500; + } else { ctx.Response.StatusCode = 200; @@ -462,6 +466,88 @@ public async Task HttpClientInstrumentationContextPropagation() Assert.Equal("b1=v1", baggages.Single()); } + [Fact] + public async Task HttpClientInstrumentationReportsExceptionEventForNetworkFailuresWithClientGetAsync() + { + var exportedItems = new List(); + var request = new HttpRequestMessage + { + RequestUri = new Uri("https://www.invalidurl.com"), + Method = new HttpMethod("GET"), + }; + + using var traceprovider = Sdk.CreateTracerProviderBuilder() + .AddHttpClientInstrumentation(o => o.RecordException = true) + .AddInMemoryExporter(exportedItems) + .Build(); + + using var c = new HttpClient(); + try + { + await c.SendAsync(request); + } + catch + { + } + + Assert.Single(exportedItems[0].Events.Where(evt => evt.Name.Equals("exception"))); + } + + [Fact] + public async Task HttpClientInstrumentationDoesNotReportsExceptionEventOnErrorResponseWithClientGetAsync() + { + var exportedItems = new List(); + bool exceptionThrown = false; + + using var traceprovider = Sdk.CreateTracerProviderBuilder() + .AddHttpClientInstrumentation(o => o.RecordException = true) + .AddInMemoryExporter(exportedItems) + .Build(); + + using var c = new HttpClient(); + try + { + await c.GetAsync($"{this.url}500"); + } + catch + { + exceptionThrown = true; + } + + Assert.False(exceptionThrown); + Assert.Empty(exportedItems[0].Events); + } + + [Fact] + public async Task HttpClientInstrumentationReportsDoesNotReportExceptionEventOnErrorResponseWithClientGetStringAsync() + { + var exportedItems = new List(); + bool exceptionThrown = false; + var request = new HttpRequestMessage + { + RequestUri = new Uri($"{this.url}500"), + Method = new HttpMethod("GET"), + }; + + using var traceprovider = Sdk.CreateTracerProviderBuilder() + .AddHttpClientInstrumentation(o => o.RecordException = true) + .AddInMemoryExporter(exportedItems) + .Build(); + + using var c = new HttpClient(); + try + { + await c.GetStringAsync($"{this.url}500"); + } + catch + { + exceptionThrown = true; + } + + Assert.True(exceptionThrown); + Assert.Empty(exportedItems[0].Events); + } + public void Dispose() { this.serverLifeTime?.Dispose(); From dfac2f926bfc339151082827807ded9fdd817677 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Wed, 12 Oct 2022 21:18:51 -0700 Subject: [PATCH 02/10] add comment --- .../HttpClientTests.Basic.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.cs b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.cs index fc96fd78032..4adfc0537e9 100644 --- a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.cs +++ b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.cs @@ -470,6 +470,7 @@ public async Task HttpClientInstrumentationContextPropagation() public async Task HttpClientInstrumentationReportsExceptionEventForNetworkFailuresWithClientGetAsync() { var exportedItems = new List(); + bool exceptionThrown = false; var request = new HttpRequestMessage { RequestUri = new Uri("https://www.invalidurl.com"), @@ -488,8 +489,11 @@ public async Task HttpClientInstrumentationReportsExceptionEventForNetworkFailur } catch { + exceptionThrown = true; } + // Exception is thrown and collected as event + Assert.True(exceptionThrown); Assert.Single(exportedItems[0].Events.Where(evt => evt.Name.Equals("exception"))); } @@ -514,6 +518,7 @@ public async Task HttpClientInstrumentationDoesNotReportsExceptionEventOnErrorRe exceptionThrown = true; } + // Exception is not thrown and not collected as event Assert.False(exceptionThrown); Assert.Empty(exportedItems[0].Events); } @@ -544,6 +549,7 @@ public async Task HttpClientInstrumentationReportsDoesNotReportExceptionEventOnE exceptionThrown = true; } + // Exception is thrown and not collected as event Assert.True(exceptionThrown); Assert.Empty(exportedItems[0].Events); } From 9b2cc131f37b5b5a8781ae705a02751086f2d5d1 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Wed, 12 Oct 2022 21:57:59 -0700 Subject: [PATCH 03/10] move tests to new class --- .../HttpClientTests.Basic.cs | 92 ----------- .../HttpWebRequestTests.netfx.cs | 15 +- .../RecordExceptionTests.cs | 151 ++++++++++++++++++ 3 files changed, 164 insertions(+), 94 deletions(-) create mode 100644 test/OpenTelemetry.Instrumentation.Http.Tests/RecordExceptionTests.cs diff --git a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.cs b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.cs index 4adfc0537e9..8d7581e49fe 100644 --- a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.cs +++ b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.cs @@ -45,10 +45,6 @@ public HttpClientTests() ctx.Response.RedirectLocation = "/"; ctx.Response.StatusCode = 302; } - else if (ctx.Request.Url.PathAndQuery.Contains("500")) - { - ctx.Response.StatusCode = 500; - } else { ctx.Response.StatusCode = 200; @@ -466,94 +462,6 @@ public async Task HttpClientInstrumentationContextPropagation() Assert.Equal("b1=v1", baggages.Single()); } - [Fact] - public async Task HttpClientInstrumentationReportsExceptionEventForNetworkFailuresWithClientGetAsync() - { - var exportedItems = new List(); - bool exceptionThrown = false; - var request = new HttpRequestMessage - { - RequestUri = new Uri("https://www.invalidurl.com"), - Method = new HttpMethod("GET"), - }; - - using var traceprovider = Sdk.CreateTracerProviderBuilder() - .AddHttpClientInstrumentation(o => o.RecordException = true) - .AddInMemoryExporter(exportedItems) - .Build(); - - using var c = new HttpClient(); - try - { - await c.SendAsync(request); - } - catch - { - exceptionThrown = true; - } - - // Exception is thrown and collected as event - Assert.True(exceptionThrown); - Assert.Single(exportedItems[0].Events.Where(evt => evt.Name.Equals("exception"))); - } - - [Fact] - public async Task HttpClientInstrumentationDoesNotReportsExceptionEventOnErrorResponseWithClientGetAsync() - { - var exportedItems = new List(); - bool exceptionThrown = false; - - using var traceprovider = Sdk.CreateTracerProviderBuilder() - .AddHttpClientInstrumentation(o => o.RecordException = true) - .AddInMemoryExporter(exportedItems) - .Build(); - - using var c = new HttpClient(); - try - { - await c.GetAsync($"{this.url}500"); - } - catch - { - exceptionThrown = true; - } - - // Exception is not thrown and not collected as event - Assert.False(exceptionThrown); - Assert.Empty(exportedItems[0].Events); - } - - [Fact] - public async Task HttpClientInstrumentationReportsDoesNotReportExceptionEventOnErrorResponseWithClientGetStringAsync() - { - var exportedItems = new List(); - bool exceptionThrown = false; - var request = new HttpRequestMessage - { - RequestUri = new Uri($"{this.url}500"), - Method = new HttpMethod("GET"), - }; - - using var traceprovider = Sdk.CreateTracerProviderBuilder() - .AddHttpClientInstrumentation(o => o.RecordException = true) - .AddInMemoryExporter(exportedItems) - .Build(); - - using var c = new HttpClient(); - try - { - await c.GetStringAsync($"{this.url}500"); - } - catch - { - exceptionThrown = true; - } - - // Exception is thrown and not collected as event - Assert.True(exceptionThrown); - Assert.Empty(exportedItems[0].Events); - } - public void Dispose() { this.serverLifeTime?.Dispose(); diff --git a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.netfx.cs b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.netfx.cs index a31ddbaaf43..981c8282e65 100644 --- a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.netfx.cs +++ b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.netfx.cs @@ -39,8 +39,19 @@ public void HttpOutCallsAreCollectedSuccessfully(HttpTestData.HttpOutTestCase tc using var serverLifeTime = TestHttpServer.RunServer( (ctx) => { - ctx.Response.StatusCode = tc.ResponseCode == 0 ? 200 : tc.ResponseCode; - ctx.Response.OutputStream.Close(); + if (ctx.Request.Url.PathAndQuery.Contains("redirect")) + { + ctx.Response.RedirectLocation = "/"; + ctx.Response.StatusCode = 302; + } + else if (ctx.Request.Url.PathAndQuery.Contains("500")) + { + ctx.Response.StatusCode = 500; + } + else + { + ctx.Response.StatusCode = 200; + } }, out var host, out var port); diff --git a/test/OpenTelemetry.Instrumentation.Http.Tests/RecordExceptionTests.cs b/test/OpenTelemetry.Instrumentation.Http.Tests/RecordExceptionTests.cs new file mode 100644 index 00000000000..9992ad64d25 --- /dev/null +++ b/test/OpenTelemetry.Instrumentation.Http.Tests/RecordExceptionTests.cs @@ -0,0 +1,151 @@ +// +// 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. +// + +using OpenTelemetry.Tests; +using OpenTelemetry.Trace; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Net.Http; +using System.Threading.Tasks; +using Xunit; + +namespace OpenTelemetry.Instrumentation.Http.Tests +{ + public class RecordExceptionTests : IDisposable + { + private readonly IDisposable serverLifeTime; + private readonly string url; + + public RecordExceptionTests() + { + this.serverLifeTime = TestHttpServer.RunServer( + (ctx) => + { + if (ctx.Request.Url.PathAndQuery.Contains("500")) + { + ctx.Response.StatusCode = 500; + } + else + { + ctx.Response.StatusCode = 200; + } + + ctx.Response.OutputStream.Close(); + }, + out var host, + out var port); + + this.url = $"http://{host}:{port}/"; + } + + [Fact] + public async Task HttpClientInstrumentationReportsExceptionEventForNetworkFailuresWithClientGetAsync() + { + var exportedItems = new List(); + bool exceptionThrown = false; + var request = new HttpRequestMessage + { + RequestUri = new Uri("https://www.invalidurl.com"), + Method = new HttpMethod("GET"), + }; + + using var traceprovider = Sdk.CreateTracerProviderBuilder() + .AddHttpClientInstrumentation(o => o.RecordException = true) + .AddInMemoryExporter(exportedItems) + .Build(); + + using var c = new HttpClient(); + try + { + await c.SendAsync(request); + } + catch + { + exceptionThrown = true; + } + + // Exception is thrown and collected as event + Assert.True(exceptionThrown); + Assert.Single(exportedItems[0].Events.Where(evt => evt.Name.Equals("exception"))); + } + + [Fact] + public async Task HttpClientInstrumentationDoesNotReportsExceptionEventOnErrorResponseWithClientGetAsync() + { + var exportedItems = new List(); + bool exceptionThrown = false; + + using var traceprovider = Sdk.CreateTracerProviderBuilder() + .AddHttpClientInstrumentation(o => o.RecordException = true) + .AddInMemoryExporter(exportedItems) + .Build(); + + using var c = new HttpClient(); + try + { + await c.GetAsync($"{this.url}500"); + } + catch + { + exceptionThrown = true; + } + + // Exception is not thrown and not collected as event + Assert.False(exceptionThrown); + Assert.Empty(exportedItems[0].Events); + } + + [Fact] + public async Task HttpClientInstrumentationReportsDoesNotReportExceptionEventOnErrorResponseWithClientGetStringAsync() + { + var exportedItems = new List(); + bool exceptionThrown = false; + var request = new HttpRequestMessage + { + RequestUri = new Uri($"{this.url}500"), + Method = new HttpMethod("GET"), + }; + + using var traceprovider = Sdk.CreateTracerProviderBuilder() + .AddHttpClientInstrumentation(o => o.RecordException = true) + .AddInMemoryExporter(exportedItems) + .Build(); + + using var c = new HttpClient(); + try + { + await c.GetStringAsync($"{this.url}500"); + } + catch + { + exceptionThrown = true; + } + + // Exception is thrown and not collected as event + Assert.True(exceptionThrown); + Assert.Empty(exportedItems[0].Events); + } + + public void Dispose() + { + this.serverLifeTime?.Dispose(); + Activity.Current = null; + GC.SuppressFinalize(this); + } + } +} From f3da47988cdc4a4af50d7ebab72ee92345936d85 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Wed, 12 Oct 2022 22:00:22 -0700 Subject: [PATCH 04/10] revert --- .../HttpWebRequestTests.netfx.cs | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.netfx.cs b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.netfx.cs index 981c8282e65..a31ddbaaf43 100644 --- a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.netfx.cs +++ b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.netfx.cs @@ -39,19 +39,8 @@ public void HttpOutCallsAreCollectedSuccessfully(HttpTestData.HttpOutTestCase tc using var serverLifeTime = TestHttpServer.RunServer( (ctx) => { - if (ctx.Request.Url.PathAndQuery.Contains("redirect")) - { - ctx.Response.RedirectLocation = "/"; - ctx.Response.StatusCode = 302; - } - else if (ctx.Request.Url.PathAndQuery.Contains("500")) - { - ctx.Response.StatusCode = 500; - } - else - { - ctx.Response.StatusCode = 200; - } + ctx.Response.StatusCode = tc.ResponseCode == 0 ? 200 : tc.ResponseCode; + ctx.Response.OutputStream.Close(); }, out var host, out var port); From 9fb08a1dad566c8d02e8148d335a34672adb02e9 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Wed, 12 Oct 2022 22:03:01 -0700 Subject: [PATCH 05/10] reorder usings --- .../RecordExceptionTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/OpenTelemetry.Instrumentation.Http.Tests/RecordExceptionTests.cs b/test/OpenTelemetry.Instrumentation.Http.Tests/RecordExceptionTests.cs index 9992ad64d25..b9385bc2a8b 100644 --- a/test/OpenTelemetry.Instrumentation.Http.Tests/RecordExceptionTests.cs +++ b/test/OpenTelemetry.Instrumentation.Http.Tests/RecordExceptionTests.cs @@ -14,14 +14,14 @@ // limitations under the License. // -using OpenTelemetry.Tests; -using OpenTelemetry.Trace; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Net.Http; using System.Threading.Tasks; +using OpenTelemetry.Tests; +using OpenTelemetry.Trace; using Xunit; namespace OpenTelemetry.Instrumentation.Http.Tests From 526da0b13e4e3fae267b0a9fe356f8e094ad6ab5 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Wed, 12 Oct 2022 22:14:14 -0700 Subject: [PATCH 06/10] add cases separately --- .../HttpClientTests.Basic.cs | 94 ++++++++++- .../HttpWebRequestTests.Basic.netfx.cs | 99 +++++++++++- .../RecordExceptionTests.cs | 151 ------------------ 3 files changed, 191 insertions(+), 153 deletions(-) delete mode 100644 test/OpenTelemetry.Instrumentation.Http.Tests/RecordExceptionTests.cs diff --git a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.cs b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.cs index 8d7581e49fe..dcf8bf38dc6 100644 --- a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.cs +++ b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.cs @@ -40,7 +40,11 @@ public HttpClientTests() this.serverLifeTime = TestHttpServer.RunServer( (ctx) => { - if (ctx.Request.Url.PathAndQuery.Contains("redirect")) + if (ctx.Request.Url.PathAndQuery.Contains("500")) + { + ctx.Response.StatusCode = 500; + } + else if (ctx.Request.Url.PathAndQuery.Contains("redirect")) { ctx.Response.RedirectLocation = "/"; ctx.Response.StatusCode = 302; @@ -462,6 +466,94 @@ public async Task HttpClientInstrumentationContextPropagation() Assert.Equal("b1=v1", baggages.Single()); } + [Fact] + public async Task HttpClientInstrumentationReportsExceptionEventForNetworkFailuresWithClientGetAsync() + { + var exportedItems = new List(); + bool exceptionThrown = false; + var request = new HttpRequestMessage + { + RequestUri = new Uri("https://www.invalidurl.com"), + Method = new HttpMethod("GET"), + }; + + using var traceprovider = Sdk.CreateTracerProviderBuilder() + .AddHttpClientInstrumentation(o => o.RecordException = true) + .AddInMemoryExporter(exportedItems) + .Build(); + + using var c = new HttpClient(); + try + { + await c.SendAsync(request); + } + catch + { + exceptionThrown = true; + } + + // Exception is thrown and collected as event + Assert.True(exceptionThrown); + Assert.Single(exportedItems[0].Events.Where(evt => evt.Name.Equals("exception"))); + } + + [Fact] + public async Task HttpClientInstrumentationDoesNotReportsExceptionEventOnErrorResponseWithClientGetAsync() + { + var exportedItems = new List(); + bool exceptionThrown = false; + + using var traceprovider = Sdk.CreateTracerProviderBuilder() + .AddHttpClientInstrumentation(o => o.RecordException = true) + .AddInMemoryExporter(exportedItems) + .Build(); + + using var c = new HttpClient(); + try + { + await c.GetAsync($"{this.url}500"); + } + catch + { + exceptionThrown = true; + } + + // Exception is not thrown and not collected as event + Assert.False(exceptionThrown); + Assert.Empty(exportedItems[0].Events); + } + + [Fact] + public async Task HttpClientInstrumentationReportsDoesNotReportExceptionEventOnErrorResponseWithClientGetStringAsync() + { + var exportedItems = new List(); + bool exceptionThrown = false; + var request = new HttpRequestMessage + { + RequestUri = new Uri($"{this.url}500"), + Method = new HttpMethod("GET"), + }; + + using var traceprovider = Sdk.CreateTracerProviderBuilder() + .AddHttpClientInstrumentation(o => o.RecordException = true) + .AddInMemoryExporter(exportedItems) + .Build(); + + using var c = new HttpClient(); + try + { + await c.GetStringAsync($"{this.url}500"); + } + catch + { + exceptionThrown = true; + } + + // Exception is thrown and not collected as event + Assert.True(exceptionThrown); + Assert.Empty(exportedItems[0].Events); + } + public void Dispose() { this.serverLifeTime?.Dispose(); diff --git a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.Basic.netfx.cs b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.Basic.netfx.cs index 382a1016343..b0e89e75f57 100644 --- a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.Basic.netfx.cs +++ b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.Basic.netfx.cs @@ -15,6 +15,7 @@ // #if NETFRAMEWORK using System; +using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Net; @@ -45,7 +46,15 @@ public HttpWebRequestTests() this.serverLifeTime = TestHttpServer.RunServer( (ctx) => { - ctx.Response.StatusCode = 200; + if (ctx.Request.Url.PathAndQuery.Contains("500")) + { + ctx.Response.StatusCode = 500; + } + else + { + ctx.Response.StatusCode = 200; + } + ctx.Response.OutputStream.Close(); }, out var host, @@ -54,6 +63,94 @@ public HttpWebRequestTests() this.url = $"http://{host}:{port}/"; } + [Fact] + public async Task HttpClientInstrumentationReportsExceptionEventForNetworkFailuresWithClientGetAsync() + { + var exportedItems = new List(); + bool exceptionThrown = false; + var request = new HttpRequestMessage + { + RequestUri = new Uri("https://www.invalidurl.com"), + Method = new HttpMethod("GET"), + }; + + using var traceprovider = Sdk.CreateTracerProviderBuilder() + .AddHttpClientInstrumentation(o => o.RecordException = true) + .AddInMemoryExporter(exportedItems) + .Build(); + + using var c = new HttpClient(); + try + { + await c.SendAsync(request); + } + catch + { + exceptionThrown = true; + } + + // Exception is thrown and collected as event + Assert.True(exceptionThrown); + Assert.Single(exportedItems[0].Events.Where(evt => evt.Name.Equals("exception"))); + } + + [Fact] + public async Task HttpClientInstrumentationDoesNotReportsExceptionEventOnErrorResponseWithClientGetAsync() + { + var exportedItems = new List(); + bool exceptionThrown = false; + + using var traceprovider = Sdk.CreateTracerProviderBuilder() + .AddHttpClientInstrumentation(o => o.RecordException = true) + .AddInMemoryExporter(exportedItems) + .Build(); + + using var c = new HttpClient(); + try + { + await c.GetAsync($"{this.url}500"); + } + catch + { + exceptionThrown = true; + } + + // Exception is not thrown and not collected as event + Assert.False(exceptionThrown); + Assert.Empty(exportedItems[0].Events); + } + + [Fact] + public async Task HttpClientInstrumentationReportsDoesNotReportExceptionEventOnErrorResponseWithClientGetStringAsync() + { + var exportedItems = new List(); + bool exceptionThrown = false; + var request = new HttpRequestMessage + { + RequestUri = new Uri($"{this.url}500"), + Method = new HttpMethod("GET"), + }; + + using var traceprovider = Sdk.CreateTracerProviderBuilder() + .AddHttpClientInstrumentation(o => o.RecordException = true) + .AddInMemoryExporter(exportedItems) + .Build(); + + using var c = new HttpClient(); + try + { + await c.GetStringAsync($"{this.url}500"); + } + catch + { + exceptionThrown = true; + } + + // Exception is thrown and not collected as event + Assert.True(exceptionThrown); + Assert.Empty(exportedItems[0].Events); + } + public void Dispose() { this.serverLifeTime?.Dispose(); diff --git a/test/OpenTelemetry.Instrumentation.Http.Tests/RecordExceptionTests.cs b/test/OpenTelemetry.Instrumentation.Http.Tests/RecordExceptionTests.cs deleted file mode 100644 index b9385bc2a8b..00000000000 --- a/test/OpenTelemetry.Instrumentation.Http.Tests/RecordExceptionTests.cs +++ /dev/null @@ -1,151 +0,0 @@ -// -// 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. -// - -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Net.Http; -using System.Threading.Tasks; -using OpenTelemetry.Tests; -using OpenTelemetry.Trace; -using Xunit; - -namespace OpenTelemetry.Instrumentation.Http.Tests -{ - public class RecordExceptionTests : IDisposable - { - private readonly IDisposable serverLifeTime; - private readonly string url; - - public RecordExceptionTests() - { - this.serverLifeTime = TestHttpServer.RunServer( - (ctx) => - { - if (ctx.Request.Url.PathAndQuery.Contains("500")) - { - ctx.Response.StatusCode = 500; - } - else - { - ctx.Response.StatusCode = 200; - } - - ctx.Response.OutputStream.Close(); - }, - out var host, - out var port); - - this.url = $"http://{host}:{port}/"; - } - - [Fact] - public async Task HttpClientInstrumentationReportsExceptionEventForNetworkFailuresWithClientGetAsync() - { - var exportedItems = new List(); - bool exceptionThrown = false; - var request = new HttpRequestMessage - { - RequestUri = new Uri("https://www.invalidurl.com"), - Method = new HttpMethod("GET"), - }; - - using var traceprovider = Sdk.CreateTracerProviderBuilder() - .AddHttpClientInstrumentation(o => o.RecordException = true) - .AddInMemoryExporter(exportedItems) - .Build(); - - using var c = new HttpClient(); - try - { - await c.SendAsync(request); - } - catch - { - exceptionThrown = true; - } - - // Exception is thrown and collected as event - Assert.True(exceptionThrown); - Assert.Single(exportedItems[0].Events.Where(evt => evt.Name.Equals("exception"))); - } - - [Fact] - public async Task HttpClientInstrumentationDoesNotReportsExceptionEventOnErrorResponseWithClientGetAsync() - { - var exportedItems = new List(); - bool exceptionThrown = false; - - using var traceprovider = Sdk.CreateTracerProviderBuilder() - .AddHttpClientInstrumentation(o => o.RecordException = true) - .AddInMemoryExporter(exportedItems) - .Build(); - - using var c = new HttpClient(); - try - { - await c.GetAsync($"{this.url}500"); - } - catch - { - exceptionThrown = true; - } - - // Exception is not thrown and not collected as event - Assert.False(exceptionThrown); - Assert.Empty(exportedItems[0].Events); - } - - [Fact] - public async Task HttpClientInstrumentationReportsDoesNotReportExceptionEventOnErrorResponseWithClientGetStringAsync() - { - var exportedItems = new List(); - bool exceptionThrown = false; - var request = new HttpRequestMessage - { - RequestUri = new Uri($"{this.url}500"), - Method = new HttpMethod("GET"), - }; - - using var traceprovider = Sdk.CreateTracerProviderBuilder() - .AddHttpClientInstrumentation(o => o.RecordException = true) - .AddInMemoryExporter(exportedItems) - .Build(); - - using var c = new HttpClient(); - try - { - await c.GetStringAsync($"{this.url}500"); - } - catch - { - exceptionThrown = true; - } - - // Exception is thrown and not collected as event - Assert.True(exceptionThrown); - Assert.Empty(exportedItems[0].Events); - } - - public void Dispose() - { - this.serverLifeTime?.Dispose(); - Activity.Current = null; - GC.SuppressFinalize(this); - } - } -} From 8659545f308754649c8a1d09a8155de5e1cc8f5d Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Wed, 12 Oct 2022 22:35:23 -0700 Subject: [PATCH 07/10] add webrequest tests --- .../HttpWebRequestTests.Basic.netfx.cs | 234 +++++++++++------- 1 file changed, 146 insertions(+), 88 deletions(-) diff --git a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.Basic.netfx.cs b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.Basic.netfx.cs index b0e89e75f57..8209826fd0a 100644 --- a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.Basic.netfx.cs +++ b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.Basic.netfx.cs @@ -63,94 +63,6 @@ public HttpWebRequestTests() this.url = $"http://{host}:{port}/"; } - [Fact] - public async Task HttpClientInstrumentationReportsExceptionEventForNetworkFailuresWithClientGetAsync() - { - var exportedItems = new List(); - bool exceptionThrown = false; - var request = new HttpRequestMessage - { - RequestUri = new Uri("https://www.invalidurl.com"), - Method = new HttpMethod("GET"), - }; - - using var traceprovider = Sdk.CreateTracerProviderBuilder() - .AddHttpClientInstrumentation(o => o.RecordException = true) - .AddInMemoryExporter(exportedItems) - .Build(); - - using var c = new HttpClient(); - try - { - await c.SendAsync(request); - } - catch - { - exceptionThrown = true; - } - - // Exception is thrown and collected as event - Assert.True(exceptionThrown); - Assert.Single(exportedItems[0].Events.Where(evt => evt.Name.Equals("exception"))); - } - - [Fact] - public async Task HttpClientInstrumentationDoesNotReportsExceptionEventOnErrorResponseWithClientGetAsync() - { - var exportedItems = new List(); - bool exceptionThrown = false; - - using var traceprovider = Sdk.CreateTracerProviderBuilder() - .AddHttpClientInstrumentation(o => o.RecordException = true) - .AddInMemoryExporter(exportedItems) - .Build(); - - using var c = new HttpClient(); - try - { - await c.GetAsync($"{this.url}500"); - } - catch - { - exceptionThrown = true; - } - - // Exception is not thrown and not collected as event - Assert.False(exceptionThrown); - Assert.Empty(exportedItems[0].Events); - } - - [Fact] - public async Task HttpClientInstrumentationReportsDoesNotReportExceptionEventOnErrorResponseWithClientGetStringAsync() - { - var exportedItems = new List(); - bool exceptionThrown = false; - var request = new HttpRequestMessage - { - RequestUri = new Uri($"{this.url}500"), - Method = new HttpMethod("GET"), - }; - - using var traceprovider = Sdk.CreateTracerProviderBuilder() - .AddHttpClientInstrumentation(o => o.RecordException = true) - .AddInMemoryExporter(exportedItems) - .Build(); - - using var c = new HttpClient(); - try - { - await c.GetStringAsync($"{this.url}500"); - } - catch - { - exceptionThrown = true; - } - - // Exception is thrown and not collected as event - Assert.True(exceptionThrown); - Assert.Empty(exportedItems[0].Events); - } - public void Dispose() { this.serverLifeTime?.Dispose(); @@ -393,6 +305,152 @@ public async Task HttpWebRequestInstrumentationOnExistingInstance() Assert.Equal(3, activityProcessor.Invocations.Count); // SetParentProvider/Begin/End called } + + [Fact] + public async Task HttpClientInstrumentationReportsExceptionEventForNetworkFailuresWithClientGetAsync() + { + var exportedItems = new List(); + bool exceptionThrown = false; + var request = new HttpRequestMessage + { + RequestUri = new Uri("https://www.invalidurl.com"), + Method = new HttpMethod("GET"), + }; + + using var traceprovider = Sdk.CreateTracerProviderBuilder() + .AddHttpClientInstrumentation(o => o.RecordException = true) + .AddInMemoryExporter(exportedItems) + .Build(); + + using var c = new HttpClient(); + try + { + await c.SendAsync(request); + } + catch + { + exceptionThrown = true; + } + + // Exception is thrown and collected as event + Assert.True(exceptionThrown); + Assert.Single(exportedItems[0].Events.Where(evt => evt.Name.Equals("exception"))); + } + + [Fact] + public async Task HttpClientInstrumentationDoesNotReportsExceptionEventOnErrorResponseWithClientGetAsync() + { + var exportedItems = new List(); + bool exceptionThrown = false; + + using var traceprovider = Sdk.CreateTracerProviderBuilder() + .AddHttpClientInstrumentation(o => o.RecordException = true) + .AddInMemoryExporter(exportedItems) + .Build(); + + using var c = new HttpClient(); + try + { + await c.GetAsync($"{this.url}500"); + } + catch + { + exceptionThrown = true; + } + + // Exception is not thrown and not collected as event + Assert.False(exceptionThrown); + Assert.Empty(exportedItems[0].Events); + } + + [Fact] + public async Task HttpClientInstrumentationReportsDoesNotReportExceptionEventOnErrorResponseWithClientGetStringAsync() + { + var exportedItems = new List(); + bool exceptionThrown = false; + var request = new HttpRequestMessage + { + RequestUri = new Uri($"{this.url}500"), + Method = new HttpMethod("GET"), + }; + + using var traceprovider = Sdk.CreateTracerProviderBuilder() + .AddHttpClientInstrumentation(o => o.RecordException = true) + .AddInMemoryExporter(exportedItems) + .Build(); + + using var c = new HttpClient(); + try + { + await c.GetStringAsync($"{this.url}500"); + } + catch + { + exceptionThrown = true; + } + + // Exception is thrown and not collected as event + Assert.True(exceptionThrown); + Assert.Empty(exportedItems[0].Events); + } + + [Fact] + public async Task HttpWebRequestInstrumentationReportsExceptionEventForNetworkFailures() + { + var exportedItems = new List(); + bool exceptionThrown = false; + + using var traceprovider = Sdk.CreateTracerProviderBuilder() + .AddHttpClientInstrumentation(o => o.RecordException = true) + .AddInMemoryExporter(exportedItems) + .Build(); + + try + { + var request = (HttpWebRequest)WebRequest.Create("https://www.invalidurl.com"); + + request.Method = "GET"; + + using var response = await request.GetResponseAsync(); + } + catch + { + exceptionThrown = true; + } + + // Exception is thrown and collected as event + Assert.True(exceptionThrown); + Assert.Single(exportedItems[0].Events.Where(evt => evt.Name.Equals("exception"))); + } + + [Fact] + public async Task HttpWebRequestInstrumentationDoesNotReportExceptionEventOnErrorResponse() + { + var exportedItems = new List(); + bool exceptionThrown = false; + + using var traceprovider = Sdk.CreateTracerProviderBuilder() + .AddHttpClientInstrumentation(o => o.RecordException = true) + .AddInMemoryExporter(exportedItems) + .Build(); + + try + { + var request = (HttpWebRequest)WebRequest.Create($"{this.url}500"); + + request.Method = "GET"; + + using var response = await request.GetResponseAsync(); + } + catch + { + exceptionThrown = true; + } + + // Exception is thrown and collected as event + Assert.True(exceptionThrown); + Assert.Single(exportedItems[0].Events.Where(evt => evt.Name.Equals("exception"))); + } } } #endif From d3185e0bb766b07344a62cd3a1ffc5e585432403 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Thu, 13 Oct 2022 09:16:05 -0700 Subject: [PATCH 08/10] rename --- .../HttpClientTests.Basic.cs | 13 ++++--------- .../HttpWebRequestTests.Basic.netfx.cs | 15 +++++---------- 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.cs b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.cs index dcf8bf38dc6..1205763af2b 100644 --- a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.cs +++ b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.cs @@ -467,15 +467,10 @@ public async Task HttpClientInstrumentationContextPropagation() } [Fact] - public async Task HttpClientInstrumentationReportsExceptionEventForNetworkFailuresWithClientGetAsync() + public async Task HttpClientInstrumentationReportsExceptionEventForNetworkFailuresWithGetAsync() { var exportedItems = new List(); bool exceptionThrown = false; - var request = new HttpRequestMessage - { - RequestUri = new Uri("https://www.invalidurl.com"), - Method = new HttpMethod("GET"), - }; using var traceprovider = Sdk.CreateTracerProviderBuilder() .AddHttpClientInstrumentation(o => o.RecordException = true) @@ -485,7 +480,7 @@ public async Task HttpClientInstrumentationReportsExceptionEventForNetworkFailur using var c = new HttpClient(); try { - await c.SendAsync(request); + await c.GetAsync("https://www.invalidurl.com"); } catch { @@ -498,7 +493,7 @@ public async Task HttpClientInstrumentationReportsExceptionEventForNetworkFailur } [Fact] - public async Task HttpClientInstrumentationDoesNotReportsExceptionEventOnErrorResponseWithClientGetAsync() + public async Task HttpClientInstrumentationDoesNotReportExceptionEventOnErrorResponseWithGetAsync() { var exportedItems = new List(); bool exceptionThrown = false; @@ -524,7 +519,7 @@ public async Task HttpClientInstrumentationDoesNotReportsExceptionEventOnErrorRe } [Fact] - public async Task HttpClientInstrumentationReportsDoesNotReportExceptionEventOnErrorResponseWithClientGetStringAsync() + public async Task HttpClientInstrumentationDoesNotReportExceptionEventOnErrorResponseWithGetStringAsync() { var exportedItems = new List(); bool exceptionThrown = false; diff --git a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.Basic.netfx.cs b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.Basic.netfx.cs index 8209826fd0a..ff7fc0e0795 100644 --- a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.Basic.netfx.cs +++ b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.Basic.netfx.cs @@ -307,15 +307,10 @@ public async Task HttpWebRequestInstrumentationOnExistingInstance() } [Fact] - public async Task HttpClientInstrumentationReportsExceptionEventForNetworkFailuresWithClientGetAsync() + public async Task HttpClientInstrumentationReportsExceptionEventForNetworkFailuresWithGetAsync() { var exportedItems = new List(); bool exceptionThrown = false; - var request = new HttpRequestMessage - { - RequestUri = new Uri("https://www.invalidurl.com"), - Method = new HttpMethod("GET"), - }; using var traceprovider = Sdk.CreateTracerProviderBuilder() .AddHttpClientInstrumentation(o => o.RecordException = true) @@ -325,7 +320,7 @@ public async Task HttpClientInstrumentationReportsExceptionEventForNetworkFailur using var c = new HttpClient(); try { - await c.SendAsync(request); + await c.GetAsync("https://www.invalidurl.com"); } catch { @@ -338,7 +333,7 @@ public async Task HttpClientInstrumentationReportsExceptionEventForNetworkFailur } [Fact] - public async Task HttpClientInstrumentationDoesNotReportsExceptionEventOnErrorResponseWithClientGetAsync() + public async Task HttpClientInstrumentationDoesNotReportExceptionEventOnErrorResponseWithGetAsync() { var exportedItems = new List(); bool exceptionThrown = false; @@ -364,7 +359,7 @@ public async Task HttpClientInstrumentationDoesNotReportsExceptionEventOnErrorRe } [Fact] - public async Task HttpClientInstrumentationReportsDoesNotReportExceptionEventOnErrorResponseWithClientGetStringAsync() + public async Task HttpClientInstrumentationDoesNotReportExceptionEventOnErrorResponseWithGetStringAsync() { var exportedItems = new List(); bool exceptionThrown = false; @@ -424,7 +419,7 @@ public async Task HttpWebRequestInstrumentationReportsExceptionEventForNetworkFa } [Fact] - public async Task HttpWebRequestInstrumentationDoesNotReportExceptionEventOnErrorResponse() + public async Task HttpWebRequestInstrumentationReportsExceptionEventOnErrorResponse() { var exportedItems = new List(); bool exceptionThrown = false; From 9c1bd445a57e9947d6bdf067bc4f65cbaf839043 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Thu, 13 Oct 2022 10:30:07 -0700 Subject: [PATCH 09/10] update test url --- .../HttpClientTests.Basic.cs | 2 +- .../HttpWebRequestTests.Basic.netfx.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.cs b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.cs index 1205763af2b..6b47c6b82d1 100644 --- a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.cs +++ b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.cs @@ -480,7 +480,7 @@ public async Task HttpClientInstrumentationReportsExceptionEventForNetworkFailur using var c = new HttpClient(); try { - await c.GetAsync("https://www.invalidurl.com"); + await c.GetAsync("https://10.0.0.0/invalidurl"); } catch { diff --git a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.Basic.netfx.cs b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.Basic.netfx.cs index ff7fc0e0795..cbd7b7f262e 100644 --- a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.Basic.netfx.cs +++ b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.Basic.netfx.cs @@ -320,7 +320,7 @@ public async Task HttpClientInstrumentationReportsExceptionEventForNetworkFailur using var c = new HttpClient(); try { - await c.GetAsync("https://www.invalidurl.com"); + await c.GetAsync("https://10.0.0.0/invalidurl"); } catch { @@ -402,7 +402,7 @@ public async Task HttpWebRequestInstrumentationReportsExceptionEventForNetworkFa try { - var request = (HttpWebRequest)WebRequest.Create("https://www.invalidurl.com"); + var request = (HttpWebRequest)WebRequest.Create("https://10.0.0.0/invalidurl"); request.Method = "GET"; From 254f5036c2f2b6b7bf219acd9c9d1d31fe36e24d Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Thu, 13 Oct 2022 10:56:16 -0700 Subject: [PATCH 10/10] fix url --- .../HttpClientTests.Basic.cs | 2 +- .../HttpWebRequestTests.Basic.netfx.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.cs b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.cs index 6b47c6b82d1..557b1c50312 100644 --- a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.cs +++ b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.cs @@ -480,7 +480,7 @@ public async Task HttpClientInstrumentationReportsExceptionEventForNetworkFailur using var c = new HttpClient(); try { - await c.GetAsync("https://10.0.0.0/invalidurl"); + await c.GetAsync("https://sdlfaldfjalkdfjlkajdflkajlsdjf.sdlkjafsdjfalfadslkf.com/"); } catch { diff --git a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.Basic.netfx.cs b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.Basic.netfx.cs index cbd7b7f262e..36b838087ff 100644 --- a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.Basic.netfx.cs +++ b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.Basic.netfx.cs @@ -320,7 +320,7 @@ public async Task HttpClientInstrumentationReportsExceptionEventForNetworkFailur using var c = new HttpClient(); try { - await c.GetAsync("https://10.0.0.0/invalidurl"); + await c.GetAsync("https://sdlfaldfjalkdfjlkajdflkajlsdjf.sdlkjafsdjfalfadslkf.com/"); } catch { @@ -402,7 +402,7 @@ public async Task HttpWebRequestInstrumentationReportsExceptionEventForNetworkFa try { - var request = (HttpWebRequest)WebRequest.Create("https://10.0.0.0/invalidurl"); + var request = (HttpWebRequest)WebRequest.Create("https://sdlfaldfjalkdfjlkajdflkajlsdjf.sdlkjafsdjfalfadslkf.com/"); request.Method = "GET";