Skip to content

Commit

Permalink
Merge pull request #363 from bugsnag/PLAT-13288-update-snapshot-on-sp…
Browse files Browse the repository at this point in the history
…an-end-time-set

[PLAT-13288] Update snapshots whenever a span's end time is set (not just when the span is closed)
  • Loading branch information
kstenerud authored Dec 13, 2024
2 parents 3b097a7 + d708dfd commit 88fd510
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ typedef enum {
SpanStateAborted = 3,
} SpanState;

typedef void (^OnSpanClosed)(BugsnagPerformanceSpan * _Nonnull);
typedef void (^SpanLifecycleCallback)(BugsnagPerformanceSpan * _Nonnull);

NS_ASSUME_NONNULL_BEGIN

Expand All @@ -40,7 +40,8 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic) OnSpanDestroyAction onSpanDestroyAction;
@property (nonatomic,readwrite) NSString *name;
@property (nonatomic,readonly) NSMutableDictionary *attributes;
@property (nonatomic) OnSpanClosed onSpanClosed;
@property (nonatomic) SpanLifecycleCallback onSpanEndSet;
@property (nonatomic) SpanLifecycleCallback onSpanClosed;
@property (nonatomic,readwrite) SpanId parentId;
@property (nonatomic) double samplingProbability;
@property (nonatomic) BSGFirstClass firstClass;
Expand All @@ -67,7 +68,8 @@ NS_ASSUME_NONNULL_BEGIN
firstClass:(BSGFirstClass) firstClass
attributeCountLimit:(NSUInteger)attributeCountLimit
instrumentRendering:(BSGInstrumentRendering)instrumentRendering
onSpanClosed:(OnSpanClosed) onSpanEnded NS_DESIGNATED_INITIALIZER;
onSpanEndSet:(SpanLifecycleCallback) onSpanEndSet
onSpanClosed:(SpanLifecycleCallback) onSpanEnded NS_DESIGNATED_INITIALIZER;

- (void)internalSetAttribute:(NSString *)attributeName withValue:(_Nullable id)value;
- (void)internalSetMultipleAttributes:(NSDictionary *)attributes;
Expand Down
1 change: 1 addition & 0 deletions Sources/BugsnagPerformance/Private/Tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class Tracer: public PhasedStartup {
BugsnagPerformanceSpan *startSpan(NSString *name, SpanOptions options, BSGFirstClass defaultFirstClass) noexcept;
void createFrozenFrameSpan(NSTimeInterval startTime, NSTimeInterval endTime, BugsnagPerformanceSpanContext *parentContext) noexcept;
void markPrewarmSpan(BugsnagPerformanceSpan *span) noexcept;
void onSpanEndSet(BugsnagPerformanceSpan *span);
void onSpanClosed(BugsnagPerformanceSpan *span);
void reprocessEarlySpans(void);
void processFrameMetrics(BugsnagPerformanceSpan *span) noexcept;
Expand Down
21 changes: 15 additions & 6 deletions Sources/BugsnagPerformance/Private/Tracer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@
firstClass = defaultFirstClass;
}
auto spanId = IdGenerator::generateSpanId();
auto onSpanEndSet = ^(BugsnagPerformanceSpan * _Nonnull endedSpan) {
blockThis->onSpanEndSet(endedSpan);
};
auto onSpanClosed = ^(BugsnagPerformanceSpan * _Nonnull endedSpan) {
blockThis->onSpanClosed(endedSpan);
};
BugsnagPerformanceSpan *span = [[BugsnagPerformanceSpan alloc] initWithName:name
traceId:traceId
spanId:spanId
Expand All @@ -117,9 +123,8 @@
firstClass:firstClass
attributeCountLimit:attributeCountLimit_
instrumentRendering: options.instrumentRendering
onSpanClosed:^(BugsnagPerformanceSpan * _Nonnull endedSpan) {
blockThis->onSpanClosed(endedSpan);
}];
onSpanEndSet:onSpanEndSet
onSpanClosed:onSpanClosed];
if (shouldInstrumentRendering(span)) {
span.startFramerateSnapshot = [frameMetricsCollector_ currentSnapshot];
}
Expand All @@ -133,12 +138,16 @@
return span;
}

