Skip to content

Commit

Permalink
Merge d2119ed into 45623f3
Browse files Browse the repository at this point in the history
  • Loading branch information
brustolin authored Dec 10, 2024
2 parents 45623f3 + d2119ed commit c994cb9
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@

### Fixes

- Don't start the SDK inside Xcode preview (#4601)

## 8.42.0-beta.2

### Fixes

- Fix GraphQL context for HTTP client error tracking (#4567)

### Improvements
Expand Down
5 changes: 5 additions & 0 deletions SentryTestUtils/TestSentryNSProcessInfoWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public class TestSentryNSProcessInfoWrapper: SentryNSProcessInfoWrapper {
public var processorCount: UInt?
public var processDirectoryPath: String?
public var thermalState: ProcessInfo.ThermalState?
public var environment: [String: String]?
}

public var overrides = Override()
Expand All @@ -20,4 +21,8 @@ public class TestSentryNSProcessInfoWrapper: SentryNSProcessInfoWrapper {
public override var thermalState: ProcessInfo.ThermalState {
overrides.thermalState ?? super.thermalState
}

public override var environment: [String: String] {
overrides.environment ?? super.environment
}
}
6 changes: 3 additions & 3 deletions Sources/Sentry/SentryFramesTracker.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
# if SENTRY_TARGET_PROFILING_SUPPORTED
# import "SentryContinuousProfiler.h"
# import "SentryTraceProfiler.h"

/** A mutable version of @c SentryFrameInfoTimeSeries so we can accumulate results. */
typedef NSMutableArray<NSDictionary<NSString *, NSNumber *> *> SentryMutableFrameInfoTimeSeries;
# endif // SENTRY_TARGET_PROFILING_SUPPORTED
Expand Down Expand Up @@ -62,6 +61,7 @@ @implementation SentryFramesTracker {
unsigned int _totalFrames;
unsigned int _slowFrames;
unsigned int _frozenFrames;
NSString *test;
}

- (instancetype)initWithDisplayLinkWrapper:(SentryDisplayLinkWrapper *)displayLinkWrapper
Expand Down Expand Up @@ -223,8 +223,8 @@ - (void)displayLinkCallback
&& frameDuration <= SentryFrozenFrameThreshold) {
_slowFrames++;
# if SENTRY_TARGET_PROFILING_SUPPORTED
SENTRY_LOG_DEBUG(
@"Detected slow frame starting at %@ (frame tracker: %@).", profilingTimestamp, self);
SENTRY_LOG_DEBUG(@"Detected slow frame starting at %@ (frame tracker: %@). %@",
profilingTimestamp, self, test);
[self recordTimestamp:profilingTimestamp
value:@(thisFrameSystemTimestamp - self.previousFrameSystemTimestamp)
array:self.slowFrameTimestamps];
Expand Down
5 changes: 5 additions & 0 deletions Sources/Sentry/SentryNSProcessInfoWrapper.mm
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,9 @@ - (NSProcessInfoThermalState)thermalState
return NSProcessInfo.processInfo.thermalState;
}

- (NSDictionary<NSString *, NSString *> *)environment
{
return NSProcessInfo.processInfo.environment;
}

@end
24 changes: 23 additions & 1 deletion Sources/Sentry/SentrySDK.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#import "SentryLog.h"
#import "SentryLogC.h"
#import "SentryMeta.h"
#import "SentryNSProcessInfoWrapper.h"
#import "SentryOptions+Private.h"
#import "SentryProfilingConditionals.h"
#import "SentryReplayApi.h"
Expand All @@ -39,6 +40,8 @@
# import "SentryProfiler+Private.h"
#endif // SENTRY_TARGET_PROFILING_SUPPORTED

NSString *const SENTRY_XCODE_PREVIEW_ENVIRONMENT_KEY = @"XCODE_RUNNING_FOR_PREVIEWS";

@interface SentrySDK ()

@property (class) SentryHub *currentHub;
Expand All @@ -47,7 +50,6 @@ @interface SentrySDK ()

NS_ASSUME_NONNULL_BEGIN
@implementation SentrySDK

