@bugsnag/plugin-react-native-span-access 3.3.0 → 3.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,132 @@
1
+ package com.bugsnag.reactnative.performance.nativespans;
2
+
3
+ import androidx.annotation.Nullable;
4
+
5
+ import com.bugsnag.android.performance.Span;
6
+ import com.bugsnag.android.performance.SpanContext;
7
+ import com.bugsnag.android.performance.OnSpanEndCallback;
8
+ import com.bugsnag.android.performance.OnSpanStartCallback;
9
+ import com.bugsnag.android.performance.Plugin;
10
+ import com.bugsnag.android.performance.PluginContext;
11
+ import com.bugsnag.android.performance.RemoteSpanContext;
12
+
13
+ import com.bugsnag.android.performance.internal.SpanImpl.Condition;
14
+ import com.bugsnag.android.performance.internal.SpanCategory;
15
+ import com.bugsnag.android.performance.internal.SpanImpl;
16
+
17
+ import com.bugsnag.reactnative.performance.AppStartProvider;
18
+ import com.bugsnag.reactnative.performance.AppStartRegistry;
19
+
20
+ import java.util.concurrent.atomic.AtomicReference;
21
+ import java.util.concurrent.atomic.AtomicBoolean;
22
+
23
+ public class BugsnagReactNativeAppStartPlugin implements Plugin, AppStartProvider {
24
+
25
+ private static final long DEFAULT_SPAN_BLOCK_TIMEOUT_MS = 5000;
26
+
27
+ // Container class to ensure atomic updates of related fields
28
+ private static class ViewLoadCondition {
29
+ final String spanId;
30
+ final Condition condition;
31
+
32
+ ViewLoadCondition(String spanId, Condition condition) {
33
+ this.spanId = spanId;
34
+ this.condition = condition;
35
+ }
36
+ }
37
+
38
+ private final AtomicReference<ViewLoadCondition> viewLoadCondition = new AtomicReference<>(null);
39
+ private volatile boolean appStartComplete = false;
40
+ private final long spanBlockTimeoutMs;
41
+
42
+ public BugsnagReactNativeAppStartPlugin() {
43
+ this(DEFAULT_SPAN_BLOCK_TIMEOUT_MS);
44
+ }
45
+
46
+ public BugsnagReactNativeAppStartPlugin(long timeoutMs) {
47
+ this.spanBlockTimeoutMs = timeoutMs;
48
+ }
49
+
50
+ @Override
51
+ public void install(PluginContext ctx) {
52
+ // Register this plugin as the AppStartProvider with the core React Native Performance module
53
+ AppStartRegistry.register(this);
54
+
55
+ ctx.addOnSpanStartCallback(PluginContext.NORM_PRIORITY + 1, new OnSpanStartCallback() {
56
+ @Override
57
+ public void onSpanStart(Span span) {
58
+ BugsnagReactNativeAppStartPlugin.this.onSpanStart(span);
59
+ }
60
+ });
61
+
62
+ ctx.addOnSpanEndCallback(PluginContext.NORM_PRIORITY - 1, new OnSpanEndCallback() {
63
+ @Override
64
+ public boolean onSpanEnd(Span span) {
65
+ return BugsnagReactNativeAppStartPlugin.this.onSpanEnd(span);
66
+ }
67
+ });
68
+ }
69
+
70
+ @Override
71
+ public void start() {}
72
+
73
+ @Nullable
74
+ @Override
75
+ public String getAppStartParent() {
76
+ ViewLoadCondition currentCondition = viewLoadCondition.get();
77
+ if (currentCondition != null) {
78
+ SpanContext nativeParent = currentCondition.condition.upgrade();
79
+ if (nativeParent != null) {
80
+ return RemoteSpanContext.encodeAsTraceParent(nativeParent);
81
+ }
82
+ }
83
+
84
+ return null;
85
+ }
86
+
87
+ @Override
88
+ public void endAppStart(long endTime) {
89
+ ViewLoadCondition currentCondition = viewLoadCondition.getAndSet(null);
90
+ if (currentCondition != null) {
91
+ currentCondition.condition.close(endTime);
92
+ }
93
+
94
+ appStartComplete = true;
95
+ }
96
+
97
+ private void onSpanStart(Span span) {
98
+ if (appStartComplete) {
99
+ return;
100
+ }
101
+
102
+ SpanImpl spanImpl = (SpanImpl)span;
103
+ if (spanImpl.getAttributes().get("bugsnag.span.category") != SpanCategory.CATEGORY_VIEW_LOAD) {
104
+ return;
105
+ }
106
+
107
+ ViewLoadCondition currentCondition = viewLoadCondition.get();
108
+ if (currentCondition != null) {
109
+ currentCondition.condition.cancel();
110
+ }
111
+
112
+ // Only set viewLoadCondition if block() returned a non-null value
113
+ Condition spanCondition = spanImpl.block(spanBlockTimeoutMs);
114
+ if (spanCondition != null) {
115
+ String spanId = RemoteSpanContext.encodeAsTraceParent(spanImpl);
116
+ viewLoadCondition.set(new ViewLoadCondition(spanId, spanCondition));
117
+ }
118
+ }
119
+
120
+ private boolean onSpanEnd(Span span) {
121
+ String spanId = RemoteSpanContext.encodeAsTraceParent(span);
122
+ ViewLoadCondition currentCondition = viewLoadCondition.get();
123
+
124
+ if (currentCondition != null && spanId.equals(currentCondition.spanId)) {
125
+ if (viewLoadCondition.compareAndSet(currentCondition, null)) {
126
+ currentCondition.condition.cancel();
127
+ }
128
+ }
129
+
130
+ return true;
131
+ }
132
+ }
@@ -9,6 +9,7 @@
9
9
  /* Begin PBXBuildFile section */
