Skip to content

Commit 1aed342

Browse files
authored
feat: Add app_memory to Context.App (#2398)
1 parent 1daeb87 commit 1aed342

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
## Unreleased
44

5-
### Fixes
5+
### Features
6+
7+
- The SDK now also reports the currently allocated memory when reporting an event or transaction. ([#2398](https://github.com/getsentry/sentry-unity/pull/2398))
8+
9+
### Fixes
610

711
- The SDK now correctly sets the currently active scene's name on the event ([#2400](https://github.com/getsentry/sentry-unity/pull/2400))
812
- Fixed an issue where screenshot capture triggered on a burst job would crash the game. The SDK can now also capture screenshots on events that occur outside of the main thread ([#2392](https://github.com/getsentry/sentry-unity/pull/2392))

src/Sentry.Unity/UnityEventProcessor.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ private void SetEventContext(IEventLike sentryEvent)
4343
{
4444
try
4545
{
46+
PopulateApp(sentryEvent.Contexts.App);
4647
PopulateDevice(sentryEvent.Contexts.Device);
4748

4849
// The Unity context should get set in the UnityScopeIntegration automatically sets it when it gets registered
@@ -67,6 +68,21 @@ private void SetEventContext(IEventLike sentryEvent)
6768
}
6869
}
6970

71+
private void PopulateApp(App app)
72+
{
73+
if (!MainThreadData.IsMainThread())
74+
{
75+
return;
76+
}
77+
78+
// The Profiler returns '0' if it is not available
79+
var totalAllocatedMemory = UnityEngine.Profiling.Profiler.GetTotalAllocatedMemoryLong();
80+
if (totalAllocatedMemory > 0)
81+
{
82+
app.Memory = totalAllocatedMemory;
83+
}
84+
}
85+
7086
private void PopulateDevice(Device device)
7187
{
7288
if (!MainThreadData.IsMainThread())

test/Sentry.Unity.Tests/UnityEventScopeTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ public void SentrySdkCaptureEvent(bool captureOnUiThread)
156156
Assert.AreEqual(systemInfo.DeviceModel!.Value, @event.Contexts.Device.Model);
157157
Assert.AreEqual(systemInfo.DeviceUniqueIdentifier!.Value, @event.Contexts.Device.DeviceUniqueIdentifier);
158158
Assert.AreEqual(systemInfo.IsDebugBuild!.Value ? "debug" : "release", @event.Contexts.App.BuildType);
159+
if (captureOnUiThread)
160+
{
161+
Assert.IsNotNull(@event.Contexts.App.Memory);
162+
}
159163

160164
@event.Contexts.TryGetValue(Unity.Protocol.Unity.Type, out var unityProtocolObject);
161165
var unityContext = unityProtocolObject as Unity.Protocol.Unity;
@@ -276,6 +280,34 @@ public void AppProtocol_Assigned()
276280
Assert.IsNotNull(scope.Contexts.App.BuildType);
277281
}
278282

283+
[Test]
284+
public void AppMemory_SetByEventProcessor()
285+
{
286+
// arrange
287+
var unityEventProcessor = new UnityEventProcessor(_sentryOptions);
288+
var sentryEvent = new SentryEvent();
289+
290+
// act
291+
unityEventProcessor.Process(sentryEvent);
292+
293+
// assert
294+
Assert.IsNotNull(sentryEvent.Contexts.App.Memory);
295+
}
296+
297+
[Test]
298+
public void AppMemory_SetByEventProcessorForTransactions()
299+
{
300+
// arrange
301+
var unityEventProcessor = new UnityEventProcessor(_sentryOptions);
302+
var transaction = new SentryTransaction("test-transaction", "test-operation");
303+
304+
// act
305+
unityEventProcessor.Process(transaction);
306+
307+
// assert
308+
Assert.IsNotNull(transaction.Contexts.App.Memory);
309+
}
310+
279311
[Test]
280312
public void AppProtocol_AppNameIsApplicationName()
281313
{

0 commit comments

Comments
 (0)