void Tracer::onSpanClosed(BugsnagPerformanceSpan *span) {
BSGLogTrace(@"Tracer::onSpanClosed: for span %@", span.name);
void Tracer::onSpanEndSet(BugsnagPerformanceSpan *span) {
BSGLogTrace(@"Tracer::onSpanEndSet: for span %@", span.name);

if (shouldInstrumentRendering(span)) {
span.endFramerateSnapshot = [frameMetricsCollector_ currentSnapshot];
}
}

void Tracer::onSpanClosed(BugsnagPerformanceSpan *span) {
BSGLogTrace(@"Tracer::onSpanClosed: for span %@", span.name);

spanStackingHandler_->onSpanClosed(span.spanId);

Expand Down
8 changes: 7 additions & 1 deletion Sources/BugsnagPerformance/Public/BugsnagPerformanceSpan.mm
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ - (instancetype)initWithName:(NSString *)name
firstClass:(BSGFirstClass) firstClass
attributeCountLimit:(NSUInteger)attributeCountLimit
instrumentRendering:(BSGInstrumentRendering)instrumentRendering
onSpanClosed:(OnSpanClosed) onSpanClosed {
onSpanEndSet:(SpanLifecycleCallback) onSpanEndSet
onSpanClosed:(SpanLifecycleCallback) onSpanClosed {
if ((self = [super initWithTraceId:traceId spanId:spanId])) {
_startClock = currentMonotonicClockNsecIfUnset(startAbsTime);
_name = name;
Expand All @@ -44,6 +45,7 @@ - (instancetype)initWithName:(NSString *)name
_startClock = currentMonotonicClockNsecIfUnset(startAbsTime);
_firstClass = firstClass;
_onSpanDestroyAction = OnSpanDestroyAbort;
_onSpanEndSet = onSpanEndSet;
_onSpanClosed = onSpanClosed;
_kind = SPAN_KIND_INTERNAL;
_samplingProbability = 1;
Expand Down Expand Up @@ -138,6 +140,10 @@ - (void)markEndTime:(NSDate *)endTime {

- (void)markEndAbsoluteTime:(CFAbsoluteTime)endTime {
self.endAbsTime = currentTimeIfUnset(endTime);
auto onSpanEndSet = self.onSpanEndSet;
if(onSpanEndSet != nil) {
onSpanEndSet(self);
}
}

- (void)sendForProcessing {
Expand Down
1 change: 1 addition & 0 deletions Tests/BugsnagPerformanceTests/BatchTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ @implementation BatchTests
firstClass:BSGFirstClassUnset
attributeCountLimit:128
instrumentRendering:BSGInstrumentRenderingNo
onSpanEndSet:^(BugsnagPerformanceSpan * _Nonnull) {}
onSpanClosed:^(BugsnagPerformanceSpan * _Nonnull) {
}];
}
Expand Down
1 change: 1 addition & 0 deletions Tests/BugsnagPerformanceTests/OtlpTraceEncodingTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ - (BugsnagPerformanceSpan *)spanWithName:(NSString *)name
firstClass:firstClass
attributeCountLimit:128
instrumentRendering:BSGInstrumentRenderingNo
onSpanEndSet:^(BugsnagPerformanceSpan * _Nonnull) {}
onSpanClosed:^(BugsnagPerformanceSpan * _Nonnull) {}];
}

Expand Down
1 change: 1 addition & 0 deletions Tests/BugsnagPerformanceTests/SamplerTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ - (void)assertSampler:(Sampler &)sampler samplesWithProbability:(double)probabil
firstClass:BSGFirstClassUnset
attributeCountLimit:128
instrumentRendering:BSGInstrumentRenderingNo
onSpanEndSet:^(BugsnagPerformanceSpan * _Nonnull) {}
onSpanClosed:^(BugsnagPerformanceSpan * _Nonnull) {
}];
if (sampler.sampled(span)) {
Expand Down
1 change: 1 addition & 0 deletions Tests/BugsnagPerformanceTests/SpanOptionsTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ - (void)testConversion {
firstClass:BSGFirstClassNo
attributeCountLimit:128
instrumentRendering:BSGInstrumentRenderingNo
onSpanEndSet:^(BugsnagPerformanceSpan * _Nonnull) {}
onSpanClosed:^(BugsnagPerformanceSpan * _Nonnull) {
}];
BugsnagPerformanceSpanOptions *objcOptions = [BugsnagPerformanceSpanOptions new];
Expand Down
1 change: 1 addition & 0 deletions Tests/BugsnagPerformanceTests/SpanStackingHandlerTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
firstClass:BSGFirstClassNo
attributeCountLimit:128
instrumentRendering:BSGInstrumentRenderingNo
onSpanEndSet:^(BugsnagPerformanceSpan * _Nonnull) {}
onSpanClosed:^(BugsnagPerformanceSpan * _Nonnull span) {
handler->onSpanClosed(span.spanId);
}];
Expand Down
4 changes: 3 additions & 1 deletion Tests/BugsnagPerformanceTests/SpanTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ @interface SpanTests : XCTestCase

@implementation SpanTests

static BugsnagPerformanceSpan *spanWithStartTime(CFAbsoluteTime startTime, OnSpanClosed onEnded) {
static BugsnagPerformanceSpan *spanWithStartTime(CFAbsoluteTime startTime, SpanLifecycleCallback onEnded) {
TraceId tid = {.value = 1};
return [[BugsnagPerformanceSpan alloc] initWithName:@"test"
traceId:tid
Expand All @@ -28,6 +28,7 @@ @implementation SpanTests
firstClass:BSGFirstClassUnset
attributeCountLimit:128
instrumentRendering:BSGInstrumentRenderingNo
onSpanEndSet:^(BugsnagPerformanceSpan *) {}
onSpanClosed:onEnded];
}

Expand Down Expand Up @@ -278,6 +279,7 @@ - (void)testTooManyAttributes {
firstClass:BSGFirstClassUnset
attributeCountLimit:5
instrumentRendering:BSGInstrumentRenderingNo
onSpanEndSet:^(BugsnagPerformanceSpan *) {}
onSpanClosed:^(BugsnagPerformanceSpan *) {}];

// Note: "bugsnag.sampling.p" is automatically added.
Expand Down
1 change: 1 addition & 0 deletions Tests/BugsnagPerformanceTests/WeakSpansListTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ @interface WeakSpansListTests : XCTestCase
firstClass:BSGFirstClassNo
attributeCountLimit:128
instrumentRendering:BSGInstrumentRenderingNo
onSpanEndSet:^(BugsnagPerformanceSpan * _Nonnull) {}
onSpanClosed:^(BugsnagPerformanceSpan * _Nonnull) {
}];
}
Expand Down

0 comments on commit 88fd510

Please sign in to comment.