Skip to content

Commit

Permalink
feat(react-native): support discarding native spans in turbo module
Browse files Browse the repository at this point in the history
  • Loading branch information
yousif-bugsnag committed Dec 19, 2024
1 parent 1dccffb commit 18931c9
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/platforms/react-native/__mocks__/react-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,22 @@ const BugsnagReactNativePerformance = {
}),
getNativeConfiguration: jest.fn(() => {
return null
}),
startNativeSpan: jest.fn((name, options) => {
return {
name,
id: 'native-span-id',
traceId: 'native-trace-id',
startTime: options.startTime,
parentSpanId: options.parentContext?.id || undefined
}
}),
endNativeSpan: jest.fn(() => {
return Promise.resolve()
}),
markNativeSpanEndTime: jest.fn(),
discardNativeSpan: jest.fn(() => {
return Promise.resolve()
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,15 @@ public void endNativeSpan(String spanId, String traceId, double endTime, Readabl
promise.resolve(null);
}

public void discardNativeSpan(String spanId, String traceId, Promise promise) {
SpanImpl nativeSpan = openSpans.remove(spanId + traceId);
if (nativeSpan != null) {
nativeSpan.discard();
}

promise.resolve(null);
}

private WritableMap nativeSpanToJsSpan(SpanImpl nativeSpan) {
WritableMap span = Arguments.createMap();
span.putString("name", nativeSpan.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,10 @@ public void endNativeSpan(String spanId, String traceId, double endTime, Readabl
public void markNativeSpanEndTime(String spanId, String traceId, double endTime) {
impl.markNativeSpanEndTime(spanId, traceId, endTime);
}

@Override
public void discardNativeSpan(String spanId, String traceId, Promise promise) {
impl.discardNativeSpan(spanId, traceId, promise);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,9 @@ public void endNativeSpan(String spanId, String traceId, double endTime, Readabl
public void markNativeSpanEndTime(String spanId, String traceId, double endTime) {
impl.markNativeSpanEndTime(spanId, traceId, endTime);
}

@ReactMethod
public void discardNativeSpan(String spanId, String traceId, Promise promise) {
impl.discardNativeSpan(spanId, traceId, promise);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,20 @@ static uint64_t hexStringToUInt64(NSString *hexString) {
return nil;
}

RCT_EXPORT_METHOD(discardNativeSpan:(NSString *)spanId
traceId:(NSString *)traceId
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject) {
NSString *spanKey = [spanId stringByAppendingString:traceId];
BugsnagPerformanceSpan *nativeSpan = openSpans[spanKey];
if (nativeSpan != nil) {
[openSpans removeObjectForKey:spanKey];
[nativeSpan abortUnconditionally];
}

resolve(nil);
}

#ifdef RCT_NEW_ARCH_ENABLED
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
(const facebook::react::ObjCTurboModule::InitParams &)params
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export interface Spec extends TurboModule {
startNativeSpan: (name: string, options: UnsafeObject) => NativeSpan
endNativeSpan: (spanId: string, traceId: string, endTime: number, attributes: UnsafeObject) => Promise<void>
markNativeSpanEndTime: (spanId: string, traceId: string, endTime: number) => void
discardNativeSpan: (spanId: string, traceId: string) => Promise<void>
}

export default TurboModuleRegistry.get<Spec>(
Expand Down

0 comments on commit 18931c9

Please sign in to comment.