Skip to content

Commit

Permalink
fix: InApp for MetricKit stack traces (#2739)
Browse files Browse the repository at this point in the history
Apply the InAppLogic for MetricKit stack traces.

Fixes GH-2738
  • Loading branch information
philipphofmann authored Mar 1, 2023
1 parent adcc7d8 commit dacf894
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Crash in AppHangs when no threads (#2725)
- MetricKit stack traces (#2723)
- InApp for MetricKit stack traces (#2739)

## 8.2.0

Expand Down
5 changes: 5 additions & 0 deletions Sources/Sentry/SentryMetricKitIntegration.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#import <SentryException.h>
#import <SentryFrame.h>
#import <SentryHexAddressFormatter.h>
#import <SentryInAppLogic.h>
#import <SentryLog.h>
#import <SentryMechanism.h>
#import <SentryMetricKitIntegration.h>
Expand Down Expand Up @@ -46,6 +47,7 @@ @implementation SentryMXExceptionParams

@property (nonatomic, strong, nullable) SentryMXManager *metricKitManager;
@property (nonatomic, strong) NSMeasurementFormatter *measurementFormatter;
@property (nonatomic, strong) SentryInAppLogic *inAppLogic;

@end

Expand All @@ -63,6 +65,8 @@ - (BOOL)installWithOptions:(SentryOptions *)options
self.measurementFormatter = [[NSMeasurementFormatter alloc] init];
self.measurementFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
self.measurementFormatter.unitOptions = NSMeasurementFormatterUnitOptionsProvidedUnit;
self.inAppLogic = [[SentryInAppLogic alloc] initWithInAppIncludes:options.inAppIncludes
inAppExcludes:options.inAppExcludes];

return YES;
}
Expand Down Expand Up @@ -380,6 +384,7 @@ - (SentryStacktrace *)convertMXFramesToSentryStacktrace:(NSEnumerator<SentryMXFr
for (SentryMXFrame *mxFrame in mxFrames) {
SentryFrame *frame = [[SentryFrame alloc] init];
frame.package = mxFrame.binaryName;
frame.inApp = @([self.inAppLogic isInApp:mxFrame.binaryName]);
frame.instructionAddress = sentry_formatHexAddress(@(mxFrame.address));
NSNumber *imageAddress = @(mxFrame.address - mxFrame.offsetIntoBinaryTextSegment);
frame.imageAddress = sentry_formatHexAddress(imageAddress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,28 @@ final class SentryMetricKitIntegrationTests: SentrySDKIntegrationTestsBase {
}
}

func testSetInAppIncludes_AppliesInAppToStackTrace() throws {
if #available(iOS 15, macOS 12, macCatalyst 15, *) {
givenSDKWithHubWithScope()

let sut = SentryMetricKitIntegration()
givenInstalledWithEnabled(sut) { options in
options.add(inAppInclude: "iOS-Swift")
}

let mxDelegate = sut as SentryMXManagerDelegate
mxDelegate.didReceiveCrashDiagnostic(MXCrashDiagnostic(), callStackTree: callStackTreePerThread, timeStampBegin: timeStampBegin, timeStampEnd: timeStampEnd)

assertEventWithScopeCaptured { event, _, _ in
let stacktrace = try! XCTUnwrap( event?.threads?.first?.stacktrace)

let inAppFramesCount = stacktrace.frames.filter { $0.inApp as? Bool ?? false }.count

XCTAssertEqual(2, inAppFramesCount)
}
}
}

func testCPUExceptionDiagnostic_PerThread() throws {
if #available(iOS 15, macOS 12, macCatalyst 15, *) {
givenSDKWithHubWithScope()
Expand Down Expand Up @@ -137,9 +159,10 @@ final class SentryMetricKitIntegrationTests: SentrySDKIntegrationTestsBase {
}

@available(iOS 15, macOS 12, macCatalyst 15, *)
private func givenInstalledWithEnabled(_ integration: SentryMetricKitIntegration) {
private func givenInstalledWithEnabled(_ integration: SentryMetricKitIntegration, optionsBlock: (Options) -> Void = { _ in }) {
let options = Options()
options.enableMetricKit = true
optionsBlock(options)
integration.install(with: options)
}

Expand Down Expand Up @@ -275,6 +298,8 @@ final class SentryMetricKitIntegrationTests: SentrySDKIntegrationTestsBase {
XCTAssertEqual(mxFrame.binaryName, sentryFrame.package)
let lastRootFrameImageAddress = formatHexAddress(value: mxFrame.address - UInt64(mxFrame.offsetIntoBinaryTextSegment))
XCTAssertEqual(lastRootFrameImageAddress, sentryFrame.imageAddress)

XCTAssertFalse(sentryFrame.inApp as? Bool ?? true)
}

}
Expand Down

0 comments on commit dacf894

Please sign in to comment.