Skip to content
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### Fixes

- Fix issue with invalid profiles uploading (#2358)
- Fix issue with invalid profiles uploading (#2358 and #2359)

## 7.30.0

Expand Down
38 changes: 32 additions & 6 deletions Sources/Sentry/SentryProfiler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -574,19 +574,38 @@ - (void)captureEnvelope

// populate info from all transactions that occurred while profiler was running
auto transactionsInfo = [NSMutableArray array];
SENTRY_LOG_DEBUG(@"Profile start timestamp: %@ absolute time: %llu", _startDate,
(unsigned long long)_startTimestamp);
SENTRY_LOG_DEBUG(@"Profile end timestamp: %@ absolute time: %llu", _endDate,
(unsigned long long)_endTimestamp);
for (SentryTransaction *transaction in _transactions) {
SENTRY_LOG_DEBUG(@"Transaction %@ start timestamp: %@", transaction.trace.context.traceId,
transaction.startTimestamp);
SENTRY_LOG_DEBUG(@"Transaction %@ end timestamp: %@", transaction.trace.context.traceId,
transaction.timestamp);
const auto relativeStart =
[NSString stringWithFormat:@"%llu",
[transaction.startTimestamp compare:_startDate] == NSOrderedAscending
? 0
: (unsigned long long)(
[transaction.startTimestamp timeIntervalSinceDate:_startDate] * 1e9)];
const auto relativeEnd =
[NSString stringWithFormat:@"%llu",
[transaction.timestamp compare:_endDate] == NSOrderedDescending
? profileDuration
: (unsigned long long)(
[transaction.timestamp timeIntervalSinceDate:_startDate] * 1e9)];

NSString *relativeEnd;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: Please add some tests for this new functionality.

Copy link
Member Author

@armcknight armcknight Nov 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improved the test here to ensure it doesn't get the same value that will cause it to fail in backend validation: 11bc396

There was already a test that ensures a profile payload will have at least one linked transaction.

if ([transaction.timestamp compare:_endDate] == NSOrderedDescending) {
relativeEnd = [NSString stringWithFormat:@"%llu", profileDuration];
} else {
const auto profileStartToTransactionEnd_ns =
[transaction.timestamp timeIntervalSinceDate:_startDate] * 1e9;
if (profileStartToTransactionEnd_ns < 0) {
SENTRY_LOG_DEBUG(@"Transaction %@ ended before the profiler started, won't "
@"associate it with this profile.",
transaction.trace.context.traceId.sentryIdString);
continue;
} else {
relativeEnd = [NSString
stringWithFormat:@"%llu", (unsigned long long)profileStartToTransactionEnd_ns];
}
}
[transactionsInfo addObject:@{
@"id" : transaction.eventId.sentryIdString,
@"trace_id" : transaction.trace.context.traceId.sentryIdString,
Expand All @@ -596,6 +615,11 @@ - (void)captureEnvelope
@"active_thread_id" : [transaction.trace.transactionContext sentry_threadInfo].threadId
}];
}

if (transactionsInfo.count == 0) {
SENTRY_LOG_DEBUG(@"No transactions to associate with this profile, will not upload.");
return;
}
profile[@"transactions"] = transactionsInfo;

NSError *error = nil;
Expand All @@ -610,6 +634,8 @@ - (void)captureEnvelope
const auto item = [[SentryEnvelopeItem alloc] initWithHeader:header data:JSONData];
const auto envelopeHeader = [[SentryEnvelopeHeader alloc] initWithId:profileID];
const auto envelope = [[SentryEnvelope alloc] initWithHeader:envelopeHeader singleItem:item];

SENTRY_LOG_DEBUG(@"Capturing profile envelope.");
[_hub captureEnvelope:envelope];
}

Expand Down
6 changes: 4 additions & 2 deletions Sources/Sentry/SentrySpan.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ @implementation SentrySpan {
- (instancetype)initWithTracer:(SentryTracer *)tracer context:(SentrySpanContext *)context
{
if (self = [super init]) {
SENTRY_LOG_DEBUG(
@"Starting span %@ with tracer %@", context.spanId.sentrySpanIdString, tracer);
SENTRY_LOG_DEBUG(@"Created span %@ for trace ID %@", context.spanId.sentrySpanIdString,
tracer.context.traceId);
_tracer = tracer;
_context = context;
self.startTimestamp = [SentryCurrentDate date];
Expand Down Expand Up @@ -128,6 +128,8 @@ - (void)finishWithStatus:(SentrySpanStatus)status
_isFinished = YES;
if (self.timestamp == nil) {
self.timestamp = [SentryCurrentDate date];
SENTRY_LOG_DEBUG(@"Setting span timestamp: %@ at system time %llu", self.timestamp,
(unsigned long long)getAbsoluteTime());
}
if (self.tracer != nil) {
[self.tracer spanFinished:self];
Expand Down
4 changes: 4 additions & 0 deletions Sources/Sentry/SentryTracer.m
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ - (instancetype)initWithTransactionContext:(SentryTransactionContext *)transacti
dispatchQueueWrapper:(nullable SentryDispatchQueueWrapper *)dispatchQueueWrapper
{
if (self = [super init]) {
SENTRY_LOG_DEBUG(
@"Starting transaction at system time %llu", (unsigned long long)getAbsoluteTime());
self.rootSpan = [[SentrySpan alloc] initWithTracer:self context:transactionContext];
self.transactionContext = transactionContext;
_children = [[NSMutableArray alloc] init];
Expand Down Expand Up @@ -399,6 +401,8 @@ - (SentryTraceHeader *)toTraceHeader

- (void)finish
{
SENTRY_LOG_DEBUG(
@"-[SentryTracer finish] for trace ID %@", _traceContext.traceId.sentryIdString);
[self finishWithStatus:kSentrySpanStatusOk];
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/SentryTests/Profiling/SentryProfilerSwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ private extension SentryProfilerSwiftTests {
}
XCTAssertNotNil(transaction["trace_id"])
XCTAssertNotNil(transaction["relative_start_ns"])
XCTAssertNotNil(transaction["relative_end_ns"])
XCTAssertFalse((transaction["relative_end_ns"] as! NSString).isEqual(to: "0"))
XCTAssertNotNil(transaction["active_thread_id"])
}

Expand Down