10
10
  DA3192AA2E0AE8B7009B1271 /* BugsnagNativeSpans.mm in Sources */ = {isa = PBXBuildFile; fileRef = DA3192A62E0AE8B7009B1271 /* BugsnagNativeSpans.mm */; };
11
11
  DA3192AB2E0AE8B7009B1271 /* BugsnagNativeSpansPlugin.mm in Sources */ = {isa = PBXBuildFile; fileRef = DA3192A82E0AE8B7009B1271 /* BugsnagNativeSpansPlugin.mm */; };
12
+ DA862E902ED89F1700F95EAD /* BugsnagReactNativeAppStartPlugin.mm in Sources */ = {isa = PBXBuildFile; fileRef = DA862E8F2ED89F1700F95EAD /* BugsnagReactNativeAppStartPlugin.mm */; };
12
13
  DAADC7312E1EBBFC0085E633 /* BugsnagJavascriptSpansPlugin.mm in Sources */ = {isa = PBXBuildFile; fileRef = DAADC7302E1EBBFC0085E633 /* BugsnagJavascriptSpansPlugin.mm */; };
13
14
  DAADC7352E1EBE750085E633 /* BugsnagJavascriptSpanControlProvider.mm in Sources */ = {isa = PBXBuildFile; fileRef = DAADC7342E1EBE750085E633 /* BugsnagJavascriptSpanControlProvider.mm */; };
14
15
  DAADC73A2E1F2E080085E633 /* BugsnagJavascriptSpanControl.mm in Sources */ = {isa = PBXBuildFile; fileRef = DAADC7392E1F2E080085E633 /* BugsnagJavascriptSpanControl.mm */; };
@@ -34,6 +35,8 @@
34
35
  DA3192A72E0AE8B7009B1271 /* BugsnagNativeSpansPlugin.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BugsnagNativeSpansPlugin.h; sourceTree = "<group>"; };
35
36
  DA3192A82E0AE8B7009B1271 /* BugsnagNativeSpansPlugin.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = BugsnagNativeSpansPlugin.mm; sourceTree = "<group>"; };
36
37
  DA3192A92E0AE8B7009B1271 /* BugsnagNativeSpansPlugin+Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "BugsnagNativeSpansPlugin+Private.h"; sourceTree = "<group>"; };
38
+ DA862E8E2ED89F1700F95EAD /* BugsnagReactNativeAppStartPlugin.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BugsnagReactNativeAppStartPlugin.h; sourceTree = "<group>"; };
39
+ DA862E8F2ED89F1700F95EAD /* BugsnagReactNativeAppStartPlugin.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = BugsnagReactNativeAppStartPlugin.mm; sourceTree = "<group>"; };
37
40
  DAADC72F2E1EBB9D0085E633 /* BugsnagJavascriptSpansPlugin.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BugsnagJavascriptSpansPlugin.h; sourceTree = "<group>"; };
38
41
  DAADC7302E1EBBFC0085E633 /* BugsnagJavascriptSpansPlugin.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = BugsnagJavascriptSpansPlugin.mm; sourceTree = "<group>"; };
39
42
  DAADC7322E1EBCA00085E633 /* BugsnagJavascriptSpansPlugin+Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "BugsnagJavascriptSpansPlugin+Private.h"; sourceTree = "<group>"; };
@@ -60,6 +63,8 @@
60
63
  DA31928B2E0AE7D4009B1271 = {
61
64
  isa = PBXGroup;
62
65
  children = (
66
+ DA862E8E2ED89F1700F95EAD /* BugsnagReactNativeAppStartPlugin.h */,
67
+ DA862E8F2ED89F1700F95EAD /* BugsnagReactNativeAppStartPlugin.mm */,
63
68
  DAADC73E2E1FF0040085E633 /* BugsnagJavascriptSpanControl+Private.h */,
