Skip to content

Commit

Permalink
ref: anonymize SentryEvent (MetricKit) (#3240)
Browse files Browse the repository at this point in the history
  • Loading branch information
armcknight authored Aug 28, 2023
1 parent 3033115 commit b9d59f7
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 40 deletions.
29 changes: 29 additions & 0 deletions Sources/Sentry/SentryEvent.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<NSString *> *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
25 changes: 0 additions & 25 deletions Sources/Sentry/SentryMetricKitIntegration.m
Original file line number Diff line number Diff line change
Expand Up @@ -449,31 +449,6 @@ - (SentryStacktrace *)convertMXFramesToSentryStacktrace:(NSEnumerator<SentryMXFr

@end

@implementation
SentryEvent (MetricKit)

- (BOOL)isMetricKitEvent
{
if (self.exceptions == nil || self.exceptions.count != 1) {
return NO;
}

NSArray<NSString *> *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
2 changes: 1 addition & 1 deletion Sources/Sentry/SentryScreenshotIntegration.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion Sources/Sentry/SentryViewHierarchyIntegration.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions Sources/Sentry/include/SentryEvent+Private.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#import "SentryDefines.h"
#import "SentryEvent.h"
#import "SentryProfilingConditionals.h"
#import <Foundation/Foundation.h>
Expand Down Expand Up @@ -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
7 changes: 0 additions & 7 deletions Sources/Sentry/include/SentryMetricKitIntegration.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit b9d59f7

Please sign in to comment.