From 82497a8ca501f57c85f5ad8e0f03102a4f4a13bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 29 Oct 2024 05:59:15 +0100 Subject: [PATCH 1/7] Fix IDE0301 --- .../PersistentBlobProvider.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/OpenTelemetry.PersistentStorage.Abstractions/PersistentBlobProvider.cs b/src/OpenTelemetry.PersistentStorage.Abstractions/PersistentBlobProvider.cs index 29d3dee8a0..465af27746 100644 --- a/src/OpenTelemetry.PersistentStorage.Abstractions/PersistentBlobProvider.cs +++ b/src/OpenTelemetry.PersistentStorage.Abstractions/PersistentBlobProvider.cs @@ -102,12 +102,12 @@ public IEnumerable GetBlobs() { try { - return this.OnGetBlobs() ?? Enumerable.Empty(); + return this.OnGetBlobs() ?? []; } catch (Exception ex) { PersistentStorageAbstractionsEventSource.Log.PersistentStorageAbstractionsException(nameof(PersistentBlobProvider), "Failed to get all the blobs", ex); - return Enumerable.Empty(); + return []; } } From 114051d0e06f7936a43e4fda9cc27d69af503686 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 29 Oct 2024 06:00:01 +0100 Subject: [PATCH 2/7] Fix IDE0007 --- .../DirectorySizeTracker.cs | 6 +++--- .../FileBlob.cs | 2 +- .../FileBlobProvider.cs | 4 ++-- .../PersistentStorageHelper.cs | 10 +++++----- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/OpenTelemetry.PersistentStorage.FileSystem/DirectorySizeTracker.cs b/src/OpenTelemetry.PersistentStorage.FileSystem/DirectorySizeTracker.cs index 2bbc352817..d251885f81 100644 --- a/src/OpenTelemetry.PersistentStorage.FileSystem/DirectorySizeTracker.cs +++ b/src/OpenTelemetry.PersistentStorage.FileSystem/DirectorySizeTracker.cs @@ -56,16 +56,16 @@ internal static long CalculateFolderSize(string path) long directorySize = 0; try { - foreach (string file in Directory.EnumerateFiles(path)) + foreach (var file in Directory.EnumerateFiles(path)) { if (File.Exists(file)) { - FileInfo fileInfo = new FileInfo(file); + var fileInfo = new FileInfo(file); directorySize += fileInfo.Length; } } - foreach (string dir in Directory.GetDirectories(path)) + foreach (var dir in Directory.GetDirectories(path)) { directorySize += CalculateFolderSize(dir); } diff --git a/src/OpenTelemetry.PersistentStorage.FileSystem/FileBlob.cs b/src/OpenTelemetry.PersistentStorage.FileSystem/FileBlob.cs index cfb1c395d0..72f121b950 100644 --- a/src/OpenTelemetry.PersistentStorage.FileSystem/FileBlob.cs +++ b/src/OpenTelemetry.PersistentStorage.FileSystem/FileBlob.cs @@ -58,7 +58,7 @@ protected override bool OnTryWrite(byte[] buffer, int leasePeriodMilliseconds = { Guard.ThrowIfNull(buffer); - string path = this.FullPath + ".tmp"; + var path = this.FullPath + ".tmp"; try { diff --git a/src/OpenTelemetry.PersistentStorage.FileSystem/FileBlobProvider.cs b/src/OpenTelemetry.PersistentStorage.FileSystem/FileBlobProvider.cs index b0abae2f40..4f7fe9ef6b 100644 --- a/src/OpenTelemetry.PersistentStorage.FileSystem/FileBlobProvider.cs +++ b/src/OpenTelemetry.PersistentStorage.FileSystem/FileBlobProvider.cs @@ -121,7 +121,7 @@ protected override IEnumerable OnGetBlobs() foreach (var file in Directory.EnumerateFiles(this.DirectoryPath, "*.blob", SearchOption.TopDirectoryOnly).OrderByDescending(f => f)) { - DateTime fileDateTime = PersistentStorageHelper.GetDateTimeFromBlobName(file); + var fileDateTime = PersistentStorageHelper.GetDateTimeFromBlobName(file); if (fileDateTime > retentionDeadline) { yield return new FileBlob(file, this.directorySizeTracker); @@ -173,7 +173,7 @@ private void OnMaintenanceEvent(object? source, ElapsedEventArgs e) private bool CheckStorageSize() { - if (!this.directorySizeTracker.IsSpaceAvailable(out long size)) + if (!this.directorySizeTracker.IsSpaceAvailable(out var size)) { // TODO: check accuracy of size reporting. PersistentStorageEventSource.Log.PersistentStorageWarning( diff --git a/src/OpenTelemetry.PersistentStorage.FileSystem/PersistentStorageHelper.cs b/src/OpenTelemetry.PersistentStorage.FileSystem/PersistentStorageHelper.cs index 63005646fc..f72d53f66f 100644 --- a/src/OpenTelemetry.PersistentStorage.FileSystem/PersistentStorageHelper.cs +++ b/src/OpenTelemetry.PersistentStorage.FileSystem/PersistentStorageHelper.cs @@ -12,7 +12,7 @@ internal static void RemoveExpiredBlob(DateTime retentionDeadline, string filePa { if (filePath.EndsWith(".blob", StringComparison.OrdinalIgnoreCase)) { - DateTime fileDateTime = GetDateTimeFromBlobName(filePath); + var fileDateTime = GetDateTimeFromBlobName(filePath); if (fileDateTime < retentionDeadline) { try @@ -30,11 +30,11 @@ internal static void RemoveExpiredBlob(DateTime retentionDeadline, string filePa internal static bool RemoveExpiredLease(DateTime leaseDeadline, string filePath) { - bool success = false; + var success = false; if (filePath.EndsWith(".lock", StringComparison.OrdinalIgnoreCase)) { - DateTime fileDateTime = GetDateTimeFromLeaseName(filePath); + var fileDateTime = GetDateTimeFromLeaseName(filePath); if (fileDateTime < leaseDeadline) { var newFilePath = filePath.Substring(0, filePath.LastIndexOf('@')); @@ -55,11 +55,11 @@ internal static bool RemoveExpiredLease(DateTime leaseDeadline, string filePath) internal static bool RemoveTimedOutTmpFiles(DateTime timeoutDeadline, string filePath) { - bool success = false; + var success = false; if (filePath.EndsWith(".tmp", StringComparison.OrdinalIgnoreCase)) { - DateTime fileDateTime = GetDateTimeFromBlobName(filePath); + var fileDateTime = GetDateTimeFromBlobName(filePath); if (fileDateTime < timeoutDeadline) { try From ae33e666f7d12e57cf517a703f845283aa8d9bef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 29 Oct 2024 06:00:36 +0100 Subject: [PATCH 3/7] Fix IDE0046 --- .../FileBlobProvider.cs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/OpenTelemetry.PersistentStorage.FileSystem/FileBlobProvider.cs b/src/OpenTelemetry.PersistentStorage.FileSystem/FileBlobProvider.cs index 4f7fe9ef6b..edd1e1d489 100644 --- a/src/OpenTelemetry.PersistentStorage.FileSystem/FileBlobProvider.cs +++ b/src/OpenTelemetry.PersistentStorage.FileSystem/FileBlobProvider.cs @@ -197,14 +197,7 @@ private bool CheckStorageSize() var blobFilePath = Path.Combine(this.DirectoryPath, PersistentStorageHelper.GetUniqueFileName(".blob")); var blob = new FileBlob(blobFilePath, this.directorySizeTracker); - if (blob.TryWrite(buffer, leasePeriodMilliseconds)) - { - return blob; - } - else - { - return null; - } + return blob.TryWrite(buffer, leasePeriodMilliseconds) ? blob : null; } catch (Exception ex) { From 0f8fd3f0e9c347db55a4d9a341bd90d2dd00c5bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 29 Oct 2024 06:05:06 +0100 Subject: [PATCH 4/7] Fix CA1514 --- .../PersistentStorageHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenTelemetry.PersistentStorage.FileSystem/PersistentStorageHelper.cs b/src/OpenTelemetry.PersistentStorage.FileSystem/PersistentStorageHelper.cs index f72d53f66f..6d2fd821cb 100644 --- a/src/OpenTelemetry.PersistentStorage.FileSystem/PersistentStorageHelper.cs +++ b/src/OpenTelemetry.PersistentStorage.FileSystem/PersistentStorageHelper.cs @@ -144,7 +144,7 @@ internal static DateTime GetDateTimeFromLeaseName(string filePath) { var fileName = Path.GetFileNameWithoutExtension(filePath); var startIndex = fileName.LastIndexOf('@') + 1; - var time = fileName.Substring(startIndex, fileName.Length - startIndex); + var time = fileName.Substring(startIndex); DateTime.TryParseExact(time, "yyyy-MM-ddTHHmmss.fffffffZ", CultureInfo.InvariantCulture, DateTimeStyles.None, out var dateTime); return dateTime.ToUniversalTime(); } From 418d31c8a4aa79fb99812a0b4fe44c6810cd6a40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 29 Oct 2024 06:05:44 +0100 Subject: [PATCH 5/7] Fix IDE0301 - tests --- test/Shared/EventSourceTestHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Shared/EventSourceTestHelper.cs b/test/Shared/EventSourceTestHelper.cs index c430f52650..7c9619dbf4 100644 --- a/test/Shared/EventSourceTestHelper.cs +++ b/test/Shared/EventSourceTestHelper.cs @@ -101,7 +101,7 @@ private static void VerifyEventMessage(MethodInfo eventMethod, EventWrittenEvent string actualMessage = string.Format( CultureInfo.InvariantCulture, actualEvent.Message!, - actualEvent.Payload?.ToArray() ?? Array.Empty()); + actualEvent.Payload?.ToArray() ?? []); AssertEqual(nameof(VerifyEventMessage), expectedMessage, actualMessage); } From f23f5c334e8e14b848f1117d68022df2c3e4dcff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 29 Oct 2024 06:07:26 +0100 Subject: [PATCH 6/7] Fix IDE0007 - tests --- test/Shared/EventSourceTestHelper.cs | 18 +++++++++--------- test/Shared/TestEventListener.cs | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/test/Shared/EventSourceTestHelper.cs b/test/Shared/EventSourceTestHelper.cs index 7c9619dbf4..00eb9e7c8f 100644 --- a/test/Shared/EventSourceTestHelper.cs +++ b/test/Shared/EventSourceTestHelper.cs @@ -13,7 +13,7 @@ internal static class EventSourceTestHelper { public static void MethodsAreImplementedConsistentlyWithTheirAttributes(EventSource eventSource) { - foreach (MethodInfo publicMethod in GetEventMethods(eventSource)) + foreach (var publicMethod in GetEventMethods(eventSource)) { VerifyMethodImplementation(eventSource, publicMethod); } @@ -25,7 +25,7 @@ private static void VerifyMethodImplementation(EventSource eventSource, MethodIn listener.EnableEvents(eventSource, EventLevel.Verbose, EventKeywords.All); try { - object[] eventArguments = GenerateEventArguments(eventMethod); + var eventArguments = GenerateEventArguments(eventMethod); eventMethod.Invoke(eventSource, eventArguments); var actualEvent = listener.Messages.First(q => q.EventName == eventMethod.Name); @@ -54,9 +54,9 @@ private static void VerifyMethodImplementation(EventSource eventSource, MethodIn private static object[] GenerateEventArguments(MethodInfo eventMethod) { - ParameterInfo[] parameters = eventMethod.GetParameters(); + var parameters = eventMethod.GetParameters(); var arguments = new object[parameters.Length]; - for (int i = 0; i < parameters.Length; i++) + for (var i = 0; i < parameters.Length; i++) { arguments[i] = GenerateArgument(parameters[i]); } @@ -82,23 +82,23 @@ private static object GenerateArgument(ParameterInfo parameter) private static void VerifyEventId(MethodInfo eventMethod, EventWrittenEventArgs actualEvent) { - int expectedEventId = GetEventAttribute(eventMethod).EventId; + var expectedEventId = GetEventAttribute(eventMethod).EventId; AssertEqual(nameof(VerifyEventId), expectedEventId, actualEvent.EventId); } private static void VerifyEventLevel(MethodInfo eventMethod, EventWrittenEventArgs actualEvent) { - EventLevel expectedLevel = GetEventAttribute(eventMethod).Level; + var expectedLevel = GetEventAttribute(eventMethod).Level; AssertEqual(nameof(VerifyEventLevel), expectedLevel, actualEvent.Level); } private static void VerifyEventMessage(MethodInfo eventMethod, EventWrittenEventArgs actualEvent, object[] eventArguments) { - string expectedMessage = eventArguments.Length == 0 + var expectedMessage = eventArguments.Length == 0 ? GetEventAttribute(eventMethod).Message! : string.Format(CultureInfo.InvariantCulture, GetEventAttribute(eventMethod).Message!, eventArguments); - string actualMessage = string.Format( + var actualMessage = string.Format( CultureInfo.InvariantCulture, actualEvent.Message!, actualEvent.Payload?.ToArray() ?? []); @@ -128,7 +128,7 @@ private static EventAttribute GetEventAttribute(MethodInfo eventMethod) private static IEnumerable GetEventMethods(EventSource eventSource) { - MethodInfo[] methods = eventSource.GetType().GetMethods(); + var methods = eventSource.GetType().GetMethods(); return methods.Where(m => m.GetCustomAttributes(typeof(EventAttribute), false).Length != 0); } } diff --git a/test/Shared/TestEventListener.cs b/test/Shared/TestEventListener.cs index c6b8db9c62..424ba512a6 100644 --- a/test/Shared/TestEventListener.cs +++ b/test/Shared/TestEventListener.cs @@ -77,7 +77,7 @@ protected override void OnEventWritten(EventWrittenEventArgs eventData) protected override void OnEventSourceCreated(EventSource eventSource) { // Check for null because this method is called by the base class constructor before we can initialize it - Action? callback = this.OnOnEventSourceCreated; + var callback = this.OnOnEventSourceCreated; callback?.Invoke(eventSource); } } From ba96e6ef61dd0d8370ada1e464c08b43162b6e20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 29 Oct 2024 06:08:08 +0100 Subject: [PATCH 7/7] Fix IDE0046 - tests --- test/Shared/EventSourceTestHelper.cs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/test/Shared/EventSourceTestHelper.cs b/test/Shared/EventSourceTestHelper.cs index 00eb9e7c8f..02de7a2a76 100644 --- a/test/Shared/EventSourceTestHelper.cs +++ b/test/Shared/EventSourceTestHelper.cs @@ -71,13 +71,11 @@ private static object GenerateArgument(ParameterInfo parameter) return "Test String"; } - if (parameter.ParameterType.IsValueType) - { - return Activator.CreateInstance(parameter.ParameterType) - ?? throw new NotSupportedException($"Could not create an instance of the '{parameter.ParameterType}' type."); - } - - throw new NotSupportedException("Complex types are not supported"); + return parameter.ParameterType.IsValueType + ? Activator.CreateInstance(parameter.ParameterType) + ?? throw new NotSupportedException( + $"Could not create an instance of the '{parameter.ParameterType}' type.") + : throw new NotSupportedException("Complex types are not supported"); } private static void VerifyEventId(MethodInfo eventMethod, EventWrittenEventArgs actualEvent)