Skip to content

Commit 6184305

Browse files
authored
Add global attributes support for Android (#1274)
* Add global attributes support for Android * Add native array type support for attribute * Add global attributes integration tests for Android * Add global attributes integration tests for desktop * Update sample app * Update changelog * Rever array type change
1 parent 2788b14 commit 6184305

6 files changed

Lines changed: 54 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Features
66

77
- Add app hang tracking for desktop platforms ([#1270](https://github.com/getsentry/sentry-unreal/pull/1270))
8+
- Add global attributes support for Android ([#1274](https://github.com/getsentry/sentry-unreal/pull/1274))
89

910
### Fixes
1011

integration-test/Integration.Android.Tests.ps1

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ Describe 'Sentry Unreal Android Integration Tests (<Platform>)' -ForEach $TestTa
432432

433433
# Fetch logs from Sentry with automatic polling
434434
try {
435-
$script:CapturedLogs = Get-SentryTestLog -AttributeName 'test_id' -AttributeValue $script:TestId -Fields @('handler_added', 'to_be_removed')
435+
$script:CapturedLogs = Get-SentryTestLog -AttributeName 'test_id' -AttributeValue $script:TestId -Fields @('handler_added', 'to_be_removed', 'global_attr', 'global_removed')
436436
}
437437
catch {
438438
Write-Host "Warning: $_" -ForegroundColor Red
@@ -483,8 +483,15 @@ Describe 'Sentry Unreal Android Integration Tests (<Platform>)' -ForEach $TestTa
483483
$log.'to_be_removed' | Should -BeNullOrEmpty
484484
}
485485

486-
# Note: Global log attributes (SetAttribute/RemoveAttribute on subsystem) are not supported
487-
# on Android (sentry-java) - the implementation is a no-op. These are tested in desktop tests only.
486+
It "Should have global attribute set on subsystem" {
487+
$log = $script:CapturedLogs[0]
488+
$log.global_attr | Should -Be 'global_value'
489+
}
490+
491+
It "Should not have global attribute that was removed from subsystem" {
492+
$log = $script:CapturedLogs[0]
493+
$log.global_removed | Should -BeNullOrEmpty
494+
}
488495
}
489496

490497
Context "Metrics Capture Tests" {
@@ -502,7 +509,7 @@ Describe 'Sentry Unreal Android Integration Tests (<Platform>)' -ForEach $TestTa
502509
Write-Host "Captured Test ID: $($script:TestId)" -ForegroundColor Cyan
503510

504511
# Fetch all three metric types from Sentry with automatic polling
505-
$metricFields = @('handler_added', 'to_be_removed')
512+
$metricFields = @('handler_added', 'to_be_removed', 'global_attr', 'global_removed')
506513

507514
try {
508515
$script:CapturedCounterMetrics = Get-SentryTestMetric -MetricName 'test.integration.counter' -AttributeName 'test_id' -AttributeValue $script:TestId -Fields $metricFields
@@ -603,6 +610,16 @@ Describe 'Sentry Unreal Android Integration Tests (<Platform>)' -ForEach $TestTa
603610
$metric = $script:CapturedCounterMetrics[0]
604611
$metric.test_id | Should -Be $script:TestId
605612
}
613+
614+
It "Should have global attribute set on subsystem" {
615+
$metric = $script:CapturedCounterMetrics[0]
616+
$metric.global_attr | Should -Be 'global_value'
617+
}
618+
619+
It "Should not have global attribute that was removed from subsystem" {
620+
$metric = $script:CapturedCounterMetrics[0]
621+
$metric.global_removed | Should -BeNullOrEmpty
622+
}
606623
}
607624

608625
Context "Tracing Capture Tests" {

integration-test/Integration.Desktop.Tests.ps1

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ Describe "Sentry Unreal Desktop Integration Tests (<Platform>)" -ForEach $TestTa
530530
Write-Host "Captured Test ID: $($script:TestId)" -ForegroundColor Cyan
531531

532532
# Fetch all three metric types from Sentry with automatic polling
533-
$metricFields = @('handler_added', 'to_be_removed')
533+
$metricFields = @('handler_added', 'to_be_removed', 'global_attr', 'global_removed')
534534

535535
try {
536536
$script:CapturedCounterMetrics = Get-SentryTestMetric -MetricName 'test.integration.counter' -AttributeName 'test_id' -AttributeValue $script:TestId -Fields $metricFields
@@ -635,6 +635,16 @@ Describe "Sentry Unreal Desktop Integration Tests (<Platform>)" -ForEach $TestTa
635635
$metric = $script:CapturedCounterMetrics[0]
636636
$metric.test_id | Should -Be $script:TestId
637637
}
638+
639+
It "Should have global attribute set on subsystem" {
640+
$metric = $script:CapturedCounterMetrics[0]
641+
$metric.global_attr | Should -Be 'global_value'
642+
}
643+
644+
It "Should not have global attribute that was removed from subsystem" {
645+
$metric = $script:CapturedCounterMetrics[0]
646+
$metric.global_removed | Should -BeNullOrEmpty
647+
}
638648
}
639649

640650
Context "Tracing Capture Tests" {

plugin-dev/Source/Sentry/Private/Android/AndroidSentrySubsystem.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,12 +382,18 @@ void FAndroidSentrySubsystem::RemoveTag(const FString& key)
382382

383383
void FAndroidSentrySubsystem::SetAttribute(const FString& key, const FSentryVariant& value)
384384
{
385-
// No-op: Android SDK doesn't support global log attributes
385+
TSharedPtr<FSentryJavaObjectWrapper> nativeValue = FAndroidSentryConverters::VariantToNative(value);
386+
if (nativeValue.IsValid())
387+
{
388+
FSentryJavaObjectWrapper::CallStaticMethod<void>(SentryJavaClasses::SentryBridgeJava, "setAttribute", "(Ljava/lang/String;Ljava/lang/Object;)V",
389+
*FSentryJavaObjectWrapper::GetJString(key), nativeValue->GetJObject());
390+
}
386391
}
387392

388393
void FAndroidSentrySubsystem::RemoveAttribute(const FString& key)
389394
{
390-
// No-op: Android SDK doesn't support global log attributes
395+
FSentryJavaObjectWrapper::CallStaticMethod<void>(SentryJavaClasses::SentryBridgeJava, "removeAttribute", "(Ljava/lang/String;)V",
396+
*FSentryJavaObjectWrapper::GetJString(key));
391397
}
392398

393399
void FAndroidSentrySubsystem::SetLevel(ESentryLevel level)

plugin-dev/Source/Sentry/Private/Android/Java/SentryBridgeJava.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,14 @@ public void run(@NonNull IScope scope) {
210210
});
211211
}
212212

213+
public static void setAttribute(final String key, final Object value) {
214+
Sentry.setAttribute(key, value);
215+
}
216+
217+
public static void removeAttribute(final String key) {
218+
Sentry.removeAttribute(key);
219+
}
220+
213221
public static void setLevel(final SentryLevel level) {
214222
Sentry.configureScope(new ScopeCallback() {
215223
@Override

sample/Source/SentryPlayground/SentryPlaygroundGameInstance.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,11 @@ void USentryPlaygroundGameInstance::RunMetricTest()
220220

221221
FString TestId = FGuid::NewGuid().ToString(EGuidFormats::DigitsWithHyphens);
222222

223+
SentrySubsystem->SetAttribute(TEXT("global_attr"), FSentryVariant(TEXT("global_value")));
224+
225+
SentrySubsystem->SetAttribute(TEXT("global_removed"), FSentryVariant(TEXT("should_not_appear")));
226+
SentrySubsystem->RemoveAttribute(TEXT("global_removed"));
227+
223228
TMap<FString, FSentryVariant> CounterAttributes;
224229
CounterAttributes.Add(TEXT("test_id"), FSentryVariant(TestId));
225230
CounterAttributes.Add(TEXT("to_be_removed"), FSentryVariant(TEXT("original_value")));

0 commit comments

Comments
 (0)