64
69
  DAADC7372E1F2AD40085E633 /* BugsnagJavascriptSpanControl.h */,
65
70
  DAADC7332E1EBE5B0085E633 /* BugsnagJavascriptSpanControlProvider.h */,
@@ -147,6 +152,7 @@
147
152
  isa = PBXSourcesBuildPhase;
148
153
  buildActionMask = 2147483647;
149
154
  files = (
155
+ DA862E902ED89F1700F95EAD /* BugsnagReactNativeAppStartPlugin.mm in Sources */,
150
156
  DAADC73A2E1F2E080085E633 /* BugsnagJavascriptSpanControl.mm in Sources */,
151
157
  DAADC7312E1EBBFC0085E633 /* BugsnagJavascriptSpansPlugin.mm in Sources */,
152
158
  DAADC7352E1EBE750085E633 /* BugsnagJavascriptSpanControlProvider.mm in Sources */,
@@ -0,0 +1,13 @@
1
+ #import <Foundation/Foundation.h>
2
+ #import <BugsnagPerformance/BugsnagPerformancePlugin.h>
3
+
4
+ NS_ASSUME_NONNULL_BEGIN
5
+
6
+ @interface BugsnagReactNativeAppStartPlugin: NSObject <BugsnagPerformancePlugin>
7
+
8
+ - (instancetype)init;
9
+ - (instancetype)initWithTimeout:(NSTimeInterval)timeout;
10
+
11
+ @end
12
+
13
+ NS_ASSUME_NONNULL_END
@@ -0,0 +1,142 @@
1
+
2
+ #import "BugsnagReactNativeAppStartPlugin.h"
3
+ #import <BugsnagPerformance/BugsnagPerformancePluginContext.h>
4
+ #import <BugsnagPerformance/BugsnagPerformanceSpan.h>
5
+ #import <BugsnagPerformance/BugsnagPerformanceSpanCondition.h>
6
+ #import <BugsnagPerformance/BugsnagPerformanceSpanContext.h>
7
+
8
+ static const NSTimeInterval kDefaultSpanBlockTimeoutInterval = 5; // 5s default timeout
9
+
10
+ @interface BugsnagReactNativeAppStartPlugin ()
11
+ @property (nonatomic, strong) NSString *currentSpanId;
12
+ @property (nonatomic, strong) BugsnagPerformanceSpanCondition *currentCondition;
13
+ @property (atomic, assign) BOOL appStartComplete;
14
+ @property (nonatomic, assign) NSTimeInterval spanBlockTimeoutInterval;
15
+ @end
16
+
17
+ // Attributes are in the private header for BugsnagPerformanceSpan,
18
+ // but are needed to check span category, so we declare them here
19
+ @interface BugsnagPerformanceSpan ()
20
+ @property (nonatomic,readonly) NSMutableDictionary *attributes;
21
+ @end
22
+
23
+ @implementation BugsnagReactNativeAppStartPlugin
24
+
25
+ static BugsnagReactNativeAppStartPlugin *_sharedInstance = nil;
26
+
27
+ - (instancetype)init {
28
+ return [self initWithTimeout:kDefaultSpanBlockTimeoutInterval];
29
+ }
30
+
31
+ - (instancetype)initWithTimeout:(NSTimeInterval)timeout {
32
+ if (self = [super init]) {
33
+ _spanBlockTimeoutInterval = timeout;
34
+ }
35
+ return self;
36
+ }
37
+
38
+ + (id)singleton {
39
+ return _sharedInstance;
40
+ }
41
+
42
+ - (void)installWithContext:(BugsnagPerformancePluginContext *)context {
43
+ _sharedInstance = self;
44
+ __weak BugsnagReactNativeAppStartPlugin *weakSelf = self;
45
+
46
+ // Add span start callback with high priority (equivalent to NORM_PRIORITY + 1)
47
+ BugsnagPerformanceSpanStartCallback spanStartCallback = ^(BugsnagPerformanceSpan *span) {
48
+ [weakSelf onSpanStart:span];
49
+ };
50
+
51
+ // Add span end callback with low priority (equivalent to NORM_PRIORITY - 1)
52
+ BugsnagPerformanceSpanEndCallback spanEndCallback = ^(BugsnagPerformanceSpan *span) {
53
+ return [weakSelf onSpanEnd:span];
54
+ };
55
+
56
+ [context addOnSpanStartCallback:spanStartCallback priority:BugsnagPerformancePriorityHigh];
57
+ [context addOnSpanEndCallback:spanEndCallback priority:BugsnagPerformancePriorityLow];
58
+ }
59
+
60
+ - (void)start {
61
+ // Plugin start implementation
62
+ }
63
+
64
+ - (NSString *)getAppStartParent {
65
+ BugsnagPerformanceSpanCondition *condition;
66
+ @synchronized (self) {
67
+ condition = _currentCondition;
68
+ }
69
+
70
+ if (condition) {
71
+ BugsnagPerformanceSpanContext *nativeParent = [condition upgrade];
72
+ if (nativeParent) {
73
+ return [nativeParent encodedAsTraceParent];
74
+ }
75
+ }
76
+ return nil;
77
+ }
78
+
79
+ - (void)endAppStart:(NSDate *)endTime {
80
+ BugsnagPerformanceSpanCondition *condition;
81
+ @synchronized (self) {
82
+ condition = _currentCondition;
83
+ _currentCondition = nil;
84
+ _currentSpanId = nil;
85
+ _appStartComplete = YES;
86
+ }
87
+
88
+ if (condition) {
89
+ [condition closeWithEndTime:endTime];
90
+ }
91
+ }
92
+
93
+ - (void)onSpanStart:(BugsnagPerformanceSpan *)span {
94
+ // Check app start completion status (atomic read)
95
+ if (_appStartComplete) {
96
+ return;
97
+ }
98
+
99
+ // Check if this is a view_load span by examining attributes
100
+ NSString *category = span.attributes[@"bugsnag.span.category"];
101
+ if (![category isEqualToString:@"view_load"]) {
102
+ return;
103
+ }
104
+
105
+ // Block the span (outside synchronized block)
106
+ BugsnagPerformanceSpanCondition *spanCondition = [span blockWithTimeout:_spanBlockTimeoutInterval];
107
+
108
+ // Update shared state atomically
109
+ @synchronized (self) {
110
+ // Cancel any existing condition
111
+ if (_currentCondition) {
112
+ [_currentCondition cancel];
113
+ }
114
+
115
+ // Only set current condition if block() returned a non-null value
116
+ if (spanCondition) {
117
+ _currentSpanId = [span encodedAsTraceParent];
118
+ _currentCondition = spanCondition;
119
+ }
120
+ }
121
+ }
122
+
123
+ - (BOOL)onSpanEnd:(BugsnagPerformanceSpan *)span {
124
+ NSString *spanId = [span encodedAsTraceParent];
125
+
126
+ BugsnagPerformanceSpanCondition *conditionToCancel = nil;
127
+ @synchronized (self) {
128
+ if (_currentCondition && [spanId isEqualToString:_currentSpanId]) {
129
+ conditionToCancel = _currentCondition;
130
+ _currentCondition = nil;
131
+ _currentSpanId = nil;
132
+ }
133
+ }
134
+
135
+ if (conditionToCancel) {
136
+ [conditionToCancel cancel];
137
+ }
138
+
139
+ return YES;
140
+ }
141
+
142
+ @end
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bugsnag/plugin-react-native-span-access",
3
- "version": "3.3.0",
3
+ "version": "3.4.0",
4
4
  "description": "BugSnag performance monitoring for react-native",
5
5
  "homepage": "https://www.bugsnag.com/",
6
6
  "license": "MIT",
@@ -24,7 +24,7 @@
24
24
  },
25
25
  "devDependencies": {
26
26
  "@bugsnag/core-performance": "^3.2.0",
27
- "@bugsnag/react-native-performance": "^3.3.0",
27
+ "@bugsnag/react-native-performance": "^3.4.0",
28
28
  "@react-native/babel-preset": "^0.74.0",
29
29
  "@testing-library/jest-native": "^5.4.3",
30
30
  "@testing-library/react-native": "^12.4.1"
@@ -48,5 +48,5 @@
48
48
  "javaPackageName": "com.bugsnag.reactnative.performance.nativespans"
49
49
  }
50
50
  },
51
- "gitHead": "808ff2648beec84abf6e2d0bee08001100e417b2"
51
+ "gitHead": "ee23ead619633202a2f639a75e869232c08713c4"
52
52
  }