-
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.
Discard unfinished spans when the app goes into the background
- Loading branch information
Showing
10 changed files
with
226 additions
and
0 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// | ||
// WeakSpansList.h | ||
// BugsnagPerformance-iOS | ||
// | ||
// Created by Karl Stenerud on 08.12.23. | ||
// Copyright © 2023 Bugsnag. All rights reserved. | ||
// | ||
|
||
#import <BugsnagPerformance/BugsnagPerformanceSpan.h> | ||
#import <mutex> | ||
|
||
@interface BSGWeakSpanPointer: NSObject | ||
|
||
// We use a weak wrapper because NSPointerArray.weakObjectsPointerArray's compact method is broken. | ||
@property(nonatomic,readonly,weak) BugsnagPerformanceSpan *span; | ||
|
||
- (instancetype) initWithSpan:(BugsnagPerformanceSpan *)span; | ||
|
||
+ (instancetype) pointerWithSpan:(BugsnagPerformanceSpan *)span; | ||
|
||
@end | ||
|
||
namespace bugsnag { | ||
|
||
class WeakSpansList { | ||
public: | ||
WeakSpansList() | ||
: spans_([NSMutableArray new]) | ||
{} | ||
|
||
void add(BugsnagPerformanceSpan *span) noexcept { | ||
auto ptr = [BSGWeakSpanPointer pointerWithSpan:span]; | ||
std::lock_guard<std::mutex> guard(mutex_); | ||
[spans_ addObject:ptr]; | ||
} | ||
|
||
void compact() noexcept { | ||
std::lock_guard<std::mutex> guard(mutex_); | ||
bool canCompact = false; | ||
for (BSGWeakSpanPointer *ptr in spans_) { | ||
if (!ptr.span.isValid) { | ||
canCompact = true; | ||
break; | ||
} | ||
} | ||
if (canCompact) { | ||
auto newSpans = [NSMutableArray arrayWithCapacity:spans_.count]; | ||
for (BSGWeakSpanPointer *ptr in spans_) { | ||
if (ptr.span.isValid) { | ||
[newSpans addObject:ptr]; | ||
} | ||
} | ||
spans_ = newSpans; | ||
} | ||
} | ||
|
||
void abortAll() noexcept { | ||
std::lock_guard<std::mutex> guard(mutex_); | ||
for (BSGWeakSpanPointer *ptr in spans_) { | ||
[ptr.span abort]; | ||
} | ||
[spans_ removeAllObjects]; | ||
} | ||
|
||
NSUInteger count() noexcept { | ||
return spans_.count; | ||
} | ||
|
||
private: | ||
std::mutex mutex_; | ||
NSMutableArray *spans_; | ||
}; | ||
|
||
} |
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,24 @@ | ||
// | ||
// WeakSpansList.c | ||
// BugsnagPerformance-iOS | ||
// | ||
// Created by Karl Stenerud on 12.12.23. | ||
// Copyright © 2023 Bugsnag. All rights reserved. | ||
// | ||
|
||
#import "WeakSpansList.h" | ||
|
||
@implementation BSGWeakSpanPointer | ||
|
||
- (instancetype) initWithSpan:(BugsnagPerformanceSpan *)span { | ||
if ((self = [super init])) { | ||
_span = span; | ||
} | ||
return self; | ||
} | ||
|
||
+ (instancetype) pointerWithSpan:(BugsnagPerformanceSpan *)span { | ||
return [[self alloc] initWithSpan:span]; | ||
} | ||
|
||
@end |
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,70 @@ | ||
// | ||
// WeakSpansListTests.m | ||
// BugsnagPerformance-iOSTests | ||
// | ||
// Created by Karl Stenerud on 08.12.23. | ||
// Copyright © 2023 Bugsnag. All rights reserved. | ||
// | ||
|
||
#import <XCTest/XCTest.h> | ||
#import "WeakSpansList.h" | ||
#import "BugsnagPerformanceSpan+Private.h" | ||
#import "SpanOptions.h" | ||
|
||
using namespace bugsnag; | ||
|
||
@interface WeakSpansListTests : XCTestCase | ||
|
||
@end | ||
|
||
static BugsnagPerformanceSpan *createSpan() { | ||
return [[BugsnagPerformanceSpan alloc] | ||
initWithSpan:std::make_unique<Span>(@"test", | ||
IdGenerator::generateTraceId(), | ||
IdGenerator::generateSpanId(), | ||
IdGenerator::generateSpanId(), | ||
SpanOptions().startTime, | ||
BSGFirstClassNo, | ||
^void(std::shared_ptr<SpanData>) { | ||
})]; | ||
} | ||
|
||
@implementation WeakSpansListTests | ||
|
||
- (void)testAllReleased { | ||
WeakSpansList list; | ||
@autoreleasepool { | ||
list.add(createSpan()); | ||
list.add(createSpan()); | ||
list.add(createSpan()); | ||
} | ||
XCTAssertEqual(3U, list.count()); | ||
list.compact(); | ||
XCTAssertEqual(0U, list.count()); | ||
} | ||
|
||
- (void)testNoneReleased { | ||
WeakSpansList list; | ||
list.add(createSpan()); | ||
list.add(createSpan()); | ||
list.add(createSpan()); | ||
XCTAssertEqual(3U, list.count()); | ||
list.compact(); | ||
XCTAssertEqual(3U, list.count()); | ||
} | ||
|
||
- (void)testSomeReleased { | ||
WeakSpansList list; | ||
list.add(createSpan()); | ||
@autoreleasepool { | ||
list.add(createSpan()); | ||
list.add(createSpan()); | ||
list.add(createSpan()); | ||
} | ||
list.add(createSpan()); | ||
XCTAssertEqual(5U, list.count()); | ||
list.compact(); | ||
XCTAssertEqual(2U, list.count()); | ||
} | ||
|
||
@end |