From a1edda618d56f3c3a5746f84bac13e834b7e0412 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Mon, 4 Nov 2024 19:10:01 +0100 Subject: [PATCH] [repo/Quartz] Prepare to .NET9 (#2293) --- .../QuartzDiagnosticListener.cs | 7 ++-- .../QuartzDiagnosticListenerTests.cs | 38 +++++++++---------- .../TestJob.cs | 6 +-- .../TestJobExecutionExceptionJob.cs | 2 +- 4 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.Quartz/Implementation/QuartzDiagnosticListener.cs b/src/OpenTelemetry.Instrumentation.Quartz/Implementation/QuartzDiagnosticListener.cs index bb6e68b523..9f390287d9 100644 --- a/src/OpenTelemetry.Instrumentation.Quartz/Implementation/QuartzDiagnosticListener.cs +++ b/src/OpenTelemetry.Instrumentation.Quartz/Implementation/QuartzDiagnosticListener.cs @@ -28,7 +28,7 @@ public QuartzDiagnosticListener(string sourceName, QuartzInstrumentationOptions public override void OnEventWritten(string name, object? payload) { - Activity? activity = Activity.Current; + var activity = Activity.Current; Guard.ThrowIfNull(activity); switch (name) { @@ -44,6 +44,8 @@ public override void OnEventWritten(string name, object? payload) case "Quartz.Job.Veto.Exception": this.OnException(activity, payload); break; + default: + break; } } @@ -127,8 +129,7 @@ private void OnException(Activity activity, object? payload) { if (activity.IsAllDataRequested) { - var exc = payload as Exception; - if (exc == null) + if (payload is not Exception exc) { QuartzInstrumentationEventSource.Log.NullPayload(nameof(QuartzDiagnosticListener), nameof(this.OnStopActivity)); return; diff --git a/test/OpenTelemetry.Instrumentation.Quartz.Tests/QuartzDiagnosticListenerTests.cs b/test/OpenTelemetry.Instrumentation.Quartz.Tests/QuartzDiagnosticListenerTests.cs index b2faa5ea71..09289ecf17 100644 --- a/test/OpenTelemetry.Instrumentation.Quartz.Tests/QuartzDiagnosticListenerTests.cs +++ b/test/OpenTelemetry.Instrumentation.Quartz.Tests/QuartzDiagnosticListenerTests.cs @@ -21,8 +21,8 @@ public QuartzDiagnosticListenerTests() public async Task Should_Create_Activity() { // Arrange - Barrier barrier = new Barrier(2); - List jobExecTimestamps = new List(); + var barrier = new Barrier(2); + List jobExecTimestamps = []; var exportedItems = new List(); using var tel = Sdk.CreateTracerProviderBuilder() @@ -38,7 +38,7 @@ public async Task Should_Create_Activity() scheduler.Context.Put("DATESTAMPS", jobExecTimestamps); await scheduler.Start(); - JobDataMap jobDataMap = new JobDataMap { { "A", "B" } }; + var jobDataMap = new JobDataMap { { "A", "B" } }; var name = Guid.NewGuid().ToString(); var job = JobBuilder.Create() @@ -74,8 +74,8 @@ public async Task Should_Create_Activity() public async Task Should_Create_Activity_And_Enrich_When_Enrich() { // Arrange - Barrier barrier = new Barrier(2); - List jobExecTimestamps = new List(); + var barrier = new Barrier(2); + List jobExecTimestamps = []; var exportedItems = new List(); @@ -105,7 +105,7 @@ public async Task Should_Create_Activity_And_Enrich_When_Enrich() await scheduler.Start(); var testId = Guid.NewGuid().ToString(); - JobDataMap jobDataMap = new JobDataMap { { "TestId", testId } }; + var jobDataMap = new JobDataMap { { "TestId", testId } }; var name = Guid.NewGuid().ToString(); var job = JobBuilder.Create() @@ -141,8 +141,8 @@ public async Task Should_Create_Activity_And_Enrich_When_Enrich() public async Task Should_Record_Exception_When_Record_Exception_Enabled() { // Arrange - Barrier barrier = new Barrier(2); - List jobExecTimestamps = new List(); + var barrier = new Barrier(2); + List jobExecTimestamps = []; var exportedItems = new List(); @@ -162,7 +162,7 @@ public async Task Should_Record_Exception_When_Record_Exception_Enabled() await scheduler.Start(); var testId = Guid.NewGuid().ToString(); - JobDataMap jobDataMap = new JobDataMap { { "TestId", testId } }; + var jobDataMap = new JobDataMap { { "TestId", testId } }; var name = Guid.NewGuid().ToString(); var job = JobBuilder.Create() @@ -195,8 +195,8 @@ public async Task Should_Record_Exception_When_Record_Exception_Enabled() public async Task Should_Enrich_Exception_When_Record_Exception_Enabled_And_Enrich() { // Arrange - Barrier barrier = new Barrier(2); - List jobExecTimestamps = new List(); + var barrier = new Barrier(2); + List jobExecTimestamps = []; var exportedItems = new List(); @@ -229,7 +229,7 @@ public async Task Should_Enrich_Exception_When_Record_Exception_Enabled_And_Enri await scheduler.Start(); var testId = Guid.NewGuid().ToString(); - JobDataMap jobDataMap = new JobDataMap { { "TestId", testId } }; + var jobDataMap = new JobDataMap { { "TestId", testId } }; var name = Guid.NewGuid().ToString(); var job = JobBuilder.Create() @@ -262,8 +262,8 @@ public async Task Should_Enrich_Exception_When_Record_Exception_Enabled_And_Enri public async Task Should_Creates_Activity_Event_On_Job_Execution_Exception() { // Arrange - Barrier barrier = new Barrier(2); - List jobExecTimestamps = new List(); + var barrier = new Barrier(2); + List jobExecTimestamps = []; var exportedItems = new List(); using var tel = Sdk.CreateTracerProviderBuilder() @@ -291,7 +291,7 @@ public async Task Should_Creates_Activity_Event_On_Job_Execution_Exception() await scheduler.Start(); var testId = Guid.NewGuid().ToString(); - JobDataMap jobDataMap = new JobDataMap { { "TestId", testId } }; + var jobDataMap = new JobDataMap { { "TestId", testId } }; var name = Guid.NewGuid().ToString(); var job = JobBuilder.Create() @@ -323,8 +323,8 @@ public async Task Should_Creates_Activity_Event_On_Job_Execution_Exception() public async Task Should_Not_Record_Activity_When_Trace_Operation_Is_Not_Present() { // Arrange - Barrier barrier = new Barrier(2); - List jobExecTimestamps = new List(); + var barrier = new Barrier(2); + List jobExecTimestamps = []; var exportedItems = new List(); @@ -332,7 +332,7 @@ public async Task Should_Not_Record_Activity_When_Trace_Operation_Is_Not_Present .SetSampler(new AlwaysOnSampler()) .AddQuartzInstrumentation(q => { - q.TracedOperations = new HashSet(); + q.TracedOperations = []; }) .AddInMemoryExporter(exportedItems) .Build(); @@ -346,7 +346,7 @@ public async Task Should_Not_Record_Activity_When_Trace_Operation_Is_Not_Present await scheduler.Start(); var testId = Guid.NewGuid().ToString(); - JobDataMap jobDataMap = new JobDataMap { { "TestId", testId } }; + var jobDataMap = new JobDataMap { { "TestId", testId } }; var name = Guid.NewGuid().ToString(); var job = JobBuilder.Create() diff --git a/test/OpenTelemetry.Instrumentation.Quartz.Tests/TestJob.cs b/test/OpenTelemetry.Instrumentation.Quartz.Tests/TestJob.cs index 7be379f91c..e1fbee1e48 100644 --- a/test/OpenTelemetry.Instrumentation.Quartz.Tests/TestJob.cs +++ b/test/OpenTelemetry.Instrumentation.Quartz.Tests/TestJob.cs @@ -5,14 +5,14 @@ namespace OpenTelemetry.Instrumentation.Quartz.Tests; -public class TestJob : IJob +internal class TestJob : IJob { public Task Execute(IJobExecutionContext context) { try { - List jobExecTimestamps = (List)context.Scheduler.Context.Get("DATESTAMPS"); - Barrier barrier = (Barrier)context.Scheduler.Context.Get("BARRIER"); + var jobExecTimestamps = (List)context.Scheduler.Context.Get("DATESTAMPS"); + var barrier = (Barrier)context.Scheduler.Context.Get("BARRIER"); jobExecTimestamps.Add(DateTime.UtcNow); diff --git a/test/OpenTelemetry.Instrumentation.Quartz.Tests/TestJobExecutionExceptionJob.cs b/test/OpenTelemetry.Instrumentation.Quartz.Tests/TestJobExecutionExceptionJob.cs index 6ebed283b5..bb81998837 100644 --- a/test/OpenTelemetry.Instrumentation.Quartz.Tests/TestJobExecutionExceptionJob.cs +++ b/test/OpenTelemetry.Instrumentation.Quartz.Tests/TestJobExecutionExceptionJob.cs @@ -5,7 +5,7 @@ namespace OpenTelemetry.Instrumentation.Quartz.Tests; -public class TestJobExecutionExceptionJob : IJob +internal class TestJobExecutionExceptionJob : IJob { public Task Execute(IJobExecutionContext context) {