From 69649f79f8c32ddd2ffe47198e36cd44640d8d51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 29 Oct 2024 10:19:24 +0100 Subject: [PATCH 01/13] Fix IDE0004 --- .../Implementation/InstanaSpanSerializer.cs | 2 +- .../Implementation/Processors/TagsActivityProcessor.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/OpenTelemetry.Exporter.Instana/Implementation/InstanaSpanSerializer.cs b/src/OpenTelemetry.Exporter.Instana/Implementation/InstanaSpanSerializer.cs index 55d8626dad..b4702f84af 100644 --- a/src/OpenTelemetry.Exporter.Instana/Implementation/InstanaSpanSerializer.cs +++ b/src/OpenTelemetry.Exporter.Instana/Implementation/InstanaSpanSerializer.cs @@ -68,7 +68,7 @@ internal static async Task SerializeToStreamWriterAsync(InstanaSpan instanaSpan, await writer.WriteAsync(COMMA).ConfigureAwait(false); await AppendPropertyAsync(DateToUnixMillis(instanaSpan.Ts), "ts", writer).ConfigureAwait(false); await writer.WriteAsync(COMMA).ConfigureAwait(false); - await AppendPropertyAsync((long)(instanaSpan.D / 10_000), "d", writer).ConfigureAwait(false); + await AppendPropertyAsync(instanaSpan.D / 10_000L, "d", writer).ConfigureAwait(false); await writer.WriteAsync(COMMA).ConfigureAwait(false); await AppendObjectAsync(SerializeDataAsync, "data", instanaSpan, writer).ConfigureAwait(false); await writer.WriteAsync(COMMA).ConfigureAwait(false); diff --git a/src/OpenTelemetry.Exporter.Instana/Implementation/Processors/TagsActivityProcessor.cs b/src/OpenTelemetry.Exporter.Instana/Implementation/Processors/TagsActivityProcessor.cs index 0f6763c1a9..8fdc0205df 100644 --- a/src/OpenTelemetry.Exporter.Instana/Implementation/Processors/TagsActivityProcessor.cs +++ b/src/OpenTelemetry.Exporter.Instana/Implementation/Processors/TagsActivityProcessor.cs @@ -28,13 +28,13 @@ public override async Task ProcessAsync(Activity activity, InstanaSpan instanaSp { if (tag.Key == "otel.status_code") { - statusCode = (tag.Value as string) ?? string.Empty; + statusCode = tag.Value ?? string.Empty; continue; } if (tag.Key == "otel.status_description") { - statusDesc = (tag.Value as string) ?? string.Empty; + statusDesc = tag.Value ?? string.Empty; continue; } From c19a73b817bda53f10c2a91401debd810a920c80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 29 Oct 2024 10:19:54 +0100 Subject: [PATCH 02/13] Fix IDE0007 --- .../Implementation/InstanaSpanFactory.cs | 2 +- .../Processors/DefaultActivityProcessor.cs | 10 +++++----- .../Processors/TagsActivityProcessor.cs | 6 +++--- .../Implementation/SpanSender.cs | 2 +- .../Implementation/Transport.cs | 14 +++++++------- .../InstanaExporter.cs | 16 ++++++++-------- 6 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/OpenTelemetry.Exporter.Instana/Implementation/InstanaSpanFactory.cs b/src/OpenTelemetry.Exporter.Instana/Implementation/InstanaSpanFactory.cs index 0d106cd0d5..efbfae2e1f 100644 --- a/src/OpenTelemetry.Exporter.Instana/Implementation/InstanaSpanFactory.cs +++ b/src/OpenTelemetry.Exporter.Instana/Implementation/InstanaSpanFactory.cs @@ -7,7 +7,7 @@ internal class InstanaSpanFactory { internal static InstanaSpan CreateSpan() { - InstanaSpan instanaSpan = new InstanaSpan + var instanaSpan = new InstanaSpan { Data = new Data() { diff --git a/src/OpenTelemetry.Exporter.Instana/Implementation/Processors/DefaultActivityProcessor.cs b/src/OpenTelemetry.Exporter.Instana/Implementation/Processors/DefaultActivityProcessor.cs index fd430b9b6a..0682328662 100644 --- a/src/OpenTelemetry.Exporter.Instana/Implementation/Processors/DefaultActivityProcessor.cs +++ b/src/OpenTelemetry.Exporter.Instana/Implementation/Processors/DefaultActivityProcessor.cs @@ -13,7 +13,7 @@ public override async Task ProcessAsync(Activity activity, InstanaSpan instanaSp instanaSpan.N = InstanaExporterConstants.OTEL_SPAN_TYPE; - string traceId = activity.TraceId.ToHexString(); + var traceId = activity.TraceId.ToHexString(); if (traceId.Length == 32) { instanaSpan.T = traceId.Substring(16); @@ -24,8 +24,8 @@ public override async Task ProcessAsync(Activity activity, InstanaSpan instanaSp instanaSpan.T = traceId; } - bool hasParent = false; - string parentSpanId = activity.ParentSpanId.ToHexString(); + var hasParent = false; + var parentSpanId = activity.ParentSpanId.ToHexString(); if (!string.IsNullOrEmpty(parentSpanId) && GetLongFromHex(parentSpanId) != 0) { hasParent = true; @@ -73,7 +73,7 @@ private static long GetLongFromHex(string hexValue) { try { - string[] ids = hexValue.Split(','); + var ids = hexValue.Split(','); return Convert.ToInt64(ids[ids.Length - 1].Trim(), 16); } catch (Exception) @@ -86,7 +86,7 @@ private static long GetLongFromHex(string hexValue) private static void SetKind(Activity activity, InstanaSpan instanaSpan) { - bool isEntrySpan = false; + var isEntrySpan = false; if (instanaSpan.Data.data != null) { diff --git a/src/OpenTelemetry.Exporter.Instana/Implementation/Processors/TagsActivityProcessor.cs b/src/OpenTelemetry.Exporter.Instana/Implementation/Processors/TagsActivityProcessor.cs index 8fdc0205df..1d6341e7a9 100644 --- a/src/OpenTelemetry.Exporter.Instana/Implementation/Processors/TagsActivityProcessor.cs +++ b/src/OpenTelemetry.Exporter.Instana/Implementation/Processors/TagsActivityProcessor.cs @@ -21,9 +21,9 @@ public override async Task ProcessAsync(Activity activity, InstanaSpan instanaSp this.PreProcess(activity, instanaSpan); - string statusCode = string.Empty; - string statusDesc = string.Empty; - Dictionary tags = new Dictionary(); + var statusCode = string.Empty; + var statusDesc = string.Empty; + var tags = new Dictionary(); foreach (var tag in activity.Tags) { if (tag.Key == "otel.status_code") diff --git a/src/OpenTelemetry.Exporter.Instana/Implementation/SpanSender.cs b/src/OpenTelemetry.Exporter.Instana/Implementation/SpanSender.cs index 0381b25a5a..e5203f3510 100644 --- a/src/OpenTelemetry.Exporter.Instana/Implementation/SpanSender.cs +++ b/src/OpenTelemetry.Exporter.Instana/Implementation/SpanSender.cs @@ -33,7 +33,7 @@ private async void TaskSpanSender() while (true) { // check if we can send spans - if (this.spansQueue.TryPeek(out InstanaSpan _)) + if (this.spansQueue.TryPeek(out var _)) { // actually send spans await Transport.SendSpansAsync(this.spansQueue).ConfigureAwait(false); diff --git a/src/OpenTelemetry.Exporter.Instana/Implementation/Transport.cs b/src/OpenTelemetry.Exporter.Instana/Implementation/Transport.cs index f39c6b2da7..023cee7ba4 100644 --- a/src/OpenTelemetry.Exporter.Instana/Implementation/Transport.cs +++ b/src/OpenTelemetry.Exporter.Instana/Implementation/Transport.cs @@ -39,16 +39,16 @@ internal static async Task SendSpansAsync(ConcurrentQueue spanQueue { try { - using (MemoryStream sendBuffer = new MemoryStream(TracesBuffer)) + using (var sendBuffer = new MemoryStream(TracesBuffer)) { - using (StreamWriter writer = new StreamWriter(sendBuffer)) + using (var writer = new StreamWriter(sendBuffer)) { await writer.WriteAsync("{\"spans\":[").ConfigureAwait(false); - bool first = true; + var first = true; // peek instead of dequeue, because we don't yet know whether the next span // fits within our MULTI_SPAN_BUFFER_LIMIT - while (spanQueue.TryPeek(out InstanaSpan span) && sendBuffer.Position < MultiSpanBufferLimit) + while (spanQueue.TryPeek(out var span) && sendBuffer.Position < MultiSpanBufferLimit) { if (!first) { @@ -68,7 +68,7 @@ internal static async Task SendSpansAsync(ConcurrentQueue spanQueue await writer.WriteAsync("]}").ConfigureAwait(false); await writer.FlushAsync().ConfigureAwait(false); - long length = sendBuffer.Position; + var length = sendBuffer.Position; sendBuffer.Position = 0; sendBuffer.SetLength(length); @@ -148,8 +148,8 @@ private static void ConfigureBackendClient() #pragma warning disable CA2000 var configuredHandler = new HttpClientHandler(); #pragma warning restore CA2000 - string proxy = Environment.GetEnvironmentVariable(InstanaExporterConstants.ENVVAR_INSTANA_ENDPOINT_PROXY); - if (Uri.TryCreate(proxy, UriKind.Absolute, out Uri proxyAddress)) + var proxy = Environment.GetEnvironmentVariable(InstanaExporterConstants.ENVVAR_INSTANA_ENDPOINT_PROXY); + if (Uri.TryCreate(proxy, UriKind.Absolute, out var proxyAddress)) { configuredHandler.Proxy = new WebProxy(proxyAddress, true); configuredHandler.UseProxy = true; diff --git a/src/OpenTelemetry.Exporter.Instana/InstanaExporter.cs b/src/OpenTelemetry.Exporter.Instana/InstanaExporter.cs index aa77513e7a..f7d4f661bf 100644 --- a/src/OpenTelemetry.Exporter.Instana/InstanaExporter.cs +++ b/src/OpenTelemetry.Exporter.Instana/InstanaExporter.cs @@ -56,13 +56,13 @@ public override ExportResult Export(in Batch batch) return ExportResult.Failure; } - From from = new From(); + var from = new From(); if (this.instanaExporterHelper.IsWindows()) { from = new From { E = Process.GetCurrentProcess().Id.ToString(CultureInfo.InvariantCulture) }; } - string serviceName = this.ExtractServiceName(ref from); + var serviceName = this.ExtractServiceName(ref from); foreach (var activity in batch) { @@ -71,7 +71,7 @@ public override ExportResult Export(in Batch batch) continue; } - InstanaSpan span = this.ParseActivityAsync(activity, serviceName, from).Result; + var span = this.ParseActivityAsync(activity, serviceName, from).Result; this.spanSender.Enqueue(span); } @@ -98,10 +98,10 @@ protected override bool OnForceFlush(int timeoutMilliseconds) private string ExtractServiceName(ref From from) { - string serviceName = string.Empty; - string serviceId = string.Empty; - string processId = string.Empty; - string hostId = string.Empty; + var serviceName = string.Empty; + var serviceId = string.Empty; + var processId = string.Empty; + var hostId = string.Empty; var resource = this.instanaExporterHelper.GetParentProviderResource(this); if (resource != Resource.Empty && resource.Attributes.Any()) { @@ -154,7 +154,7 @@ private string ExtractServiceName(ref From from) private async Task ParseActivityAsync(Activity activity, string? serviceName = null, From? from = null) { - InstanaSpan instanaSpan = InstanaSpanFactory.CreateSpan(); + var instanaSpan = InstanaSpanFactory.CreateSpan(); await this.activityProcessor.ProcessAsync(activity, instanaSpan).ConfigureAwait(false); From b7cefea638452c37547c522629512630cda85a6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 29 Oct 2024 10:24:20 +0100 Subject: [PATCH 03/13] Fix IDE0007 - tests --- .../InstanaExporterTests.cs | 18 +++++++++--------- .../InstanaSpanFactoryTests.cs | 2 +- .../InstanaSpanSerializerTests.cs | 10 +++++----- .../InstanaSpanTest.cs | 2 +- .../DefaultActivityProcessorTests.cs | 4 ++-- .../Processors/ErrorActivityProcessorTests.cs | 12 ++++++------ .../Processors/EventsActivityProcessorTests.cs | 6 +++--- .../Processors/TagsActivityProcessorTests.cs | 8 ++++---- 8 files changed, 31 insertions(+), 31 deletions(-) diff --git a/test/OpenTelemetry.Exporter.Instana.Tests/InstanaExporterTests.cs b/test/OpenTelemetry.Exporter.Instana.Tests/InstanaExporterTests.cs index e83dc8c27b..535034b2a0 100644 --- a/test/OpenTelemetry.Exporter.Instana.Tests/InstanaExporterTests.cs +++ b/test/OpenTelemetry.Exporter.Instana.Tests/InstanaExporterTests.cs @@ -32,12 +32,12 @@ public void Export() SpanSender = this.spanSender, }; - Activity activity = new Activity("testOperationName"); + var activity = new Activity("testOperationName"); activity.SetStatus(ActivityStatusCode.Error, "TestErrorDesc"); activity.TraceStateString = "TraceStateString"; - Activity[] activities = new Activity[1] { activity }; - Batch batch = new Batch(activities, activities.Length); + Activity[] activities =[activity]; + var batch = new Batch(activities, activities.Length); var result = this.instanaExporter.Export(in batch); Assert.Equal(ExportResult.Success, result); @@ -65,11 +65,11 @@ public void Export_ProcessPidDoesNotExistButServiceIdExists() SpanSender = this.spanSender, }; - Activity activity = new Activity("testOperationName"); + var activity = new Activity("testOperationName"); activity.SetStatus(ActivityStatusCode.Error, "TestErrorDesc"); - Activity[] activities = new Activity[1] { activity }; - Batch batch = new Batch(activities, activities.Length); + Activity[] activities = [activity]; + var batch = new Batch(activities, activities.Length); var result = this.instanaExporter.Export(in batch); Assert.Equal(ExportResult.Success, result); @@ -89,13 +89,13 @@ public void Export_ExporterIsShotDown() SpanSender = this.spanSender, }; - Activity activity = new Activity("testOperationName"); + var activity = new Activity("testOperationName"); activity.SetStatus(ActivityStatusCode.Error, "TestErrorDesc"); this.instanaExporter.Shutdown(); - Activity[] activities = new Activity[1] { activity }; - Batch batch = new Batch(activities, activities.Length); + Activity[] activities = [activity]; + var batch = new Batch(activities, activities.Length); var result = this.instanaExporter.Export(in batch); Assert.Equal(ExportResult.Failure, result); diff --git a/test/OpenTelemetry.Exporter.Instana.Tests/InstanaSpanFactoryTests.cs b/test/OpenTelemetry.Exporter.Instana.Tests/InstanaSpanFactoryTests.cs index 3e1e14f773..f53310e25c 100644 --- a/test/OpenTelemetry.Exporter.Instana.Tests/InstanaSpanFactoryTests.cs +++ b/test/OpenTelemetry.Exporter.Instana.Tests/InstanaSpanFactoryTests.cs @@ -12,7 +12,7 @@ public static class InstanaSpanFactoryTests public static void CreateSpan() { _ = new InstanaSpanFactory(); - InstanaSpan instanaSpan = InstanaSpanFactory.CreateSpan(); + var instanaSpan = InstanaSpanFactory.CreateSpan(); Assert.NotNull(instanaSpan); Assert.NotNull(instanaSpan.TransformInfo); diff --git a/test/OpenTelemetry.Exporter.Instana.Tests/InstanaSpanSerializerTests.cs b/test/OpenTelemetry.Exporter.Instana.Tests/InstanaSpanSerializerTests.cs index 389acfdf74..98692e0817 100644 --- a/test/OpenTelemetry.Exporter.Instana.Tests/InstanaSpanSerializerTests.cs +++ b/test/OpenTelemetry.Exporter.Instana.Tests/InstanaSpanSerializerTests.cs @@ -13,7 +13,7 @@ public static class InstanaSpanSerializerTests [Fact] public static async Task SerializeToStreamWriterAsync() { - InstanaSpan instanaOtelSpan = InstanaSpanFactory.CreateSpan(); + var instanaOtelSpan = InstanaSpanFactory.CreateSpan(); instanaOtelSpan.F = new Implementation.From { E = "12345", H = "localhost" }; instanaOtelSpan.N = "otel"; instanaOtelSpan.T = "hexNumberT1234"; @@ -44,17 +44,17 @@ public static async Task SerializeToStreamWriterAsync() }; InstanaSpanTest? span; - using (MemoryStream sendBuffer = new MemoryStream(new byte[4096000])) + using (var sendBuffer = new MemoryStream(new byte[4096000])) { - using (StreamWriter writer = new StreamWriter(sendBuffer)) + using (var writer = new StreamWriter(sendBuffer)) { await InstanaSpanSerializer.SerializeToStreamWriterAsync(instanaOtelSpan, writer); await writer.FlushAsync(); - long length = sendBuffer.Position; + var length = sendBuffer.Position; sendBuffer.Position = 0; sendBuffer.SetLength(length); - JsonSerializer serializer = new JsonSerializer(); + var serializer = new JsonSerializer(); serializer.Converters.Add(new JavaScriptDateTimeConverter()); serializer.NullValueHandling = NullValueHandling.Ignore; diff --git a/test/OpenTelemetry.Exporter.Instana.Tests/InstanaSpanTest.cs b/test/OpenTelemetry.Exporter.Instana.Tests/InstanaSpanTest.cs index 201b0f3918..e830fa610c 100644 --- a/test/OpenTelemetry.Exporter.Instana.Tests/InstanaSpanTest.cs +++ b/test/OpenTelemetry.Exporter.Instana.Tests/InstanaSpanTest.cs @@ -145,7 +145,7 @@ public override bool CanConvert(Type objectType) } else { - JObject obj = JObject.Load(reader); + var obj = JObject.Load(reader); var data = obj.Root; if (data != null) { diff --git a/test/OpenTelemetry.Exporter.Instana.Tests/Processors/DefaultActivityProcessorTests.cs b/test/OpenTelemetry.Exporter.Instana.Tests/Processors/DefaultActivityProcessorTests.cs index 85ae5c3577..3e4c5fe771 100644 --- a/test/OpenTelemetry.Exporter.Instana.Tests/Processors/DefaultActivityProcessorTests.cs +++ b/test/OpenTelemetry.Exporter.Instana.Tests/Processors/DefaultActivityProcessorTests.cs @@ -15,11 +15,11 @@ public class DefaultActivityProcessorTests [Fact] public async Task ProcessAsync() { - Activity activity = new Activity("testOperationName"); + var activity = new Activity("testOperationName"); activity.Start(); await Task.Delay(200); activity.Stop(); - InstanaSpan instanaSpan = new InstanaSpan(); + var instanaSpan = new InstanaSpan(); await this.defaultActivityProcessor.ProcessAsync(activity, instanaSpan); Assert.False(string.IsNullOrEmpty(instanaSpan.S)); diff --git a/test/OpenTelemetry.Exporter.Instana.Tests/Processors/ErrorActivityProcessorTests.cs b/test/OpenTelemetry.Exporter.Instana.Tests/Processors/ErrorActivityProcessorTests.cs index 18c37f236a..f241bc0bf4 100644 --- a/test/OpenTelemetry.Exporter.Instana.Tests/Processors/ErrorActivityProcessorTests.cs +++ b/test/OpenTelemetry.Exporter.Instana.Tests/Processors/ErrorActivityProcessorTests.cs @@ -15,9 +15,9 @@ public class ErrorActivityProcessorTests [Fact] public async Task Process_ErrorStatusCodeIsSet() { - Activity activity = new Activity("testOperationName"); + var activity = new Activity("testOperationName"); activity.SetStatus(ActivityStatusCode.Error, "TestErrorDesc"); - InstanaSpan instanaSpan = new InstanaSpan(); + var instanaSpan = new InstanaSpan(); await this.errorActivityProcessor.ProcessAsync(activity, instanaSpan); Assert.True(instanaSpan.Ec == 1); @@ -30,8 +30,8 @@ public async Task Process_ErrorStatusCodeIsSet() [Fact] public async Task Process_ExistsExceptionEvent() { - Activity activity = new Activity("testOperationName"); - InstanaSpan instanaSpan = new InstanaSpan() { TransformInfo = new Implementation.InstanaSpanTransformInfo() { HasExceptionEvent = true } }; + var activity = new Activity("testOperationName"); + var instanaSpan = new InstanaSpan() { TransformInfo = new Implementation.InstanaSpanTransformInfo() { HasExceptionEvent = true } }; await this.errorActivityProcessor.ProcessAsync(activity, instanaSpan); Assert.True(instanaSpan.Ec == 1); @@ -40,8 +40,8 @@ public async Task Process_ExistsExceptionEvent() [Fact] public async Task Process_NoError() { - Activity activity = new Activity("testOperationName"); - InstanaSpan instanaSpan = new InstanaSpan() { TransformInfo = new Implementation.InstanaSpanTransformInfo() }; + var activity = new Activity("testOperationName"); + var instanaSpan = new InstanaSpan() { TransformInfo = new Implementation.InstanaSpanTransformInfo() }; await this.errorActivityProcessor.ProcessAsync(activity, instanaSpan); Assert.True(instanaSpan.Ec == 0); diff --git a/test/OpenTelemetry.Exporter.Instana.Tests/Processors/EventsActivityProcessorTests.cs b/test/OpenTelemetry.Exporter.Instana.Tests/Processors/EventsActivityProcessorTests.cs index ce3b79dc8e..4ee8789a96 100644 --- a/test/OpenTelemetry.Exporter.Instana.Tests/Processors/EventsActivityProcessorTests.cs +++ b/test/OpenTelemetry.Exporter.Instana.Tests/Processors/EventsActivityProcessorTests.cs @@ -31,10 +31,10 @@ public async Task ProcessAsync() DateTimeOffset.MaxValue, activityTagsCollection2); - Activity activity = new Activity("testOperationName"); + var activity = new Activity("testOperationName"); activity.AddEvent(activityEvent); activity.AddEvent(activityEvent2); - InstanaSpan instanaSpan = new InstanaSpan() { TransformInfo = new Implementation.InstanaSpanTransformInfo() }; + var instanaSpan = new InstanaSpan() { TransformInfo = new Implementation.InstanaSpanTransformInfo() }; if (this.eventsActivityProcessor != null) { await this.eventsActivityProcessor.ProcessAsync(activity, instanaSpan); @@ -46,7 +46,7 @@ public async Task ProcessAsync() Assert.True(instanaSpan.Data.Events[0].Name == "testActivityEvent"); Assert.True(instanaSpan.Data.Events[0].Ts > 0); Assert.NotNull(instanaSpan.Data?.Events[0]?.Tags); - string? eventTagValue = string.Empty; + var eventTagValue = string.Empty; _ = instanaSpan.Data.Events[0].Tags?.TryGetValue("eventTagKey", out eventTagValue); Assert.True(eventTagValue == "eventTagValue"); Assert.True(instanaSpan.Data.Events[1].Name == "testActivityEvent2"); diff --git a/test/OpenTelemetry.Exporter.Instana.Tests/Processors/TagsActivityProcessorTests.cs b/test/OpenTelemetry.Exporter.Instana.Tests/Processors/TagsActivityProcessorTests.cs index b70bc79bf6..e3561c5bb1 100644 --- a/test/OpenTelemetry.Exporter.Instana.Tests/Processors/TagsActivityProcessorTests.cs +++ b/test/OpenTelemetry.Exporter.Instana.Tests/Processors/TagsActivityProcessorTests.cs @@ -15,12 +15,12 @@ public class TagsActivityProcessorTests [Fact] public async Task ProcessAsync_StatusTagsExist() { - Activity activity = new Activity("testOperationName"); + var activity = new Activity("testOperationName"); activity.AddTag("otel.status_code", "testStatusCode"); activity.AddTag("otel.status_description", "testStatusDescription"); activity.AddTag("otel.testTag", "testTag"); - InstanaSpan instanaSpan = new InstanaSpan(); + var instanaSpan = new InstanaSpan(); await this.tagsActivityProcessor.ProcessAsync(activity, instanaSpan); Assert.NotNull(instanaSpan.Data); @@ -33,10 +33,10 @@ public async Task ProcessAsync_StatusTagsExist() [Fact] public async Task ProcessAsync_StatusTagsDoNotExist() { - Activity activity = new Activity("testOperationName"); + var activity = new Activity("testOperationName"); activity.AddTag("otel.testTag", "testTag"); - InstanaSpan instanaSpan = new InstanaSpan(); + var instanaSpan = new InstanaSpan(); await this.tagsActivityProcessor.ProcessAsync(activity, instanaSpan); Assert.NotNull(instanaSpan.Data); From b6c46297f4d2f5bb934f06597f1c755579e60bec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 29 Oct 2024 10:25:51 +0100 Subject: [PATCH 04/13] Fix IDE0090 --- .../Implementation/InstanaSpan.cs | 12 ++++++------ .../Implementation/SpanSender.cs | 2 +- .../Implementation/Transport.cs | 2 +- .../Processors/DefaultActivityProcessorTests.cs | 2 +- .../Processors/ErrorActivityProcessorTests.cs | 2 +- .../Processors/EventsActivityProcessorTests.cs | 2 +- .../Processors/TagsActivityProcessorTests.cs | 2 +- 7 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/OpenTelemetry.Exporter.Instana/Implementation/InstanaSpan.cs b/src/OpenTelemetry.Exporter.Instana/Implementation/InstanaSpan.cs index e9a6ce6e34..3230e8d0e0 100644 --- a/src/OpenTelemetry.Exporter.Instana/Implementation/InstanaSpan.cs +++ b/src/OpenTelemetry.Exporter.Instana/Implementation/InstanaSpan.cs @@ -23,11 +23,11 @@ internal enum SpanKind internal class InstanaSpan { - private InstanaSpanTransformInfo transformInfo = new InstanaSpanTransformInfo(); + private InstanaSpanTransformInfo transformInfo = new(); private string n = string.Empty; private string t = string.Empty; private string lt = string.Empty; - private From f = new From(); + private From f = new(); private string p = string.Empty; private string s = string.Empty; private SpanKind k = SpanKind.NOT_SET; @@ -35,7 +35,7 @@ internal class InstanaSpan private long d; private bool tp; private int ec; - private Data data = new Data() + private Data data = new() { data = new Dictionary(8), Events = new List(8), @@ -197,9 +197,9 @@ internal bool IsEmpty() internal class Data #pragma warning restore SA1402 // File may only contain a single type { - private List events = new List(8); - private Dictionary dataField = new Dictionary(8); - private Dictionary tags = new Dictionary(2); + private List events = new(8); + private Dictionary dataField = new(8); + private Dictionary tags = new(2); #pragma warning disable SA1300 // Element should begin with upper-case letter diff --git a/src/OpenTelemetry.Exporter.Instana/Implementation/SpanSender.cs b/src/OpenTelemetry.Exporter.Instana/Implementation/SpanSender.cs index e5203f3510..9e20b6e805 100644 --- a/src/OpenTelemetry.Exporter.Instana/Implementation/SpanSender.cs +++ b/src/OpenTelemetry.Exporter.Instana/Implementation/SpanSender.cs @@ -10,7 +10,7 @@ internal sealed class SpanSender : ISpanSender #pragma warning restore CA1001 // Types that own disposable fields should be disposable { private readonly Task queueSenderTask; - private readonly ConcurrentQueue spansQueue = new ConcurrentQueue(); + private readonly ConcurrentQueue spansQueue = new(); public SpanSender() { diff --git a/src/OpenTelemetry.Exporter.Instana/Implementation/Transport.cs b/src/OpenTelemetry.Exporter.Instana/Implementation/Transport.cs index 023cee7ba4..e9fac2ec7c 100644 --- a/src/OpenTelemetry.Exporter.Instana/Implementation/Transport.cs +++ b/src/OpenTelemetry.Exporter.Instana/Implementation/Transport.cs @@ -15,7 +15,7 @@ internal static class Transport { private const int MultiSpanBufferSize = 4096000; private const int MultiSpanBufferLimit = 4070000; - private static readonly MediaTypeHeaderValue MEDIAHEADER = new MediaTypeHeaderValue("application/json"); + private static readonly MediaTypeHeaderValue MEDIAHEADER = new("application/json"); private static readonly byte[] TracesBuffer = new byte[MultiSpanBufferSize]; private static bool isConfigured; private static int backendTimeout; diff --git a/test/OpenTelemetry.Exporter.Instana.Tests/Processors/DefaultActivityProcessorTests.cs b/test/OpenTelemetry.Exporter.Instana.Tests/Processors/DefaultActivityProcessorTests.cs index 3e4c5fe771..5f37fd9ad2 100644 --- a/test/OpenTelemetry.Exporter.Instana.Tests/Processors/DefaultActivityProcessorTests.cs +++ b/test/OpenTelemetry.Exporter.Instana.Tests/Processors/DefaultActivityProcessorTests.cs @@ -10,7 +10,7 @@ namespace OpenTelemetry.Exporter.Instana.Tests.Processors; public class DefaultActivityProcessorTests { - private DefaultActivityProcessor defaultActivityProcessor = new DefaultActivityProcessor(); + private DefaultActivityProcessor defaultActivityProcessor = new(); [Fact] public async Task ProcessAsync() diff --git a/test/OpenTelemetry.Exporter.Instana.Tests/Processors/ErrorActivityProcessorTests.cs b/test/OpenTelemetry.Exporter.Instana.Tests/Processors/ErrorActivityProcessorTests.cs index f241bc0bf4..6537b28386 100644 --- a/test/OpenTelemetry.Exporter.Instana.Tests/Processors/ErrorActivityProcessorTests.cs +++ b/test/OpenTelemetry.Exporter.Instana.Tests/Processors/ErrorActivityProcessorTests.cs @@ -10,7 +10,7 @@ namespace OpenTelemetry.Exporter.Instana.Tests.Processors; public class ErrorActivityProcessorTests { - private ErrorActivityProcessor errorActivityProcessor = new ErrorActivityProcessor(); + private ErrorActivityProcessor errorActivityProcessor = new(); [Fact] public async Task Process_ErrorStatusCodeIsSet() diff --git a/test/OpenTelemetry.Exporter.Instana.Tests/Processors/EventsActivityProcessorTests.cs b/test/OpenTelemetry.Exporter.Instana.Tests/Processors/EventsActivityProcessorTests.cs index 4ee8789a96..fbf51cb744 100644 --- a/test/OpenTelemetry.Exporter.Instana.Tests/Processors/EventsActivityProcessorTests.cs +++ b/test/OpenTelemetry.Exporter.Instana.Tests/Processors/EventsActivityProcessorTests.cs @@ -12,7 +12,7 @@ namespace OpenTelemetry.Exporter.Instana.Tests.Processors; public class EventsActivityProcessorTests { - private EventsActivityProcessor eventsActivityProcessor = new EventsActivityProcessor(); + private EventsActivityProcessor eventsActivityProcessor = new(); [Fact] public async Task ProcessAsync() diff --git a/test/OpenTelemetry.Exporter.Instana.Tests/Processors/TagsActivityProcessorTests.cs b/test/OpenTelemetry.Exporter.Instana.Tests/Processors/TagsActivityProcessorTests.cs index e3561c5bb1..377477f956 100644 --- a/test/OpenTelemetry.Exporter.Instana.Tests/Processors/TagsActivityProcessorTests.cs +++ b/test/OpenTelemetry.Exporter.Instana.Tests/Processors/TagsActivityProcessorTests.cs @@ -10,7 +10,7 @@ namespace OpenTelemetry.Exporter.Instana.Tests.Processors; public class TagsActivityProcessorTests { - private TagsActivityProcessor tagsActivityProcessor = new TagsActivityProcessor(); + private TagsActivityProcessor tagsActivityProcessor = new(); [Fact] public async Task ProcessAsync_StatusTagsExist() From 8dc9a722934bcdd26768203ae1aed06961021f79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 29 Oct 2024 10:26:33 +0100 Subject: [PATCH 05/13] Fix IDE0044 --- .../Processors/DefaultActivityProcessorTests.cs | 2 +- .../Processors/ErrorActivityProcessorTests.cs | 2 +- .../Processors/EventsActivityProcessorTests.cs | 2 +- .../Processors/TagsActivityProcessorTests.cs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/OpenTelemetry.Exporter.Instana.Tests/Processors/DefaultActivityProcessorTests.cs b/test/OpenTelemetry.Exporter.Instana.Tests/Processors/DefaultActivityProcessorTests.cs index 5f37fd9ad2..bdf6907982 100644 --- a/test/OpenTelemetry.Exporter.Instana.Tests/Processors/DefaultActivityProcessorTests.cs +++ b/test/OpenTelemetry.Exporter.Instana.Tests/Processors/DefaultActivityProcessorTests.cs @@ -10,7 +10,7 @@ namespace OpenTelemetry.Exporter.Instana.Tests.Processors; public class DefaultActivityProcessorTests { - private DefaultActivityProcessor defaultActivityProcessor = new(); + private readonly DefaultActivityProcessor defaultActivityProcessor = new(); [Fact] public async Task ProcessAsync() diff --git a/test/OpenTelemetry.Exporter.Instana.Tests/Processors/ErrorActivityProcessorTests.cs b/test/OpenTelemetry.Exporter.Instana.Tests/Processors/ErrorActivityProcessorTests.cs index 6537b28386..216eae523c 100644 --- a/test/OpenTelemetry.Exporter.Instana.Tests/Processors/ErrorActivityProcessorTests.cs +++ b/test/OpenTelemetry.Exporter.Instana.Tests/Processors/ErrorActivityProcessorTests.cs @@ -10,7 +10,7 @@ namespace OpenTelemetry.Exporter.Instana.Tests.Processors; public class ErrorActivityProcessorTests { - private ErrorActivityProcessor errorActivityProcessor = new(); + private readonly ErrorActivityProcessor errorActivityProcessor = new(); [Fact] public async Task Process_ErrorStatusCodeIsSet() diff --git a/test/OpenTelemetry.Exporter.Instana.Tests/Processors/EventsActivityProcessorTests.cs b/test/OpenTelemetry.Exporter.Instana.Tests/Processors/EventsActivityProcessorTests.cs index fbf51cb744..54faaa87be 100644 --- a/test/OpenTelemetry.Exporter.Instana.Tests/Processors/EventsActivityProcessorTests.cs +++ b/test/OpenTelemetry.Exporter.Instana.Tests/Processors/EventsActivityProcessorTests.cs @@ -12,7 +12,7 @@ namespace OpenTelemetry.Exporter.Instana.Tests.Processors; public class EventsActivityProcessorTests { - private EventsActivityProcessor eventsActivityProcessor = new(); + private readonly EventsActivityProcessor eventsActivityProcessor = new(); [Fact] public async Task ProcessAsync() diff --git a/test/OpenTelemetry.Exporter.Instana.Tests/Processors/TagsActivityProcessorTests.cs b/test/OpenTelemetry.Exporter.Instana.Tests/Processors/TagsActivityProcessorTests.cs index 377477f956..fb2a4d05a2 100644 --- a/test/OpenTelemetry.Exporter.Instana.Tests/Processors/TagsActivityProcessorTests.cs +++ b/test/OpenTelemetry.Exporter.Instana.Tests/Processors/TagsActivityProcessorTests.cs @@ -10,7 +10,7 @@ namespace OpenTelemetry.Exporter.Instana.Tests.Processors; public class TagsActivityProcessorTests { - private TagsActivityProcessor tagsActivityProcessor = new(); + private readonly TagsActivityProcessor tagsActivityProcessor = new(); [Fact] public async Task ProcessAsync_StatusTagsExist() From 3c919d308b25a41898eb8701dcc7e265e47b069a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 29 Oct 2024 10:27:43 +0100 Subject: [PATCH 06/13] Fix IDE0045 --- .../InstanaExporter.cs | 20 +++++-------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/src/OpenTelemetry.Exporter.Instana/InstanaExporter.cs b/src/OpenTelemetry.Exporter.Instana/InstanaExporter.cs index f7d4f661bf..aa7e3eee56 100644 --- a/src/OpenTelemetry.Exporter.Instana/InstanaExporter.cs +++ b/src/OpenTelemetry.Exporter.Instana/InstanaExporter.cs @@ -18,23 +18,13 @@ internal sealed class InstanaExporter : BaseExporter public InstanaExporter(IActivityProcessor? activityProcessor = null) { - if (activityProcessor != null) + this.activityProcessor = activityProcessor ?? new DefaultActivityProcessor { - this.activityProcessor = activityProcessor; - } - else - { - this.activityProcessor = new DefaultActivityProcessor + NextProcessor = new TagsActivityProcessor { - NextProcessor = new TagsActivityProcessor - { - NextProcessor = new EventsActivityProcessor - { - NextProcessor = new ErrorActivityProcessor(), - }, - }, - }; - } + NextProcessor = new EventsActivityProcessor { NextProcessor = new ErrorActivityProcessor() }, + }, + }; } internal ISpanSender SpanSender From f4add4cc8606db9c43bf6cbb8cfa9c38803bc539 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 29 Oct 2024 10:29:33 +0100 Subject: [PATCH 07/13] Fix IDE0063 --- .../Implementation/InstanaSpanSerializer.cs | 78 ++++++++-------- .../Implementation/Transport.cs | 88 +++++++++---------- .../InstanaSpanSerializerTests.cs | 26 +++--- 3 files changed, 91 insertions(+), 101 deletions(-) diff --git a/src/OpenTelemetry.Exporter.Instana/Implementation/InstanaSpanSerializer.cs b/src/OpenTelemetry.Exporter.Instana/Implementation/InstanaSpanSerializer.cs index b4702f84af..3046fa58ba 100644 --- a/src/OpenTelemetry.Exporter.Instana/Implementation/InstanaSpanSerializer.cs +++ b/src/OpenTelemetry.Exporter.Instana/Implementation/InstanaSpanSerializer.cs @@ -227,55 +227,53 @@ private static async Task SerializeEventsAsync(InstanaSpan instanaSpan, StreamWr return; } - using (var enumerator = instanaSpan.Data.Events.GetEnumerator()) + using var enumerator = instanaSpan.Data.Events.GetEnumerator(); + byte i = 0; + try { - byte i = 0; - try + await writer.WriteAsync("[").ConfigureAwait(false); + while (enumerator.MoveNext()) { - await writer.WriteAsync("[").ConfigureAwait(false); - while (enumerator.MoveNext()) + if (i > 0) { - if (i > 0) - { - await writer.WriteAsync(COMMA).ConfigureAwait(false); - } - else - { - i = 1; - } + await writer.WriteAsync(COMMA).ConfigureAwait(false); + } + else + { + i = 1; + } - await writer.WriteAsync(OPEN_BRACE).ConfigureAwait(false); + await writer.WriteAsync(OPEN_BRACE).ConfigureAwait(false); + await writer.WriteAsync(QUOTE).ConfigureAwait(false); + await writer.WriteAsync(InstanaExporterConstants.EVENT_NAME_FIELD).ConfigureAwait(false); + await writer.WriteAsync(QUOTE_COLON_QUOTE).ConfigureAwait(false); + await writer.WriteAsync(enumerator.Current.Name).ConfigureAwait(false); + await writer.WriteAsync(QUOTE_COMMA_QUOTE).ConfigureAwait(false); + await writer.WriteAsync(InstanaExporterConstants.EVENT_TIMESTAMP_FIELD).ConfigureAwait(false); + await writer.WriteAsync(QUOTE_COLON_QUOTE).ConfigureAwait(false); + await writer.WriteAsync(DateToUnixMillis(enumerator.Current.Ts).ToString(CultureInfo.InvariantCulture)).ConfigureAwait(false); + await writer.WriteAsync(QUOTE).ConfigureAwait(false); + + if (enumerator.Current.Tags.Count > 0) + { + await writer.WriteAsync(COMMA).ConfigureAwait(false); await writer.WriteAsync(QUOTE).ConfigureAwait(false); - await writer.WriteAsync(InstanaExporterConstants.EVENT_NAME_FIELD).ConfigureAwait(false); - await writer.WriteAsync(QUOTE_COLON_QUOTE).ConfigureAwait(false); - await writer.WriteAsync(enumerator.Current.Name).ConfigureAwait(false); - await writer.WriteAsync(QUOTE_COMMA_QUOTE).ConfigureAwait(false); - await writer.WriteAsync(InstanaExporterConstants.EVENT_TIMESTAMP_FIELD).ConfigureAwait(false); - await writer.WriteAsync(QUOTE_COLON_QUOTE).ConfigureAwait(false); - await writer.WriteAsync(DateToUnixMillis(enumerator.Current.Ts).ToString(CultureInfo.InvariantCulture)).ConfigureAwait(false); + await writer.WriteAsync(InstanaExporterConstants.TAGS_FIELD).ConfigureAwait(false); await writer.WriteAsync(QUOTE).ConfigureAwait(false); - - if (enumerator.Current.Tags.Count > 0) - { - await writer.WriteAsync(COMMA).ConfigureAwait(false); - await writer.WriteAsync(QUOTE).ConfigureAwait(false); - await writer.WriteAsync(InstanaExporterConstants.TAGS_FIELD).ConfigureAwait(false); - await writer.WriteAsync(QUOTE).ConfigureAwait(false); - await writer.WriteAsync(COLON).ConfigureAwait(false); - await SerializeTagsLogicAsync(enumerator.Current.Tags, writer).ConfigureAwait(false); - } - - await writer.WriteAsync(CLOSE_BRACE).ConfigureAwait(false); + await writer.WriteAsync(COLON).ConfigureAwait(false); + await SerializeTagsLogicAsync(enumerator.Current.Tags, writer).ConfigureAwait(false); } - await writer.WriteAsync("]").ConfigureAwait(false); - } - catch (InvalidOperationException) - { - // if the collection gets modified while serializing, we might get a collision. - // There is no good way of preventing this and continuing normally except locking - // which needs investigation + await writer.WriteAsync(CLOSE_BRACE).ConfigureAwait(false); } + + await writer.WriteAsync("]").ConfigureAwait(false); + } + catch (InvalidOperationException) + { + // if the collection gets modified while serializing, we might get a collision. + // There is no good way of preventing this and continuing normally except locking + // which needs investigation } } } diff --git a/src/OpenTelemetry.Exporter.Instana/Implementation/Transport.cs b/src/OpenTelemetry.Exporter.Instana/Implementation/Transport.cs index e9fac2ec7c..7472a6a705 100644 --- a/src/OpenTelemetry.Exporter.Instana/Implementation/Transport.cs +++ b/src/OpenTelemetry.Exporter.Instana/Implementation/Transport.cs @@ -39,56 +39,50 @@ internal static async Task SendSpansAsync(ConcurrentQueue spanQueue { try { - using (var sendBuffer = new MemoryStream(TracesBuffer)) + using var sendBuffer = new MemoryStream(TracesBuffer); + using var writer = new StreamWriter(sendBuffer); + await writer.WriteAsync("{\"spans\":[").ConfigureAwait(false); + var first = true; + + // peek instead of dequeue, because we don't yet know whether the next span + // fits within our MULTI_SPAN_BUFFER_LIMIT + while (spanQueue.TryPeek(out var span) && sendBuffer.Position < MultiSpanBufferLimit) { - using (var writer = new StreamWriter(sendBuffer)) + if (!first) { - await writer.WriteAsync("{\"spans\":[").ConfigureAwait(false); - var first = true; - - // peek instead of dequeue, because we don't yet know whether the next span - // fits within our MULTI_SPAN_BUFFER_LIMIT - while (spanQueue.TryPeek(out var span) && sendBuffer.Position < MultiSpanBufferLimit) - { - if (!first) - { - await writer.WriteAsync(",").ConfigureAwait(false); - } - - await InstanaSpanSerializer.SerializeToStreamWriterAsync(span, writer).ConfigureAwait(false); - await writer.FlushAsync().ConfigureAwait(false); - - first = false; - - // Now we can dequeue. Note, this means we'll be giving up/losing - // this span if we fail to send for any reason. - spanQueue.TryDequeue(out _); - } - - await writer.WriteAsync("]}").ConfigureAwait(false); - await writer.FlushAsync().ConfigureAwait(false); - - var length = sendBuffer.Position; - sendBuffer.Position = 0; - sendBuffer.SetLength(length); - - HttpContent content = new StreamContent(sendBuffer, (int)length); - content.Headers.ContentType = MEDIAHEADER; - content.Headers.Add("X-INSTANA-TIME", DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString(CultureInfo.InvariantCulture)); - - using (var httpMsg = new HttpRequestMessage() - { - Method = HttpMethod.Post, - RequestUri = new Uri(bundleUrl), - }) - { - httpMsg.Content = content; - if (Client != null) - { - await Client.SendAsync(httpMsg).ConfigureAwait(false); - } - } + await writer.WriteAsync(",").ConfigureAwait(false); } + + await InstanaSpanSerializer.SerializeToStreamWriterAsync(span, writer).ConfigureAwait(false); + await writer.FlushAsync().ConfigureAwait(false); + + first = false; + + // Now we can dequeue. Note, this means we'll be giving up/losing + // this span if we fail to send for any reason. + spanQueue.TryDequeue(out _); + } + + await writer.WriteAsync("]}").ConfigureAwait(false); + await writer.FlushAsync().ConfigureAwait(false); + + var length = sendBuffer.Position; + sendBuffer.Position = 0; + sendBuffer.SetLength(length); + + HttpContent content = new StreamContent(sendBuffer, (int)length); + content.Headers.ContentType = MEDIAHEADER; + content.Headers.Add("X-INSTANA-TIME", DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString(CultureInfo.InvariantCulture)); + + using var httpMsg = new HttpRequestMessage() + { + Method = HttpMethod.Post, + RequestUri = new Uri(bundleUrl), + }; + httpMsg.Content = content; + if (Client != null) + { + await Client.SendAsync(httpMsg).ConfigureAwait(false); } } catch (Exception e) diff --git a/test/OpenTelemetry.Exporter.Instana.Tests/InstanaSpanSerializerTests.cs b/test/OpenTelemetry.Exporter.Instana.Tests/InstanaSpanSerializerTests.cs index 98692e0817..db27cd629c 100644 --- a/test/OpenTelemetry.Exporter.Instana.Tests/InstanaSpanSerializerTests.cs +++ b/test/OpenTelemetry.Exporter.Instana.Tests/InstanaSpanSerializerTests.cs @@ -46,22 +46,20 @@ public static async Task SerializeToStreamWriterAsync() InstanaSpanTest? span; using (var sendBuffer = new MemoryStream(new byte[4096000])) { - using (var writer = new StreamWriter(sendBuffer)) - { - await InstanaSpanSerializer.SerializeToStreamWriterAsync(instanaOtelSpan, writer); - await writer.FlushAsync(); - var length = sendBuffer.Position; - sendBuffer.Position = 0; - sendBuffer.SetLength(length); + using var writer = new StreamWriter(sendBuffer); + await InstanaSpanSerializer.SerializeToStreamWriterAsync(instanaOtelSpan, writer); + await writer.FlushAsync(); + var length = sendBuffer.Position; + sendBuffer.Position = 0; + sendBuffer.SetLength(length); - var serializer = new JsonSerializer(); - serializer.Converters.Add(new JavaScriptDateTimeConverter()); - serializer.NullValueHandling = NullValueHandling.Ignore; + var serializer = new JsonSerializer(); + serializer.Converters.Add(new JavaScriptDateTimeConverter()); + serializer.NullValueHandling = NullValueHandling.Ignore; - TextReader textReader = new StreamReader(sendBuffer); - JsonReader reader = new JsonTextReader(textReader); - span = serializer.Deserialize(reader); - } + TextReader textReader = new StreamReader(sendBuffer); + JsonReader reader = new JsonTextReader(textReader); + span = serializer.Deserialize(reader); } Assert.NotNull(span); From 143066eda9244be79ce73b9619ce6abd0264453c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 29 Oct 2024 10:30:21 +0100 Subject: [PATCH 08/13] Fix IDE0074 --- .../Processors/ActivityProcessorBase.cs | 18 ++++++------------ .../InstanaSpanTest.cs | 5 +---- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/src/OpenTelemetry.Exporter.Instana/Implementation/Processors/ActivityProcessorBase.cs b/src/OpenTelemetry.Exporter.Instana/Implementation/Processors/ActivityProcessorBase.cs index e4dbea1f3a..ff86416aae 100644 --- a/src/OpenTelemetry.Exporter.Instana/Implementation/Processors/ActivityProcessorBase.cs +++ b/src/OpenTelemetry.Exporter.Instana/Implementation/Processors/ActivityProcessorBase.cs @@ -19,19 +19,13 @@ public virtual async Task ProcessAsync(Activity activity, InstanaSpan instanaSpa protected virtual void PreProcess(Activity activity, InstanaSpan instanaSpan) { - if (instanaSpan.TransformInfo == null) - { - instanaSpan.TransformInfo = new InstanaSpanTransformInfo(); - } + instanaSpan.TransformInfo ??= new InstanaSpanTransformInfo(); - if (instanaSpan.Data == null) + instanaSpan.Data ??= new Data() { - instanaSpan.Data = new Data() - { - data = new Dictionary(), - Events = new List(8), - Tags = new Dictionary(), - }; - } + data = [], + Events = new List(8), + Tags = [], + }; } } diff --git a/test/OpenTelemetry.Exporter.Instana.Tests/InstanaSpanTest.cs b/test/OpenTelemetry.Exporter.Instana.Tests/InstanaSpanTest.cs index e830fa610c..f6680301e7 100644 --- a/test/OpenTelemetry.Exporter.Instana.Tests/InstanaSpanTest.cs +++ b/test/OpenTelemetry.Exporter.Instana.Tests/InstanaSpanTest.cs @@ -158,10 +158,7 @@ public override bool CanConvert(Type objectType) continue; } - if (newData.data == null) - { - newData.data = new Dictionary(); - } + newData.data ??= []; newData.data[((JProperty)field).Name] = ((JProperty)field).Value.ToString(); } From e17d203b233ecb4372d31a7a2c295d958026d21b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 29 Oct 2024 10:30:52 +0100 Subject: [PATCH 09/13] Fix IDE0078 --- test/OpenTelemetry.Exporter.Instana.Tests/InstanaSpanTest.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/OpenTelemetry.Exporter.Instana.Tests/InstanaSpanTest.cs b/test/OpenTelemetry.Exporter.Instana.Tests/InstanaSpanTest.cs index f6680301e7..32bfef6851 100644 --- a/test/OpenTelemetry.Exporter.Instana.Tests/InstanaSpanTest.cs +++ b/test/OpenTelemetry.Exporter.Instana.Tests/InstanaSpanTest.cs @@ -152,8 +152,7 @@ public override bool CanConvert(Type objectType) var newData = new Data(); foreach (var field in data) { - if (((JProperty)field).Name == "tags" || - ((JProperty)field).Name == "events") + if (((JProperty)field).Name is "tags" or "events") { continue; } From 76e76ff2840db0cc8d088ffe1111146523122715 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 29 Oct 2024 10:32:31 +0100 Subject: [PATCH 10/13] Fix IDE0032 --- .../InstanaExporter.cs | 20 +++++-------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/src/OpenTelemetry.Exporter.Instana/InstanaExporter.cs b/src/OpenTelemetry.Exporter.Instana/InstanaExporter.cs index aa7e3eee56..1c2fc4ad23 100644 --- a/src/OpenTelemetry.Exporter.Instana/InstanaExporter.cs +++ b/src/OpenTelemetry.Exporter.Instana/InstanaExporter.cs @@ -12,8 +12,6 @@ namespace OpenTelemetry.Exporter.Instana; internal sealed class InstanaExporter : BaseExporter { private readonly IActivityProcessor activityProcessor; - private ISpanSender spanSender = new SpanSender(); - private IInstanaExporterHelper instanaExporterHelper = new InstanaExporterHelper(); private bool shutdownCalled; public InstanaExporter(IActivityProcessor? activityProcessor = null) @@ -27,17 +25,9 @@ public InstanaExporter(IActivityProcessor? activityProcessor = null) }; } - internal ISpanSender SpanSender - { - get { return this.spanSender; } - set { this.spanSender = value; } - } + internal ISpanSender SpanSender { get; set; } = new SpanSender(); - internal IInstanaExporterHelper InstanaExporterHelper - { - get { return this.instanaExporterHelper; } - set { this.instanaExporterHelper = value; } - } + internal IInstanaExporterHelper InstanaExporterHelper { get; set; } = new InstanaExporterHelper(); public override ExportResult Export(in Batch batch) { @@ -47,7 +37,7 @@ public override ExportResult Export(in Batch batch) } var from = new From(); - if (this.instanaExporterHelper.IsWindows()) + if (this.InstanaExporterHelper.IsWindows()) { from = new From { E = Process.GetCurrentProcess().Id.ToString(CultureInfo.InvariantCulture) }; } @@ -62,7 +52,7 @@ public override ExportResult Export(in Batch batch) } var span = this.ParseActivityAsync(activity, serviceName, from).Result; - this.spanSender.Enqueue(span); + this.SpanSender.Enqueue(span); } return ExportResult.Success; @@ -92,7 +82,7 @@ private string ExtractServiceName(ref From from) var serviceId = string.Empty; var processId = string.Empty; var hostId = string.Empty; - var resource = this.instanaExporterHelper.GetParentProviderResource(this); + var resource = this.InstanaExporterHelper.GetParentProviderResource(this); if (resource != Resource.Empty && resource.Attributes.Any()) { foreach (var resourceAttribute in resource.Attributes) From 9c4551689a809aae334c2a5270283fb73f4d8813 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 29 Oct 2024 10:32:55 +0100 Subject: [PATCH 11/13] Fix IDE0032 --- .../Implementation/Transport.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/OpenTelemetry.Exporter.Instana/Implementation/Transport.cs b/src/OpenTelemetry.Exporter.Instana/Implementation/Transport.cs index 7472a6a705..97cee57f7b 100644 --- a/src/OpenTelemetry.Exporter.Instana/Implementation/Transport.cs +++ b/src/OpenTelemetry.Exporter.Instana/Implementation/Transport.cs @@ -28,10 +28,7 @@ static Transport() Configure(); } - internal static bool IsAvailable - { - get { return isConfigured && Client != null; } - } + internal static bool IsAvailable => isConfigured && Client != null; internal static InstanaHttpClient? Client { get; set; } From c2458a5c8e97eae4599722bc16218962d735bf32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 29 Oct 2024 10:33:27 +0100 Subject: [PATCH 12/13] Fix IDE0066 --- .../Processors/DefaultActivityProcessor.cs | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/OpenTelemetry.Exporter.Instana/Implementation/Processors/DefaultActivityProcessor.cs b/src/OpenTelemetry.Exporter.Instana/Implementation/Processors/DefaultActivityProcessor.cs index 0682328662..3c6447bcec 100644 --- a/src/OpenTelemetry.Exporter.Instana/Implementation/Processors/DefaultActivityProcessor.cs +++ b/src/OpenTelemetry.Exporter.Instana/Implementation/Processors/DefaultActivityProcessor.cs @@ -52,19 +52,13 @@ public override async Task ProcessAsync(Activity activity, InstanaSpan instanaSp private static SpanKind GetSpanKind(ActivityKind activityKind) { - switch (activityKind) + return activityKind switch { - case ActivityKind.Consumer: - case ActivityKind.Server: - return SpanKind.ENTRY; - case ActivityKind.Client: - case ActivityKind.Producer: - return SpanKind.EXIT; - case ActivityKind.Internal: - return SpanKind.INTERMEDIATE; - default: - return SpanKind.NOT_SET; - } + ActivityKind.Consumer or ActivityKind.Server => SpanKind.ENTRY, + ActivityKind.Client or ActivityKind.Producer => SpanKind.EXIT, + ActivityKind.Internal => SpanKind.INTERMEDIATE, + _ => SpanKind.NOT_SET, + }; } private static long GetLongFromHex(string hexValue) From 2e956bb83420bda93f1c210ee1558a30893b7e69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 29 Oct 2024 11:40:08 +0100 Subject: [PATCH 13/13] Fix IDe0028 --- .../Implementation/InstanaSpan.cs | 2 +- .../Implementation/InstanaSpanFactory.cs | 4 ++-- .../Processors/EventsActivityProcessor.cs | 2 +- .../InstanaExporterTests.cs | 2 +- .../InstanaSpanSerializerTests.cs | 24 +++++++++---------- .../EventsActivityProcessorTests.cs | 6 ++--- .../TestInstanaExporterHelper.cs | 2 +- 7 files changed, 20 insertions(+), 22 deletions(-) diff --git a/src/OpenTelemetry.Exporter.Instana/Implementation/InstanaSpan.cs b/src/OpenTelemetry.Exporter.Instana/Implementation/InstanaSpan.cs index 3230e8d0e0..159baf28cf 100644 --- a/src/OpenTelemetry.Exporter.Instana/Implementation/InstanaSpan.cs +++ b/src/OpenTelemetry.Exporter.Instana/Implementation/InstanaSpan.cs @@ -241,7 +241,7 @@ internal class SpanEvent #pragma warning restore SA1402 // File may only contain a single type { private string name = string.Empty; - private Dictionary tags = new Dictionary(); + private Dictionary tags = []; public string Name { diff --git a/src/OpenTelemetry.Exporter.Instana/Implementation/InstanaSpanFactory.cs b/src/OpenTelemetry.Exporter.Instana/Implementation/InstanaSpanFactory.cs index efbfae2e1f..fe3dbf4843 100644 --- a/src/OpenTelemetry.Exporter.Instana/Implementation/InstanaSpanFactory.cs +++ b/src/OpenTelemetry.Exporter.Instana/Implementation/InstanaSpanFactory.cs @@ -11,8 +11,8 @@ internal static InstanaSpan CreateSpan() { Data = new Data() { - data = new Dictionary(), - Tags = new Dictionary(), + data = [], + Tags = [], Events = new List(8), }, diff --git a/src/OpenTelemetry.Exporter.Instana/Implementation/Processors/EventsActivityProcessor.cs b/src/OpenTelemetry.Exporter.Instana/Implementation/Processors/EventsActivityProcessor.cs index 1c1fb00c61..df3e8decf8 100644 --- a/src/OpenTelemetry.Exporter.Instana/Implementation/Processors/EventsActivityProcessor.cs +++ b/src/OpenTelemetry.Exporter.Instana/Implementation/Processors/EventsActivityProcessor.cs @@ -22,7 +22,7 @@ public override async Task ProcessAsync(Activity activity, InstanaSpan instanaSp { Name = activityEvent.Name, Ts = activityEvent.Timestamp.Ticks, - Tags = new Dictionary(), + Tags = [], }; foreach (var eventTag in activityEvent.Tags) diff --git a/test/OpenTelemetry.Exporter.Instana.Tests/InstanaExporterTests.cs b/test/OpenTelemetry.Exporter.Instana.Tests/InstanaExporterTests.cs index 535034b2a0..d076911310 100644 --- a/test/OpenTelemetry.Exporter.Instana.Tests/InstanaExporterTests.cs +++ b/test/OpenTelemetry.Exporter.Instana.Tests/InstanaExporterTests.cs @@ -36,7 +36,7 @@ public void Export() activity.SetStatus(ActivityStatusCode.Error, "TestErrorDesc"); activity.TraceStateString = "TraceStateString"; - Activity[] activities =[activity]; + Activity[] activities = [activity]; var batch = new Batch(activities, activities.Length); var result = this.instanaExporter.Export(in batch); diff --git a/test/OpenTelemetry.Exporter.Instana.Tests/InstanaSpanSerializerTests.cs b/test/OpenTelemetry.Exporter.Instana.Tests/InstanaSpanSerializerTests.cs index db27cd629c..6063f4befc 100644 --- a/test/OpenTelemetry.Exporter.Instana.Tests/InstanaSpanSerializerTests.cs +++ b/test/OpenTelemetry.Exporter.Instana.Tests/InstanaSpanSerializerTests.cs @@ -23,25 +23,25 @@ public static async Task SerializeToStreamWriterAsync() instanaOtelSpan.D = 123456; instanaOtelSpan.Lt = "hexNumberLT1234567890123"; instanaOtelSpan.Tp = true; - instanaOtelSpan.Data.Tags = new Dictionary(); - instanaOtelSpan.Data.Tags["tag1Key"] = "tag1Vale"; - instanaOtelSpan.Data.Tags["tag2Key"] = "tag2Vale"; - instanaOtelSpan.Data.data = new Dictionary(); - instanaOtelSpan.Data.data["data1Key"] = "data1Vale"; - instanaOtelSpan.Data.data["data2Key"] = "data2Vale"; - instanaOtelSpan.Data.Events = new List - { + instanaOtelSpan.Data.Tags = new Dictionary { ["tag1Key"] = "tag1Vale", ["tag2Key"] = "tag2Vale" }; + instanaOtelSpan.Data.data = new Dictionary { ["data1Key"] = "data1Vale", ["data2Key"] = "data2Vale" }; + instanaOtelSpan.Data.Events = + [ new() { - Name = "testEvent", Ts = 111111, + Name = "testEvent", + Ts = 111111, Tags = new Dictionary { { "eventTagKey", "eventTagValue" } }, }, + new() { - Name = "testEvent2", Ts = 222222, + Name = "testEvent2", + Ts = 222222, Tags = new Dictionary { { "eventTag2Key", "eventTag2Value" } }, - }, - }; + } + + ]; InstanaSpanTest? span; using (var sendBuffer = new MemoryStream(new byte[4096000])) diff --git a/test/OpenTelemetry.Exporter.Instana.Tests/Processors/EventsActivityProcessorTests.cs b/test/OpenTelemetry.Exporter.Instana.Tests/Processors/EventsActivityProcessorTests.cs index 54faaa87be..35452ace15 100644 --- a/test/OpenTelemetry.Exporter.Instana.Tests/Processors/EventsActivityProcessorTests.cs +++ b/test/OpenTelemetry.Exporter.Instana.Tests/Processors/EventsActivityProcessorTests.cs @@ -17,15 +17,13 @@ public class EventsActivityProcessorTests [Fact] public async Task ProcessAsync() { - var activityTagsCollection = new ActivityTagsCollection(); - activityTagsCollection.Add(new KeyValuePair("eventTagKey", "eventTagValue")); + var activityTagsCollection = new ActivityTagsCollection { new KeyValuePair("eventTagKey", "eventTagValue") }; var activityEvent = new ActivityEvent( "testActivityEvent", DateTimeOffset.MinValue, activityTagsCollection); - var activityTagsCollection2 = new ActivityTagsCollection(); - activityTagsCollection2.Add(new KeyValuePair("eventTagKey2", "eventTagValue2")); + var activityTagsCollection2 = new ActivityTagsCollection { new KeyValuePair("eventTagKey2", "eventTagValue2") }; var activityEvent2 = new ActivityEvent( "testActivityEvent2", DateTimeOffset.MaxValue, diff --git a/test/OpenTelemetry.Exporter.Instana.Tests/TestInstanaExporterHelper.cs b/test/OpenTelemetry.Exporter.Instana.Tests/TestInstanaExporterHelper.cs index bf48310c1f..7a88315de0 100644 --- a/test/OpenTelemetry.Exporter.Instana.Tests/TestInstanaExporterHelper.cs +++ b/test/OpenTelemetry.Exporter.Instana.Tests/TestInstanaExporterHelper.cs @@ -8,7 +8,7 @@ namespace OpenTelemetry.Exporter.Instana.Tests; internal class TestInstanaExporterHelper : IInstanaExporterHelper { - public Dictionary Attributes { get; } = new(); + public Dictionary Attributes { get; } = []; public Resource GetParentProviderResource(BaseExporter otelExporter) {