Skip to content

Commit

Permalink
fix: set the profile payload "timestamp" field to the transaction's s…
Browse files Browse the repository at this point in the history
…tart (#2771)
  • Loading branch information
armcknight committed Mar 9, 2023
1 parent 4d6f273 commit a9103fe
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
17 changes: 13 additions & 4 deletions Sources/Sentry/SentryProfiler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ + (SentryEnvelopeItem *)createProfilingEnvelopeItemForTransaction:(SentryTransac

// add the remaining basic metadata for the profile
const auto profileID = [[SentryId alloc] init];
[self serializeBasicProfileInfo:payload profileID:profileID platform:transaction.platform];
[self serializeBasicProfileInfo:payload profileID:profileID transaction:transaction];

return [self envelopeItemForProfileData:payload profileID:profileID];
}
Expand Down Expand Up @@ -736,7 +736,7 @@ - (void)stop

+ (void)serializeBasicProfileInfo:(NSMutableDictionary<NSString *, id> *)profile
profileID:(SentryId *const &)profileID
platform:(NSString *)platform
transaction:(SentryTransaction *)transaction
{
profile[@"version"] = @"1";
const auto debugImages = [NSMutableArray<NSDictionary<NSString *, id> *> new];
Expand Down Expand Up @@ -766,10 +766,19 @@ + (void)serializeBasicProfileInfo:(NSMutableDictionary<NSString *, id> *)profile
profile[@"profile_id"] = profileID.sentryIdString;
profile[@"truncation_reason"]
= profilerTruncationReasonName(_gCurrentProfiler->_truncationReason);
profile[@"platform"] = platform;
profile[@"platform"] = transaction.platform;
profile[@"environment"] = _gCurrentProfiler->_hub.scope.environmentString
?: _gCurrentProfiler->_hub.getClient.options.environment;
profile[@"timestamp"] = [[SentryCurrentDate date] sentry_toIso8601String];

const auto timestamp = transaction.startTimestamp;
if (UNLIKELY(timestamp == nil)) {
SENTRY_LOG_WARN(@"There was no start timestamp on the provided transaction. Falling back "
@"to old behavior of using the current time.");
profile[@"timestamp"] = [[SentryCurrentDate date] sentry_toIso8601String];
} else {
profile[@"timestamp"] = [timestamp sentry_toIso8601String];
}

profile[@"release"] = _gCurrentProfiler->_hub.getClient.options.releaseName;
}

Expand Down
9 changes: 4 additions & 5 deletions Tests/SentryProfilerTests/SentryProfilerSwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -469,11 +469,6 @@ private extension SentryProfilerSwiftTests {
let profile = try XCTUnwrap(try JSONSerialization.jsonObject(with: data) as? [String: Any])

XCTAssertNotNil(profile["version"])
if let timestampString = profile["timestamp"] as? String {
XCTAssertNotNil(NSDate.sentry_from(iso8601String: timestampString))
} else {
XCTFail("Expected a top-level timestamp")
}

let device = try XCTUnwrap(profile["device"] as? [String: Any?])
XCTAssertNotNil(device)
Expand Down Expand Up @@ -547,6 +542,10 @@ private extension SentryProfilerSwiftTests {
let latestTransaction = try getLatestTransaction()
let linkedTransactionInfo = try XCTUnwrap(profile["transaction"] as? [String: Any])

let linkedTransactionTimestampString = try XCTUnwrap(profile["timestamp"] as? String)
let latestTransactionTimestampString = (latestTransaction.startTimestamp as NSDate?)?.sentry_toIso8601String()
XCTAssertEqual(linkedTransactionTimestampString, latestTransactionTimestampString)

XCTAssertEqual(fixture.transactionName, latestTransaction.transaction)
XCTAssertEqual(fixture.transactionName, try XCTUnwrap(linkedTransactionInfo["name"] as? String))

Expand Down

0 comments on commit a9103fe

Please sign in to comment.