From 6b7ad127061c01295201b1f8a29884b9f3a1d207 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Thu, 31 Oct 2024 20:29:26 +0100 Subject: [PATCH] [repo/AspNet] Prepare to .NET9 (#2282) --- .../ActivityHelperTest.cs | 27 +++++++---------- .../HttpContextHelper.cs | 29 ++++--------------- .../WebConfigWithLocationTagTransformTest.cs | 5 ---- .../HttpInMetricsListenerTests.cs | 2 +- 4 files changed, 18 insertions(+), 45 deletions(-) diff --git a/test/OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule.Tests/ActivityHelperTest.cs b/test/OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule.Tests/ActivityHelperTest.cs index 0e7bacce92..93d9c59ea2 100644 --- a/test/OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule.Tests/ActivityHelperTest.cs +++ b/test/OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule.Tests/ActivityHelperTest.cs @@ -30,7 +30,7 @@ public void Has_Started_Returns_Correctly() { var context = HttpContextHelper.GetFakeHttpContext(); - bool result = ActivityHelper.HasStarted(context, out Activity? aspNetActivity); + var result = ActivityHelper.HasStarted(context, out var aspNetActivity); Assert.False(result); Assert.Null(aspNetActivity); @@ -42,7 +42,7 @@ public void Has_Started_Returns_Correctly() Assert.True(result); Assert.Null(aspNetActivity); - Activity activity = new Activity(TestActivityName); + var activity = new Activity(TestActivityName); context.Items[ActivityHelper.ContextKey] = new ActivityHelper.ContextHolder(activity); result = ActivityHelper.HasStarted(context, out aspNetActivity); @@ -90,7 +90,7 @@ public async Task Can_Restore_Baggage() }; var context = HttpContextHelper.GetFakeHttpContext(headers: requestHeaders); - using var rootActivity = ActivityHelper.StartAspNetActivity(new CompositeTextMapPropagator(new TextMapPropagator[] { new TraceContextPropagator(), new BaggagePropagator() }), context, null)!; + using var rootActivity = ActivityHelper.StartAspNetActivity(new CompositeTextMapPropagator([new TraceContextPropagator(), new BaggagePropagator()]), context, null)!; rootActivity.AddTag("k1", "v1"); rootActivity.AddTag("k2", "v2"); @@ -237,7 +237,7 @@ public async Task Can_Stop_Root_Activity_If_It_Is_Broken() using var root = ActivityHelper.StartAspNetActivity(this.noopTextMapPropagator, context, null)!; new Activity("child").Start(); - for (int i = 0; i < 2; i++) + for (var i = 0; i < 2; i++) { await Task.Run(() => { @@ -266,7 +266,7 @@ public void Stop_Root_Activity_With_129_Nesting_Depth() var context = HttpContextHelper.GetFakeHttpContext(); using var root = ActivityHelper.StartAspNetActivity(this.noopTextMapPropagator, context, null)!; - for (int i = 0; i < 129; i++) + for (var i = 0; i < 129; i++) { new Activity("child" + i).Start(); } @@ -368,7 +368,7 @@ public void Can_Create_RootActivity_From_W3C_Traceparent_With_Baggage() }; var context = HttpContextHelper.GetFakeHttpContext(headers: requestHeaders); - using var rootActivity = ActivityHelper.StartAspNetActivity(new CompositeTextMapPropagator(new TextMapPropagator[] { new TraceContextPropagator(), new BaggagePropagator() }), context, null); + using var rootActivity = ActivityHelper.StartAspNetActivity(new CompositeTextMapPropagator([new TraceContextPropagator(), new BaggagePropagator()]), context, null); Assert.NotNull(rootActivity); Assert.Equal(ActivityIdFormat.W3C, rootActivity.IdFormat); @@ -416,11 +416,11 @@ public void Can_Create_RootActivity_And_Saved_In_HttContext() public void Fire_Exception_Events() #pragma warning restore CA1030 // Use events where appropriate { - int callbacksFired = 0; + var callbacksFired = 0; var context = HttpContextHelper.GetFakeHttpContext(); - Activity activity = new Activity(TestActivityName); + var activity = new Activity(TestActivityName); ActivityHelper.WriteActivityException(activity, context, new InvalidOperationException(), (a, c, e) => { callbacksFired++; }); @@ -480,12 +480,7 @@ private void EnableListener(Action? onStarted = null, Action ActivityStopped = (a) => onStopped?.Invoke(a), Sample = (ref ActivityCreationOptions options) => { - if (onSample != null) - { - return onSample(options.Parent); - } - - return ActivitySamplingResult.AllDataAndRecorded; + return onSample?.Invoke(options.Parent) ?? ActivitySamplingResult.AllDataAndRecorded; }, }; @@ -494,7 +489,7 @@ private void EnableListener(Action? onStarted = null, Action private class TestHttpRequest : HttpRequestBase { - private readonly NameValueCollection headers = new(); + private readonly NameValueCollection headers = []; public override NameValueCollection Headers => this.headers; @@ -537,7 +532,7 @@ private class TestHttpContext : HttpContextBase public TestHttpContext(Exception? error = null) { this.Server = new TestHttpServerUtility(this); - this.items = new Hashtable(); + this.items = []; this.Error = error; } diff --git a/test/OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule.Tests/HttpContextHelper.cs b/test/OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule.Tests/HttpContextHelper.cs index 0b7541eb36..a9c079584a 100644 --- a/test/OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule.Tests/HttpContextHelper.cs +++ b/test/OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule.Tests/HttpContextHelper.cs @@ -33,23 +33,16 @@ private class SimpleWorkerRequestWithHeaders : SimpleWorkerRequest public SimpleWorkerRequestWithHeaders(string page, string query, TextWriter output, IDictionary? headers) : base(page, query, output) { - if (headers != null) - { - this.headers = headers; - } - else - { - this.headers = new Dictionary(); - } + this.headers = headers ?? new Dictionary(); } public override string[][] GetUnknownRequestHeaders() { - List result = new List(); + List result = []; foreach (var header in this.headers) { - result.Add(new string[] { header.Key, header.Value }); + result.Add([header.Key, header.Value]); } var baseResult = base.GetUnknownRequestHeaders(); @@ -58,29 +51,19 @@ public override string[][] GetUnknownRequestHeaders() result.AddRange(baseResult); } - return result.ToArray(); + return [.. result]; } public override string GetUnknownRequestHeader(string name) { - if (this.headers.TryGetValue(name, out var value)) - { - return value; - } - - return base.GetUnknownRequestHeader(name); + return this.headers.TryGetValue(name, out var value) ? value : base.GetUnknownRequestHeader(name); } public override string GetKnownRequestHeader(int index) { var name = GetKnownRequestHeaderName(index); - if (this.headers.TryGetValue(name, out var value)) - { - return value; - } - - return base.GetKnownRequestHeader(index); + return this.headers.TryGetValue(name, out var value) ? value : base.GetKnownRequestHeader(index); } } } diff --git a/test/OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule.Tests/WebConfigWithLocationTagTransformTest.cs b/test/OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule.Tests/WebConfigWithLocationTagTransformTest.cs index ce3f46ec55..aa3c072680 100644 --- a/test/OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule.Tests/WebConfigWithLocationTagTransformTest.cs +++ b/test/OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule.Tests/WebConfigWithLocationTagTransformTest.cs @@ -384,11 +384,6 @@ private XDocument ApplyInstallTransformation(string originalConfiguration, strin return this.ApplyTransformation(originalConfiguration, resourceName); } - private XDocument ApplyUninstallTransformation(string originalConfiguration, string resourceName) - { - return this.ApplyTransformation(originalConfiguration, resourceName); - } - private void VerifyTransformation(string expectedConfigContent, XDocument transformedWebConfig) { Assert.True( diff --git a/test/OpenTelemetry.Instrumentation.AspNet.Tests/HttpInMetricsListenerTests.cs b/test/OpenTelemetry.Instrumentation.AspNet.Tests/HttpInMetricsListenerTests.cs index 1a259e3c1d..2f7c82d65e 100644 --- a/test/OpenTelemetry.Instrumentation.AspNet.Tests/HttpInMetricsListenerTests.cs +++ b/test/OpenTelemetry.Instrumentation.AspNet.Tests/HttpInMetricsListenerTests.cs @@ -154,7 +154,7 @@ public void AspNetMetricTagsAreCollectedSuccessfully( } Assert.Equal( - expected: new List { 0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10, double.PositiveInfinity }, + expected: [0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10, double.PositiveInfinity], actual: histogramBounds); void ExpectTag(T? expected, string tagName)