Skip to content

Commit

Permalink
track trace ids instead of span ids; allow transaction to start befor…
Browse files Browse the repository at this point in the history
…e profile again, doesnt matter since we always slice a profile out anyways
  • Loading branch information
armcknight committed Feb 18, 2023
1 parent 96c76eb commit 643214c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 58 deletions.
6 changes: 2 additions & 4 deletions Sources/Sentry/SentrySpan.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#import "SentryLog.h"
#import "SentryMeasurementValue.h"
#import "SentryNoOpSpan.h"
#import "SentryProfilingConditionals.h"
#import "SentrySerializable.h"
#import "SentrySpan+Private.h"
#import "SentrySpanContext.h"
Expand All @@ -30,6 +29,8 @@ @implementation SentrySpan {
- (instancetype)initWithContext:(SentrySpanContext *)context
{
if (self = [super init]) {
self.startTimestamp = [SentryCurrentDate date];
self.startSystemTime = getAbsoluteTime();
SENTRY_LOG_DEBUG(@"Created span %@ for trace ID %@ at system time %llu",
context.spanId.sentrySpanIdString, context.traceId, self.startSystemTime);
_data = [[NSMutableDictionary alloc] init];
Expand Down Expand Up @@ -154,9 +155,6 @@ - (void)finishWithStatus:(SentrySpanStatus)status
if (self.tracer == nil) {
SENTRY_LOG_DEBUG(
@"No tracer associated with span with id %@", self.spanId.sentrySpanIdString);
#if SENTRY_TARGET_PROFILING_SUPPORTED
[SentryTracer removeSpanIDFromGlobalBookkeeping:self.spanId];
#endif // SENTRY_TARGET_PROFILING_SUPPORTED
return;
}
[self.tracer spanFinished:self];
Expand Down
75 changes: 27 additions & 48 deletions Sources/Sentry/SentryTracer.m
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ @implementation SentryTracer {

static NSObject *_gGlobalStateLock;
static BOOL appStartMeasurementRead;
static NSMutableArray<SentrySpanId *> *_gInFlightSpanIDs;
static NSMutableArray<SentryId *> *_gInFlightTraceIDs;

+ (void)initialize
{
Expand Down Expand Up @@ -156,17 +156,6 @@ - (instancetype)initWithTransactionContext:(SentryTransactionContext *)transacti
timerWrapper:(nullable SentryNSTimerWrapper *)timerWrapper
{
if (self = [super initWithContext:transactionContext]) {
#if SENTRY_TARGET_PROFILING_SUPPORTED
if (profilesSamplerDecision.decision == kSentrySampleDecisionYes) {
_isProfiling = YES;
[SentryProfiler startWithHub:hub];
SENTRY_LOG_DEBUG(
@"[span tracking] Adding root span id %@", self.spanId.sentrySpanIdString);
[self trackSpanForConcurrentProfiling:self.spanId];
}
#endif // SENTRY_TARGET_PROFILING_SUPPORTED
self.startTimestamp = [SentryCurrentDate date];
self.startSystemTime = getAbsoluteTime();
self.transactionContext = transactionContext;
_children = [[NSMutableArray alloc] init];
self.hub = hub;
Expand Down Expand Up @@ -206,6 +195,12 @@ - (instancetype)initWithTransactionContext:(SentryTransactionContext *)transacti
initFrozenFrames = currentFrames.frozen;
}
#endif // SENTRY_HAS_UIKIT

#if SENTRY_TARGET_PROFILING_SUPPORTED
[self trackTracerForConcurrentProfiling:self.traceId
hub:hub
sampled:profilesSamplerDecision.decision];
#endif // SENTRY_TARGET_PROFILING_SUPPORTED
}

return self;
Expand Down Expand Up @@ -343,15 +338,6 @@ - (void)cancelDeadlineTimer
@synchronized(_children) {
[_children addObject:child];
}
#if SENTRY_TARGET_PROFILING_SUPPORTED
@synchronized(_gGlobalStateLock) {
if (self.isProfiling) {
SENTRY_LOG_DEBUG(
@"[span tracking] Adding child span id %@", child.spanId.sentrySpanIdString);
[_gInFlightSpanIDs addObject:child.spanId];
}
}
#endif // SENTRY_TARGET_PROFILING_SUPPORTED

return child;
}
Expand Down Expand Up @@ -520,7 +506,7 @@ - (void)finishInternal
}

#if SENTRY_TARGET_PROFILING_SUPPORTED
[self stopTrackingSpanForConcurrentProfiling];
[self stopTrackingTracerForConcurrentProfiling];
#endif // SENTRY_TARGET_PROFILING_SUPPORTED

[self captureTransactionInEnvelope];
Expand Down Expand Up @@ -561,13 +547,13 @@ - (void)captureTransactionInEnvelope
additionalEnvelopeItems:@[ profileEnvelopeItem ]];

@synchronized(_gGlobalStateLock) {
if (_gInFlightSpanIDs.count == 0) {
SENTRY_LOG_DEBUG(@"Last in flight span completed, stopping profiler.");
if (_gInFlightTraceIDs.count == 0) {
SENTRY_LOG_DEBUG(@"Last in flight tracer completed, stopping profiler.");
[SentryProfiler stop];
} else {
SENTRY_LOG_DEBUG(
@"Waiting on %lu other spans to complete before stopping profiler: %@.",
_gInFlightSpanIDs.count, _gInFlightSpanIDs);
@"Waiting on %lu other tracers to complete before stopping profiler: %@.",
_gInFlightTraceIDs.count, _gInFlightTraceIDs);
}
}
#else
Expand Down Expand Up @@ -837,40 +823,33 @@ + (nullable SentryTracer *)getTracer:(id<SentrySpan>)span
}

#if SENTRY_TARGET_PROFILING_SUPPORTED
- (void)trackSpanForConcurrentProfiling:(SentrySpanId *)spanID
- (void)trackTracerForConcurrentProfiling:(SentryId *)traceID
hub:(SentryHub *)hub
sampled:(SentrySampleDecision)sampleDecision
{
if (!self.isProfiling) {
if (sampleDecision != kSentrySampleDecisionYes) {
return;
}
_isProfiling = YES;
[SentryProfiler startWithHub:hub];
SENTRY_LOG_DEBUG(@"[span tracking] Adding root span id %@", self.spanId.sentrySpanIdString);

@synchronized(_gGlobalStateLock) {
if (_gInFlightSpanIDs == nil) {
_gInFlightSpanIDs = [NSMutableArray<SentrySpanId *> array];
if (_gInFlightTraceIDs == nil) {
_gInFlightTraceIDs = [NSMutableArray<SentryId *> array];
}
[_gInFlightSpanIDs addObject:spanID];
[_gInFlightTraceIDs addObject:traceID];
}
}

- (void)stopTrackingSpanForConcurrentProfiling
- (void)stopTrackingTracerForConcurrentProfiling
{
if (self.isProfiling) {
if (!self.isProfiling) {
return;
}
@synchronized(_gGlobalStateLock) {
SENTRY_LOG_DEBUG(
@"[span tracking] removing root span id %@", self.spanId.sentrySpanIdString);
[_gInFlightSpanIDs removeObject:self.spanId];
for (id<SentrySpan> span in _children) {
SENTRY_LOG_DEBUG(
@"[span tracking] removing child span id %@", span.spanId.sentrySpanIdString);
[_gInFlightSpanIDs removeObject:span.spanId];
}
}
}

+ (void)removeSpanIDFromGlobalBookkeeping:(SentrySpanId *)spanID
{
@synchronized(_gGlobalStateLock) {
[_gInFlightSpanIDs removeObject:spanID];
SENTRY_LOG_DEBUG(@"[tracer tracking] removing trace id %@", self.traceId);
[_gInFlightTraceIDs removeObject:self.traceId];
}
}
#endif // SENTRY_TARGET_PROFILING_SUPPORTED
Expand Down
4 changes: 0 additions & 4 deletions Sources/Sentry/include/SentryTracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,6 @@ static NSTimeInterval const SentryTracerDefaultTimeout = 3.0;

- (void)dispatchIdleTimeout;

#if SENTRY_TARGET_PROFILING_SUPPORTED
+ (void)removeSpanIDFromGlobalBookkeeping:(SentrySpanId *)spanID;
#endif // SENTRY_TARGET_PROFILING_SUPPORTED

@end

NS_ASSUME_NONNULL_END
4 changes: 2 additions & 2 deletions Tests/SentryTests/Transaction/SentrySpanTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class SentrySpanTests: XCTestCase {
}

func testFinishSpanWithDefaultTimestamp() {
let span = fixture.getSut()
let span = SentrySpan(tracer: fixture.tracer, context: SpanContext(operation: fixture.someOperation, sampled: .undecided))
span.finish()

XCTAssertEqual(span.startTimestamp, TestData.timestamp)
Expand All @@ -103,7 +103,7 @@ class SentrySpanTests: XCTestCase {
}

func testFinishSpanWithCustomTimestamp() {
let span = fixture.getSut()
let span = SentrySpan(tracer: fixture.tracer, context: SpanContext(operation: fixture.someOperation, sampled: .undecided))
span.timestamp = Date(timeIntervalSince1970: 123)
span.finish()

Expand Down

0 comments on commit 643214c

Please sign in to comment.