@bugsnag/plugin-react-native-span-access 2.15.0 → 3.0.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.
Files changed (42) hide show
  1. package/android/build.gradle +2 -1
  2. package/android/src/main/java/com/bugsnag/reactnative/performance/nativespans/BugsnagJavascriptSpansPlugin.java +32 -0
  3. package/android/src/main/java/com/bugsnag/reactnative/performance/nativespans/BugsnagNativeSpans.java +217 -131
  4. package/android/src/main/java/com/bugsnag/reactnative/performance/nativespans/BugsnagNativeSpansPlugin.java +91 -91
  5. package/android/src/main/java/com/bugsnag/reactnative/performance/nativespans/CallbackState.java +32 -0
  6. package/android/src/main/java/com/bugsnag/reactnative/performance/nativespans/JavascriptSpanByName.java +37 -0
  7. package/android/src/main/java/com/bugsnag/reactnative/performance/nativespans/JavascriptSpanControl.java +15 -0
  8. package/android/src/main/java/com/bugsnag/reactnative/performance/nativespans/JavascriptSpanControlImpl.java +46 -0
  9. package/android/src/main/java/com/bugsnag/reactnative/performance/nativespans/JavascriptSpanControlProvider.java +19 -0
  10. package/android/src/main/java/com/bugsnag/reactnative/performance/nativespans/JavascriptSpanTransaction.java +13 -0
  11. package/android/src/main/java/com/bugsnag/reactnative/performance/nativespans/JavascriptSpanTransactionImpl.java +153 -0
  12. package/android/src/main/java/com/bugsnag/reactnative/performance/nativespans/OnRemoteSpanUpdatedCallback.java +5 -0
  13. package/android/src/main/java/com/bugsnag/reactnative/performance/nativespans/OnSpanContextRetrievedCallback.java +14 -0
  14. package/android/src/newarch/java/com/bugsnag/reactnative/performance/nativespans/BugsnagNativeSpansModule.java +20 -0
  15. package/android/src/oldarch/java/com/bugsnag/reactnative/performance/nativespans/BugsnagNativeSpansModule.java +20 -0
  16. package/dist/NativeBugsnagNativeSpans.ts +8 -0
  17. package/dist/index.js +2 -58
  18. package/dist/javascript-spans-plugin.js +83 -0
  19. package/dist/native-spans-plugin.js +61 -0
  20. package/dist/types/NativeBugsnagNativeSpans.d.ts +4 -0
  21. package/dist/types/NativeBugsnagNativeSpans.d.ts.map +1 -1
  22. package/dist/types/index.d.ts +2 -23
  23. package/dist/types/index.d.ts.map +1 -1
  24. package/dist/types/javascript-spans-plugin.d.ts +15 -0
  25. package/dist/types/javascript-spans-plugin.d.ts.map +1 -0
  26. package/dist/types/native-spans-plugin.d.ts +24 -0
  27. package/dist/types/native-spans-plugin.d.ts.map +1 -0
  28. package/ios/BugsnagJavascriptSpanControl+Private.h +7 -0
  29. package/ios/BugsnagJavascriptSpanControl.h +31 -0
  30. package/ios/BugsnagJavascriptSpanControl.mm +148 -0
  31. package/ios/BugsnagJavascriptSpanControlProvider.h +10 -0
  32. package/ios/BugsnagJavascriptSpanControlProvider.mm +18 -0
  33. package/ios/BugsnagJavascriptSpanQuery.h +12 -0
  34. package/ios/BugsnagJavascriptSpanQuery.mm +13 -0
  35. package/ios/BugsnagJavascriptSpansPlugin+Private.h +24 -0
  36. package/ios/BugsnagJavascriptSpansPlugin.h +10 -0
  37. package/ios/BugsnagJavascriptSpansPlugin.mm +117 -0
  38. package/ios/BugsnagNativeSpans.h +2 -1
  39. package/ios/BugsnagNativeSpans.mm +49 -3
  40. package/ios/BugsnagNativeSpans.xcodeproj/project.pbxproj +30 -2
  41. package/ios/BugsnagNativeSpansPlugin.mm +24 -6
  42. package/package.json +4 -4
