From 00efa1a41de947f48af1ab75d404aee7e18ede70 Mon Sep 17 00:00:00 2001 From: Yousif Ahmed Date: Fri, 13 Dec 2024 17:16:05 +0000 Subject: [PATCH] feat(react-native): support marking native span end times independently --- .../NativeBugsnagPerformanceImpl.java | 22 +++++++++++++++---- .../BugsnagReactNativePerformance.java | 8 ++++++- .../BugsnagReactNativePerformance.java | 7 +++++- .../lib/NativeBugsnagPerformance.ts | 1 + 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/packages/platforms/react-native/android/src/main/java/com/bugsnag/android/performance/NativeBugsnagPerformanceImpl.java b/packages/platforms/react-native/android/src/main/java/com/bugsnag/android/performance/NativeBugsnagPerformanceImpl.java index ed9f60579..6200e0c5c 100644 --- a/packages/platforms/react-native/android/src/main/java/com/bugsnag/android/performance/NativeBugsnagPerformanceImpl.java +++ b/packages/platforms/react-native/android/src/main/java/com/bugsnag/android/performance/NativeBugsnagPerformanceImpl.java @@ -155,14 +155,28 @@ public WritableMap startNativeSpan(String name, ReadableMap options) { return nativeSpanToJsSpan(nativeSpan); } - public void endNativeSpan(String spanId, double endTime, ReadableMap jsAttributes, Promise promise) { - SpanImpl nativeSpan = openSpans.remove(spanId); + public void markNativeSpanEndTime(String spanId, double endTime) { + SpanImpl nativeSpan = openSpans.get(spanId); if (nativeSpan != null) { - ReactNativeSpanAttributes.setAttributesFromReadableMap(nativeSpan.getAttributes(), jsAttributes); long nativeEndTime = BugsnagClock.INSTANCE.unixNanoTimeToElapsedRealtime((long)endTime); - nativeSpan.end(nativeEndTime); + nativeSpan.markEndTime$internal(nativeEndTime); + } + } + + public void endNativeSpan(String spanId, double endTime, ReadableMap jsAttributes, Promise promise) { + SpanImpl nativeSpan = openSpans.remove(spanId); + if (nativeSpan == null) { + promise.resolve(null); + return; + } + + ReactNativeSpanAttributes.setAttributesFromReadableMap(nativeSpan.getAttributes(), jsAttributes); + long nativeEndTime = BugsnagClock.INSTANCE.unixNanoTimeToElapsedRealtime((long)endTime); + if (nativeEndTime > nativeSpan.getEndTime$internal()) { + nativeSpan.markEndTime$internal(nativeEndTime); } + nativeSpan.sendForProcessing$internal(); promise.resolve(null); } diff --git a/packages/platforms/react-native/android/src/newarch/java/com/bugsnag/android/performance/BugsnagReactNativePerformance.java b/packages/platforms/react-native/android/src/newarch/java/com/bugsnag/android/performance/BugsnagReactNativePerformance.java index 560370fb3..41b81918d 100644 --- a/packages/platforms/react-native/android/src/newarch/java/com/bugsnag/android/performance/BugsnagReactNativePerformance.java +++ b/packages/platforms/react-native/android/src/newarch/java/com/bugsnag/android/performance/BugsnagReactNativePerformance.java @@ -46,7 +46,8 @@ public WritableMap getNativeConfiguration() { return impl.getNativeConfiguration(); } - @Override public WritableMap startNativeSpan(String name, ReadableMap options) { + @Override + public WritableMap startNativeSpan(String name, ReadableMap options) { return impl.startNativeSpan(name, options); } @@ -54,5 +55,10 @@ public WritableMap getNativeConfiguration() { public void endNativeSpan(String spanId, double endTime, ReadableMap attributes, Promise promise) { impl.endNativeSpan(spanId, endTime, attributes, promise); } + + @Override + public void markNativeSpanEndTime(String spanId, double endTime) { + impl.markNativeSpanEndTime(spanId, endTime); + } } diff --git a/packages/platforms/react-native/android/src/oldarch/java/com/bugsnag/android/performance/BugsnagReactNativePerformance.java b/packages/platforms/react-native/android/src/oldarch/java/com/bugsnag/android/performance/BugsnagReactNativePerformance.java index df207fa29..e00486b8d 100644 --- a/packages/platforms/react-native/android/src/oldarch/java/com/bugsnag/android/performance/BugsnagReactNativePerformance.java +++ b/packages/platforms/react-native/android/src/oldarch/java/com/bugsnag/android/performance/BugsnagReactNativePerformance.java @@ -55,6 +55,11 @@ public WritableMap startNativeSpan(String name, ReadableMap options) { @ReactMethod public void endNativeSpan(String spanId, double endTime, ReadableMap attributes, Promise promise) { - impl.endNativeSpan(spanId, endTime, attributes, promise); + impl.endNativeSpan(spanId, endTime, attributes, promise); + } + + @ReactMethod(isBlockingSynchronousMethod = true) + public void markNativeSpanEndTime(String spanId, double endTime) { + impl.markNativeSpanEndTime(spanId, endTime); } } diff --git a/packages/platforms/react-native/lib/NativeBugsnagPerformance.ts b/packages/platforms/react-native/lib/NativeBugsnagPerformance.ts index 2e9950c0c..ecb47d8dc 100644 --- a/packages/platforms/react-native/lib/NativeBugsnagPerformance.ts +++ b/packages/platforms/react-native/lib/NativeBugsnagPerformance.ts @@ -45,6 +45,7 @@ export interface Spec extends TurboModule { getNativeConfiguration: () => NativeConfiguration | null startNativeSpan: (name: string, options: UnsafeObject) => NativeSpan endNativeSpan: (spanId: string, endTime: number, attributes: UnsafeObject) => Promise + markNativeSpanEndTime: (spanId: string, endTime: number) => void } export default TurboModuleRegistry.get(