Skip to content

Commit 5df0c03

Browse files
committed
feat: ❇️ added sanitize feature in session recording android
1 parent 9ef06c3 commit 5df0c03

9 files changed

+63
-13
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,15 @@ MiddlewareRum.updateLocation(latitude: number, longitude: number)
116116

117117
### Enable session recording
118118

119-
To enable session recording pass `sessionRecording: true` configuration as follows -
119+
By default session recording is enabled, to disable session recording pass `sessionRecording: false` configuration as follows -
120120

121121
```js
122122
const MiddlewareConfig: ReactNativeConfiguration = {
123123
serviceName: 'Mobile-SDK-ReactNative',
124124
projectName: '$Mobile-SDK-ReactNative',
125125
accountKey: '<middleware-account-key>',
126126
target: '<target-url>',
127-
sessionRecording: true,
127+
sessionRecording: false,
128128
deploymentEnvironment: 'PROD',
129129
globalAttributes: {
130130
name: '<your-name>',

android/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ dependencies {
8282
// For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
8383
//noinspection GradleDynamicVersion
8484
implementation "com.facebook.react:react-native:+"
85-
implementation 'io.github.middleware-labs:android-sdk:1.0.8'
85+
implementation 'io.github.middleware-labs:android-sdk:2.0.1'
8686
implementation 'io.opentelemetry.android:instrumentation:+'
8787
implementation 'io.opentelemetry:opentelemetry-sdk:+'
8888
implementation 'io.opentelemetry.semconv:opentelemetry-semconv:+'

android/src/main/java/com/middlewarereactnative/ConfigMapReader.java

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public String getServiceName() {
2626
return Keys.SERVICE_NAME.get(map);
2727
}
2828

29+
public String getSessionRecording() {
30+
return Keys.SESSION_RECORDING.get(map);
31+
}
32+
2933
public String getDeploymentEnvironment() {
3034
return Keys.DEPLOYMENT_ENVIRONMENT.get(map);
3135
}
@@ -39,6 +43,7 @@ private interface Keys {
3943
StringKey ACCOUNT_KEY = new StringKey("accountKey");
4044
StringKey PROJECT_NAME = new StringKey("projectName");
4145
StringKey SERVICE_NAME = new StringKey("serviceName");
46+
StringKey SESSION_RECORDING = new StringKey("sessionRecording");
4247
StringKey DEPLOYMENT_ENVIRONMENT = new StringKey("deploymentEnvironment");
4348
MapKey GLOBAL_ATTRIBUTES = new MapKey("globalAttributes");
4449
}

android/src/main/java/com/middlewarereactnative/MiddlewareReactNativeModule.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public void initialize(ReadableMap configMap, Promise promise) {
6363
final String accountKey = mapReader.getAccountKey();
6464
final String projectName = mapReader.getProjectName();
6565
final String serviceName = mapReader.getServiceName();
66+
final String sessionRecording = mapReader.getSessionRecording();
6667
final String deploymentEnvironment = mapReader.getDeploymentEnvironment();
6768
final ReadableMap globalAttributes = mapReader.getGlobalAttributes();
6869

@@ -79,7 +80,12 @@ public void initialize(ReadableMap configMap, Promise promise) {
7980
.setGlobalAttributes(attributesFromMap(globalAttributes))
8081
.setDeploymentEnvironment(deploymentEnvironment)
8182
.disableActivityLifecycleMonitoring()
82-
.build((Application) getReactApplicationContext().getApplicationContext().getApplicationContext());
83+
.build(Objects.requireNonNull(getReactApplicationContext().getCurrentActivity()).getApplication());
84+
85+
if("true".equals(sessionRecording)) {
86+
Middleware middleware = Middleware.getInstance();
87+
middleware.startNativeRecording(getCurrentActivity());
88+
}
8389

8490
middlewareSpanExporter = Middleware.getInstance().getMiddlewareRum().getSpanExporter();
8591
WritableMap appStartInfo = Arguments.createMap();
@@ -162,7 +168,9 @@ public void export(ReadableArray spanMaps, Promise promise) {
162168

163169
@ReactMethod
164170
public void setSessionId(String sessionId) {
165-
Middleware.getInstance().setGlobalAttribute(AttributeKey.stringKey("session.id"), sessionId);
171+
Middleware middleware = Middleware.getInstance();
172+
middleware.setGlobalAttribute(AttributeKey.stringKey("session.id"), sessionId);
173+
middleware.setNativeSessionId(sessionId);
166174
this.nativeSessionId = sessionId;
167175
}
168176

android/src/main/java/com/middlewarereactnative/MiddlewareReactNativePackage.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import com.facebook.react.uimanager.ViewManager;
99

1010
import java.util.ArrayList;
11-
import java.util.Collections;
1211
import java.util.List;
1312

1413
public class MiddlewareReactNativePackage implements ReactPackage {
@@ -23,6 +22,6 @@ public List<NativeModule> createNativeModules(@NonNull ReactApplicationContext r
2322
@NonNull
2423
@Override
2524
public List<ViewManager> createViewManagers(@NonNull ReactApplicationContext reactContext) {
26-
return Collections.emptyList();
25+
return List.of(new NativeSanitizedViewManager());
2726
}
2827
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.middlewarereactnative;
2+
3+
import android.content.Context;
4+
import android.view.ViewGroup;
5+
6+
import io.middleware.android.sdk.Middleware;
7+
8+
public class NativeSanitizedView extends ViewGroup {
9+
public NativeSanitizedView(Context context) {
10+
super(context);
11+
Middleware.getInstance().addSanitizedElement(this);
12+
}
13+
14+
@Override
15+
protected void onLayout(boolean changed, int l, int t, int r, int b) {
16+
17+
}
18+
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.middlewarereactnative;
2+
3+
import android.view.ViewGroup;
4+
5+
import androidx.annotation.NonNull;
6+
7+
import com.facebook.react.uimanager.ThemedReactContext;
8+
import com.facebook.react.uimanager.ViewGroupManager;
9+
10+
public class NativeSanitizedViewManager extends ViewGroupManager<ViewGroup> {
11+
@NonNull
12+
@Override
13+
public String getName() {
14+
return "NativeSanitizedView";
15+
}
16+
17+
@NonNull
18+
@Override
19+
protected ViewGroup createViewInstance(@NonNull ThemedReactContext themedReactContext) {
20+
return new NativeSanitizedView(themedReactContext);
21+
}
22+
}

src/MiddlewareWrapper.tsx

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import {
2-
Platform,
32
UIManager,
4-
View,
53
requireNativeComponent,
64
type ViewProps,
75
} from 'react-native';
@@ -41,5 +39,4 @@ const NativeSanitizedView =
4139
throw new Error('NativeSanitizedView; ' + LINKING_ERROR);
4240
};
4341

44-
export const MiddlewareSanitizedView =
45-
Platform.OS === 'ios' ? NativeSanitizedView : View;
42+
export const MiddlewareSanitizedView = NativeSanitizedView;

src/middlewareRum.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export interface ReactNativeConfiguration {
3838
accountKey: string;
3939
serviceName: string;
4040
projectName: string;
41-
sessionRecording: boolean;
41+
sessionRecording?: boolean;
4242
deploymentEnvironment?: string;
4343
appStartEnabled?: boolean;
4444
enableDiskBuffering?: boolean;
@@ -143,7 +143,7 @@ export const MiddlewareRum: MiddlewareRumType = {
143143
accountKey: config.accountKey,
144144
serviceName: config.serviceName,
145145
projectName: config.projectName,
146-
sessionRecording: String(config.sessionRecording),
146+
sessionRecording: String(config.sessionRecording || true),
147147
globalAttributes: {
148148
...getResource(),
149149
[SemanticResourceAttributes.SERVICE_NAME]: config.serviceName,

0 commit comments

Comments
 (0)