Skip to content

Commit cb6de3a

Browse files
authored
[RN] Allow customer to change okHttpInstrumentationType in init call + updated docs (#239)
* Allow customer to change okHttpInstrumentationType in init call + updated docs * Update withAndroid.ts
1 parent 30ee7ef commit cb6de3a

File tree

3 files changed

+73
-10
lines changed

3 files changed

+73
-10
lines changed

packages/react-native/README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ Enabling this via the Objective-C API is not yet supported if initialization is
166166

167167
#### Android
168168

169-
To enable network integration in Android a Gradle plugin needs to be added to the project. This can be done by adding a dependency on the `io.bitdrift.capture-plugin` plugin in the `plugins` block of the apps's `build.gradle` file:
169+
To enable automatic OkHttp instrumentation on Android, add the `io.bitdrift.capture-plugin` plugin in the app `build.gradle` file:
170170

171171
```gradle
172172
plugins {
@@ -176,6 +176,18 @@ plugins {
176176

177177
To find the version to use, use the same version of the Capture SDK that is being used in the React Native project. Check the `build.gradle` file in the `node_modules/@bitdrift/react-native/android` directory for the version of the Capture SDK being used.
178178

179+
For manual Gradle setup, enable plugin instrumentation:
180+
181+
```gradle
182+
bitdrift {
183+
instrumentation {
184+
automaticOkHttpInstrumentation = true
185+
// Optional. Defaults to PROXY when omitted.
186+
// okHttpInstrumentationType = OVERWRITE
187+
}
188+
}
189+
```
190+
179191
In addition to this the plugin repository needs to be added to the `pluginManagement` block in the `settings.gradle` file:
180192

181193
```gradle
@@ -209,3 +221,30 @@ When using Expo to generate the project, this can be achieved by setting the `ne
209221
}
210222
}
211223
```
224+
225+
When using the Expo plugin with `networkInstrumentation: true`, the Android Gradle plugin and `bitdrift { instrumentation { ... } }` block are generated automatically.
226+
227+
The Android plugin mode can be configured with `okHttpInstrumentationType`:
228+
229+
```json
230+
{
231+
"expo": {
232+
"plugins": [
233+
[
234+
"@bitdrift/react-native",
235+
{
236+
"networkInstrumentation": true,
237+
"okHttpInstrumentationType": "OVERWRITE"
238+
}
239+
]
240+
]
241+
}
242+
}
243+
```
244+
245+
`okHttpInstrumentationType` is Android-only and has no effect on iOS.
246+
247+
- `PROXY` (default): preserves existing `EventListener.Factory` behavior in OkHttp clients.
248+
- `OVERWRITE`: replaces the listener factory and can avoid duplicate network spans in some app setups.
249+
250+
If you recently upgraded and started seeing duplicate spans on Android, set `okHttpInstrumentationType` to `OVERWRITE`.

packages/react-native/src/plugin/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77

88
export default interface PluginProps {
99
networkInstrumentation?: boolean;
10+
// Android-only. Only used when networkInstrumentation is true.
11+
okHttpInstrumentationType?: 'PROXY' | 'OVERWRITE';
1012
}

packages/react-native/src/plugin/withAndroid.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,46 @@ import { withAppBuildGradle, withSettingsGradle } from '@expo/config-plugins';
1010
import type { ConfigPlugin } from '@expo/config-plugins';
1111
import PluginProps from './config';
1212

13+
const CAPTURE_PLUGIN_ID = "id 'io.bitdrift.capture-plugin'";
14+
const BITDRIFT_MAVEN_HOST = 'dl.bitdrift.io';
15+
1316
const withBitdriftAppBuildGradle: ConfigPlugin<PluginProps | void> = (
1417
config,
1518
props,
1619
) => {
1720
return withAppBuildGradle(config, (config) => {
18-
if (!config.modResults.contents.includes('dl.bitdrift.io')) {
19-
// TODO(snowp): Eventually we'd always install the plugin and gate the okhttp instrumentation
20-
// config on the networkInstrumentation prop.
21-
if (props && props.networkInstrumentation) {
22-
// Add the capture-plugin at the very top of the file.
23-
config.modResults.contents =
24-
`plugins {
21+
const shouldEnableNetworkInstrumentation = props?.networkInstrumentation === true;
22+
const okHttpInstrumentationType =
23+
props?.okHttpInstrumentationType === 'OVERWRITE' ? 'OVERWRITE' : 'PROXY';
24+
25+
if (
26+
shouldEnableNetworkInstrumentation &&
27+
!config.modResults.contents.includes(CAPTURE_PLUGIN_ID)
28+
) {
29+
config.modResults.contents =
30+
`plugins {
2531
id 'io.bitdrift.capture-plugin' version '0.22.12'
2632
}
2733
2834
` + config.modResults.contents;
29-
}
35+
}
36+
37+
if (
38+
shouldEnableNetworkInstrumentation &&
39+
!config.modResults.contents.includes('automaticOkHttpInstrumentation')
40+
) {
41+
config.modResults.contents += `
42+
43+
bitdrift {
44+
instrumentation {
45+
automaticOkHttpInstrumentation = true
46+
okHttpInstrumentationType = ${okHttpInstrumentationType}
47+
}
48+
}
49+
`;
50+
}
3051

52+
if (!config.modResults.contents.includes(BITDRIFT_MAVEN_HOST)) {
3153
// Define the bitdrift maven repository at the end. This is necessary to resolve the SDK dependency specified by the plugin.
3254
config.modResults.contents += `
3355
@@ -52,7 +74,7 @@ const withBitdriftSettingsGradle: ConfigPlugin<PluginProps | void> = (
5274
) => {
5375
// Add the bitdrift maven repository to the pluginManagement block to allow the plugin to resolve the SDK dependency. This is safe to do regardless of whether the network instrumentation is enabled or not.
5476
return withSettingsGradle(config, (config) => {
55-
if (!config.modResults.contents.includes('dl.bitdrift.io')) {
77+
if (!config.modResults.contents.includes(BITDRIFT_MAVEN_HOST)) {
5678
// There will be a pluginManagement block in the settings.gradle file already, so we need to insert ourselves
5779
// into the existing block.
5880
//

0 commit comments

Comments
 (0)