Skip to content

Commit

Permalink
Merge pull request #362 from bugsnag/PLAT-13306/span-context-traceid
Browse files Browse the repository at this point in the history
Replace internal access to `traceId` property with `traceIdHi` and `traceIdLo`
  • Loading branch information
yousif-bugsnag authored Dec 12, 2024
2 parents 990805f + 0191e72 commit 9bec60b
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 15 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 @@ -60,7 +60,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 @@ -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
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 9bec60b

Please sign in to comment.