Skip to content

Commit

Permalink
Merge branch 'next' into integration/react-native-integration
Browse files Browse the repository at this point in the history
  • Loading branch information
yousif-bugsnag committed Dec 12, 2024
2 parents c8406ac + 3b097a7 commit 99665ae
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 34 deletions.
14 changes: 7 additions & 7 deletions Sources/BugsnagPerformance/Private/Batch.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,39 +60,39 @@ class Batch: public PhasedStartup {
}
}

void removeSpan(TraceId traceId, SpanId spanId) noexcept {
BSGLogDebug(@"Batch:removeSpan(%llx%llx, %llx)", traceId.hi, traceId.lo, spanId);
void removeSpan(uint64_t traceIdHi, uint64_t traceIdLo, SpanId spanId) noexcept {
BSGLogDebug(@"Batch:removeSpan(%llx%llx, %llx)", traceIdHi, traceIdLo, spanId);
std::lock_guard<std::mutex> guard(mutex_);

if (spans_.count == 0) {
BSGLogDebug(@"Batch:removeSpan(%llx%llx, %llx): Batch is empty", traceId.hi, traceId.lo, spanId);
BSGLogDebug(@"Batch:removeSpan(%llx%llx, %llx): Batch is empty", traceIdHi, traceIdLo, spanId);
return;
}

BugsnagPerformanceSpan *found = nil;
size_t index = 0;
for(; index < spans_.count; index++) {
BugsnagPerformanceSpan *potential = spans_[index];
if (potential.spanId == spanId && potential.traceId.value == traceId.value) {
if (potential.spanId == spanId && potential.traceIdHi == traceIdHi && potential.traceIdLo == traceIdLo) {
found = potential;
}
}

if (found == nil) {
BSGLogDebug(@"Batch:removeSpan(%llx%llx, %llx): Span not found", traceId.hi, traceId.lo, spanId);
BSGLogDebug(@"Batch:removeSpan(%llx%llx, %llx): Span not found", traceIdHi, traceIdLo, spanId);
return;
}

[spans_ removeObject:found];
// [spans_ removeObjectAtIndex:index];

for (BugsnagPerformanceSpan *span in spans_) {
if (span.parentId == spanId && span.traceId.value == traceId.value) {
if (span.parentId == spanId && span.traceIdHi == traceIdHi && span.traceIdLo == traceIdLo) {
span.parentId = 0;
}
}
BSGLogDebug(@"Batch:removeSpan(%llx%llx, %llx): Span %@ removed. Batch size is now %zu",
traceId.hi, traceId.lo, spanId, found.name, spans_.count);
traceIdHi, traceIdLo, spanId, found.name, spans_.count);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ - (NSArray * _Nullable)getCurrentTraceAndSpanIdV1 {
return nil;
}
return @[
[NSString stringWithFormat:@"%llx%llx", span.traceId.hi, span.traceId.lo],
[NSString stringWithFormat:@"%llx%llx", span.traceIdHi, span.traceIdLo],
[NSString stringWithFormat:@"%llx", span.spanId]
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic) BSGFirstClass firstClass;
@property (nonatomic) SpanKind kind;
@property (nonatomic,readwrite) BOOL isMutable;
@property (nonatomic,readwrite) BOOL hasBeenProcessed;
@property (nonatomic,readonly) NSUInteger attributeCountLimit;
@property (nonatomic,readwrite) BOOL wasStartOrEndTimeProvided;
@property (nonatomic) BSGInstrumentRendering instrumentRendering;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
}
// Sampled status assumes that the current P value won't change soon.
return [NSString stringWithFormat:@"00-%016llx%016llx-%016llx-0%d",
span.traceId.hi, span.traceId.lo,
span.traceIdHi, span.traceIdLo,
span.spanId, sampler_->sampled(span)];
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/BugsnagPerformance/Private/OtlpTraceEncoding.mm
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
return [NSString stringWithFormat:@"%016llx", spanId];
}

static NSString * EncodeTraceId(TraceId const &traceId) {
return [NSString stringWithFormat:@"%016llx%016llx", traceId.hi, traceId.lo];
static NSString * EncodeTraceId(uint64_t traceIdHi, uint64_t traceIdLo) {
return [NSString stringWithFormat:@"%016llx%016llx", traceIdHi, traceIdLo];
}

static NSString * EncodeCFAbsoluteTime(CFAbsoluteTime time) {
Expand All @@ -37,7 +37,7 @@
// random trace_id if empty or invalid trace_id was received.
//
// This field is required.
result[@"traceId"] = EncodeTraceId(span.traceId);
result[@"traceId"] = EncodeTraceId(span.traceIdHi, span.traceIdLo);

// A unique identifier for a span within a trace, assigned when the span
// is created. The ID is an 8-byte array. An ID with all zeroes is considered
Expand Down
2 changes: 1 addition & 1 deletion Sources/BugsnagPerformance/Private/Sampler.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Sampler {
} else {
idUpperBound = uint64_t(p * double(UINT64_MAX));
}
return span.traceId.hi <= idUpperBound;
return span.traceIdHi <= idUpperBound;
}

private:
Expand Down
5 changes: 3 additions & 2 deletions Sources/BugsnagPerformance/Private/Tracer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@
BSGLogTrace(@"Tracer::startSpan: No parent specified; using current span");
parentSpan = spanStackingHandler_->currentSpan();
}
auto traceId = parentSpan.traceId;

TraceId traceId = { .hi = parentSpan.traceIdHi, .lo = parentSpan.traceIdLo };
if (traceId.value == 0) {
BSGLogTrace(@"Tracer::startSpan: No parent traceId; generating one");
traceId = IdGenerator::generateTraceId();
Expand Down Expand Up @@ -280,7 +281,7 @@
BSGLogTrace(@"Tracer::cancelQueuedSpan(%@)", span.name);
if (span) {
[span abortIfOpen];
batch_->removeSpan(span.traceId, span.spanId);
batch_->removeSpan(span.traceIdHi, span.traceIdLo, span.spanId);
}
}

Expand Down
48 changes: 29 additions & 19 deletions Sources/BugsnagPerformance/Public/BugsnagPerformanceSpan.mm
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,13 @@ - (void)abortIfOpen {
}
self.state = SpanStateAborted;
}
[self callOnSpanClosed];
self.isMutable = false;
[self sendForProcessing];
}

