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

Ensure no code runs in the constructors #204

Draft
wants to merge 3 commits into
base: next
Choose a base branch
from
Draft
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
13 changes: 7 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ GEM
selenium-webdriver (~> 4.2, < 4.6)
bugsnag (6.26.0)
concurrent-ruby (~> 1.0)
bugsnag-maze-runner (8.4.0)
bugsnag-maze-runner (8.4.1)
appium_lib (~> 12.0.0)
appium_lib_core (~> 5.4.0)
bugsnag (~> 6.24)
Expand Down Expand Up @@ -70,17 +70,17 @@ GEM
faye-websocket (0.11.3)
eventmachine (>= 0.12.0)
websocket-driver (>= 0.5.1)
ffi (1.15.5)
ffi (1.16.3)
hana (1.3.7)
json_schemer (0.2.25)
ecma-re-validator (~> 0.3)
hana (~> 1.3)
regexp_parser (~> 2.0)
simpleidn (~> 0.2)
uri_template (~> 0.7)
mime-types (3.5.0)
mime-types (3.5.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2023.0808)
mime-types-data (3.2023.1003)
multi_test (0.1.2)
nokogiri (1.15.4-arm64-darwin)
racc (~> 1.4)
Expand All @@ -92,7 +92,7 @@ GEM
racc (1.7.1)
rack (2.2.8)
rake (12.3.3)
regexp_parser (2.8.1)
regexp_parser (2.8.2)
rexml (3.2.6)
rubyzip (2.3.2)
selenium-webdriver (4.5.0)
Expand All @@ -112,13 +112,14 @@ GEM
unf_ext (0.0.8.2)
uri_template (0.7.0)
webrick (1.7.0)
websocket (1.2.9)
websocket (1.2.10)
websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)

PLATFORMS
arm64-darwin-21
arm64-darwin-22
x86_64-darwin-20

