-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Span ending conditions * Changes requested in code review --------- Co-authored-by: Robert <[email protected]>
- Loading branch information
1 parent
fa26ce7
commit b40fc3d
Showing
28 changed files
with
1,193 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
Sources/BugsnagPerformance/Private/BugsnagPerformanceSpanCondition+Private.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// BugsnagPerformanceSpanCondition+Private.h | ||
// BugsnagPerformance-iOS | ||
// | ||
// Created by Robert B on 15/01/2025. | ||
// Copyright © 2025 Bugsnag. All rights reserved. | ||
// | ||
|
||
#import <BugsnagPerformance/BugsnagPerformanceSpanCondition.h> | ||
|
||
@class BugsnagPerformanceSpan; | ||
@class BugsnagPerformanceSpanContext; | ||
|
||
typedef uint64_t SpanConditionId; | ||
typedef void (^ SpanConditionClosedCallback)(BugsnagPerformanceSpanCondition *condition, CFAbsoluteTime endTime); | ||
typedef BugsnagPerformanceSpanContext *(^ SpanConditionUpgradedCallback)(BugsnagPerformanceSpanCondition *condition); | ||
typedef void (^ SpanConditionDeavtivatedCallback)(BugsnagPerformanceSpanCondition *condition); | ||
|
||
@interface BugsnagPerformanceSpanCondition () | ||
|
||
@property (nonatomic, readonly) SpanConditionId conditionId; | ||
@property (nonatomic, weak) BugsnagPerformanceSpan *span; | ||
|
||
+ (instancetype)conditionWithSpan:(BugsnagPerformanceSpan *)span | ||
onClosedCallback:(SpanConditionClosedCallback)onClosedCallback | ||
onUpgradedCallback:(SpanConditionUpgradedCallback)onUpgradedCallback; | ||
|
||
- (void)didTimeout; | ||
- (void)addOnDeactivatedCallback:(SpanConditionDeavtivatedCallback)onDeactivated; | ||
|
||
@end | ||
|
177 changes: 177 additions & 0 deletions
177
Sources/BugsnagPerformance/Private/BugsnagPerformanceSpanCondition.mm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
// | ||
// SpanCondition.m | ||
// BugsnagPerformance-iOS | ||
// | ||
// Created by Robert B on 15/01/2025. | ||
// Copyright © 2025 Bugsnag. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
#import "BugsnagPerformanceSpanCondition+Private.h" | ||
|
||
typedef NS_ENUM(uint8_t, BSGSpanConditionState) { | ||
BSGSpanConditionStateInitial = 0, | ||
BSGSpanConditionStateUpgraded = 1, | ||
BSGSpanConditionStateClosed = 2, | ||
BSGSpanConditionStateCancelled = 3, | ||
}; | ||
|
||
@interface SpanConditionIdProvider: NSObject | ||
|
||
@property (atomic) SpanConditionId conditionId; | ||
|
||
+ (instancetype)sharedInstance; | ||
|
||
- (SpanConditionId)next; | ||
|
||
@end | ||
|
||
@implementation SpanConditionIdProvider | ||
|
||
+ (instancetype)sharedInstance { | ||
static id sharedInstance; | ||
static dispatch_once_t once; | ||
dispatch_once(&once, ^{ sharedInstance = [[self alloc] init]; }); | ||
return sharedInstance; | ||
} | ||
|
||
- (instancetype)init { | ||
self = [super init]; | ||
if (self) { | ||
_conditionId = 0; | ||
} | ||
return self; | ||
} | ||
|
||
- (SpanConditionId)next { | ||
@synchronized (self) { | ||
return self.conditionId++; | ||
} | ||
} | ||
|
||
@end | ||
|
||
@interface BugsnagPerformanceSpanCondition () | ||
|
||
@property (nonatomic) SpanConditionId conditionId_; | ||
@property (nonatomic) BSGSpanConditionState state; | ||
@property (nonatomic) SpanConditionClosedCallback onClosedCallback; | ||
@property (nonatomic) SpanConditionUpgradedCallback onUpgradedCallback; | ||
@property (nonatomic) NSMutableArray<SpanConditionDeavtivatedCallback> *onDeactivatedCallbacks; | ||
|
||
@end | ||
|
||
@implementation BugsnagPerformanceSpanCondition | ||
|
||
+ (instancetype)conditionWithSpan:(BugsnagPerformanceSpan *)span | ||
onClosedCallback:(SpanConditionClosedCallback)onClosedCallback | ||
onUpgradedCallback:(SpanConditionUpgradedCallback)onUpgradedCallback { | ||
return [[self alloc] initWithId:[[SpanConditionIdProvider sharedInstance] next] | ||
span:span | ||
onClosedCallback:onClosedCallback | ||
onUpgradedCallback:onUpgradedCallback]; | ||
} | ||
|
||
- (instancetype)initWithId:(SpanConditionId)conditionId | ||
span:(BugsnagPerformanceSpan *)span | ||
onClosedCallback:(SpanConditionClosedCallback)onClosedCallback | ||
onUpgradedCallback:(SpanConditionUpgradedCallback)onUpgradedCallback { | ||
self = [super init]; | ||
if (self) { | ||
_conditionId_ = conditionId; | ||
_state = BSGSpanConditionStateInitial; | ||
_span = span; | ||
_onClosedCallback = onClosedCallback; | ||
_onUpgradedCallback = onUpgradedCallback; | ||
_onDeactivatedCallbacks = [NSMutableArray new]; | ||
} | ||
return self; | ||
} | ||
|
||
- (BOOL)isActive { | ||
@synchronized (self) { | ||
return self.state == BSGSpanConditionStateInitial || self.state == BSGSpanConditionStateUpgraded; | ||
} | ||
} | ||
|
||
- (SpanConditionId)conditionId { | ||
return self.conditionId_; | ||
} | ||
|
||
- (void)closeWithEndTime:(NSDate *)endTime { | ||
@synchronized (self) { | ||
switch (self.state) { | ||
case BSGSpanConditionStateClosed: | ||
case BSGSpanConditionStateCancelled: | ||
return; | ||
case BSGSpanConditionStateInitial: | ||
case BSGSpanConditionStateUpgraded: | ||
self.state = BSGSpanConditionStateClosed; | ||
break; | ||
} | ||
} | ||
self.onClosedCallback(self, [endTime timeIntervalSinceReferenceDate]); | ||
[self didDeactivate]; | ||
} | ||
|
||
- (BugsnagPerformanceSpanContext *)upgrade { | ||
@synchronized (self) { | ||
switch (self.state) { | ||
case BSGSpanConditionStateClosed: | ||
case BSGSpanConditionStateCancelled: | ||
case BSGSpanConditionStateUpgraded: | ||
return nil; | ||
case BSGSpanConditionStateInitial: | ||
self.state = BSGSpanConditionStateUpgraded; | ||
break; | ||
} | ||
} | ||
return self.onUpgradedCallback(self); | ||
} | ||
|
||
- (void)cancel { | ||
@synchronized (self) { | ||
switch (self.state) { | ||
case BSGSpanConditionStateClosed: | ||
case BSGSpanConditionStateCancelled: | ||
return; | ||
case BSGSpanConditionStateInitial: | ||
case BSGSpanConditionStateUpgraded: | ||
self.state = BSGSpanConditionStateCancelled; | ||
break; | ||
} | ||
} | ||
[self didDeactivate]; | ||
} | ||
|
||
- (void)didTimeout { | ||
@synchronized (self) { | ||
switch (self.state) { | ||
case BSGSpanConditionStateClosed: | ||
case BSGSpanConditionStateCancelled: | ||
case BSGSpanConditionStateUpgraded: | ||
return; | ||
case BSGSpanConditionStateInitial: | ||
break; | ||
} | ||
[self cancel]; | ||
} | ||
} | ||
|
||
- (void)addOnDeactivatedCallback:(SpanConditionDeavtivatedCallback)onDeactivated { | ||
@synchronized (self) { | ||
[self.onDeactivatedCallbacks addObject:onDeactivated]; | ||
} | ||
} | ||
|
||
- (void)didDeactivate { | ||
NSArray *callbacks; | ||
@synchronized (self) { | ||
callbacks = [self.onDeactivatedCallbacks copy]; | ||
} | ||
for (SpanConditionDeavtivatedCallback callback in callbacks) { | ||
callback(self); | ||
} | ||
} | ||
|
||
@end |
Oops, something went wrong.