- (void)abortUnconditionally {
bool wasOpen = false;
@synchronized (self) {
BSGLogDebug(@"Span.abortUnconditionally: %@: Was open: %d", self.name, self.state == SpanStateOpen);
wasOpen = self.state == SpanStateOpen;
self.state = SpanStateAborted;
}
if (wasOpen) {
[self callOnSpanClosed];
}
self.isMutable = false;
BSGLogDebug(@"Span.abortUnconditionally: %@: Was open: %d", self.name, self.state == SpanStateOpen);
self.state = SpanStateAborted;
[self sendForProcessing];
}

- (void)end {
Expand Down Expand Up @@ -134,18 +126,36 @@ - (void)endWithAbsoluteTime:(CFAbsoluteTime)endTime {
}
}

self.endAbsTime = currentTimeIfUnset(endTime);
[self markEndAbsoluteTime:endTime];
self.state = SpanStateEnded;
}
[self callOnSpanClosed];
self.isMutable = false;
[self sendForProcessing];
}

- (void)markEndTime:(NSDate *)endTime {
[self markEndAbsoluteTime:dateToAbsoluteTime(endTime)];
}

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

- (void)sendForProcessing {
BOOL hasBeenProcessed = false;
@synchronized (self) {
hasBeenProcessed = self.hasBeenProcessed;
self.hasBeenProcessed = true;
}
if (!hasBeenProcessed) {
auto onSpanClosed = self.onSpanClosed;
if(onSpanClosed != nil) {
onSpanClosed(self);
}
}
if (self.state == SpanStateOpen) {
self.state = SpanStateEnded;
}
self.isMutable = false;
}

- (void)endOnDestroy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@ - (instancetype) initWithTraceIdHi:(uint64_t)traceIdHi traceIdLo:(uint64_t)trace
return [self initWithTraceId:TraceId{.hi=traceIdHi, .lo=traceIdLo} spanId:spanId];
}

- (uint64_t) traceIdHi {
return self.traceId.hi;
}

- (uint64_t) traceIdLo {
return self.traceId.lo;
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ OBJC_EXPORT

@property(nonatomic,readonly) TraceId traceId;
@property(nonatomic,readonly) SpanId spanId;
@property(nonatomic,readonly) uint64_t traceIdHi;
@property(nonatomic,readonly) uint64_t traceIdLo;

- (instancetype) initWithTraceId:(TraceId)traceId spanId:(SpanId)spanId;

Expand Down

0 comments on commit 99665ae

Please sign in to comment.