Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore zero properties for MemoryInfo #1531

Merged
merged 4 commits into from
Mar 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
55 changes: 55 additions & 0 deletions src/Sentry/Internal/Extensions/JsonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions src/Sentry/Internal/MainSentryEventProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -168,7 +168,6 @@ private void AddMemoryInfo(Contexts contexts)
memory.PinnedObjectsCount,
memory.PauseTimePercentage,
memory.Index,
memory.Generation,
memory.FinalizationPendingCount,
memory.Compacted,
memory.Concurrent,
Expand Down
28 changes: 13 additions & 15 deletions src/Sentry/Internal/MemoryInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Text.Json;
using Sentry.Extensibility;
using Sentry.Internal.Extensions;

namespace Sentry
{
Expand All @@ -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; }
Expand All @@ -39,7 +39,6 @@ public MemoryInfo(
long pinnedObjectsCount,
double pauseTimePercentage,
long index,
int generation,
long finalizationPendingCount,
bool compacted,
bool concurrent,
Expand All @@ -57,7 +56,6 @@ public MemoryInfo(
PauseTimePercentage = pauseTimePercentage;
PauseDurations = pauseDurations;
Index = index;
Generation = generation;
FinalizationPendingCount = finalizationPendingCount;
Compacted = compacted;
Concurrent = concurrent;
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/Sentry.Tests/Internals/MemoryInfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down