From b01f17c86731c76dc08c9e6ee57fe2b71cd6c26d Mon Sep 17 00:00:00 2001 From: SimonCropp Date: Wed, 24 Nov 2021 07:16:35 +1100 Subject: [PATCH 01/16] AddMemoryInfo --- .../Internal/MainSentryEventProcessor.cs | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/Sentry/Internal/MainSentryEventProcessor.cs b/src/Sentry/Internal/MainSentryEventProcessor.cs index e79b46946d..8e43b556ff 100644 --- a/src/Sentry/Internal/MainSentryEventProcessor.cs +++ b/src/Sentry/Internal/MainSentryEventProcessor.cs @@ -12,6 +12,7 @@ internal class MainSentryEventProcessor : ISentryEventProcessor { internal const string CultureInfoKey = "Current Culture"; internal const string CurrentUiCultureKey = "Current UI Culture"; + internal const string MemoryInfoKey = "Memory Info"; private readonly Enricher _enricher; @@ -54,6 +55,7 @@ public SentryEvent Process(SentryEvent @event) @event.Contexts[CurrentUiCultureKey] = currentUiCultureMap; } + AddMemoryInfo(@event.Contexts); if (@event.ServerName == null) { // Value set on the options take precedence over device name. @@ -134,6 +136,35 @@ public SentryEvent Process(SentryEvent @event) return @event; } + private void AddMemoryInfo(Contexts contexts) + { +#if NETCOREAPP3_0 || NET5_0_OR_GREATER + var memoryInfo = GC.GetGCMemoryInfo(); + var values = new Dictionary + { + {"TotalAllocatedBytes", GC.GetTotalAllocatedBytes().ToString()}, + {"FragmentedBytes", memoryInfo.FragmentedBytes.ToString()}, + {"HeapSizeBytes", memoryInfo.HeapSizeBytes.ToString()}, + {"HighMemoryLoadThresholdBytes", memoryInfo.HighMemoryLoadThresholdBytes.ToString()}, + {"TotalAvailableMemoryBytes", memoryInfo.TotalAvailableMemoryBytes.ToString()}, + {"MemoryLoadBytes", memoryInfo.MemoryLoadBytes.ToString()}, + }; +#if NET5_0_OR_GREATER + values.Add("TotalCommittedBytes", memoryInfo.TotalCommittedBytes.ToString()); + values.Add("PromotedBytes", memoryInfo.PromotedBytes.ToString()); + values.Add("PinnedObjectsCount", memoryInfo.PinnedObjectsCount.ToString()); + values.Add("PauseTimePercentage", memoryInfo.PauseTimePercentage.ToString()); + values.Add("PauseDurations", memoryInfo.PauseDurations.ToString()); + values.Add("Index", memoryInfo.Index.ToString()); + values.Add("Generation", memoryInfo.Generation.ToString()); + values.Add("FinalizationPendingCount", memoryInfo.FinalizationPendingCount.ToString()); + values.Add("Concurrent", memoryInfo.Concurrent.ToString()); + values.Add("Compacted", memoryInfo.Compacted.ToString()); +#endif + contexts[MemoryInfoKey] = values; +#endif + } + private static IDictionary? CultureInfoToDictionary(CultureInfo cultureInfo) { var dic = new Dictionary(); From fd8855a1f9f65450f4ae886be8df183dfe796f66 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Thu, 9 Dec 2021 09:26:04 +1100 Subject: [PATCH 02/16] Update src/Sentry/Internal/MainSentryEventProcessor.cs Co-authored-by: Bruno Garcia --- src/Sentry/Internal/MainSentryEventProcessor.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Sentry/Internal/MainSentryEventProcessor.cs b/src/Sentry/Internal/MainSentryEventProcessor.cs index 8e43b556ff..82fb134f7c 100644 --- a/src/Sentry/Internal/MainSentryEventProcessor.cs +++ b/src/Sentry/Internal/MainSentryEventProcessor.cs @@ -138,7 +138,7 @@ public SentryEvent Process(SentryEvent @event) private void AddMemoryInfo(Contexts contexts) { -#if NETCOREAPP3_0 || NET5_0_OR_GREATER +#if NETCOREAPP3_0_OR_GREATER var memoryInfo = GC.GetGCMemoryInfo(); var values = new Dictionary { From 05462d4dab1c8a07ece4062c830eaefc0a17950d Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Wed, 15 Dec 2021 08:41:53 +1100 Subject: [PATCH 03/16] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9abe05ead2..e99fd8dd39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - Operation cancel while flushing cache no longer logs an errors ([#1352](https://github.com/getsentry/sentry-dotnet/pull/1352)) - Dont fail for attachment read error ([#1378](https://github.com/getsentry/sentry-dotnet/pull/1378)) - Fix file locking in attachments ([#1377](https://github.com/getsentry/sentry-dotnet/pull/1377)) +- Add MemoryInfo to sentry event ([#1337](https://github.com/getsentry/sentry-dotnet/pull/1337)) ## 3.12.1 From c147960f7ac7c3554513b252df3795068b0b1463 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Wed, 15 Dec 2021 09:14:34 +1100 Subject: [PATCH 04/16] . --- .../Internal/MainSentryEventProcessor.cs | 5 +-- .../MainSentryEventProcessorTests.cs | 36 ++++++++++++++++++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/Sentry/Internal/MainSentryEventProcessor.cs b/src/Sentry/Internal/MainSentryEventProcessor.cs index be2007e83b..86602ef0b9 100644 --- a/src/Sentry/Internal/MainSentryEventProcessor.cs +++ b/src/Sentry/Internal/MainSentryEventProcessor.cs @@ -152,8 +152,9 @@ private void AddMemoryInfo(Contexts contexts) values.Add("TotalCommittedBytes", memoryInfo.TotalCommittedBytes.ToString()); values.Add("PromotedBytes", memoryInfo.PromotedBytes.ToString()); values.Add("PinnedObjectsCount", memoryInfo.PinnedObjectsCount.ToString()); - values.Add("PauseTimePercentage", memoryInfo.PauseTimePercentage.ToString()); - values.Add("PauseDurations", memoryInfo.PauseDurations.ToString()); + values.Add("PauseTimePercentage", memoryInfo.PauseTimePercentage.ToString(CultureInfo.InvariantCulture)); + var pauseDurations = memoryInfo.PauseDurations.ToArray(); + values.Add("PauseDurations", string.Join(';',pauseDurations.Select(x=>x.ToString()))); values.Add("Index", memoryInfo.Index.ToString()); values.Add("Generation", memoryInfo.Generation.ToString()); values.Add("FinalizationPendingCount", memoryInfo.FinalizationPendingCount.ToString()); diff --git a/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs b/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs index 6dd18f826c..e0055a3287 100644 --- a/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs +++ b/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs @@ -3,6 +3,7 @@ namespace Sentry.Tests.Internals; +[UsesVerify] public class MainSentryEventProcessorTests { private class Fixture @@ -24,7 +25,7 @@ public void Process_DefaultOptions_NoUserNameSet() Assert.Null(evt.User.Username); } - + [Fact] public void Process_SendDefaultPiiTrueIdEnvironmentDefault_UserNameSet() { @@ -38,6 +39,39 @@ public void Process_SendDefaultPiiTrueIdEnvironmentDefault_UserNameSet() Assert.Equal(Environment.UserName, evt.User.Username); } +#if NETCOREAPP3_0_OR_GREATER + [Fact] + public void EnsureMemoryInfoExists() + { + var evt = new SentryEvent(); + + _fixture.SentryOptions.SendDefaultPii = true; + var sut = _fixture.GetSut(); + + _ = sut.Process(evt); + var evtContext = (Dictionary)evt.Contexts[MainSentryEventProcessor.MemoryInfoKey]; + var keys = evtContext.Keys; + Assert.Contains("TotalAllocatedBytes",keys); + Assert.Contains("FragmentedBytes",keys); + Assert.Contains("HeapSizeBytes",keys); + Assert.Contains("HighMemoryLoadThresholdBytes",keys); + Assert.Contains("TotalAvailableMemoryBytes",keys); + Assert.Contains("MemoryLoadBytes",keys); +#if NET5_0_OR_GREATER + Assert.Contains("TotalCommittedBytes",keys); + Assert.Contains("PromotedBytes",keys); + Assert.Contains("PinnedObjectsCount",keys); + Assert.Contains("PauseTimePercentage",keys); + Assert.Contains("PauseDurations",keys); + Assert.Contains("Index",keys); + Assert.Contains("Generation",keys); + Assert.Contains("FinalizationPendingCount",keys); + Assert.Contains("Concurrent",keys); + Assert.Contains("Compacted",keys); +#endif + } +#endif + [Fact] public void Process_SendDefaultPiiTrueIdEnvironmentTrue_UserNameSet() { From dfe09cfd5842fed895e147b5e4fc17589c6d7567 Mon Sep 17 00:00:00 2001 From: Sentry Github Bot Date: Tue, 14 Dec 2021 22:15:39 +0000 Subject: [PATCH 05/16] Format code --- .../Internal/MainSentryEventProcessor.cs | 2 +- .../MainSentryEventProcessorTests.cs | 34 +++++++++---------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/Sentry/Internal/MainSentryEventProcessor.cs b/src/Sentry/Internal/MainSentryEventProcessor.cs index 86602ef0b9..ff989b29ff 100644 --- a/src/Sentry/Internal/MainSentryEventProcessor.cs +++ b/src/Sentry/Internal/MainSentryEventProcessor.cs @@ -154,7 +154,7 @@ private void AddMemoryInfo(Contexts contexts) values.Add("PinnedObjectsCount", memoryInfo.PinnedObjectsCount.ToString()); values.Add("PauseTimePercentage", memoryInfo.PauseTimePercentage.ToString(CultureInfo.InvariantCulture)); var pauseDurations = memoryInfo.PauseDurations.ToArray(); - values.Add("PauseDurations", string.Join(';',pauseDurations.Select(x=>x.ToString()))); + values.Add("PauseDurations", string.Join(';', pauseDurations.Select(x => x.ToString()))); values.Add("Index", memoryInfo.Index.ToString()); values.Add("Generation", memoryInfo.Generation.ToString()); values.Add("FinalizationPendingCount", memoryInfo.FinalizationPendingCount.ToString()); diff --git a/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs b/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs index e0055a3287..8ca741f59f 100644 --- a/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs +++ b/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs @@ -25,7 +25,7 @@ public void Process_DefaultOptions_NoUserNameSet() Assert.Null(evt.User.Username); } - + [Fact] public void Process_SendDefaultPiiTrueIdEnvironmentDefault_UserNameSet() { @@ -51,23 +51,23 @@ public void EnsureMemoryInfoExists() _ = sut.Process(evt); var evtContext = (Dictionary)evt.Contexts[MainSentryEventProcessor.MemoryInfoKey]; var keys = evtContext.Keys; - Assert.Contains("TotalAllocatedBytes",keys); - Assert.Contains("FragmentedBytes",keys); - Assert.Contains("HeapSizeBytes",keys); - Assert.Contains("HighMemoryLoadThresholdBytes",keys); - Assert.Contains("TotalAvailableMemoryBytes",keys); - Assert.Contains("MemoryLoadBytes",keys); + Assert.Contains("TotalAllocatedBytes", keys); + Assert.Contains("FragmentedBytes", keys); + Assert.Contains("HeapSizeBytes", keys); + Assert.Contains("HighMemoryLoadThresholdBytes", keys); + Assert.Contains("TotalAvailableMemoryBytes", keys); + Assert.Contains("MemoryLoadBytes", keys); #if NET5_0_OR_GREATER - Assert.Contains("TotalCommittedBytes",keys); - Assert.Contains("PromotedBytes",keys); - Assert.Contains("PinnedObjectsCount",keys); - Assert.Contains("PauseTimePercentage",keys); - Assert.Contains("PauseDurations",keys); - Assert.Contains("Index",keys); - Assert.Contains("Generation",keys); - Assert.Contains("FinalizationPendingCount",keys); - Assert.Contains("Concurrent",keys); - Assert.Contains("Compacted",keys); + Assert.Contains("TotalCommittedBytes", keys); + Assert.Contains("PromotedBytes", keys); + Assert.Contains("PinnedObjectsCount", keys); + Assert.Contains("PauseTimePercentage", keys); + Assert.Contains("PauseDurations", keys); + Assert.Contains("Index", keys); + Assert.Contains("Generation", keys); + Assert.Contains("FinalizationPendingCount", keys); + Assert.Contains("Concurrent", keys); + Assert.Contains("Compacted", keys); #endif } #endif From c332a1c940c18e18ef58f743edc148e79f904328 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Wed, 15 Dec 2021 10:45:49 +1100 Subject: [PATCH 06/16] Update src/Sentry/Internal/MainSentryEventProcessor.cs Co-authored-by: Bruno Garcia --- src/Sentry/Internal/MainSentryEventProcessor.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Sentry/Internal/MainSentryEventProcessor.cs b/src/Sentry/Internal/MainSentryEventProcessor.cs index ff989b29ff..8cbdaf3c6c 100644 --- a/src/Sentry/Internal/MainSentryEventProcessor.cs +++ b/src/Sentry/Internal/MainSentryEventProcessor.cs @@ -154,7 +154,7 @@ private void AddMemoryInfo(Contexts contexts) values.Add("PinnedObjectsCount", memoryInfo.PinnedObjectsCount.ToString()); values.Add("PauseTimePercentage", memoryInfo.PauseTimePercentage.ToString(CultureInfo.InvariantCulture)); var pauseDurations = memoryInfo.PauseDurations.ToArray(); - values.Add("PauseDurations", string.Join(';', pauseDurations.Select(x => x.ToString()))); + values.Add("PauseDurations", string.Join(';', pauseDurations.Select(p => p.ToString()))); values.Add("Index", memoryInfo.Index.ToString()); values.Add("Generation", memoryInfo.Generation.ToString()); values.Add("FinalizationPendingCount", memoryInfo.FinalizationPendingCount.ToString()); From e9cfa4924b7aa45f4a49825de8858a8ce5dd973d Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Thu, 16 Dec 2021 11:04:25 +1100 Subject: [PATCH 07/16] . --- .../Internal/MainSentryEventProcessor.cs | 50 ++++---- src/Sentry/Internal/MemoryInfo.cs | 115 ++++++++++++++++++ .../MainSentryEventProcessorTests.cs | 34 +++--- 3 files changed, 159 insertions(+), 40 deletions(-) create mode 100644 src/Sentry/Internal/MemoryInfo.cs diff --git a/src/Sentry/Internal/MainSentryEventProcessor.cs b/src/Sentry/Internal/MainSentryEventProcessor.cs index 8cbdaf3c6c..f171139a3e 100644 --- a/src/Sentry/Internal/MainSentryEventProcessor.cs +++ b/src/Sentry/Internal/MainSentryEventProcessor.cs @@ -138,30 +138,35 @@ public SentryEvent Process(SentryEvent @event) private void AddMemoryInfo(Contexts contexts) { #if NETCOREAPP3_0_OR_GREATER - var memoryInfo = GC.GetGCMemoryInfo(); - var values = new Dictionary - { - {"TotalAllocatedBytes", GC.GetTotalAllocatedBytes().ToString()}, - {"FragmentedBytes", memoryInfo.FragmentedBytes.ToString()}, - {"HeapSizeBytes", memoryInfo.HeapSizeBytes.ToString()}, - {"HighMemoryLoadThresholdBytes", memoryInfo.HighMemoryLoadThresholdBytes.ToString()}, - {"TotalAvailableMemoryBytes", memoryInfo.TotalAvailableMemoryBytes.ToString()}, - {"MemoryLoadBytes", memoryInfo.MemoryLoadBytes.ToString()}, - }; + var memory = GC.GetGCMemoryInfo(); + var allocatedBytes = GC.GetTotalAllocatedBytes(); #if NET5_0_OR_GREATER - values.Add("TotalCommittedBytes", memoryInfo.TotalCommittedBytes.ToString()); - values.Add("PromotedBytes", memoryInfo.PromotedBytes.ToString()); - values.Add("PinnedObjectsCount", memoryInfo.PinnedObjectsCount.ToString()); - values.Add("PauseTimePercentage", memoryInfo.PauseTimePercentage.ToString(CultureInfo.InvariantCulture)); - var pauseDurations = memoryInfo.PauseDurations.ToArray(); - values.Add("PauseDurations", string.Join(';', pauseDurations.Select(p => p.ToString()))); - values.Add("Index", memoryInfo.Index.ToString()); - values.Add("Generation", memoryInfo.Generation.ToString()); - values.Add("FinalizationPendingCount", memoryInfo.FinalizationPendingCount.ToString()); - values.Add("Concurrent", memoryInfo.Concurrent.ToString()); - values.Add("Compacted", memoryInfo.Compacted.ToString()); + contexts[MemoryInfoKey] = new MemoryInfo( + allocatedBytes, + memory.FragmentedBytes, + memory.HeapSizeBytes, + memory.HighMemoryLoadThresholdBytes, + memory.TotalAvailableMemoryBytes, + memory.MemoryLoadBytes, + memory.TotalCommittedBytes, + memory.PromotedBytes, + memory.PinnedObjectsCount, + memory.PauseTimePercentage, + memory.PauseDurations.ToArray(), + memory.Index, + memory.Generation, + memory.FinalizationPendingCount, + memory.Compacted, + memory.Concurrent); +#else + contexts[MemoryInfoKey] = new MemoryInfo( + allocatedBytes, + memory.FragmentedBytes, + memory.HeapSizeBytes, + memory.HighMemoryLoadThresholdBytes, + memory.TotalAvailableMemoryBytes, + memory.MemoryLoadBytes); #endif - contexts[MemoryInfoKey] = values; #endif } @@ -185,4 +190,5 @@ private void AddMemoryInfo(Contexts contexts) return dic.Count > 0 ? dic : null; } } + } diff --git a/src/Sentry/Internal/MemoryInfo.cs b/src/Sentry/Internal/MemoryInfo.cs new file mode 100644 index 0000000000..9c2f472a32 --- /dev/null +++ b/src/Sentry/Internal/MemoryInfo.cs @@ -0,0 +1,115 @@ +#if NETCOREAPP3_0_OR_GREATER + +using System; +using System.Text.Json; + +namespace Sentry +{ + internal sealed class MemoryInfo : IJsonSerializable + { + public long AllocatedBytes { get; } + public long FragmentedBytes { get; } + public long HeapSizeBytes { get; } + public long HighMemoryLoadThresholdBytes { get; } + public long TotalAvailableMemoryBytes { get; } + public long MemoryLoadBytes { get; } + +#if NET5_0_OR_GREATER + public long TotalCommittedBytes { get; } + public long PromotedBytes { get; } + public long PinnedObjectsCount { get; } + 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; } + + public MemoryInfo( + long allocatedBytes, + long fragmentedBytes, + long heapSizeBytes, + long highMemoryLoadThresholdBytes, + long totalAvailableMemoryBytes, + long memoryLoadBytes, + long totalCommittedBytes, + long promotedBytes, + long pinnedObjectsCount, + double pauseTimePercentage, + TimeSpan[] pauseDurations, + long index, + int generation, + long finalizationPendingCount, + bool compacted, + bool concurrent) + { + AllocatedBytes = allocatedBytes; + FragmentedBytes = fragmentedBytes; + HeapSizeBytes = heapSizeBytes; + HighMemoryLoadThresholdBytes = highMemoryLoadThresholdBytes; + TotalAvailableMemoryBytes = totalAvailableMemoryBytes; + MemoryLoadBytes = memoryLoadBytes; + TotalCommittedBytes = totalCommittedBytes; + PromotedBytes = promotedBytes; + PinnedObjectsCount = pinnedObjectsCount; + PauseTimePercentage = pauseTimePercentage; + PauseDurations = pauseDurations; + Index = index; + Generation = generation; + FinalizationPendingCount = finalizationPendingCount; + Compacted = compacted; + Concurrent = concurrent; + } +#else + public MemoryInfo( + long allocatedBytes, + long fragmentedBytes, + long heapSizeBytes, + long highMemoryLoadThresholdBytes, + long totalAvailableMemoryBytes, + long memoryLoadBytes) + { + AllocatedBytes = allocatedBytes; + FragmentedBytes = fragmentedBytes; + HeapSizeBytes = heapSizeBytes; + HighMemoryLoadThresholdBytes = highMemoryLoadThresholdBytes; + TotalAvailableMemoryBytes = totalAvailableMemoryBytes; + MemoryLoadBytes = memoryLoadBytes; + } +#endif + public void WriteTo(Utf8JsonWriter writer) + { + writer.WriteStartObject(); + + writer.WriteNumber("allocatedBytes", AllocatedBytes); + writer.WriteNumber("fragmentedBytes", FragmentedBytes); + writer.WriteNumber("heapSizeBytes", HeapSizeBytes); + writer.WriteNumber("highMemoryLoadThresholdBytes", HighMemoryLoadThresholdBytes); + writer.WriteNumber("totalAvailableMemoryBytes", TotalAvailableMemoryBytes); + writer.WriteNumber("memoryLoadBytes", MemoryLoadBytes); + +#if NET5_0_OR_GREATER + writer.WriteNumber("totalCommittedBytes", TotalCommittedBytes); + writer.WriteNumber("promotedBytes", PromotedBytes); + writer.WriteNumber("pinnedObjectsCount", PinnedObjectsCount); + writer.WriteNumber("pauseTimePercentage", PauseTimePercentage); + writer.WriteNumber("index", Index); + writer.WriteNumber("generation", Generation); + writer.WriteNumber("finalizationPendingCount", FinalizationPendingCount); + writer.WriteBoolean("compacted", Compacted); + writer.WriteBoolean("concurrent", Concurrent); + writer.WriteStartArray(); + writer.WritePropertyName("pauseDurations"); + foreach (var duration in PauseDurations) + { + writer.WriteNumberValue(duration.TotalMilliseconds); + } + writer.WriteEndArray(); +#endif + writer.WriteEndObject(); + } + } +} + +#endif diff --git a/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs b/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs index 8ca741f59f..ff8bae3021 100644 --- a/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs +++ b/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs @@ -49,25 +49,23 @@ public void EnsureMemoryInfoExists() var sut = _fixture.GetSut(); _ = sut.Process(evt); - var evtContext = (Dictionary)evt.Contexts[MainSentryEventProcessor.MemoryInfoKey]; - var keys = evtContext.Keys; - Assert.Contains("TotalAllocatedBytes", keys); - Assert.Contains("FragmentedBytes", keys); - Assert.Contains("HeapSizeBytes", keys); - Assert.Contains("HighMemoryLoadThresholdBytes", keys); - Assert.Contains("TotalAvailableMemoryBytes", keys); - Assert.Contains("MemoryLoadBytes", keys); + var memory = (MemoryInfo)evt.Contexts[MainSentryEventProcessor.MemoryInfoKey]; + Assert.NotEqual(0, memory.TotalAvailableMemoryBytes); + Assert.NotEqual(0, memory.FragmentedBytes); + Assert.NotEqual(0, memory.HeapSizeBytes); + Assert.NotEqual(0, memory.HighMemoryLoadThresholdBytes); + Assert.NotEqual(0, memory.TotalAvailableMemoryBytes); + Assert.NotEqual(0, memory.MemoryLoadBytes); #if NET5_0_OR_GREATER - Assert.Contains("TotalCommittedBytes", keys); - Assert.Contains("PromotedBytes", keys); - Assert.Contains("PinnedObjectsCount", keys); - Assert.Contains("PauseTimePercentage", keys); - Assert.Contains("PauseDurations", keys); - Assert.Contains("Index", keys); - Assert.Contains("Generation", keys); - Assert.Contains("FinalizationPendingCount", keys); - Assert.Contains("Concurrent", keys); - Assert.Contains("Compacted", keys); + Assert.NotEqual(0, memory.TotalCommittedBytes); + Assert.NotEqual(0, memory.PromotedBytes); + Assert.NotEqual(0, memory.PinnedObjectsCount); + Assert.NotEqual(0, memory.PauseTimePercentage); + Assert.NotEmpty(memory.PauseDurations); + Assert.NotEqual(0, memory.Index); + Assert.NotEqual(0, memory.Generation); + Assert.NotEqual(0, memory.FinalizationPendingCount); + Assert.NotEqual(0, memory.Concurrent); #endif } #endif From f5f6d88932514746161d7a4628c0e20412e2f16c Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Thu, 16 Dec 2021 11:28:26 +1100 Subject: [PATCH 08/16] . --- src/Sentry/Internal/MemoryInfo.cs | 5 +++-- test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Sentry/Internal/MemoryInfo.cs b/src/Sentry/Internal/MemoryInfo.cs index 9c2f472a32..ce20b92846 100644 --- a/src/Sentry/Internal/MemoryInfo.cs +++ b/src/Sentry/Internal/MemoryInfo.cs @@ -2,10 +2,11 @@ using System; using System.Text.Json; +using Sentry.Extensibility; namespace Sentry { - internal sealed class MemoryInfo : IJsonSerializable + sealed class MemoryInfo : IJsonSerializable { public long AllocatedBytes { get; } public long FragmentedBytes { get; } @@ -78,7 +79,7 @@ public MemoryInfo( MemoryLoadBytes = memoryLoadBytes; } #endif - public void WriteTo(Utf8JsonWriter writer) + public void WriteTo(Utf8JsonWriter writer, IDiagnosticLogger? logger) { writer.WriteStartObject(); diff --git a/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs b/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs index ff8bae3021..bc3e5db955 100644 --- a/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs +++ b/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs @@ -1,5 +1,6 @@ using System.Globalization; using Sentry.Testing; +using Sentry; namespace Sentry.Tests.Internals; @@ -39,7 +40,7 @@ public void Process_SendDefaultPiiTrueIdEnvironmentDefault_UserNameSet() Assert.Equal(Environment.UserName, evt.User.Username); } -#if NETCOREAPP3_0_OR_GREATER +#if NETCOREAPP3_1_OR_GREATER [Fact] public void EnsureMemoryInfoExists() { @@ -65,7 +66,6 @@ public void EnsureMemoryInfoExists() Assert.NotEqual(0, memory.Index); Assert.NotEqual(0, memory.Generation); Assert.NotEqual(0, memory.FinalizationPendingCount); - Assert.NotEqual(0, memory.Concurrent); #endif } #endif From dd7827b59b4e93e47d903de585ad17e0fe22a1d3 Mon Sep 17 00:00:00 2001 From: Sentry Github Bot Date: Thu, 16 Dec 2021 00:29:17 +0000 Subject: [PATCH 09/16] Format code --- src/Sentry/Internal/MemoryInfo.cs | 2 +- test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Sentry/Internal/MemoryInfo.cs b/src/Sentry/Internal/MemoryInfo.cs index ce20b92846..69da7ed12e 100644 --- a/src/Sentry/Internal/MemoryInfo.cs +++ b/src/Sentry/Internal/MemoryInfo.cs @@ -89,7 +89,7 @@ public void WriteTo(Utf8JsonWriter writer, IDiagnosticLogger? logger) writer.WriteNumber("highMemoryLoadThresholdBytes", HighMemoryLoadThresholdBytes); writer.WriteNumber("totalAvailableMemoryBytes", TotalAvailableMemoryBytes); writer.WriteNumber("memoryLoadBytes", MemoryLoadBytes); - + #if NET5_0_OR_GREATER writer.WriteNumber("totalCommittedBytes", TotalCommittedBytes); writer.WriteNumber("promotedBytes", PromotedBytes); diff --git a/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs b/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs index bc3e5db955..7254c2d925 100644 --- a/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs +++ b/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs @@ -1,6 +1,6 @@ using System.Globalization; -using Sentry.Testing; using Sentry; +using Sentry.Testing; namespace Sentry.Tests.Internals; From 46fe6b10cac6e4ecb1e62c3a59ddfba57bd10d62 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Thu, 16 Dec 2021 11:29:27 +1100 Subject: [PATCH 10/16] Update MainSentryEventProcessorTests.cs --- test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs b/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs index bc3e5db955..ae49ac6a41 100644 --- a/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs +++ b/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs @@ -60,7 +60,6 @@ public void EnsureMemoryInfoExists() #if NET5_0_OR_GREATER Assert.NotEqual(0, memory.TotalCommittedBytes); Assert.NotEqual(0, memory.PromotedBytes); - Assert.NotEqual(0, memory.PinnedObjectsCount); Assert.NotEqual(0, memory.PauseTimePercentage); Assert.NotEmpty(memory.PauseDurations); Assert.NotEqual(0, memory.Index); From f4b2f96a217abcd3f409a896eed00b99a5acccd0 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Thu, 16 Dec 2021 11:33:12 +1100 Subject: [PATCH 11/16] Update MainSentryEventProcessorTests.cs --- test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs b/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs index ae49ac6a41..359c7d8a53 100644 --- a/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs +++ b/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs @@ -1,10 +1,8 @@ using System.Globalization; using Sentry.Testing; -using Sentry; namespace Sentry.Tests.Internals; -[UsesVerify] public class MainSentryEventProcessorTests { private class Fixture From 6b1e286bb98ce6cd6ae4ca1319b82a4acce75275 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Thu, 16 Dec 2021 11:52:27 +1100 Subject: [PATCH 12/16] . --- .../Internal/MainSentryEventProcessor.cs | 4 +-- src/Sentry/Internal/MemoryInfo.cs | 8 +++--- .../Sentry.Tests/Internals/MemoryInfoTests.cs | 27 +++++++++++++++++++ 3 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 test/Sentry.Tests/Internals/MemoryInfoTests.cs diff --git a/src/Sentry/Internal/MainSentryEventProcessor.cs b/src/Sentry/Internal/MainSentryEventProcessor.cs index f171139a3e..d97b3ebe42 100644 --- a/src/Sentry/Internal/MainSentryEventProcessor.cs +++ b/src/Sentry/Internal/MainSentryEventProcessor.cs @@ -152,12 +152,12 @@ private void AddMemoryInfo(Contexts contexts) memory.PromotedBytes, memory.PinnedObjectsCount, memory.PauseTimePercentage, - memory.PauseDurations.ToArray(), memory.Index, memory.Generation, memory.FinalizationPendingCount, memory.Compacted, - memory.Concurrent); + memory.Concurrent, + memory.PauseDurations.ToArray()); #else contexts[MemoryInfoKey] = new MemoryInfo( allocatedBytes, diff --git a/src/Sentry/Internal/MemoryInfo.cs b/src/Sentry/Internal/MemoryInfo.cs index 69da7ed12e..ddd3d9c6ee 100644 --- a/src/Sentry/Internal/MemoryInfo.cs +++ b/src/Sentry/Internal/MemoryInfo.cs @@ -6,7 +6,7 @@ namespace Sentry { - sealed class MemoryInfo : IJsonSerializable + internal sealed class MemoryInfo : IJsonSerializable { public long AllocatedBytes { get; } public long FragmentedBytes { get; } @@ -38,12 +38,12 @@ public MemoryInfo( long promotedBytes, long pinnedObjectsCount, double pauseTimePercentage, - TimeSpan[] pauseDurations, long index, int generation, long finalizationPendingCount, bool compacted, - bool concurrent) + bool concurrent, + TimeSpan[] pauseDurations) { AllocatedBytes = allocatedBytes; FragmentedBytes = fragmentedBytes; @@ -100,8 +100,8 @@ public void WriteTo(Utf8JsonWriter writer, IDiagnosticLogger? logger) writer.WriteNumber("finalizationPendingCount", FinalizationPendingCount); writer.WriteBoolean("compacted", Compacted); writer.WriteBoolean("concurrent", Concurrent); - writer.WriteStartArray(); writer.WritePropertyName("pauseDurations"); + writer.WriteStartArray(); foreach (var duration in PauseDurations) { writer.WriteNumberValue(duration.TotalMilliseconds); diff --git a/test/Sentry.Tests/Internals/MemoryInfoTests.cs b/test/Sentry.Tests/Internals/MemoryInfoTests.cs new file mode 100644 index 0000000000..fd23078271 --- /dev/null +++ b/test/Sentry.Tests/Internals/MemoryInfoTests.cs @@ -0,0 +1,27 @@ +#if NETCOREAPP3_1_OR_GREATER +using System.Text.Json; + +public class MemoryInfoTests +{ + [Fact] + 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)}); + #else + var info = new MemoryInfo(1, 2, 3, 4, 5, 6); + #endif + + var stream = new MemoryStream(); + var writer = new Utf8JsonWriter(stream, new JsonWriterOptions {Indented = true}); + info.WriteTo(writer, null); + writer.Flush(); + var json = Encoding.UTF8.GetString(stream.ToArray()); + Assert.NotNull(json); + Assert.NotEmpty(json); + //Validate json + var serializer = new Newtonsoft.Json.JsonSerializer(); + serializer.Deserialize(new Newtonsoft.Json.JsonTextReader(new StringReader(json))); + } +} +#endif From b914fbf83dcc41a90fc744d686356f8674400d94 Mon Sep 17 00:00:00 2001 From: Sentry Github Bot Date: Thu, 16 Dec 2021 00:53:16 +0000 Subject: [PATCH 13/16] Format code --- test/Sentry.Tests/Internals/MemoryInfoTests.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/Sentry.Tests/Internals/MemoryInfoTests.cs b/test/Sentry.Tests/Internals/MemoryInfoTests.cs index fd23078271..f84837ffcb 100644 --- a/test/Sentry.Tests/Internals/MemoryInfoTests.cs +++ b/test/Sentry.Tests/Internals/MemoryInfoTests.cs @@ -6,14 +6,14 @@ public class MemoryInfoTests [Fact] 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)}); - #else +#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) }); +#else var info = new MemoryInfo(1, 2, 3, 4, 5, 6); - #endif +#endif var stream = new MemoryStream(); - var writer = new Utf8JsonWriter(stream, new JsonWriterOptions {Indented = true}); + var writer = new Utf8JsonWriter(stream, new JsonWriterOptions { Indented = true }); info.WriteTo(writer, null); writer.Flush(); var json = Encoding.UTF8.GetString(stream.ToArray()); From 5104de7b215ef972471b46b12f7aedb2ac643cd1 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Thu, 16 Dec 2021 11:53:49 +1100 Subject: [PATCH 14/16] Update MainSentryEventProcessorTests.cs --- test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs b/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs index 359c7d8a53..85a2b7a97c 100644 --- a/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs +++ b/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs @@ -61,7 +61,6 @@ public void EnsureMemoryInfoExists() Assert.NotEqual(0, memory.PauseTimePercentage); Assert.NotEmpty(memory.PauseDurations); Assert.NotEqual(0, memory.Index); - Assert.NotEqual(0, memory.Generation); Assert.NotEqual(0, memory.FinalizationPendingCount); #endif } From aa844a941651694ab88ed3e82c66b93275556c11 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Thu, 16 Dec 2021 11:54:06 +1100 Subject: [PATCH 15/16] Update MainSentryEventProcessorTests.cs --- test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs b/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs index 85a2b7a97c..764b346f1b 100644 --- a/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs +++ b/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs @@ -60,8 +60,6 @@ public void EnsureMemoryInfoExists() Assert.NotEqual(0, memory.PromotedBytes); Assert.NotEqual(0, memory.PauseTimePercentage); Assert.NotEmpty(memory.PauseDurations); - Assert.NotEqual(0, memory.Index); - Assert.NotEqual(0, memory.FinalizationPendingCount); #endif } #endif From 6e234b2494162b71ac3de2d3863feb96b200c52e Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Thu, 16 Dec 2021 11:57:50 +1100 Subject: [PATCH 16/16] Update CHANGELOG.md --- CHANGELOG.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index deaf4b10ed..48a3a03200 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### Fixes + +- Add MemoryInfo to sentry event ([#1337](https://github.com/getsentry/sentry-dotnet/pull/1337)) + ## 3.12.2 ### Fixes @@ -9,7 +15,6 @@ - Operation cancel while flushing cache no longer logs an errors ([#1352](https://github.com/getsentry/sentry-dotnet/pull/1352)) - Dont fail for attachment read error ([#1378](https://github.com/getsentry/sentry-dotnet/pull/1378)) - Fix file locking in attachments ([#1377](https://github.com/getsentry/sentry-dotnet/pull/1377)) -- Add MemoryInfo to sentry event ([#1337](https://github.com/getsentry/sentry-dotnet/pull/1337)) ## 3.12.1