Skip to content

Commit 5f42520

Browse files
Merge pull request #1306 from Instabug/feat/network-spans-filtering
Feat/network spans filtering
2 parents e4ab074 + bb0df23 commit 5f42520

25 files changed

+1041
-217
lines changed

android/jacoco.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ task jacocoTestReport(type: JacocoReport) {
3737
'**/*Binding.*'
3838
]
3939

40-
def jClasses = "${project.buildDir}/intermediates/javac/debug/classes"
40+
def jClasses = "${project.buildDir}/intermediates/javac/debug/compileDebugJavaWithJavac/classes"
4141
def javaClasses = fileTree(dir: jClasses, excludes: excludes)
4242
def sourceDirs = ["${project.projectDir}/src/main/java"]
4343
classDirectories.from = files(javaClasses)

android/native.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
project.ext.instabug = [
2-
version: '14.0.0.6273738-SNAPSHOT'
2+
version: '14.0.0.6273368-SNAPSHOT'
33
]
44

55
dependencies {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ final class Constants {
99

1010
final static String IBG_ON_NEW_MESSAGE_HANDLER = "IBGonNewMessageHandler";
1111
final static String IBG_ON_NEW_REPLY_RECEIVED_CALLBACK = "IBGOnNewReplyReceivedCallback";
12+
13+
final static String IBG_ON_FEATURES_UPDATED_CALLBACK = "IBGOnFeatureUpdatedCallback";
14+
final static String IBG_NETWORK_LOGGER_HANDLER = "IBGNetworkLoggerHandler";
1215
}

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

Lines changed: 113 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,42 @@
33

44
import static com.instabug.apm.configuration.cp.APMFeature.APM_NETWORK_PLUGIN_INSTALLED;
55
import static com.instabug.apm.configuration.cp.APMFeature.CP_NATIVE_INTERCEPTION_ENABLED;
6-
import static com.instabug.apm.configuration.cp.APMFeature.NETWORK_INTERCEPTION_ENABLED;
6+
7+
import android.util.Log;
78

89
import androidx.annotation.NonNull;
910

11+
import com.facebook.react.bridge.Arguments;
1012
import com.facebook.react.bridge.Promise;
1113
import com.facebook.react.bridge.ReactApplicationContext;
1214
import com.facebook.react.bridge.ReactMethod;
15+
import com.facebook.react.bridge.ReadableMap;
16+
import com.facebook.react.bridge.ReadableMapKeySetIterator;
17+
import com.facebook.react.bridge.WritableMap;
18+
import com.facebook.react.bridge.WritableNativeMap;
1319
import com.instabug.apm.InternalAPM;
20+
import com.instabug.apm.sanitization.OnCompleteCallback;
21+
import com.instabug.library.logging.listeners.networklogs.NetworkLogSnapshot;
1422
import com.instabug.reactlibrary.utils.EventEmitterModule;
1523
import com.instabug.reactlibrary.utils.MainThreadHandler;
1624

25+
import org.json.JSONException;
26+
import org.json.JSONObject;
27+
28+
import java.util.HashMap;
29+
import java.util.Map;
30+
import java.util.concurrent.ConcurrentHashMap;
31+
1732

1833
public class RNInstabugNetworkLoggerModule extends EventEmitterModule {
1934

35+
public final ConcurrentHashMap<String, OnCompleteCallback<NetworkLogSnapshot>> callbackMap = new ConcurrentHashMap<String, OnCompleteCallback<NetworkLogSnapshot>>();
36+
2037
public RNInstabugNetworkLoggerModule(ReactApplicationContext reactContext) {
2138
super(reactContext);
2239
}
2340

41+
2442
@NonNull
2543
@Override
2644
public String getName() {
@@ -39,7 +57,28 @@ public void removeListeners(Integer count) {
3957
}
4058

4159
private boolean getFlagValue(String key) {
42-
return InternalAPM._isFeatureEnabledCP( key , "");
60+
return InternalAPM._isFeatureEnabledCP(key, "");
61+
}
62+
63+
private WritableMap convertFromMapToWritableMap(Map<String, Object> map) {
64+
WritableMap writableMap = new WritableNativeMap();
65+
for (String key : map.keySet()) {
66+
Object value = map.get(key);
67+
writableMap.putString(key, (String) value);
68+
}
69+
return writableMap;
70+
}
71+
72+
private Map<String, Object> convertReadableMapToMap(ReadableMap readableMap) {
73+
Map<String, Object> map = new HashMap<>();
74+
if (readableMap != null) {
75+
ReadableMapKeySetIterator iterator = readableMap.keySetIterator();
76+
while (iterator.hasNextKey()) {
77+
String key = iterator.nextKey();
78+
map.put(key, readableMap.getString(key));
79+
}
80+
}
81+
return map;
4382
}
4483

4584
/**
@@ -81,4 +120,76 @@ public void run() {
81120
}
82121
});
83122
}
123+
124+
125+
@ReactMethod
126+
public void registerNetworkLogsListener() {
127+
MainThreadHandler.runOnMainThread(new Runnable() {
128+
@Override
129+
public void run() {
130+
InternalAPM._registerNetworkLogSanitizer((networkLogSnapshot, onCompleteCallback) -> {
131+
final String id = String.valueOf(onCompleteCallback.hashCode());
132+
callbackMap.put(id, onCompleteCallback);
133+
134+
WritableMap networkSnapshotParams = Arguments.createMap();
135+
networkSnapshotParams.putString("id", id);
136+
networkSnapshotParams.putString("url", networkLogSnapshot.getUrl());
137+
networkSnapshotParams.putInt("responseCode", networkLogSnapshot.getResponseCode());
138+
networkSnapshotParams.putString("requestBody", networkLogSnapshot.getRequestBody());
139+
networkSnapshotParams.putString("response", networkLogSnapshot.getResponse());
140+
final Map<String, Object> requestHeaders = networkLogSnapshot.getRequestHeaders();
141+
if (requestHeaders != null) {
142+
networkSnapshotParams.putMap("requestHeader", convertFromMapToWritableMap(requestHeaders));
143+
}
144+
final Map<String, Object> responseHeaders = networkLogSnapshot.getResponseHeaders();
145+
if (responseHeaders != null) {
146+
networkSnapshotParams.putMap("responseHeader", convertFromMapToWritableMap(responseHeaders));
147+
}
148+
149+
sendEvent(Constants.IBG_NETWORK_LOGGER_HANDLER, networkSnapshotParams);
150+
});
151+
}
152+
});
153+
}
154+
155+
@ReactMethod
156+
public void resetNetworkLogsListener() {
157+
MainThreadHandler.runOnMainThread(new Runnable() {
158+
@Override
159+
public void run() {
160+
InternalAPM._registerNetworkLogSanitizer(null);
161+
}
162+
});
163+
}
164+
165+
@ReactMethod
166+
public void updateNetworkLogSnapshot(
167+
String url,
168+
String callbackID,
169+
String requestBody,
170+
String responseBody,
171+
int responseCode,
172+
ReadableMap requestHeaders,
173+
ReadableMap responseHeaders
174+
) {
175+
try {
176+
// Convert ReadableMap to a Java Map for easier handling
177+
Map<String, Object> requestHeadersMap = convertReadableMapToMap(requestHeaders);
178+
Map<String, Object> responseHeadersMap = convertReadableMapToMap(responseHeaders);
179+
180+
NetworkLogSnapshot modifiedSnapshot = null;
181+
if (!url.isEmpty()) {
182+
modifiedSnapshot = new NetworkLogSnapshot(url, requestHeadersMap, requestBody, responseHeadersMap, responseBody, responseCode);
183+
}
184+
185+
final OnCompleteCallback<NetworkLogSnapshot> callback = callbackMap.get(callbackID);
186+
if (callback != null) {
187+
callback.onComplete(modifiedSnapshot);
188+
callbackMap.remove(callbackID);
189+
}
190+
} catch (Exception e) {
191+
// Reject the promise to indicate an error occurred
192+
Log.e("IB-CP-Bridge", "InstabugNetworkLogger.updateNetworkLogSnapshot failed to parse the network snapshot object.");
193+
}
194+
}
84195
}

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

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.instabug.reactlibrary;
22

3+
import static com.instabug.apm.configuration.cp.APMFeature.APM_NETWORK_PLUGIN_INSTALLED;
4+
import static com.instabug.apm.configuration.cp.APMFeature.CP_NATIVE_INTERCEPTION_ENABLED;
35
import static com.instabug.reactlibrary.utils.InstabugUtil.getMethod;
46

57
import android.app.Application;
@@ -22,6 +24,7 @@
2224
import com.facebook.react.bridge.WritableNativeArray;
2325
import com.facebook.react.bridge.WritableNativeMap;
2426
import com.facebook.react.uimanager.UIManagerModule;
27+
import com.instabug.apm.InternalAPM;
2528
import com.instabug.library.Feature;
2629
import com.instabug.library.Instabug;
2730
import com.instabug.library.InstabugColorTheme;
@@ -31,13 +34,14 @@
3134
import com.instabug.library.ReproConfigurations;
3235
import com.instabug.library.core.InstabugCore;
3336
import com.instabug.library.featuresflags.model.IBGFeatureFlag;
37+
import com.instabug.library.internal.crossplatform.InternalCore;
38+
import com.instabug.library.internal.crossplatform.OnFeaturesUpdatedListener;
3439
import com.instabug.library.internal.module.InstabugLocale;
3540
import com.instabug.library.invocation.InstabugInvocationEvent;
3641
import com.instabug.library.logging.InstabugLog;
3742
import com.instabug.library.model.NetworkLog;
3843
import com.instabug.library.model.Report;
3944
import com.instabug.library.ui.onboarding.WelcomeMessage;
40-
import com.instabug.library.util.InstabugSDKLogger;
4145
import com.instabug.reactlibrary.utils.ArrayUtil;
4246
import com.instabug.reactlibrary.utils.EventEmitterModule;
4347
import com.instabug.reactlibrary.utils.MainThreadHandler;
@@ -104,6 +108,7 @@ public void removeListeners(Integer count) {
104108

105109
/**
106110
* Enables or disables Instabug functionality.
111+
*
107112
* @param isEnabled A boolean to enable/disable Instabug.
108113
*/
109114
@ReactMethod
@@ -112,7 +117,7 @@ public void setEnabled(final boolean isEnabled) {
112117
@Override
113118
public void run() {
114119
try {
115-
if(isEnabled)
120+
if (isEnabled)
116121
Instabug.enable();
117122
else
118123
Instabug.disable();
@@ -125,10 +130,11 @@ public void run() {
125130

126131
/**
127132
* Initializes the SDK.
128-
* @param token The token that identifies the app. You can find it on your dashboard.
133+
*
134+
* @param token The token that identifies the app. You can find it on your dashboard.
129135
* @param invocationEventValues The events that invoke the SDK's UI.
130-
* @param logLevel The level of detail in logs that you want to print.
131-
* @param codePushVersion The Code Push version to be used for all reports.
136+
* @param logLevel The level of detail in logs that you want to print.
137+
* @param codePushVersion The Code Push version to be used for all reports.
132138
*/
133139
@ReactMethod
134140
public void init(
@@ -154,8 +160,8 @@ public void run() {
154160
.setInvocationEvents(invocationEvents)
155161
.setLogLevel(parsedLogLevel);
156162

157-
if(codePushVersion != null) {
158-
if(Instabug.isBuilt()) {
163+
if (codePushVersion != null) {
164+
if (Instabug.isBuilt()) {
159165
Instabug.setCodePushVersion(codePushVersion);
160166
} else {
161167
builder.setCodePushVersion(codePushVersion);
@@ -321,7 +327,7 @@ public void run() {
321327
*
322328
* @param userEmail User's default email
323329
* @param userName Username.
324-
* @param userId User's ID
330+
* @param userId User's ID
325331
*/
326332
@ReactMethod
327333
public void identifyUser(
@@ -741,15 +747,15 @@ public void addFileAttachmentWithDataToReport(String data, String fileName) {
741747

742748
private WritableMap convertFromHashMapToWriteableMap(HashMap hashMap) {
743749
WritableMap writableMap = new WritableNativeMap();
744-
for(int i = 0; i < hashMap.size(); i++) {
750+
for (int i = 0; i < hashMap.size(); i++) {
745751
Object key = hashMap.keySet().toArray()[i];
746752
Object value = hashMap.get(key);
747-
writableMap.putString((String) key,(String) value);
753+
writableMap.putString((String) key, (String) value);
748754
}
749755
return writableMap;
750756
}
751757

752-
private static JSONObject objectToJSONObject(Object object){
758+
private static JSONObject objectToJSONObject(Object object) {
753759
Object json = null;
754760
JSONObject jsonObject = null;
755761
try {
@@ -766,13 +772,12 @@ private static JSONObject objectToJSONObject(Object object){
766772
private WritableArray convertArrayListToWritableArray(List arrayList) {
767773
WritableArray writableArray = new WritableNativeArray();
768774

769-
for(int i = 0; i < arrayList.size(); i++) {
775+
for (int i = 0; i < arrayList.size(); i++) {
770776
Object object = arrayList.get(i);
771777

772-
if(object instanceof String) {
778+
if (object instanceof String) {
773779
writableArray.pushString((String) object);
774-
}
775-
else {
780+
} else {
776781
JSONObject jsonObject = objectToJSONObject(object);
777782
writableArray.pushMap((WritableMap) jsonObject);
778783
}
@@ -828,7 +833,7 @@ public void run() {
828833
* Shows the welcome message in a specific mode.
829834
*
830835
* @param welcomeMessageMode An enum to set the welcome message mode to
831-
* live, or beta.
836+
* live, or beta.
832837
*/
833838
@ReactMethod
834839
public void showWelcomeMessageWithMode(final String welcomeMessageMode) {
@@ -850,7 +855,7 @@ public void run() {
850855
* Sets the welcome message mode to live, beta or disabled.
851856
*
852857
* @param welcomeMessageMode An enum to set the welcome message mode to
853-
* live, beta or disabled.
858+
* live, beta or disabled.
854859
*/
855860
@ReactMethod
856861
public void setWelcomeMessageMode(final String welcomeMessageMode) {
@@ -985,7 +990,6 @@ public void run() {
985990
* Reports that the screen name been changed (Current View).
986991
*
987992
* @param screenName string containing the screen name
988-
*
989993
*/
990994
@ReactMethod
991995
public void reportCurrentViewChange(final String screenName) {
@@ -1008,7 +1012,6 @@ public void run() {
10081012
* Reports that the screen has been changed (Repro Steps) the screen sent to this method will be the 'current view' on the dashboard
10091013
*
10101014
* @param screenName string containing the screen name
1011-
*
10121015
*/
10131016
@ReactMethod
10141017
public void reportScreenChange(final String screenName) {
@@ -1018,7 +1021,7 @@ public void run() {
10181021
try {
10191022
Method method = getMethod(Class.forName("com.instabug.library.Instabug"), "reportScreenChange", Bitmap.class, String.class);
10201023
if (method != null) {
1021-
method.invoke(null , null, screenName);
1024+
method.invoke(null, null, screenName);
10221025
}
10231026
} catch (Exception e) {
10241027
e.printStackTrace();
@@ -1112,7 +1115,7 @@ public void removeFeatureFlags(final ReadableArray featureFlags) {
11121115
@Override
11131116
public void run() {
11141117
try {
1115-
ArrayList<String> stringArray = ArrayUtil.parseReadableArrayOfStrings(featureFlags);
1118+
ArrayList<String> stringArray = ArrayUtil.parseReadableArrayOfStrings(featureFlags);
11161119
Instabug.removeFeatureFlag(stringArray);
11171120
} catch (Exception e) {
11181121
e.printStackTrace();
@@ -1153,7 +1156,7 @@ public void run() {
11531156
* Map between the exported JS constant and the arg key in {@link ArgsRegistry}.
11541157
* The constant name and the arg key should match to be able to resolve the
11551158
* constant with its actual value from the {@link ArgsRegistry} maps.
1156-
*
1159+
* <p>
11571160
* This is a workaround, because RN cannot resolve enums in the constants map.
11581161
*/
11591162
@Override
@@ -1167,4 +1170,20 @@ public Map<String, Object> getConstants() {
11671170

11681171
return constants;
11691172
}
1173+
1174+
@ReactMethod
1175+
public void setOnFeaturesUpdatedListener() {
1176+
InternalCore.INSTANCE._setOnFeaturesUpdatedListener(new OnFeaturesUpdatedListener() {
1177+
@Override
1178+
public void invoke() {
1179+
final boolean cpNativeInterceptionEnabled = InternalAPM._isFeatureEnabledCP(CP_NATIVE_INTERCEPTION_ENABLED, "");
1180+
final boolean hasAPMPlugin = InternalAPM._isFeatureEnabledCP(APM_NETWORK_PLUGIN_INSTALLED, "");
1181+
1182+
WritableMap params = Arguments.createMap();
1183+
params.putBoolean("cpNativeInterceptionEnabled", cpNativeInterceptionEnabled);
1184+
params.putBoolean("hasAPMPlugin", hasAPMPlugin);
1185+
sendEvent(Constants.IBG_ON_FEATURES_UPDATED_CALLBACK, params);
1186+
}
1187+
});
1188+
}
11701189
}

0 commit comments

Comments
 (0)