@@ -38,5 +38,6 @@ android {
38
38
  dependencies {
39
39
  implementation 'com.facebook.react:react-native'
40
40
  implementation project(':bugsnag_react-native-performance')
41
- api 'com.bugsnag:bugsnag-android-performance:1.16.0'
41
+ api 'com.bugsnag:bugsnag-android-performance:2.0.0'
42
+ implementation 'com.bugsnag:bugsnag-android-performance-impl:2.0.0'
42
43
  }
@@ -0,0 +1,32 @@
1
+ package com.bugsnag.reactnative.performance.nativespans;
2
+
3
+ import com.bugsnag.android.performance.Span;
4
+ import com.bugsnag.android.performance.OnSpanEndCallback;
5
+ import com.bugsnag.android.performance.OnSpanStartCallback;
6
+ import com.bugsnag.android.performance.Plugin;
7
+ import com.bugsnag.android.performance.PluginContext;
8
+ import com.bugsnag.android.performance.internal.EncodingUtils;
9
+ import com.bugsnag.android.performance.internal.SpanImpl;
10
+ import com.bugsnag.android.performance.internal.processing.Timeout;
11
+ import com.bugsnag.android.performance.internal.processing.TimeoutExecutor;
12
+
13
+ import android.os.SystemClock;
14
+
15
+ import java.util.UUID;
16
+ import java.util.concurrent.Delayed;
17
+ import java.util.concurrent.TimeUnit;
18
+ import java.util.concurrent.ConcurrentMap;
19
+ import java.util.concurrent.ConcurrentHashMap;
20
+
21
+ public class BugsnagJavascriptSpansPlugin implements Plugin {
22
+
23
+ @Override
24
+ public void install(PluginContext ctx) {
25
+ ctx.addSpanControlProvider(new JavascriptSpanControlProvider());
26
+ }
27
+
28
+ @Override
29
+ public void start() {
30
+ }
31
+
32
+ }
@@ -1,15 +1,11 @@
1
1
  package com.bugsnag.reactnative.performance.nativespans;
2
2
 
3
- import java.util.Date;
4
- import java.text.DateFormat;
5
- import java.text.ParseException;
6
- import java.text.SimpleDateFormat;
7
-
8
- import android.util.Log;
9
-
10
3
  import androidx.annotation.Nullable;
11
4
 
5
+ import android.util.SparseArray;
6
+
12
7
  import com.bugsnag.android.performance.Span;
8
+ import com.bugsnag.android.performance.RemoteSpanContext;
13
9
  import com.bugsnag.android.performance.internal.BugsnagClock;
14
10
  import com.bugsnag.android.performance.internal.EncodingUtils;
15
11
  import com.bugsnag.reactnative.performance.ReactNativeSpanAttributes;
@@ -20,132 +16,222 @@ import com.facebook.react.bridge.ReadableArray;
20
16
  import com.facebook.react.bridge.ReadableMap;
21
17
  import com.facebook.react.bridge.ReadableType;
22
18
  import com.facebook.react.bridge.WritableMap;
19
+ import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter;
23
20
 
24
21
  class BugsnagNativeSpans {
25
22
 
26
- // Attribute keys
27
- private static final String ATTRIBUTES = "attributes";
28
- private static final String ATTR_NAME = "name";
29
- private static final String ATTR_VALUE = "value";
30
-
31
- // Span properties
32
- private static final String END_TIME = "endTime";
33
- private static final String IS_ENDED = "isEnded";
34
- private static final String SPAN_ID = "spanId";
35
- private static final String TRACE_ID = "traceId";
36
-
37
- static final String MODULE_NAME = "BugsnagNativeSpans";
38
-
39
- private final ReactApplicationContext reactContext;
40
-
41
- public BugsnagNativeSpans(ReactApplicationContext reactContext) {
42
- this.reactContext = reactContext;
43
- }
44
-
45
- @Nullable
46
- public WritableMap getSpanIdByName(String spanName) {
47
- BugsnagNativeSpansPlugin nativeSpanAccessPlugin = BugsnagNativeSpansPlugin.getInstance();
48
-
49
- if (nativeSpanAccessPlugin == null) {
50
- return null;
51
- }
52
-
53
- Span span = nativeSpanAccessPlugin.getSpanByName(spanName);
54
- if (span == null) {
55
- return null;
56
- }
57
-
58
- WritableMap map = Arguments.createMap();
59
- map.putString(SPAN_ID, EncodingUtils.toHexString(span.getSpanId()));
60
- map.putString(TRACE_ID, EncodingUtils.toHexString(span.getTraceId()));
61
-
62
- return map;
63
- }
64
-
65
- public void updateSpan(ReadableMap spanId, ReadableMap updates, Promise promise) {
66
- BugsnagNativeSpansPlugin nativeSpanAccessPlugin = BugsnagNativeSpansPlugin.getInstance();
67
-
68
- if (nativeSpanAccessPlugin == null) {
69
- promise.resolve(false);
70
- return;
71
- }
72
-
73
- String traceIdHex = spanId.getString(TRACE_ID);
74
- String spanIdHex = spanId.getString(SPAN_ID);
75
- Span span = nativeSpanAccessPlugin.getSpanById(traceIdHex, spanIdHex);
76
- if (span == null) {
77
- promise.resolve(false);
78
- return;
79
- }
80
-
81
- ReadableArray attributes = updates.getArray(ATTRIBUTES);
82
- if (attributes != null) {
83
- updateSpanAttributes(attributes, span);
84
- }
85
-
86
- if (updates.getBoolean(IS_ENDED)) {
87
- endSpan(updates, span);
88
- }
89
-
90
- promise.resolve(true);
91
- }
92
-
93
- private static void endSpan(ReadableMap updates, Span span) {
94
- if (updates.hasKey(END_TIME)) {
95
- double endTime = updates.getDouble(END_TIME);
96
- span.end(BugsnagClock.INSTANCE.unixNanoTimeToElapsedRealtime((long) endTime));
97
- } else {
98
- span.end();
99
- }
100
- }
101
-
102
- private static void updateSpanAttributes(ReadableArray attributeUpdates, Span span) {
103
- for (int i = 0; i < attributeUpdates.size(); i++) {
104
- ReadableMap attribute = attributeUpdates.getMap(i);
105
- String name = attribute.getString(ATTR_NAME);
106
- ReadableType type = attribute.getType(ATTR_VALUE);
107
-
108
- switch (type) {
109
- case Null:
110
- span.setAttribute(name, (String) null);
111
- break;
112
- case Boolean:
113
- span.setAttribute(name, attribute.getBoolean(ATTR_VALUE));
114
- break;
115
- case Number:
116
- setNumberAttribute(span, attribute, name);
117
- break;
118
- case String:
119
- span.setAttribute(name, attribute.getString(ATTR_VALUE));
120
- break;
121
- case Array:
122
- setArrayAttribute(span, attribute, name);
123
- break;
124
- }
125
- }
126
- }
127
-
128
- private static void setArrayAttribute(Span span, ReadableMap attribute, String name) {
129
- Object array = ReactNativeSpanAttributes.transformArray(attribute.getArray(ATTR_VALUE));
130
- if (array instanceof String[]) {
131
- span.setAttribute(name, (String[]) array);
132
- } else if (array instanceof long[]) {
133
- span.setAttribute(name, (long[]) array);
134
- } else if (array instanceof double[]) {
135
- span.setAttribute(name, (double[]) array);
136
- }
137
- }
138
-
139
- private static void setNumberAttribute(Span span, ReadableMap attribute, String name) {
140
- double n = attribute.getDouble(ATTR_VALUE);
141
- if (isInteger(n)) {
142
- span.setAttribute(name, (long) n);
143
- } else {
144
- span.setAttribute(name, n);
145
- }
146
- }
147
-
148
- private static boolean isInteger(double n) {
149
- return n % 1 == 0;
23
+ // Attribute keys
24
+ private static final String ATTRIBUTES = "attributes";
25
+ private static final String ATTR_NAME = "name";
26
+ private static final String ATTR_VALUE = "value";
27
+
28
+ // Span properties
29
+ private static final String END_TIME = "endTime";
30
+ private static final String IS_ENDED = "isEnded";
31
+ private static final String SPAN_ID = "spanId";
32
+ private static final String TRACE_ID = "traceId";
33
+
34
+ private static final String SPAN_UPDATE_EVENT_TYPE = "bugsnag:spanUpdate";
35
+ private static final String RETRIEVE_SPAN_CONTEXT_EVENT_TYPE = "bugsnag:retrieveSpanContext";
36
+
37
+ private static volatile BugsnagNativeSpans INSTANCE;
38
+
39
+ static final String MODULE_NAME = "BugsnagNativeSpans";
40
+
41
+ private final ReactApplicationContext reactContext;
42
+
43
+ private final CallbackState<OnRemoteSpanUpdatedCallback> updateCallbacks = new CallbackState<>();
44
+ private final CallbackState<OnSpanContextRetrievedCallback> spanContextCallbacks = new CallbackState<>();
45
+
46
+ public BugsnagNativeSpans(ReactApplicationContext reactContext) {
47
+ this.reactContext = reactContext;
48
+
49
+ INSTANCE = this;
50
+ }
51
+
52
+ static @Nullable BugsnagNativeSpans getInstance() {
53
+ return INSTANCE;
54
+ }
55
+
56
+ @Nullable
57
+ public WritableMap getSpanIdByName(String spanName) {
58
+ BugsnagNativeSpansPlugin nativeSpanAccessPlugin = BugsnagNativeSpansPlugin.getInstance();
59
+
60
+ if (nativeSpanAccessPlugin == null) {
61
+ return null;
150
62
  }
63
+
64
+ Span span = nativeSpanAccessPlugin.getSpanByName(spanName);
65
+ if (span == null) {
66
+ return null;
67
+ }
68
+
69
+ WritableMap map = Arguments.createMap();
70
+ map.putString(SPAN_ID, EncodingUtils.toHexString(span.getSpanId()));
71
+ map.putString(TRACE_ID, EncodingUtils.toHexString(span.getTraceId()));
72
+
73
+ return map;
74
+ }
75
+
76
+ public void updateSpan(ReadableMap spanId, ReadableMap updates, Promise promise) {
77
+ BugsnagNativeSpansPlugin nativeSpanAccessPlugin = BugsnagNativeSpansPlugin.getInstance();
78
+
79
+ if (nativeSpanAccessPlugin == null) {
80
+ promise.resolve(false);
81
+ return;
82
+ }
83
+
84
+ String traceIdHex = spanId.getString(TRACE_ID);
85
+ String spanIdHex = spanId.getString(SPAN_ID);
86
+ Span span = nativeSpanAccessPlugin.getSpanById(traceIdHex, spanIdHex);
87
+ if (span == null) {
88
+ promise.resolve(false);
89
+ return;
90
+ }
91
+
92
+ ReadableArray attributes = updates.getArray(ATTRIBUTES);
93
+ if (attributes != null) {
94
+ updateSpanAttributes(attributes, span);
95
+ }
96
+
97
+ if (updates.getBoolean(IS_ENDED)) {
98
+ endSpan(updates, span);
99
+ }
100
+
101
+ promise.resolve(true);
102
+ }
103
+
104
+ public void reportSpanUpdateResult(double eventId, boolean result, Promise promise) {
105
+ try {
106
+ onRemoteSpanUpdated((int) eventId, result);
107
+ } finally {
108
+ promise.resolve(null);
109
+ }
110
+ }
111
+
112
+ public void reportSpanContextResult(double eventId, String result, Promise promise) {
113
+ try {
114
+ onRemoteSpanContextRetrieved((int) eventId, result);
115
+ } finally {
116
+ promise.resolve(null);
117
+ }
118
+ }
119
+
120
+ boolean emitSpanUpdateEvent(ReadableMap updates) {
121
+ try {
122
+ RCTDeviceEventEmitter eventEmitter = reactContext.getJSModule(RCTDeviceEventEmitter.class);
123
+ if (eventEmitter == null) {
124
+ return false;
125
+ }
126
+ eventEmitter.emit(SPAN_UPDATE_EVENT_TYPE, updates);
127
+ return true;
128
+ } catch (IllegalStateException ise) {
129
+ return false; // This can happen if the React context is not active
130
+ }
131
+ }
132
+
133
+ boolean emitRetrieveSpanContextEvent(ReadableMap request) {
134
+ try {
135
+ RCTDeviceEventEmitter eventEmitter = reactContext.getJSModule(RCTDeviceEventEmitter.class);
136
+ if (eventEmitter == null) {
137
+ return false;
138
+ }
139
+ eventEmitter.emit(RETRIEVE_SPAN_CONTEXT_EVENT_TYPE, request);
140
+ return true;
141
+ } catch (IllegalStateException ise) {
142
+ return false; // This can happen if the React context is not active
143
+ }
144
+ }
145
+
146
+ private void onRemoteSpanUpdated(int callbackId, boolean updateResult) {
147
+ OnRemoteSpanUpdatedCallback callback = takeUpdateCallback(callbackId);
148
+ if (callback != null) {
149
+ callback.onRemoteSpanUpdated(updateResult);
150
+ }
151
+ }
152
+
153
+ private void onRemoteSpanContextRetrieved(int callbackId, String result) {
154
+ OnSpanContextRetrievedCallback callback = takeSpanContextCallback(callbackId);
155
+ if (callback != null) {
156
+ RemoteSpanContext remoteSpanContext = RemoteSpanContext.parseTraceParentOrNull(result);
157
+ callback.onSpanContextRetrieved(remoteSpanContext);
158
+ }
159
+ }
160
+
161
+ int registerUpdateCallback(OnRemoteSpanUpdatedCallback callback) {
162
+ return updateCallbacks.registerCallback(callback);
163
+ }
164
+
165
+ int registerSpanContextCallback(OnSpanContextRetrievedCallback callback) {
166
+ return spanContextCallbacks.registerCallback(callback);
167
+ }
168
+
169
+ @Nullable
170
+ OnRemoteSpanUpdatedCallback takeUpdateCallback(int callbackId) {
171
+ return updateCallbacks.takeCallback(callbackId);
172
+ }
173
+
174
+ @Nullable
175
+ OnSpanContextRetrievedCallback takeSpanContextCallback(int callbackId) {
176
+ return spanContextCallbacks.takeCallback(callbackId);
177
+ }
178
+
179
+ private static void endSpan(ReadableMap updates, Span span) {
180
+ if (updates.hasKey(END_TIME)) {
181
+ double endTime = updates.getDouble(END_TIME);
182
+ span.end(BugsnagClock.INSTANCE.unixNanoTimeToElapsedRealtime((long) endTime));
183
+ } else {
184
+ span.end();
185
+ }
186
+ }
187
+
188
+ private static void updateSpanAttributes(ReadableArray attributeUpdates, Span span) {
189
+ for (int i = 0; i < attributeUpdates.size(); i++) {
190
+ ReadableMap attribute = attributeUpdates.getMap(i);
191
+ String name = attribute.getString(ATTR_NAME);
192
+ ReadableType type = attribute.getType(ATTR_VALUE);
193
+
194
+ switch (type) {
195
+ case Null:
196
+ span.setAttribute(name, (String) null);
197
+ break;
198
+ case Boolean:
199
+ span.setAttribute(name, attribute.getBoolean(ATTR_VALUE));
200
+ break;
201
+ case Number:
202
+ setNumberAttribute(span, attribute, name);
203
+ break;
204
+ case String:
205
+ span.setAttribute(name, attribute.getString(ATTR_VALUE));
206
+ break;
207
+ case Array:
208
+ setArrayAttribute(span, attribute, name);
209
+ break;
210
+ }
211
+ }
212
+ }
213
+
214
+ private static void setArrayAttribute(Span span, ReadableMap attribute, String name) {
215
+ Object array = ReactNativeSpanAttributes.transformArray(attribute.getArray(ATTR_VALUE));
216
+ if (array instanceof String[]) {
217
+ span.setAttribute(name, (String[]) array);
218
+ } else if (array instanceof long[]) {
219
+ span.setAttribute(name, (long[]) array);
220
+ } else if (array instanceof double[]) {
221
+ span.setAttribute(name, (double[]) array);
222
+ }
223
+ }
224
+
225
+ private static void setNumberAttribute(Span span, ReadableMap attribute, String name) {
226
+ double n = attribute.getDouble(ATTR_VALUE);
227
+ if (isInteger(n)) {
228
+ span.setAttribute(name, (long) n);
229
+ } else {
230
+ span.setAttribute(name, n);
231
+ }
232
+ }
233
+
234
+ private static boolean isInteger(double n) {
235
+ return n % 1 == 0;
236
+ }
151
237
  }
@@ -19,113 +19,113 @@ import java.util.concurrent.ConcurrentMap;
19
19
  import java.util.concurrent.ConcurrentHashMap;
20
20
 
21
21
  public class BugsnagNativeSpansPlugin implements Plugin {
22
- /**
23
- * Default 10 minute validity time
24
- */
25
- private static final long DEFAULT_VALIDITY_TIME = 10 * 60 * 1000;
22
+ /**
23
+ * Default 10 minute validity time
24
+ */
25
+ private static final long DEFAULT_VALIDITY_TIME = 10 * 60 * 1000;
26
26
 
27
- private static BugsnagNativeSpansPlugin INSTANCE;
27
+ private static BugsnagNativeSpansPlugin INSTANCE;
28
28
 
29
- private final ConcurrentMap<String, Span> spansByName = new ConcurrentHashMap<>();
30
- private final ConcurrentMap<String, Span> spansById = new ConcurrentHashMap<>();
29
+ private final ConcurrentMap<String, Span> spansByName = new ConcurrentHashMap<>();
30
+ private final ConcurrentMap<String, Span> spansById = new ConcurrentHashMap<>();
31
31
 
32
- @Override
33
- public void install(PluginContext ctx) {
34
- if (INSTANCE == null) {
35
- INSTANCE = this;
36
- }
37
-
38
- ctx.addOnSpanStartCallback(PluginContext.NORM_PRIORITY + 1, new OnSpanStartCallback() {
39
- @Override
40
- public void onSpanStart(Span span) {
41
- BugsnagNativeSpansPlugin.this.onSpanStart(span);
42
- }
43
- });
44
-
45
- ctx.addOnSpanEndCallback(PluginContext.NORM_PRIORITY - 1, new OnSpanEndCallback() {
46
- @Override
47
- public boolean onSpanEnd(Span span) {
48
- return BugsnagNativeSpansPlugin.this.onSpanEnd(span);
49
- }
50
- });
51
- }
52
-
53
- private void onSpanStart(Span span) {
54
- spansByName.put(span.getName(), span);
55
- spansById.put(createSpanId(span), span);
56
-
57
- TimeoutExecutor timeoutExecutor = ((SpanImpl) span).getTimeoutExecutor$internal();
58
- if (timeoutExecutor != null) {
59
- timeoutExecutor.scheduleTimeout(new SpanLostTimeout(DEFAULT_VALIDITY_TIME, span));
60
- }
32
+ @Override
33
+ public void install(PluginContext ctx) {
34
+ if (INSTANCE == null) {
35
+ INSTANCE = this;
61
36
  }
62
37
 
63
- private boolean onSpanEnd(Span span) {
64
- spansByName.remove(span.getName(), span);
65
- spansById.remove(createSpanId(span));
66
- return true;
38
+ ctx.addOnSpanStartCallback(PluginContext.NORM_PRIORITY + 1, new OnSpanStartCallback() {
39
+ @Override
40
+ public void onSpanStart(Span span) {
41
+ BugsnagNativeSpansPlugin.this.onSpanStart(span);
42
+ }
43
+ });
44
+
45
+ ctx.addOnSpanEndCallback(PluginContext.NORM_PRIORITY - 1, new OnSpanEndCallback() {
46
+ @Override
47
+ public boolean onSpanEnd(Span span) {
48
+ return BugsnagNativeSpansPlugin.this.onSpanEnd(span);
49
+ }
50
+ });
51
+ }
52
+
53
+ private void onSpanStart(Span span) {
54
+ spansByName.put(span.getName(), span);
55
+ spansById.put(createSpanId(span), span);
56
+
57
+ TimeoutExecutor timeoutExecutor = ((SpanImpl) span).getTimeoutExecutor$internal();
58
+ if (timeoutExecutor != null) {
59
+ timeoutExecutor.scheduleTimeout(new SpanLostTimeout(DEFAULT_VALIDITY_TIME, span));
67
60
  }
68
-
69
- private void removeSpan(Span span) {
70
- onSpanEnd(span);
71
- }
72
-
73
- private String createSpanId(Span span) {
74
- StringBuilder id = new StringBuilder(193);
75
- EncodingUtils.appendHexUUID(id, span.getTraceId());
76
- id.append(':');
77
- EncodingUtils.appendHexLong(id, span.getSpanId());
78
- return id.toString();
61
+ }
62
+
63
+ private boolean onSpanEnd(Span span) {
64
+ spansByName.remove(span.getName(), span);
65
+ spansById.remove(createSpanId(span));
66
+ return true;
67
+ }
68
+
69
+ private void removeSpan(Span span) {
70
+ onSpanEnd(span);
71
+ }
72
+
73
+ private String createSpanId(Span span) {
74
+ StringBuilder id = new StringBuilder(193);
75
+ EncodingUtils.appendHexUUID(id, span.getTraceId());
76
+ id.append(':');
77
+ EncodingUtils.appendHexLong(id, span.getSpanId());
78
+ return id.toString();
79
+ }
80
+
81
+ @Override
82
+ public void start() {
83
+ }
84
+
85
+ Span getSpanByName(String spanName) {
86
+ return spansByName.get(spanName);
87
+ }
88
+
89
+ Span getSpanById(String traceIdHex, String spanIdHex) {
90
+ return spansById.get(traceIdHex + ':' + spanIdHex);
91
+ }
92
+
93
+ static BugsnagNativeSpansPlugin getInstance() {
94
+ return INSTANCE;
95
+ }
96
+
97
+ private class SpanLostTimeout implements Timeout {
98
+ private final long timeoutMs;
99
+ private final Span span;
100
+
101
+ public SpanLostTimeout(long timeoutMs, Span span) {
102
+ this.timeoutMs = SystemClock.elapsedRealtime() + timeoutMs;
103
+ this.span = span;
79
104
  }
80
105
 
81
106
  @Override
82
- public void start() {
107
+ public long getTarget() {
108
+ return timeoutMs;
83
109
  }
84
110
 
85
- Span getSpanByName(String spanName) {
86
- return spansByName.get(spanName);
111
+ @Override
112
+ public void run() {
113
+ removeSpan(span);
87
114
  }
88
115
 
89
- Span getSpanById(String traceIdHex, String spanIdHex) {
90
- return spansById.get(traceIdHex + ':' + spanIdHex);
116
+ @Override
117
+ public long getRelativeMs() {
118
+ return Timeout.DefaultImpls.getRelativeMs(this);
91
119
  }
92
120
 
93
- static BugsnagNativeSpansPlugin getInstance() {
94
- return INSTANCE;
121
+ @Override
122
+ public long getDelay(TimeUnit unit) {
123
+ return Timeout.DefaultImpls.getDelay(this, unit);
95
124
  }
96
125
 
97
- private class SpanLostTimeout implements Timeout {
98
- private final long timeoutMs;
99
- private final Span span;
100
-
101
- public SpanLostTimeout(long timeoutMs, Span span) {
102
- this.timeoutMs = SystemClock.elapsedRealtime() + timeoutMs;
103
- this.span = span;
104
- }
105
-
106
- @Override
107
- public long getTarget() {
108
- return timeoutMs;
109
- }
110
-
111
- @Override
112
- public void run() {
113
- removeSpan(span);
114
- }
115
-
116
- @Override
117
- public long getRelativeMs() {
118
- return Timeout.DefaultImpls.getRelativeMs(this);
119
- }
120
-
121
- @Override
122
- public long getDelay(TimeUnit unit) {
123
- return Timeout.DefaultImpls.getDelay(this, unit);
124
- }
125
-
126
- @Override
127
- public int compareTo(Delayed other) {
128
- return Timeout.DefaultImpls.compareTo(this, other);
129
- }
126
+ @Override
127
+ public int compareTo(Delayed other) {
128
+ return Timeout.DefaultImpls.compareTo(this, other);
130
129
  }
130
+ }
131
131
  }
@@ -0,0 +1,32 @@
1
+ package com.bugsnag.reactnative.performance.nativespans;
2
+
3
+ import androidx.annotation.NonNull;
4
+ import androidx.annotation.Nullable;
5
+
6
+ import android.util.SparseArray;
7
+
8
+ class CallbackState<T> {
9
+ private final SparseArray<T> callbacks = new SparseArray<>(4);
10
+ private int nextCallbackId = 0;
11
+
12
+ int registerCallback(@NonNull T callback) {
13
+ synchronized (callbacks) {
14
+ int callbackId = nextCallbackId++;
15
+ callbacks.put(callbackId, callback);
16
+ return callbackId;
17
+ }
18
+ }
19
+
20
+ T takeCallback(int callbackId) {
21
+ synchronized (callbacks) {
22
+ int index = callbacks.indexOfKey(callbackId);
23
+ if (index < 0) {
24
+ return null;
25
+ }
26
+
27
+ T callback = callbacks.valueAt(index);
28
+ callbacks.removeAt(index);
29
+ return callback;
30
+ }
31
+ }
32
+ }
@@ -0,0 +1,37 @@
1
+ package com.bugsnag.reactnative.performance.nativespans;
2
+
3
+ import androidx.annotation.NonNull;
4
+
5
+ import com.bugsnag.android.performance.controls.SpanQuery;
6
+
7
+ public final class JavascriptSpanByName implements SpanQuery<JavascriptSpanControl> {
8
+ private final String name;
9
+
10
+ public JavascriptSpanByName(@NonNull String name) {
11
+ if (name == null) {
12
+ throw new IllegalArgumentException("Span name cannot be null");
13
+ }
14
+
15
+ this.name = name;
16
+ }
17
+
18
+ public String getSpanName() {
19
+ return name;
20
+ }
21
+
22
+ @Override
23
+ public boolean equals(Object o) {
24
+ if (!(o instanceof JavascriptSpanByName)) return false;
25
+ return this.name.equals(((JavascriptSpanByName) o).name);
26
+ }
27
+
28
+ @Override
29
+ public int hashCode() {
30
+ return this.name.hashCode();
31
+ }
32
+
33
+ @Override
34
+ public String toString() {
35
+ return "JavascriptSpanByName{name='" + name + "'}";
36
+ }
37
+ }