static SentryHub *_Nullable currentHub;
static NSObject *currentHubLock;
static BOOL crashedLastRunCalled;
Expand Down Expand Up @@ -201,6 +203,11 @@ + (void)setStartTimestamp:(NSDate *)value

+ (void)startWithOptions:(SentryOptions *)options
{
if ([SentryDependencyContainer.sharedInstance.processInfoWrapper
.environment[SENTRY_XCODE_PREVIEW_ENVIRONMENT_KEY] isEqualToString:@"1"]) {
return;
}

startOption = options;
[SentryLog configure:options.debug diagnosticLevel:options.diagnosticLevel];

Expand Down Expand Up @@ -613,6 +620,21 @@ + (void)stopProfiler
}
#endif // SENTRY_TARGET_PROFILING_SUPPORTED

#if TEST || TESTCI
static NSDictionary<NSString *, NSString *> *_processInfoEnvironment;

+ (void)setProcessInfoEnvironment:(NSDictionary<NSString *, NSString *> *)dictionary
{
_processInfoEnvironment = dictionary;
}

+ (NSDictionary<NSString *, NSString *> *)processInfoEnvironment
{
return _processInfoEnvironment;
}

#endif

@end

NS_ASSUME_NONNULL_END
1 change: 1 addition & 0 deletions Sources/Sentry/include/SentryNSProcessInfoWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nullable, nonatomic, readonly) NSString *processPath;
@property (readonly) NSUInteger processorCount;
@property (readonly) NSProcessInfoThermalState thermalState;
@property (readonly) NSDictionary<NSString *, NSString *> *environment;

#if defined(TEST) || defined(TESTCI) || defined(DEBUG)
- (void)setProcessPath:(NSString *)path;
Expand Down
2 changes: 2 additions & 0 deletions Sources/SentrySwiftUI/SentryInternal/SentryInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

NS_ASSUME_NONNULL_BEGIN

extern NSString *const SENTRY_XCODE_PREVIEW_ENVIRONMENT_KEY;

typedef NS_ENUM(NSInteger, SentryTransactionNameSource);

@class SentrySpanId;
Expand Down
2 changes: 2 additions & 0 deletions Sources/SentrySwiftUI/SentrySwiftUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ FOUNDATION_EXPORT double SentryUIVersionNumber;
//! Project version string for SentryUI.
FOUNDATION_EXPORT const unsigned char SentryUIVersionString[];

extern NSString *const SENTRY_XCODE_PREVIEW_ENVIRONMENT_KEY;

#if __has_include("SentryInternal.h")
# import "SentryInternal.h"
#endif
11 changes: 10 additions & 1 deletion Sources/SentrySwiftUI/SentryTracedView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,21 @@ public struct SentryTracedView<Content: View>: View {

return result
}

private func shouldTrace() -> Bool {
#if DEBUG
if ProcessInfo.processInfo.environment[SENTRY_XCODE_PREVIEW_ENVIRONMENT_KEY] == "1" {
return false
}
#endif
return !viewAppeared
}

public var body: some View {
var trace: SentryTracer?
var spanId: SpanId?

if !viewAppeared {
if shouldTrace() {
trace = ensureTransactionExists()
spanId = createAndPushBodySpan(transactionCreated: trace != nil)
}
Expand Down
13 changes: 13 additions & 0 deletions Tests/SentryTests/SentrySDKTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,19 @@ class SentrySDKTests: XCTestCase {
XCTAssertIdentical(scope, SentrySDK.currentHub().scope)
}

func testDontStartInsideXcodePreview() {
let testProcessInfoWrapper = TestSentryNSProcessInfoWrapper()
testProcessInfoWrapper.overrides.environment = ["XCODE_RUNNING_FOR_PREVIEWS": "1"]

SentryDependencyContainer.sharedInstance().processInfoWrapper = testProcessInfoWrapper

SentrySDK.start { options in
options.debug = true
}

XCTAssertFalse(SentrySDK.isEnabled)
}

func testCrashedLastRun() {
XCTAssertEqual(SentryDependencyContainer.sharedInstance().crashReporter.crashedLastLaunch, SentrySDK.crashedLastRun)
}
Expand Down

0 comments on commit c994cb9

Please sign in to comment.