Skip to content

Commit b203816

Browse files
author
Robert Bartoszewski
committed
Moved code from Tracer to SpanLifecycleHandler
1 parent b020a1b commit b203816

32 files changed

+560
-406
lines changed

BugsnagPerformance.xcodeproj/project.pbxproj

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@
185185
966634DC2C8A39C1004A934D /* FrozenFrameData.mm in Sources */ = {isa = PBXBuildFile; fileRef = 966634DB2C8A39C1004A934D /* FrozenFrameData.mm */; };
186186
966DD5012A211D7F002030B2 /* BugsnagPerformance.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 72E4BB63359EA30E80116E2A /* BugsnagPerformance.framework */; };
187187
966DD5022A211D7F002030B2 /* BugsnagPerformance.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 72E4BB63359EA30E80116E2A /* BugsnagPerformance.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
188+
967949B62E97CC9E005FD87F /* SpanLifecycleHandler.h in Headers */ = {isa = PBXBuildFile; fileRef = 967949B52E97CC95005FD87F /* SpanLifecycleHandler.h */; };
189+
967949B82E97CCA9005FD87F /* SpanLifecycleHandlerImpl.mm in Sources */ = {isa = PBXBuildFile; fileRef = 967949B72E97CCA6005FD87F /* SpanLifecycleHandlerImpl.mm */; };
190+
967949BA2E97D5A5005FD87F /* SpanLifecycleHandlerImpl.h in Headers */ = {isa = PBXBuildFile; fileRef = 967949B92E97D59B005FD87F /* SpanLifecycleHandlerImpl.h */; };
188191
967F6F1829C3783B0054EED8 /* BugsnagPerformanceConfiguration+Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 967F6F1729C3782D0054EED8 /* BugsnagPerformanceConfiguration+Private.h */; };
189192
968AA5FB2CCA5A9200BA69CF /* BSGPerformanceSharedSessionProxy.h in Headers */ = {isa = PBXBuildFile; fileRef = 968AA5FA2CCA5A9200BA69CF /* BSGPerformanceSharedSessionProxy.h */; };
190193
968AA5FD2CCA5AB000BA69CF /* BSGPerformanceSharedSessionProxy.mm in Sources */ = {isa = PBXBuildFile; fileRef = 968AA5FC2CCA5AB000BA69CF /* BSGPerformanceSharedSessionProxy.mm */; };
@@ -540,6 +543,9 @@
540543
966634D62C8A365B004A934D /* FrameMetricsSnapshot.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = FrameMetricsSnapshot.mm; sourceTree = "<group>"; };
541544
966634D92C8A39B1004A934D /* FrozenFrameData.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FrozenFrameData.h; sourceTree = "<group>"; };
542545
966634DB2C8A39C1004A934D /* FrozenFrameData.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = FrozenFrameData.mm; sourceTree = "<group>"; };
546+
967949B52E97CC95005FD87F /* SpanLifecycleHandler.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SpanLifecycleHandler.h; sourceTree = "<group>"; };
547+
967949B72E97CCA6005FD87F /* SpanLifecycleHandlerImpl.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = SpanLifecycleHandlerImpl.mm; sourceTree = "<group>"; };
548+
967949B92E97D59B005FD87F /* SpanLifecycleHandlerImpl.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SpanLifecycleHandlerImpl.h; sourceTree = "<group>"; };
543549
967F6F1729C3782D0054EED8 /* BugsnagPerformanceConfiguration+Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "BugsnagPerformanceConfiguration+Private.h"; sourceTree = "<group>"; };
544550
968AA5FA2CCA5A9200BA69CF /* BSGPerformanceSharedSessionProxy.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BSGPerformanceSharedSessionProxy.h; sourceTree = "<group>"; };
545551
968AA5FC2CCA5AB000BA69CF /* BSGPerformanceSharedSessionProxy.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = BSGPerformanceSharedSessionProxy.mm; sourceTree = "<group>"; };
@@ -857,6 +863,7 @@
857863
963726C42DEAB14D00C739E6 /* SpanControl */,
858864
969EE0EE2E7872A600600F63 /* SpanFactory */,
859865
0122C22329019770002D243C /* SpanKind.h */,
866+
967949B42E97CC7F005FD87F /* SpanLifecycle */,
860867
CB7FD935299D330500499E13 /* SpanOptions.h */,
861868
96D55C7D2A1EA5A8006D1F29 /* SpanStackingHandler.h */,
862869
96D55C7F2A1EA5C6006D1F29 /* SpanStackingHandler.mm */,
@@ -1180,6 +1187,16 @@
11801187
path = FrameRateMetrics;
11811188
sourceTree = "<group>";
11821189
};
1190+
967949B42E97CC7F005FD87F /* SpanLifecycle */ = {
1191+
isa = PBXGroup;
1192+
children = (
1193+
967949B52E97CC95005FD87F /* SpanLifecycleHandler.h */,
1194+
967949B92E97D59B005FD87F /* SpanLifecycleHandlerImpl.h */,
1195+
967949B72E97CCA6005FD87F /* SpanLifecycleHandlerImpl.mm */,
1196+
);
1197+
path = SpanLifecycle;
1198+
sourceTree = "<group>";
1199+
};
11831200
969EE0EE2E7872A600600F63 /* SpanFactory */ = {
11841201
isa = PBXGroup;
11851202
children = (
@@ -1300,6 +1317,7 @@
13001317
isa = PBXHeadersBuildPhase;
13011318
buildActionMask = 2147483647;
13021319
files = (
1320+
967949B62E97CC9E005FD87F /* SpanLifecycleHandler.h in Headers */,
13031321
9638291D2E54B4A200404F3A /* AppStartupLifecycleHandlerImpl.h in Headers */,
13041322
CB04969729150D860097E526 /* OtlpPackage.h in Headers */,
13051323
960EECF32B24CA24009FAA11 /* BugsnagPerformanceTrackedViewContainer.h in Headers */,
@@ -1373,6 +1391,7 @@
13731391
0122C24229019770002D243C /* SpanKind.h in Headers */,
13741392
098FC84E2D350E60001B627D /* FixedLengthDequeue.h in Headers */,
13751393
CB78819D29E587CE00A58906 /* BugsnagPerformanceLibrary.h in Headers */,
1394+
967949BA2E97D5A5005FD87F /* SpanLifecycleHandlerImpl.h in Headers */,
13761395
098FC8462D2EB8E8001B627D /* BSGPSystemInfo.h in Headers */,
13771396
CB572EAA29BB783200FD7A2A /* AppStateTracker.h in Headers */,
13781397
CBEC51DC2976F1F9009C0CE3 /* RetryQueue.h in Headers */,
@@ -1770,6 +1789,7 @@
17701789
963829452E57537900404F3A /* ViewLoadLifecycleHandlerImpl.mm in Sources */,
17711790
0122C24429019770002D243C /* Tracer.mm in Sources */,
17721791
962CE8112E65224300380522 /* BSGURLSessionPerformanceDelegate.mm in Sources */,
1792+
967949B82E97CCA9005FD87F /* SpanLifecycleHandlerImpl.mm in Sources */,
17731793
CB04969829150D860097E526 /* OtlpPackage.mm in Sources */,
17741794
0122C23D29019770002D243C /* BugsnagPerformance.mm in Sources */,
17751795
9638293E2E56284E00404F3A /* ViewLoadSwizzlingCallbacks.m in Sources */,

Sources/BugsnagPerformance/Private/BugsnagPerformanceImpl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#import "SpanFactory/AppStartup/AppStartupSpanFactoryImpl.h"
3636
#import "SpanFactory/ViewLoad/ViewLoadSpanFactoryImpl.h"
3737
#import "SpanFactory/Network/NetworkSpanFactoryImpl.h"
38+
#import "SpanLifecycle/SpanLifecycleHandlerImpl.h"
3839

3940
#import <mutex>
4041

@@ -108,6 +109,7 @@ class BugsnagPerformanceImpl: public PhasedStartup {
108109
std::shared_ptr<AppStartupSpanFactoryImpl> appStartupSpanFactory_;
109110
std::shared_ptr<ViewLoadSpanFactoryImpl> viewLoadSpanFactory_;
110111
std::shared_ptr<NetworkSpanFactoryImpl> networkSpanFactory_;
112+
std::shared_ptr<SpanLifecycleHandlerImpl> spanLifecycleHandler_;
111113
std::shared_ptr<Tracer> tracer_;
112114
std::unique_ptr<RetryQueue> retryQueue_;
113115
AppStateTracker *appStateTracker_;

Sources/BugsnagPerformance/Private/BugsnagPerformanceImpl.mm

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
, appStartupSpanFactory_(std::make_shared<AppStartupSpanFactoryImpl>(plainSpanFactory_, spanAttributesProvider_))
5757
, viewLoadSpanFactory_(std::make_shared<ViewLoadSpanFactoryImpl>(plainSpanFactory_, spanAttributesProvider_))
5858
, networkSpanFactory_(std::make_shared<NetworkSpanFactoryImpl>(plainSpanFactory_, spanAttributesProvider_))
59-
, tracer_(std::make_shared<Tracer>(spanStackingHandler_, sampler_, batch_, frameMetricsCollector_, conditionTimeoutExecutor_, plainSpanFactory_, viewLoadSpanFactory_, networkSpanFactory_, spanStartCallbacks_, spanEndCallbacks_, ^{this->onSpanStarted();}))
59+
, spanLifecycleHandler_(std::make_shared<SpanLifecycleHandlerImpl>(sampler_, spanStackingHandler_, conditionTimeoutExecutor_, plainSpanFactory_, batch_, frameMetricsCollector_, spanStartCallbacks_, spanEndCallbacks_, ^{this->onSpanStarted();}))
60+
, tracer_(std::make_shared<Tracer>(plainSpanFactory_, viewLoadSpanFactory_, networkSpanFactory_, spanLifecycleHandler_, spanStackingHandler_))
6061
, retryQueue_(std::make_unique<RetryQueue>([persistence_->bugsnagPerformanceDir() stringByAppendingPathComponent:@"retry-queue"]))
6162
, appStateTracker_(appStateTracker)
6263
, viewControllersToSpans_([NSMapTable mapTableWithKeyOptions:NSMapTableWeakMemory | NSMapTableObjectPointerPersonality
@@ -94,6 +95,7 @@
9495
networkHeaderInjector_->earlyConfigure(config);
9596
retryQueue_->earlyConfigure(config);
9697
batch_->earlyConfigure(config);
98+
spanLifecycleHandler_->earlyConfigure(config);
9799
instrumentation_->earlyConfigure(config);
98100
[worker_ earlyConfigure:config];
99101
[frameMetricsCollector_ earlyConfigure:config];
@@ -125,6 +127,7 @@
125127
networkHeaderInjector_->earlySetup();
126128
retryQueue_->earlySetup();
127129
batch_->earlySetup();
130+
spanLifecycleHandler_->earlySetup();
128131
instrumentation_->earlySetup();
129132
[worker_ earlySetup];
130133
[frameMetricsCollector_ earlySetup];
@@ -176,6 +179,7 @@
176179
networkHeaderInjector_->configure(config);
177180
retryQueue_->configure(config);
178181
batch_->configure(config);
182+
spanLifecycleHandler_->configure(config);
179183
instrumentation_->configure(config);
180184
[worker_ configure:config];
181185
[frameMetricsCollector_ configure:config];
@@ -193,6 +197,7 @@
193197
networkHeaderInjector_->preStartSetup();
194198
retryQueue_->preStartSetup();
195199
batch_->preStartSetup();
200+
spanLifecycleHandler_->preStartSetup();
196201
instrumentation_->preStartSetup();
197202
[worker_ preStartSetup];
198203
}
@@ -233,6 +238,7 @@
233238
persistentState_->start();
234239
deviceID_->start();
235240
traceEncoding_.start();
241+
spanLifecycleHandler_->start();
236242

237243
retryQueue_->setOnFilesystemError(^{
238244
blockThis->onFilesystemError();
@@ -412,7 +418,7 @@
412418

413419
bool BugsnagPerformanceImpl::sweepTracerTask() noexcept {
414420
BSGLogDebug(@"BugsnagPerformanceImpl::sweepTracerTask()");
415-
tracer_->sweep();
421+
spanLifecycleHandler_->sweep();
416422
// Never auto-repeat this task, even if work was done; it can wait.
417423
return false;
418424
}
@@ -488,11 +494,7 @@
488494
instrumentation_->abortAppStartupSpans();
489495
}
490496

491-
if (!isStarted_) {
492-
return;
493-
}
494-
495-
tracer_->abortAllOpenSpans();
497+
spanLifecycleHandler_->onAppEnteredBackground();
496498
}
497499

498500
void BugsnagPerformanceImpl::onAppEnteredForeground() noexcept {

Sources/BugsnagPerformance/Private/BugsnagPerformanceSpan+Private.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ NS_ASSUME_NONNULL_BEGIN
5151
@property (nonatomic,readonly) NSMutableDictionary *attributes;
5252
@property (nonatomic) SpanLifecycleCallback onSpanEndSet;
5353
@property (nonatomic) SpanLifecycleCallback onSpanClosed;
54+
@property (nonatomic) SpanLifecycleCallback onSpanCancelled;
5455
@property (nonatomic) SpanBlockedCallback onSpanBlocked;
5556
@property (nonatomic,readwrite) SpanId parentId;
5657
@property (nonatomic) double samplingProbability;
@@ -83,7 +84,8 @@ NS_ASSUME_NONNULL_BEGIN
8384
conditionsToEndOnClose:(NSArray<BugsnagPerformanceSpanCondition *> *)conditionsToEndOnClose
8485
onSpanEndSet:(SpanLifecycleCallback) onSpanEndSet
8586
onSpanClosed:(SpanLifecycleCallback) onSpanEnded
86-
onSpanBlocked:(SpanBlockedCallback) onSpanBlocked NS_DESIGNATED_INITIALIZER;
87+
onSpanBlocked:(SpanBlockedCallback) onSpanBlocked
88+
onSpanCancelled:(SpanLifecycleCallback) onSpanCancelled NS_DESIGNATED_INITIALIZER;
8789

8890
- (void)internalSetAttribute:(NSString *)attributeName withValue:(_Nullable id)value;
8991
- (void)internalSetMultipleAttributes:(NSDictionary *)attributes;
@@ -95,6 +97,7 @@ NS_ASSUME_NONNULL_BEGIN
9597
- (void)endWithAbsoluteTime:(CFAbsoluteTime)endTime;
9698

9799
- (void)endOnDestroy;
100+
- (void)cancel;
98101

99102
- (SpanId)parentId;
100103
- (void)updateName:(NSString *)name;

Sources/BugsnagPerformance/Private/Instrumentation/AppStartupInstrumentation/Lifecycle/AppStartupLifecycleHandlerImpl.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#import "../System/AppStartupInstrumentationSystemUtils.h"
1212
#import "../../../SpanAttributesProvider.h"
1313
#import "../../../BugsnagPerformanceCrossTalkAPI.h"
14-
#import "../../../Tracer.h"
1514

1615
#import <memory>
1716

@@ -23,7 +22,6 @@ class AppStartupLifecycleHandlerImpl: public AppStartupLifecycleHandler {
2322
public:
2423
AppStartupLifecycleHandlerImpl(std::shared_ptr<AppStartupSpanFactory> spanFactory,
2524
std::shared_ptr<SpanAttributesProvider> spanAttributesProvider,
26-
std::shared_ptr<Tracer> tracer,
2725
std::shared_ptr<AppStartupInstrumentationSystemUtils> systemUtils,
2826
BugsnagPerformanceCrossTalkAPI *crossTalkAPI) noexcept;
2927

@@ -38,7 +36,6 @@ class AppStartupLifecycleHandlerImpl: public AppStartupLifecycleHandler {
3836
private:
3937
std::shared_ptr<AppStartupSpanFactory> spanFactory_;
4038
std::shared_ptr<SpanAttributesProvider> spanAttributesProvider_;
41-
std::shared_ptr<Tracer> tracer_;
4239
std::shared_ptr<AppStartupInstrumentationSystemUtils> systemUtils_;
4340
BugsnagPerformanceCrossTalkAPI *crossTalkAPI_;
4441

Sources/BugsnagPerformance/Private/Instrumentation/AppStartupInstrumentation/Lifecycle/AppStartupLifecycleHandlerImpl.mm

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@ static bool isAppStartInProgress(AppStartupInstrumentationState *state) noexcept
2222

2323
AppStartupLifecycleHandlerImpl::AppStartupLifecycleHandlerImpl(std::shared_ptr<AppStartupSpanFactory> spanFactory,
2424
std::shared_ptr<SpanAttributesProvider> spanAttributesProvider,
25-
std::shared_ptr<Tracer> tracer,
2625
std::shared_ptr<AppStartupInstrumentationSystemUtils> systemUtils,
2726
BugsnagPerformanceCrossTalkAPI *crossTalkAPI) noexcept
2827
: spanFactory_(spanFactory)
2928
, spanAttributesProvider_(spanAttributesProvider)
30-
, tracer_(tracer)
3129
, systemUtils_(systemUtils)
3230
, crossTalkAPI_(crossTalkAPI) {}
3331

@@ -93,10 +91,10 @@ static bool isAppStartInProgress(AppStartupInstrumentationState *state) noexcept
9391

9492
void
9593
AppStartupLifecycleHandlerImpl::onAppInstrumentationDisabled(AppStartupInstrumentationState *state) noexcept {
96-
tracer_->cancelQueuedSpan(state.preMainSpan);
97-
tracer_->cancelQueuedSpan(state.postMainSpan);
98-
tracer_->cancelQueuedSpan(state.uiInitSpan);
99-
tracer_->cancelQueuedSpan(state.appStartSpan);
94+
[state.preMainSpan cancel];
95+
[state.postMainSpan cancel];
96+
[state.uiInitSpan cancel];
97+
[state.appStartSpan cancel];
10098
state.preMainSpan = nil;
10199
state.postMainSpan = nil;
102100
state.uiInitSpan = nil;

Sources/BugsnagPerformance/Private/Instrumentation/Instrumentation.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,13 @@ NS_ASSUME_NONNULL_BEGIN
2727

2828
typedef AppStartupInstrumentationStateSnapshot * _Nullable (^GetAppStartInstrumentationStateSnapshot)();
2929

30-
std::shared_ptr<AppStartupInstrumentation> createAppStartupInstrumentation(std::shared_ptr<Tracer> tracer,
31-
std::shared_ptr<AppStartupSpanFactory> spanFactory,
30+
std::shared_ptr<AppStartupInstrumentation> createAppStartupInstrumentation(std::shared_ptr<AppStartupSpanFactory> spanFactory,
3231
std::shared_ptr<SpanAttributesProvider> spanAttributesProvider);
3332

34-
std::shared_ptr<ViewLoadInstrumentation> createViewLoadInstrumentation(std::shared_ptr<Tracer> tracer,
35-
std::shared_ptr<ViewLoadSpanFactory> spanFactory,
33+
std::shared_ptr<ViewLoadInstrumentation> createViewLoadInstrumentation(std::shared_ptr<ViewLoadSpanFactory> spanFactory,
3634
std::shared_ptr<SpanAttributesProvider> spanAttributesProvider);
3735

38-
std::shared_ptr<NetworkInstrumentation> createNetworkInstrumentation(std::shared_ptr<Tracer> tracer,
39-
std::shared_ptr<NetworkSpanFactory> spanFactory,
36+
std::shared_ptr<NetworkInstrumentation> createNetworkInstrumentation(std::shared_ptr<NetworkSpanFactory> spanFactory,
4037
std::shared_ptr<SpanAttributesProvider> spanAttributesProvider,
4138
std::shared_ptr<NetworkHeaderInjector> networkHeaderInjector);
4239

@@ -50,9 +47,9 @@ class Instrumentation: public PhasedStartup {
5047
std::shared_ptr<NetworkSpanFactory> networkSpanFactory,
5148
std::shared_ptr<SpanAttributesProvider> spanAttributesProvider,
5249
std::shared_ptr<NetworkHeaderInjector> networkHeaderInjector) noexcept
53-
: appStartupInstrumentation_(createAppStartupInstrumentation(tracer, appStartupSpanFactory, spanAttributesProvider))
54-
, viewLoadInstrumentation_(createViewLoadInstrumentation(tracer, viewLoadSpanFactory, spanAttributesProvider))
55-
, networkInstrumentation_(createNetworkInstrumentation(tracer, networkSpanFactory, spanAttributesProvider, networkHeaderInjector))
50+
: appStartupInstrumentation_(createAppStartupInstrumentation(appStartupSpanFactory, spanAttributesProvider))
51+
, viewLoadInstrumentation_(createViewLoadInstrumentation(viewLoadSpanFactory, spanAttributesProvider))
52+
, networkInstrumentation_(createNetworkInstrumentation(networkSpanFactory, spanAttributesProvider, networkHeaderInjector))
5653
{
5754
tracer->setGetAppStartInstrumentationState([=]{ return appStartupInstrumentation_->stateSnapshot(); });
5855
}

0 commit comments

Comments
 (0)