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

impr: Stop sending empty thread names #3361

Merged
merged 2 commits into from
Oct 30, 2023
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- Add thread id and name to span data (#3359)

### Improvements

- Stop sending empty thread names (#3361)

## 8.14.2

### Features
Expand Down
4 changes: 2 additions & 2 deletions Sources/Sentry/SentryCrashDefaultMachineContextWrapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ - (SentryCrashThread)getThread:(struct SentryCrashMachineContext *)context withI
return thread;
}

- (void)getThreadName:(const SentryCrashThread)thread
- (BOOL)getThreadName:(const SentryCrashThread)thread
andBuffer:(char *const)buffer
andBufLength:(int)bufLength;
{
sentrycrashthread_getThreadName(thread, buffer, bufLength);
return sentrycrashthread_getThreadName(thread, buffer, bufLength) == true;
}

- (BOOL)isMainThread:(SentryCrashThread)thread
Expand Down
5 changes: 1 addition & 4 deletions Sources/Sentry/SentrySpan.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,8 @@ - (instancetype)initWithContext:(SentrySpanContext *)context
if ([NSThread isMainThread]) {
_data[SPAN_DATA_THREAD_NAME] = @"main";
} else {
NSString *threadName = [SentryDependencyContainer.sharedInstance.threadInspector
_data[SPAN_DATA_THREAD_NAME] = [SentryDependencyContainer.sharedInstance.threadInspector
getThreadName:currentThread];
if (threadName.length > 0) {
_data[SPAN_DATA_THREAD_NAME] = threadName;
}
}

_tags = [[NSMutableDictionary alloc] init];
Expand Down
21 changes: 14 additions & 7 deletions Sources/Sentry/SentryThreadInspector.m
Original file line number Diff line number Diff line change
Expand Up @@ -202,17 +202,24 @@ - (SentryStacktrace *)stacktraceForCurrentThreadAsyncUnsafe
return threads;
}

- (NSString *)getThreadName:(SentryCrashThread)thread
- (nullable NSString *)getThreadName:(SentryCrashThread)thread
{
char buffer[128];
int bufferLength = 128;
char buffer[bufferLength];
char *const pBuffer = buffer;
[self.machineContextWrapper getThreadName:thread andBuffer:pBuffer andBufLength:128];

NSString *threadName = [NSString stringWithCString:pBuffer encoding:NSUTF8StringEncoding];
if (nil == threadName) {
threadName = @"";
BOOL didGetThreadNameSucceed = [self.machineContextWrapper getThreadName:thread
andBuffer:pBuffer
andBufLength:bufferLength];

if (didGetThreadNameSucceed == YES) {
NSString *threadName = [NSString stringWithCString:pBuffer encoding:NSUTF8StringEncoding];
if (threadName.length > 0) {
return threadName;
}
}
return threadName;

return nil;
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ NS_ASSUME_NONNULL_BEGIN

- (SentryCrashThread)getThread:(struct SentryCrashMachineContext *)context withIndex:(int)index;

- (void)getThreadName:(const SentryCrashThread)thread
- (BOOL)getThreadName:(const SentryCrashThread)thread
andBuffer:(char *const)buffer
andBufLength:(int)bufLength;

Expand Down
2 changes: 1 addition & 1 deletion Sources/Sentry/include/SentryThreadInspector.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ SENTRY_NO_INIT
*/
- (NSArray<SentryThread *> *)getCurrentThreadsWithStackTrace;

- (NSString *)getThreadName:(SentryCrashThread)thread;
- (nullable NSString *)getThreadName:(SentryCrashThread)thread;

@end

Expand Down
9 changes: 9 additions & 0 deletions Tests/SentryTests/Protocol/SentryThreadTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ class SentryThreadTests: XCTestCase {
XCTAssertTrue(actual["main"] as! Bool)
}

func testSerialize_ThreadNameNil() {
let thread = TestData.thread
thread.name = nil

let actual = thread.serialize()

XCTAssertNil(actual["name"])
}

func testSerialize_Bools() {
let thread = SentryThread(threadId: 0)

Expand Down
30 changes: 24 additions & 6 deletions Tests/SentryTests/SentryCrash/SentryThreadInspectorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,27 @@ class SentryThreadInspectorTests: XCTestCase {
XCTAssertEqual(threadName, actual[0].name)
}

func testThreadNameIsNull() {
fixture.testMachineContextWrapper.threadName = nil
func testGetThreadName_EmptyThreadName() {
fixture.testMachineContextWrapper.threadName = ""
fixture.testMachineContextWrapper.threadCount = 1

let actual = fixture.getSut().getCurrentThreads()
XCTAssertEqual(1, actual.count)

let thread = actual[0]
XCTAssertEqual("", thread.name)
XCTAssertNil(thread.name)
}

func testGetThreadNameFails() {
fixture.testMachineContextWrapper.threadName = ""
fixture.testMachineContextWrapper.getThreadNameSucceeds = false
fixture.testMachineContextWrapper.threadCount = 1

let actual = fixture.getSut().getCurrentThreads()
XCTAssertEqual(1, actual.count)

let thread = actual[0]
XCTAssertNil(thread.name)
}

func testLongThreadName() {
Expand Down Expand Up @@ -270,16 +282,22 @@ private class TestMachineContextWrapper: NSObject, SentryCrashMachineContextWrap
mockThreads?[Int(index)].threadId ?? 0
}

var threadName: String? = ""
func getThreadName(_ thread: SentryCrashThread, andBuffer buffer: UnsafeMutablePointer<Int8>, andBufLength bufLength: Int32) {
var threadName: String = ""
var getThreadNameSucceeds = true
func getThreadName(_ thread: SentryCrashThread, andBuffer buffer: UnsafeMutablePointer<Int8>, andBufLength bufLength: Int32) -> Bool {
if let mocks = mockThreads, let index = mocks.firstIndex(where: { $0.threadId == thread }) {
strcpy(buffer, mocks[index].name)
} else if threadName != nil {
return true
}

if getThreadNameSucceeds {
strcpy(buffer, threadName)
return true
} else {
_ = Array(repeating: 0, count: Int(bufLength)).withUnsafeBufferPointer { bufferPointer in
strcpy(buffer, bufferPointer.baseAddress)
}
return false
}
}

Expand Down
Loading