Skip to content

Commit

Permalink
Merge 63d5f90 into a7b49bd
Browse files Browse the repository at this point in the history
  • Loading branch information
armcknight authored Nov 2, 2022
2 parents a7b49bd + 63d5f90 commit e8dd8d9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
15 changes: 9 additions & 6 deletions Sources/Sentry/SentryProfiler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,6 @@ - (void)start
NSMutableDictionary<NSString *, id> *metadata = threadMetadata[threadID];
if (metadata == nil) {
metadata = [NSMutableDictionary<NSString *, id> dictionary];
if (backtrace.threadMetadata.threadID == mainThreadID) {
metadata[@"is_main_thread"] = @YES;
}
threadMetadata[threadID] = metadata;
}
if (!backtrace.threadMetadata.name.empty() && metadata[@"name"] == nil) {
Expand Down Expand Up @@ -427,7 +424,7 @@ - (void)start
}

const auto sample = [NSMutableDictionary<NSString *, id> dictionary];
sample[@"relative_timestamp_ns"] =
sample[@"elapsed_since_start_ns"] =
[@(getDurationNs(strongSelf->_startTimestamp, backtrace.absoluteTimestamp))
stringValue];
sample[@"thread_id"] = threadID;
Expand Down Expand Up @@ -486,6 +483,7 @@ - (void)captureEnvelope
@synchronized(self) {
profile = [_profile mutableCopy];
}
profile[@"version"] = @"1";
const auto debugImages = [NSMutableArray<NSDictionary<NSString *, id> *> new];
const auto debugMeta = [_debugImageProvider getDebugImages];
for (SentryDebugMeta *debugImage in debugMeta) {
Expand Down Expand Up @@ -570,7 +568,9 @@ - (void)captureEnvelope

// populate info from all transactions that occurred while profiler was running
profile[@"platform"] = _transactions.firstObject.platform;
profile[@"environment"] = _hub.scope.environmentString ?: _hub.getClient.options.environment ?: kSentryDefaultEnvironment;
auto transactionsInfo = [NSMutableArray array];
NSString *mainThreadID = [profile[@"sampled_profile"][@"samples"] firstObject][@"thread_id"];
for (SentryTransaction *transaction in _transactions) {
const auto relativeStart =
[NSString stringWithFormat:@"%llu",
Expand All @@ -585,12 +585,15 @@ - (void)captureEnvelope
: (unsigned long long)(
[transaction.timestamp timeIntervalSinceDate:_startDate] * 1e9)];
[transactionsInfo addObject:@{
@"environment" : _hub.scope.environmentString ?: _hub.getClient.options.environment ?: kSentryDefaultEnvironment,
@"id" : transaction.eventId.sentryIdString,
@"trace_id" : transaction.trace.context.traceId.sentryIdString,
@"name" : transaction.transaction,
@"relative_start_ns" : relativeStart,
@"relative_end_ns" : relativeEnd
@"relative_end_ns" : relativeEnd,
@"active_thread_id" :
mainThreadID // TODO: we are just using the main thread ID for all transactions to
// fix a backend validation error, but this needs to be gathered from
// transaction starts in their contexts and carried forward to here
}];
}
profile[@"transactions"] = transactionsInfo;
Expand Down
9 changes: 7 additions & 2 deletions Tests/SentryTests/Profiling/SentryProfilerSwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ private extension SentryProfilerSwiftTests {

func assertValidProfileData(data: Data, transactionEnvironment: String = kSentryDefaultEnvironment, numberOfTransactions: Int = 1, shouldTimeout: Bool = false) {
let profile = try! JSONSerialization.jsonObject(with: data) as! [String: Any]

XCTAssertNotNil(profile["version"])

let device = profile["device"] as? [String: Any?]
XCTAssertNotNil(device)
XCTAssertEqual("Apple", device!["manufacturer"] as! String)
Expand All @@ -258,6 +261,7 @@ private extension SentryProfilerSwiftTests {
XCTAssertFalse((os!["build_number"] as! String).isEmpty)

XCTAssertEqual("cocoa", profile["platform"] as! String)
XCTAssertEqual(transactionEnvironment, profile["environment"] as! String)

let version = Bundle.main.object(forInfoDictionaryKey: kCFBundleVersionKey as String) ?? "(null)"
let build = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") ?? "(null)"
Expand All @@ -280,7 +284,6 @@ private extension SentryProfilerSwiftTests {
let queueMetadata = sampledProfile["queue_metadata"] as! [String: Any]
XCTAssertFalse(threadMetadata.isEmpty)
XCTAssertFalse(threadMetadata.values.compactMap { $0["priority"] }.filter { ($0 as! Int) > 0 }.isEmpty)
XCTAssertFalse(threadMetadata.values.filter { $0["is_main_thread"] as? Bool == true }.isEmpty)
XCTAssertFalse(queueMetadata.isEmpty)
XCTAssertFalse(((queueMetadata.first?.value as! [String: Any])["label"] as! String).isEmpty)

Expand Down Expand Up @@ -316,13 +319,15 @@ private extension SentryProfilerSwiftTests {
if let traceIDString = transaction["trace_id"] {
XCTAssertNotEqual(SentryId.empty, SentryId(uuidString: traceIDString))
}
XCTAssertEqual(transactionEnvironment, transaction["environment"])
XCTAssertNotNil(transaction["trace_id"])
XCTAssertNotNil(transaction["relative_start_ns"])
XCTAssertNotNil(transaction["relative_end_ns"])
XCTAssertNotNil(transaction["active_thread_id"])
}

for sample in samples {
XCTAssertNotNil(sample["elapsed_since_start_ns"] as! String)
XCTAssertNotNil(sample["thread_id"])
XCTAssertNotNil(stacks[sample["stack_id"] as! Int])
}

Expand Down

0 comments on commit e8dd8d9

Please sign in to comment.