Skip to content

Commit 567f9fc

Browse files
feat: support app flows APIs (#1138)
1 parent 057909f commit 567f9fc

File tree

14 files changed

+357
-22
lines changed

14 files changed

+357
-22
lines changed

.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/** @type {import('eslint').ESLint.ConfigData} */
22
module.exports = {
33
extends: '@react-native-community',
4-
plugins: ['prettier', 'jest'],
4+
plugins: ['prettier', 'jest', 'jsdoc'],
55
overrides: [
66
{
77
// Jest Overrides
@@ -27,6 +27,7 @@ module.exports = {
2727
},
2828
],
2929
rules: {
30+
'jsdoc/no-undefined-types': 'warn',
3031
'prettier/prettier': 'error',
3132
'prefer-const': 'error',
3233
},

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717

1818
- Adds symbol files upload script ([#1137](https://github.com/Instabug/Instabug-React-Native/pull/1137))
1919
- Support enabling NDK crash capturing on Android ([#1132](https://github.com/Instabug/Instabug-React-Native/pull/1132)).
20+
- Add support for App Flows APIs `APM.startFlow`, `APM.setFlowAttribute` and `APM.endFlow` ([#1138](https://github.com/Instabug/Instabug-React-Native/pull/1138)).
21+
22+
### Deprecated
23+
24+
- Deprecate Execution Traces APIs `APM.startExecutionTrace`, `Trace.end` and `Trace.setAttribute` in favor of the new App Flows APIs ([#1138](https://github.com/Instabug/Instabug-React-Native/pull/1138)).
2025

2126
### Changed
2227

android/src/main/java/com/instabug/reactlibrary/RNInstabugAPMModule.java

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.os.SystemClock;
55
import android.util.Log;
66

7+
import androidx.annotation.NonNull;
78
import com.facebook.react.bridge.Promise;
89
import com.facebook.react.bridge.ReactApplicationContext;
910
import com.facebook.react.bridge.ReactContextBaseJavaModule;
@@ -29,6 +30,8 @@ public class RNInstabugAPMModule extends ReactContextBaseJavaModule {
2930
public RNInstabugAPMModule(ReactApplicationContext reactApplicationContext) {
3031
super(reactApplicationContext);
3132
}
33+
34+
@Deprecated
3235
HashMap<String, ExecutionTrace> traces = new HashMap<String, ExecutionTrace>();
3336

3437
@Nonnull
@@ -121,10 +124,92 @@ public void run() {
121124
});
122125
}
123126

127+
/**
128+
* Starts an AppFlow with the specified name.
129+
* <br/>
130+
* On starting two flows with the same name the older flow will end with force abandon end reason.
131+
* AppFlow name cannot exceed 150 characters otherwise it's truncated,
132+
* leading and trailing whitespaces are also ignored.
133+
*
134+
* @param name AppFlow name. It can not be empty string or null.
135+
* Starts a new AppFlow, if APM is enabled, feature is enabled
136+
* and Instabug SDK is initialised.
137+
*/
138+
@ReactMethod
139+
public void startFlow(@NonNull final String name) {
140+
MainThreadHandler.runOnMainThread(new Runnable() {
141+
@Override
142+
public void run() {
143+
try {
144+
APM.startFlow(name);
145+
} catch (Exception e) {
146+
e.printStackTrace();
147+
}
148+
}
149+
});
150+
}
151+
152+
/**
153+
* Sets custom attributes for AppFlow with a given name.
154+
* <br/>
155+
* Setting an attribute value to null will remove its corresponding key if it already exists.
156+
* <br/>
157+
* Attribute key name cannot exceed 30 characters.
158+
* Leading and trailing whitespaces are also ignored.
159+
* Does not accept empty strings or null.
160+
* <br/>
161+
* Attribute value name cannot exceed 60 characters,
162+
* leading and trailing whitespaces are also ignored.
163+
* Does not accept empty strings.
164+
* <br/>
165+
* If a trace is ended, attributes will not be added and existing ones will not be updated.
166+
* <br/>
167+
*
168+
* @param name AppFlow name. It can not be empty string or null
169+
* @param key AppFlow attribute key. It can not be empty string or null
170+
* @param value AppFlow attribute value. It can not be empty string. Null to remove attribute
171+
*/
172+
@ReactMethod
173+
public void setFlowAttribute(@NonNull final String name, @NonNull final String key, final String value) {
174+
MainThreadHandler.runOnMainThread(new Runnable() {
175+
@Override
176+
public void run() {
177+
try {
178+
APM.setFlowAttribute(name, key, value);
179+
} catch (Exception e) {
180+
e.printStackTrace();
181+
}
182+
}
183+
});
184+
}
185+
186+
/**
187+
* Ends AppFlow with a given name.
188+
*
189+
* @param name AppFlow name to be ended. It can not be empty string or null
190+
*/
191+
@ReactMethod
192+
public void endFlow(@NonNull final String name) {
193+
MainThreadHandler.runOnMainThread(new Runnable() {
194+
@Override
195+
public void run() {
196+
try {
197+
APM.endFlow(name);
198+
} catch (Exception e) {
199+
e.printStackTrace();
200+
}
201+
}
202+
});
203+
}
204+
124205
/**
125206
* Starts an execution trace
207+
*
126208
* @param name string name of the trace.
209+
*
210+
* @deprecated see {@link #startFlow(String)}
127211
*/
212+
@Deprecated
128213
@ReactMethod
129214
public void startExecutionTrace(final String name, final String id, final Promise promise) {
130215
MainThreadHandler.runOnMainThread(new Runnable() {
@@ -148,10 +233,14 @@ public void run() {
148233

149234
/**
150235
* Adds a new attribute to trace
151-
* @param id String id of the trace.
236+
*
237+
* @param id String id of the trace.
152238
* @param key attribute key
153239
* @param value attribute value. Null to remove attribute
240+
*
241+
* @deprecated see {@link #setFlowAttribute}
154242
*/
243+
@Deprecated
155244
@ReactMethod
156245
public void setExecutionTraceAttribute(final String id, final String key, final String value) {
157246
MainThreadHandler.runOnMainThread(new Runnable() {
@@ -168,8 +257,12 @@ public void run() {
168257

169258
/**
170259
* Ends a trace
260+
*
171261
* @param id string id of the trace.
262+
*
263+
* @deprecated see {@link #endFlow}
172264
*/
265+
@Deprecated
173266
@ReactMethod
174267
public void endExecutionTrace(final String id) {
175268
MainThreadHandler.runOnMainThread(new Runnable() {

android/src/test/java/com/instabug/reactlibrary/RNInstabugAPMModuleTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,37 @@ public void givenTruesetEnabled_whenQuery_thenShouldCallNativeApiWithEnabled() {
125125
verify(promise).resolve(any());
126126
}
127127

128+
@Test
129+
public void testStartFlow() {
130+
String appFlowName = "appFlowName";
131+
132+
apmModule.startFlow(appFlowName);
133+
134+
mockAPM.verify(() -> APM.startFlow(appFlowName));
135+
mockAPM.verifyNoMoreInteractions();
136+
}
137+
138+
@Test
139+
public void testEndFlow() {
140+
String appFlowName = "appFlowName";
141+
142+
apmModule.endFlow(appFlowName);
143+
144+
mockAPM.verify(() -> APM.endFlow(appFlowName));
145+
mockAPM.verifyNoMoreInteractions();
146+
}
147+
148+
@Test
149+
public void testSetFlowAttribute() {
150+
String appFlowName = "appFlowName";
151+
String flowAttributeKey = "attributeKey";
152+
String flowAttributeValue = "attributeValue";
153+
apmModule.setFlowAttribute(appFlowName, flowAttributeKey, flowAttributeValue);
154+
155+
mockAPM.verify(() -> APM.setFlowAttribute(appFlowName, flowAttributeKey, flowAttributeValue));
156+
mockAPM.verifyNoMoreInteractions();
157+
}
158+
128159
// @Test
129160
// public void givenString$setExecutionTraceAttribute_whenQuery_thenShouldCallNativeApiWithIntArgs() {
130161
// // given

examples/default/ios/InstabugTests/InstabugAPMTests.m

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ - (void) testSetAPMEnabled {
6262
- (void) testSetAppLaunchEnabled {
6363
id mock = OCMClassMock([IBGAPM class]);
6464
BOOL isEnabled = YES;
65-
65+
6666
OCMStub([mock setColdAppLaunchEnabled:isEnabled]);
6767
[self.instabugBridge setAppLaunchEnabled:isEnabled];
6868
OCMVerify([mock setColdAppLaunchEnabled:isEnabled]);
6969
}
7070

7171
- (void) testEndAppLaunch {
7272
id mock = OCMClassMock([IBGAPM class]);
73-
73+
7474
OCMStub([mock endAppLaunch]);
7575
[self.instabugBridge endAppLaunch];
7676
OCMVerify([mock endAppLaunch]);
@@ -79,7 +79,7 @@ - (void) testEndAppLaunch {
7979
- (void) testSetAutoUITraceEnabled {
8080
id mock = OCMClassMock([IBGAPM class]);
8181
BOOL isEnabled = YES;
82-
82+
8383
OCMStub([mock setAutoUITraceEnabled:isEnabled]);
8484
[self.instabugBridge setAutoUITraceEnabled:isEnabled];
8585
OCMVerify([mock setAutoUITraceEnabled:isEnabled]);
@@ -91,7 +91,7 @@ - (void) testStartExecutionTrace {
9191
NSString* traceKey = @"1";
9292
RCTPromiseResolveBlock resolve = ^(id result) {};
9393
RCTPromiseRejectBlock reject = ^(NSString *code, NSString *message, NSError *error) {};
94-
94+
9595
OCMStub([mock startExecutionTraceWithName:traceName]);
9696
[self.instabugBridge startExecutionTrace:traceName :traceKey :resolve :reject];
9797
OCMVerify([mock startExecutionTraceWithName:traceName]);
@@ -107,10 +107,10 @@ - (void) testSetExecutionTraceAttribute {
107107
IBGExecutionTrace * trace = [IBGExecutionTrace alloc];
108108
id mock = OCMClassMock([IBGAPM class]);
109109
id traceMock = OCMPartialMock(trace);
110-
110+
111111
OCMStub([mock startExecutionTraceWithName:traceName]).andReturn(trace);
112112
[self.instabugBridge startExecutionTrace:traceName :traceId :resolve :reject];
113-
113+
114114
OCMStub([traceMock setAttributeWithKey:traceKey value:traceValue]);
115115
[self.instabugBridge setExecutionTraceAttribute:traceId :traceKey :traceValue];
116116
OCMVerify([traceMock setAttributeWithKey:traceKey value:traceValue]);
@@ -127,24 +127,50 @@ - (void) testEndExecutionTrace {
127127

128128
OCMStub([apmMock startExecutionTraceWithName:traceName]).andReturn(trace);
129129
[self.instabugBridge startExecutionTrace:traceName :traceId :resolve :reject];
130-
130+
131131
OCMStub([traceMock end]);
132132
[self.instabugBridge endExecutionTrace:traceId];
133133
OCMVerify([traceMock end]);
134134
}
135135

136+
- (void) testStartFlow {
137+
id mock = OCMClassMock([IBGAPM class]);
138+
NSString* appFlowName = @"APP_Flow_1";
139+
140+
[self.instabugBridge startFlow:appFlowName];
141+
OCMVerify([mock startFlowWithName:appFlowName]);
142+
}
143+
144+
- (void) testEndFlow {
145+
id mock = OCMClassMock([IBGAPM class]);
146+
NSString* appFlowName = @"APP_Flow_1";
147+
148+
[self.instabugBridge endFlow:appFlowName];
149+
OCMVerify([mock endFlowWithName:appFlowName]);
150+
}
151+
152+
- (void) testSetFlowAttribute {
153+
id mock = OCMClassMock([IBGAPM class]);
154+
NSString* appFlowName = @"APP_Flow_1";
155+
NSString* attributeKey = @"Attribute_Key_1";
156+
NSString* attributeValue = @"Attribute_Value_1";
157+
158+
[self.instabugBridge setFlowAttribute:appFlowName :attributeKey :attributeValue];
159+
OCMVerify([mock setAttributeForFlowWithName:appFlowName key:attributeKey value:attributeValue]);
160+
}
161+
136162
- (void) testStartUITrace {
137163
id mock = OCMClassMock([IBGAPM class]);
138164
NSString* traceName = @"UITrace_1";
139-
165+
140166
OCMStub([mock startUITraceWithName:traceName]);
141167
[self.instabugBridge startUITrace:traceName];
142168
OCMVerify([mock startUITraceWithName:traceName]);
143169
}
144170

145171
- (void) testEndUITrace {
146172
id mock = OCMClassMock([IBGAPM class]);
147-
173+
148174
OCMStub([mock endUITrace]);
149175
[self.instabugBridge endUITrace];
150176
OCMVerify([mock endUITrace]);

ios/RNInstabug/InstabugAPMBridge.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717
- (void)setAutoUITraceEnabled:(BOOL)isEnabled;
1818
- (void)startExecutionTrace:(NSString *)name :(NSString *)id
1919
:(RCTPromiseResolveBlock)resolve
20-
:(RCTPromiseRejectBlock)reject;
20+
:(RCTPromiseRejectBlock)reject DEPRECATED_MSG_ATTRIBUTE("Please use APM.startFlow instead.");
2121
- (void)setExecutionTraceAttribute:(NSString *)id:(NSString *)key
22-
:(NSString *)value;
23-
- (void)endExecutionTrace:(NSString *)id;
22+
:(NSString *)value DEPRECATED_MSG_ATTRIBUTE("Please use APM.setTraceAttribute instead.");
23+
- (void)endExecutionTrace:(NSString *)id DEPRECATED_MSG_ATTRIBUTE("Please use APM.endFlow instead.");
24+
- (void)startFlow:(NSString *)name;
25+
- (void)endFlow:(NSString *)name;
26+
- (void)setFlowAttribute:(NSString *)name :(NSString *)key :(NSString *_Nullable)value;
2427
- (void)startUITrace:(NSString *)name;
2528
- (void)endUITrace;
2629

ios/RNInstabug/InstabugAPMBridge.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,19 @@ - (id) init
8888
}
8989
}
9090

91+
RCT_EXPORT_METHOD(startFlow: (NSString *)name) {
92+
[IBGAPM startFlowWithName:name];
93+
}
94+
95+
RCT_EXPORT_METHOD(endFlow: (NSString *)name) {
96+
[IBGAPM endFlowWithName:name];
97+
}
98+
99+
100+
RCT_EXPORT_METHOD(setFlowAttribute:(NSString *)name :(NSString *)key :(NSString *_Nullable)value) {
101+
[IBGAPM setAttributeForFlowWithName:name key:key value:value];
102+
}
103+
91104
RCT_EXPORT_METHOD(startUITrace:(NSString *)name) {
92105
[IBGAPM startUITraceWithName:name];
93106
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"commander": "^11.0.0",
6161
"danger": "^11.2.5",
6262
"eslint": "^8.24.0",
63+
"eslint-plugin-jsdoc": "^48.1.0",
6364
"eslint-plugin-prettier": "^5.0.0",
6465
"esprima": "^4.0.1",
6566
"form-data": "^4.0.0",

src/models/Trace.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { NativeAPM } from '../native/NativeAPM';
2+
import type * as APM from '../modules/APM';
23

34
export default class Trace {
45
constructor(
@@ -8,18 +9,24 @@ export default class Trace {
89
) {}
910

1011
/**
11-
* Add an attribute with key and value to the Trace to be sent.
12-
* @param key
13-
* @param value
12+
* Adds an attribute with a specified key and value to the Trace to be sent.
13+
*
14+
* @param key - The key of the attribute.
15+
* @param value - The value of the attribute.
16+
*
17+
* @deprecated Please migrate to the App Flows APIs: {@link APM.startFlow}, {@link APM.endFlow}, and {@link APM.setFlowAttribute}.
1418
*/
1519
setAttribute(key: string, value: string) {
1620
NativeAPM.setExecutionTraceAttribute(this.id, key, value);
1721
this.attributes[key] = value;
1822
}
1923

2024
/**
21-
* End Execution Trace
25+
* Ends the execution trace.
26+
*
27+
* @deprecated Please migrate to the App Flows APIs: {@link APM.startFlow}, {@link APM.endFlow}, and {@link APM.setFlowAttribute}.
2228
*/
29+
2330
end() {
2431
NativeAPM.endExecutionTrace(this.id);
2532
}

0 commit comments

Comments
 (0)