DEPENDENCIES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class BugsnagPerformanceImpl: public PhasedStartup {

void onSpanStarted() noexcept;

void setOnViewLoadSpanStarted(void (^onViewLoadSpanStarted)(NSString *className)) noexcept {
void setOnViewLoadSpanStarted(std::function<void(NSString *)> onViewLoadSpanStarted) noexcept {
tracer_->setOnViewLoadSpanStarted(onViewLoadSpanStarted);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@
return NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
}

void (^generateOnSpanStarted(BugsnagPerformanceImpl *impl))(void) {
__block auto blockImpl = impl;
return ^{
blockImpl->onSpanStarted();
};
}

BugsnagPerformanceImpl::BugsnagPerformanceImpl(std::shared_ptr<Reachability> reachability,
AppStateTracker *appStateTracker) noexcept
: persistence_(std::make_shared<Persistence>(getPersistenceDir()))
Expand All @@ -36,7 +29,7 @@
, reachability_(reachability)
, batch_(std::make_shared<Batch>())
, sampler_(std::make_shared<Sampler>())
, tracer_(std::make_shared<Tracer>(spanStackingHandler_, sampler_, batch_, generateOnSpanStarted(this)))
, tracer_(std::make_shared<Tracer>(spanStackingHandler_, sampler_, batch_, ^{this->onSpanStarted();}))
, retryQueue_(std::make_unique<RetryQueue>([persistence_->bugsnagPerformanceDir() stringByAppendingPathComponent:@"retry-queue"]))
, appStateTracker_(appStateTracker)
, viewControllersToSpans_([NSMapTable mapTableWithKeyOptions:NSMapTableWeakMemory | NSMapTableObjectPointerPersonality
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@
: appStateTracker_([[AppStateTracker alloc] init])
, reachability_(std::make_shared<Reachability>())
, bugsnagPerformanceImpl_(std::make_shared<BugsnagPerformanceImpl>(reachability_, appStateTracker_))
{
bugsnagPerformanceImpl_->setOnViewLoadSpanStarted(^(NSString *className) {
bugsnagPerformanceImpl_->didStartViewLoadSpan(className);
});
}
{}

void BugsnagPerformanceLibrary::earlyConfigure(BSGEarlyConfiguration *config) noexcept {
bugsnagPerformanceImpl_->earlyConfigure(config);
}

void BugsnagPerformanceLibrary::earlySetup() noexcept {
auto impl = bugsnagPerformanceImpl_;
bugsnagPerformanceImpl_->setOnViewLoadSpanStarted([=](NSString *className) {
impl->didStartViewLoadSpan(className);
});
bugsnagPerformanceImpl_->earlySetup();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class AppStartupInstrumentation: public PhasedStartup {
std::shared_ptr<SpanAttributesProvider> spanAttributesProvider) noexcept;

void earlyConfigure(BSGEarlyConfiguration *) noexcept {}
void earlySetup() noexcept {}
void earlySetup() noexcept;
void configure(BugsnagPerformanceConfiguration *config) noexcept;
void start() noexcept {}

Expand All @@ -35,6 +35,7 @@ class AppStartupInstrumentation: public PhasedStartup {
std::shared_ptr<Tracer> tracer_;
std::shared_ptr<SpanAttributesProvider> spanAttributesProvider_;
CFAbsoluteTime didStartProcessAtTime_{0};
CFAbsoluteTime didInitializeAtTime_{0};
CFAbsoluteTime didCallMainFunctionAtTime_{0};
CFAbsoluteTime didBecomeActiveAtTime_{0};
CFAbsoluteTime didFinishLaunchingAtTime_{0};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ static inline bool isActivePrewarm(void) {
, tracer_(tracer)
, spanAttributesProvider_(spanAttributesProvider)
, didStartProcessAtTime_(getProcessStartTime())
, didCallMainFunctionAtTime_(CFAbsoluteTimeGetCurrent())
, didInitializeAtTime_(CFAbsoluteTimeGetCurrent())
, isColdLaunch_(isColdLaunch())
{
{}

void AppStartupInstrumentation::earlySetup() noexcept {
if (!canInstallInstrumentation()) {
disable();
}
Expand All @@ -86,7 +88,7 @@ static inline bool isActivePrewarm(void) {

beginAppStartSpan();
beginPreMainSpan();
[preMainSpan_ endWithAbsoluteTime:didCallMainFunctionAtTime_];
[preMainSpan_ endWithAbsoluteTime:didInitializeAtTime_];
beginPostMainSpan();

shouldRespondToAppDidBecomeActive_ = true;
Expand All @@ -103,6 +105,7 @@ static inline bool isActivePrewarm(void) {
CFSTR("UIApplicationDidBecomeActiveNotification"),
nullptr,
CFNotificationSuspensionBehaviorDeliverImmediately);
didCallMainFunctionAtTime_ = CFAbsoluteTimeGetCurrent();
}

void AppStartupInstrumentation::disable() noexcept {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,16 @@

static void replace_NSURLSessionTask_resume(Class cls, BSGSessionTaskResumeCallback onResume) {
__weak BSGSessionTaskResumeCallback weakOnResume = onResume;
SEL selector = @selector(resume);
typedef void (*IMPPrototype)(id, SEL);
__block IMPPrototype originalIMP = (IMPPrototype)ObjCSwizzle::replaceInstanceMethodOverride(cls,
selector,
^(id self) {
__block SEL selector = @selector(resume);
__block IMP resume = ObjCSwizzle::replaceInstanceMethodOverride(cls, selector, ^(id self) {
BSGSessionTaskResumeCallback localOnResume = weakOnResume;
if (localOnResume != nil) {
localOnResume(self);
}
originalIMP(self, selector);
});
if (resume) {
reinterpret_cast<void (*)(id, SEL)>(resume)(self, selector);
}
}, nullptr);
}

void bsg_installNSURLSessionTaskPerformance(void (^onResume)(NSURLSessionTask *)) noexcept {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,22 @@ class ViewLoadInstrumentation: public PhasedStartup {
void earlyConfigure(BSGEarlyConfiguration *config) noexcept;
void earlySetup() noexcept;
void configure(BugsnagPerformanceConfiguration *config) noexcept;
void start() noexcept {}
void start() noexcept {};

private:
static std::vector<const char *> imagesToInstrument() noexcept;
static std::vector<Class> viewControllerSubclasses(const char *image) noexcept;
static bool isViewControllerSubclass(Class subclass) noexcept;

void instrument(Class cls) noexcept;

void instrumentLoadView(Class cls) noexcept;
void instrumentViewDidLoad(Class cls) noexcept;
void instrumentViewWillAppear(Class cls) noexcept;
void instrumentViewDidAppear(Class cls) noexcept;
void instrumentViewWillDisappear(Class cls) noexcept;
void instrumentViewWillLayoutSubviews(Class cls) noexcept;
void instrumentViewDidLayoutSubviews(Class cls) noexcept;

void onLoadView(UIViewController *viewController) noexcept;
void onViewDidAppear(UIViewController *viewController) noexcept;
void onViewWillDisappear(UIViewController *viewController) noexcept;
Expand Down
Loading
Loading