diff --git a/CHANGELOG.md b/CHANGELOG.md index 11ea5576e7..d3821448d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### Fixes + +- Ignore zero properties for MemoryInfo ([#1531](https://github.com/getsentry/sentry-dotnet/pull/1531)) + ## 3.15.0 ### Features diff --git a/src/Sentry/Internal/Extensions/JsonExtensions.cs b/src/Sentry/Internal/Extensions/JsonExtensions.cs index 802a728ac8..1fbd32097a 100644 --- a/src/Sentry/Internal/Extensions/JsonExtensions.cs +++ b/src/Sentry/Internal/Extensions/JsonExtensions.cs @@ -373,6 +373,61 @@ public static void WriteNumberIfNotNull( } } + public static void WriteNumberIfNotZero( + this Utf8JsonWriter writer, + string propertyName, + short value) + { + if (value is not 0) + { + writer.WriteNumber(propertyName, value); + } + } + + public static void WriteNumberIfNotZero( + this Utf8JsonWriter writer, + string propertyName, + int value) + { + if (value is not 0) + { + writer.WriteNumber(propertyName, value); + } + } + + public static void WriteNumberIfNotZero( + this Utf8JsonWriter writer, + string propertyName, + long value) + { + if (value is not 0) + { + writer.WriteNumber(propertyName, value); + } + } + + public static void WriteNumberIfNotZero( + this Utf8JsonWriter writer, + string propertyName, + float value) + { + if (value is not 0) + { + writer.WriteNumber(propertyName, value); + } + } + + public static void WriteNumberIfNotZero( + this Utf8JsonWriter writer, + string propertyName, + double value) + { + if (value is not 0) + { + writer.WriteNumber(propertyName, value); + } + } + public static void WriteStringIfNotWhiteSpace( this Utf8JsonWriter writer, string propertyName, diff --git a/src/Sentry/Internal/MainSentryEventProcessor.cs b/src/Sentry/Internal/MainSentryEventProcessor.cs index a756abfc44..05c2eb3a4e 100644 --- a/src/Sentry/Internal/MainSentryEventProcessor.cs +++ b/src/Sentry/Internal/MainSentryEventProcessor.cs @@ -150,7 +150,7 @@ public SentryEvent Process(SentryEvent @event) return @event; } - private void AddMemoryInfo(Contexts contexts) + private static void AddMemoryInfo(Contexts contexts) { #if NETCOREAPP3_0_OR_GREATER var memory = GC.GetGCMemoryInfo(); @@ -168,7 +168,6 @@ private void AddMemoryInfo(Contexts contexts) memory.PinnedObjectsCount, memory.PauseTimePercentage, memory.Index, - memory.Generation, memory.FinalizationPendingCount, memory.Compacted, memory.Concurrent, diff --git a/src/Sentry/Internal/MemoryInfo.cs b/src/Sentry/Internal/MemoryInfo.cs index 03740acba9..e7bdd3d51a 100644 --- a/src/Sentry/Internal/MemoryInfo.cs +++ b/src/Sentry/Internal/MemoryInfo.cs @@ -3,6 +3,7 @@ using System; using System.Text.Json; using Sentry.Extensibility; +using Sentry.Internal.Extensions; namespace Sentry { @@ -22,7 +23,6 @@ internal sealed class MemoryInfo : IJsonSerializable public double PauseTimePercentage { get; } public TimeSpan[] PauseDurations { get; } public long Index { get; } - public int Generation { get; } public long FinalizationPendingCount { get; } public bool Compacted { get; } public bool Concurrent { get; } @@ -39,7 +39,6 @@ public MemoryInfo( long pinnedObjectsCount, double pauseTimePercentage, long index, - int generation, long finalizationPendingCount, bool compacted, bool concurrent, @@ -57,7 +56,6 @@ public MemoryInfo( PauseTimePercentage = pauseTimePercentage; PauseDurations = pauseDurations; Index = index; - Generation = generation; FinalizationPendingCount = finalizationPendingCount; Compacted = compacted; Concurrent = concurrent; @@ -81,22 +79,22 @@ public MemoryInfo( #endif public void WriteTo(Utf8JsonWriter writer, IDiagnosticLogger? logger) { + //WriteNumberIfNotZero since on OS that dont implement all props, those props are stubbed out to zero writer.WriteStartObject(); - writer.WriteNumber("allocated_bytes", AllocatedBytes); - writer.WriteNumber("fragmented_bytes", FragmentedBytes); - writer.WriteNumber("heap_size_bytes", HeapSizeBytes); - writer.WriteNumber("high_memory_load_threshold_bytes", HighMemoryLoadThresholdBytes); - writer.WriteNumber("total_available_memory_bytes", TotalAvailableMemoryBytes); - writer.WriteNumber("memory_load_bytes", MemoryLoadBytes); + writer.WriteNumberIfNotZero("allocated_bytes", AllocatedBytes); + writer.WriteNumberIfNotZero("fragmented_bytes", FragmentedBytes); + writer.WriteNumberIfNotZero("heap_size_bytes", HeapSizeBytes); + writer.WriteNumberIfNotZero("high_memory_load_threshold_bytes", HighMemoryLoadThresholdBytes); + writer.WriteNumberIfNotZero("total_available_memory_bytes", TotalAvailableMemoryBytes); + writer.WriteNumberIfNotZero("memory_load_bytes", MemoryLoadBytes); #if NET5_0_OR_GREATER - writer.WriteNumber("total_committed_bytes", TotalCommittedBytes); - writer.WriteNumber("promoted_bytes", PromotedBytes); - writer.WriteNumber("pinned_objects_count", PinnedObjectsCount); - writer.WriteNumber("pause_time_percentage", PauseTimePercentage); - writer.WriteNumber("index", Index); - writer.WriteNumber("generation", Generation); + writer.WriteNumberIfNotZero("total_committed_bytes", TotalCommittedBytes); + writer.WriteNumberIfNotZero("promoted_bytes", PromotedBytes); + writer.WriteNumberIfNotZero("pinned_objects_count", PinnedObjectsCount); + writer.WriteNumberIfNotZero("pause_time_percentage", PauseTimePercentage); + writer.WriteNumberIfNotZero("index", Index); writer.WriteNumber("finalization_pending_count", FinalizationPendingCount); writer.WriteBoolean("compacted", Compacted); writer.WriteBoolean("concurrent", Concurrent); diff --git a/test/Sentry.Tests/Internals/MemoryInfoTests.cs b/test/Sentry.Tests/Internals/MemoryInfoTests.cs index f84837ffcb..c44ccc4ce7 100644 --- a/test/Sentry.Tests/Internals/MemoryInfoTests.cs +++ b/test/Sentry.Tests/Internals/MemoryInfoTests.cs @@ -7,7 +7,7 @@ public class MemoryInfoTests public void WriteTo() { #if NET5_0_OR_GREATER - var info = new MemoryInfo(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, true, false, new[] { TimeSpan.FromSeconds(1) }); + var info = new MemoryInfo(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, true, false, new[] { TimeSpan.FromSeconds(1) }); #else var info = new MemoryInfo(1, 2, 3, 4, 5, 6); #endif