Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref: move SentryExtraContextProvider singleton to SentryDependencyContainer #3244

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions SentryTestUtils/TestClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class TestClient: SentryClient {

// Without this override we get a fatal error: use of unimplemented initializer
// see https://stackoverflow.com/questions/28187261/ios-swift-fatal-error-use-of-unimplemented-initializer-init
public override init(options: Options, transportAdapter: SentryTransportAdapter, fileManager: SentryFileManager, deleteOldEnvelopeItems: Bool, threadInspector: SentryThreadInspector, random: SentryRandomProtocol, locale: Locale, timezone: TimeZone, extraContextProvider: SentryExtraContextProvider) {
public override init(options: Options, transportAdapter: SentryTransportAdapter, fileManager: SentryFileManager, deleteOldEnvelopeItems: Bool, threadInspector: SentryThreadInspector, random: SentryRandomProtocol, locale: Locale, timezone: TimeZone) {
super.init(
options: options,
transportAdapter: transportAdapter,
Expand All @@ -24,8 +24,7 @@ public class TestClient: SentryClient {
threadInspector: threadInspector,
random: random,
locale: locale,
timezone: timezone,
extraContextProvider: extraContextProvider
timezone: timezone
)
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Sentry/PrivateSentrySDKOnly.mm
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@

+ (NSDictionary *)getExtraContext
{
return [[SentryExtraContextProvider sharedInstance] getExtraContext];
return [SentryDependencyContainer.sharedInstance.extraContextProvider getExtraContext];

Check warning on line 122 in Sources/Sentry/PrivateSentrySDKOnly.mm

View check run for this annotation

Codecov / codecov/patch

Sources/Sentry/PrivateSentrySDKOnly.mm#L122

Added line #L122 was not covered by tests
}

#if SENTRY_TARGET_PROFILING_SUPPORTED
Expand Down
11 changes: 3 additions & 8 deletions Sources/Sentry/SentryClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
@property (nonatomic, strong) id<SentryRandom> random;
@property (nonatomic, strong) NSLocale *locale;
@property (nonatomic, strong) NSTimeZone *timezone;
@property (nonatomic, strong) SentryExtraContextProvider *extraContextProvider;

@end

Expand Down Expand Up @@ -124,17 +123,14 @@ - (instancetype)initWithOptions:(SentryOptions *)options
SentryThreadInspector *threadInspector =
[[SentryThreadInspector alloc] initWithOptions:options];

SentryExtraContextProvider *extraContextProvider = [SentryExtraContextProvider sharedInstance];

return [self initWithOptions:options
transportAdapter:transportAdapter
fileManager:fileManager
deleteOldEnvelopeItems:deleteOldEnvelopeItems
threadInspector:threadInspector
random:[SentryDependencyContainer sharedInstance].random
locale:[NSLocale autoupdatingCurrentLocale]
timezone:[NSCalendar autoupdatingCurrentCalendar].timeZone
extraContextProvider:extraContextProvider];
timezone:[NSCalendar autoupdatingCurrentCalendar].timeZone];
}

- (instancetype)initWithOptions:(SentryOptions *)options
Expand All @@ -145,7 +141,6 @@ - (instancetype)initWithOptions:(SentryOptions *)options
random:(id<SentryRandom>)random
locale:(NSLocale *)locale
timezone:(NSTimeZone *)timezone
extraContextProvider:(SentryExtraContextProvider *)extraContentProvider
{
if (self = [super init]) {
_isEnabled = YES;
Expand All @@ -158,7 +153,6 @@ - (instancetype)initWithOptions:(SentryOptions *)options
self.locale = locale;
self.timezone = timezone;
self.attachmentProcessors = [[NSMutableArray alloc] init];
self.extraContextProvider = extraContentProvider;

if (deleteOldEnvelopeItems) {
[fileManager deleteOldEnvelopeItems];
Expand Down Expand Up @@ -792,7 +786,8 @@ - (void)applyCultureContextToEvent:(SentryEvent *)event

- (void)applyExtraDeviceContextToEvent:(SentryEvent *)event
{
NSDictionary *extraContext = [[self extraContextProvider] getExtraContext];
NSDictionary *extraContext =
[SentryDependencyContainer.sharedInstance.extraContextProvider getExtraContext];
[self modifyContext:event
key:@"device"
block:^(NSMutableDictionary *device) {
Expand Down
13 changes: 13 additions & 0 deletions Sources/Sentry/SentryDependencyContainer.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#import "SentryDispatchFactory.h"
#import "SentryDispatchQueueWrapper.h"
#import "SentryDisplayLinkWrapper.h"
#import "SentryExtraContextProvider.h"
#import "SentryNSProcessInfoWrapper.h"
#import "SentryNSTimerFactory.h"
#import "SentryRandom.h"
Expand Down Expand Up @@ -101,6 +102,18 @@ - (SentryCrashWrapper *)crashWrapper
return _crashWrapper;
}

- (SentryExtraContextProvider *)extraContextProvider
{
if (_extraContextProvider == nil) {
@synchronized(sentryDependencyContainerLock) {
if (_extraContextProvider == nil) {
_extraContextProvider = [[SentryExtraContextProvider alloc] init];
}
}
}
return _extraContextProvider;
}

- (SentryThreadWrapper *)threadWrapper
{
if (_threadWrapper == nil) {
Expand Down
8 changes: 4 additions & 4 deletions Sources/Sentry/SentryExtraContextProvider.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#import "SentryCrashWrapper.h"
#import "SentryNSProcessInfoWrapper.h"
#import <Foundation/Foundation.h>

@class SentryCrashWrapper;
@class SentryNSProcessInfoWrapper;
@class SentryUIDeviceWrapper;

NS_ASSUME_NONNULL_BEGIN

/**
* Provider of dynamic context data that we need to read at the time of an exception.
*/
@interface SentryExtraContextProvider : NSObject

+ (instancetype)sharedInstance;

- (instancetype)initWithCrashWrapper:(SentryCrashWrapper *)crashWrapper
processInfoWrapper:(SentryNSProcessInfoWrapper *)processInfoWrapper;

Expand Down
8 changes: 0 additions & 8 deletions Sources/Sentry/SentryExtraContextProvider.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@

@implementation SentryExtraContextProvider

+ (instancetype)sharedInstance
{
static SentryExtraContextProvider *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ instance = [[self alloc] init]; });
return instance;
}

- (instancetype)init
{
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
@class SentryDebugImageProvider;
@class SentryDispatchFactory;
@class SentryDispatchQueueWrapper;
@class SentryExtraContextProvider;
@class SentryFileManager;
@class SentryNSNotificationCenterWrapper;
@class SentryNSProcessInfoWrapper;
Expand Down Expand Up @@ -60,6 +61,7 @@ SENTRY_NO_INIT
@property (nonatomic, strong) SentryNSTimerFactory *timerFactory;
@property (nonatomic, strong) SentryCurrentDateProvider *dateProvider;
@property (nonatomic, strong) SentryBinaryImageCache *binaryImageCache;
@property (nonatomic, strong) SentryExtraContextProvider *extraContextProvider;

#if SENTRY_HAS_UIKIT
@property (nonatomic, strong) SentryFramesTracker *framesTracker;
Expand Down
4 changes: 1 addition & 3 deletions Tests/SentryTests/SentryClient+TestInit.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#import "SentryExtraContextProvider.h"
#import "SentryRandom.h"
#import "SentryTransport.h"

Expand Down Expand Up @@ -32,8 +31,7 @@ SentryClient ()
threadInspector:(SentryThreadInspector *)threadInspector
random:(id<SentryRandom>)random
locale:(NSLocale *)locale
timezone:(NSTimeZone *)timezone
extraContextProvider:(SentryExtraContextProvider *)extraContextProvider;
timezone:(NSTimeZone *)timezone;

@end

Expand Down
4 changes: 2 additions & 2 deletions Tests/SentryTests/SentryClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class SentryClientTest: XCTestCase {
#endif // os(iOS) || targetEnvironment(macCatalyst)

extraContentProvider = SentryExtraContextProvider(crashWrapper: crashWrapper, processInfoWrapper: processWrapper)
SentryDependencyContainer.sharedInstance().extraContextProvider = extraContentProvider
}

func getSut(configureOptions: (Options) -> Void = { _ in }) -> SentryClient {
Expand All @@ -88,8 +89,7 @@ class SentryClientTest: XCTestCase {
threadInspector: threadInspector,
random: random,
locale: locale,
timezone: timezone,
extraContextProvider: extraContentProvider
timezone: timezone
)
} catch {
XCTFail("Options could not be created")
Expand Down