From b9d59f71c5e6d620b54c57bd85e97ea3ab6785c1 Mon Sep 17 00:00:00 2001 From: Andrew McKnight Date: Mon, 28 Aug 2023 06:28:05 -0800 Subject: [PATCH] ref: anonymize SentryEvent (MetricKit) (#3240) --- Sources/Sentry/SentryEvent.m | 29 +++++++++++++++++++ Sources/Sentry/SentryMetricKitIntegration.m | 25 ---------------- Sources/Sentry/SentryScreenshotIntegration.m | 2 +- .../Sentry/SentryViewHierarchyIntegration.m | 2 +- Sources/Sentry/include/SentryEvent+Private.h | 5 ++++ .../include/SentryMetricKitIntegration.h | 7 ----- .../MetricKit/SentryMetricKitEventTests.swift | 12 ++++---- 7 files changed, 42 insertions(+), 40 deletions(-) diff --git a/Sources/Sentry/SentryEvent.m b/Sources/Sentry/SentryEvent.m index 95c8828888b..de83a6fa1a6 100644 --- a/Sources/Sentry/SentryEvent.m +++ b/Sources/Sentry/SentryEvent.m @@ -17,6 +17,11 @@ #import "SentryThread.h" #import "SentryUser.h" +#if SENTRY_HAS_METRIC_KIT +# import "SentryMechanism.h" +# import "SentryMetricKitIntegration.h" +#endif // SENTRY_HAS_METRIC_KIT + NS_ASSUME_NONNULL_BEGIN @implementation SentryEvent @@ -172,6 +177,30 @@ - (NSMutableArray *)serializeBreadcrumbs return crumbs; } +#if SENTRY_HAS_METRIC_KIT + +- (BOOL)isMetricKitEvent +{ + if (self.exceptions == nil || self.exceptions.count != 1) { + return NO; + } + + NSArray *metricKitMechanisms = @[ + SentryMetricKitDiskWriteExceptionMechanism, SentryMetricKitCpuExceptionMechanism, + SentryMetricKitHangDiagnosticMechanism, @"MXCrashDiagnostic" + ]; + + SentryException *exception = self.exceptions[0]; + if (exception.mechanism != nil && + [metricKitMechanisms containsObject:exception.mechanism.type]) { + return YES; + } else { + return NO; + } +} + +#endif // SENTRY_HAS_METRIC_KIT + @end NS_ASSUME_NONNULL_END diff --git a/Sources/Sentry/SentryMetricKitIntegration.m b/Sources/Sentry/SentryMetricKitIntegration.m index bd5ba54f240..1cb7af79422 100644 --- a/Sources/Sentry/SentryMetricKitIntegration.m +++ b/Sources/Sentry/SentryMetricKitIntegration.m @@ -449,31 +449,6 @@ - (SentryStacktrace *)convertMXFramesToSentryStacktrace:(NSEnumerator *metricKitMechanisms = @[ - SentryMetricKitDiskWriteExceptionMechanism, SentryMetricKitCpuExceptionMechanism, - SentryMetricKitHangDiagnosticMechanism, @"MXCrashDiagnostic" - ]; - - SentryException *exception = self.exceptions[0]; - if (exception.mechanism != nil && - [metricKitMechanisms containsObject:exception.mechanism.type]) { - return YES; - } else { - return NO; - } -} - -@end - NS_ASSUME_NONNULL_END #endif // SENTRY_HAS_METRIC_KIT diff --git a/Sources/Sentry/SentryScreenshotIntegration.m b/Sources/Sentry/SentryScreenshotIntegration.m index 08d3cee4119..f3ceecb897b 100644 --- a/Sources/Sentry/SentryScreenshotIntegration.m +++ b/Sources/Sentry/SentryScreenshotIntegration.m @@ -57,7 +57,7 @@ - (void)uninstall // We don't take screenshots if the event is a crash or metric kit event. if ((event.exceptions == nil && event.error == nil) || event.isCrashEvent # if SENTRY_HAS_METRIC_KIT - || event.isMetricKitEvent + || [event isMetricKitEvent] # endif // SENTRY_HAS_METRIC_KIT ) { return attachments; diff --git a/Sources/Sentry/SentryViewHierarchyIntegration.m b/Sources/Sentry/SentryViewHierarchyIntegration.m index 7d784eaca13..4154460372e 100644 --- a/Sources/Sentry/SentryViewHierarchyIntegration.m +++ b/Sources/Sentry/SentryViewHierarchyIntegration.m @@ -63,7 +63,7 @@ - (void)uninstall // We don't attach the view hierarchy if the event is a crash or metric kit event. if ((event.exceptions == nil && event.error == nil) || event.isCrashEvent # if SENTRY_HAS_METRIC_KIT - || event.isMetricKitEvent + || [event isMetricKitEvent] # endif // SENTRY_HAS_METRIC_KIT ) { return attachments; diff --git a/Sources/Sentry/include/SentryEvent+Private.h b/Sources/Sentry/include/SentryEvent+Private.h index 73464098fd2..c28e338b29e 100644 --- a/Sources/Sentry/include/SentryEvent+Private.h +++ b/Sources/Sentry/include/SentryEvent+Private.h @@ -1,3 +1,4 @@ +#import "SentryDefines.h" #import "SentryEvent.h" #import "SentryProfilingConditionals.h" #import @@ -25,4 +26,8 @@ SentryEvent () @property (nonatomic) uint64_t endSystemTime; #endif // SENTRY_TARGET_PROFILING_SUPPORTED +#if SENTRY_HAS_METRIC_KIT +- (BOOL)isMetricKitEvent; +#endif // SENTRY_HAS_METRIC_KIT + @end diff --git a/Sources/Sentry/include/SentryMetricKitIntegration.h b/Sources/Sentry/include/SentryMetricKitIntegration.h index 6bc9dbb1c13..fdd5f738d89 100644 --- a/Sources/Sentry/include/SentryMetricKitIntegration.h +++ b/Sources/Sentry/include/SentryMetricKitIntegration.h @@ -26,13 +26,6 @@ API_UNAVAILABLE(tvos, watchos) @end -@interface -SentryEvent (MetricKit) - -@property (nonatomic, readonly) BOOL isMetricKitEvent; - -@end - NS_ASSUME_NONNULL_END #endif // SENTRY_HAS_METRIC_KIT diff --git a/Tests/SentryTests/Integrations/MetricKit/SentryMetricKitEventTests.swift b/Tests/SentryTests/Integrations/MetricKit/SentryMetricKitEventTests.swift index 3fdda88e83f..707934ad568 100644 --- a/Tests/SentryTests/Integrations/MetricKit/SentryMetricKitEventTests.swift +++ b/Tests/SentryTests/Integrations/MetricKit/SentryMetricKitEventTests.swift @@ -5,27 +5,27 @@ import XCTest final class SentryMetricKitEventTests: XCTestCase { func testMXCPUException_IsMetricKitEvent() { - XCTAssertTrue(TestData.metricKitEvent.isMetricKitEvent) + XCTAssertTrue(TestData.metricKitEvent.isMetricKitEvent()) } func testMXDiskWriteException_IsMetricKitEvent() { - XCTAssertTrue(createMetricKitEventWith(mechanismType: SentryMetricKitDiskWriteExceptionMechanism).isMetricKitEvent) + XCTAssertTrue(createMetricKitEventWith(mechanismType: SentryMetricKitDiskWriteExceptionMechanism).isMetricKitEvent()) } func testMXHangDiagnostic_IsMetricKitEvent() { - XCTAssertTrue(createMetricKitEventWith(mechanismType: SentryMetricKitHangDiagnosticMechanism).isMetricKitEvent) + XCTAssertTrue(createMetricKitEventWith(mechanismType: SentryMetricKitHangDiagnosticMechanism).isMetricKitEvent()) } func testWatchDogEvent_IsNotMetricKitEvent() { - XCTAssertFalse(TestData.oomEvent.isMetricKitEvent) + XCTAssertFalse(TestData.oomEvent.isMetricKitEvent()) } func testNormalEvent_IsNotMetricKitEvent() { - XCTAssertFalse(TestData.event.isMetricKitEvent) + XCTAssertFalse(TestData.event.isMetricKitEvent()) } func testEmptyEvent_IsNotMetricKitEvent() { - XCTAssertFalse(Event().isMetricKitEvent) + XCTAssertFalse(Event().isMetricKitEvent()) } private func createMetricKitEventWith(mechanismType: String) -> Event {