Skip to content

feat: exclude DEV server from network logs #1307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased](https://github.com/Instabug/Instabug-React-Native/compare/v13.4.0...dev)

### Added

- Exclude DEV server from network logs ([#1307](https://github.com/Instabug/Instabug-React-Native/pull/1307)).

### Fixed

- Replace thrown errors with logs ([#1220](https://github.com/Instabug/Instabug-React-Native/pull/1220))
Expand Down
26 changes: 25 additions & 1 deletion src/modules/NetworkLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,30 @@ import type { RequestHandler } from '@apollo/client';

import InstabugConstants from '../utils/InstabugConstants';
import xhr, { NetworkData, ProgressCallback } from '../utils/XhrNetworkInterceptor';
import { reportNetworkLog, isContentTypeNotAllowed } from '../utils/InstabugUtils';
import { isContentTypeNotAllowed, reportNetworkLog } from '../utils/InstabugUtils';
import { NativeModules } from 'react-native';

export type { NetworkData };

export type NetworkDataObfuscationHandler = (data: NetworkData) => Promise<NetworkData>;
let _networkDataObfuscationHandler: NetworkDataObfuscationHandler | null | undefined;
let _requestFilterExpression = 'false';

const filterURL = (url: string): string => {
const [protocol, rest] = url.split('://');
return (rest || protocol).split('/')[0];
};

const getDevServerURL = (): string | null => {
if (NativeModules.SourceCode) {
const { scriptURL } = NativeModules.SourceCode;
if (scriptURL) {
return filterURL(scriptURL.toString());
}
}
return null;
};

/**
* Sets whether network logs should be sent with bug reports.
* It is enabled by default.
Expand All @@ -27,6 +43,14 @@ export const setEnabled = (isEnabled: boolean) => {
network = await _networkDataObfuscationHandler(network);
}

if (__DEV__) {
const devServerURL = getDevServerURL();
const networkServerURL = filterURL(network.url);
// Skip logging for requests to the dev server
if (devServerURL === networkServerURL) {
return;
}
}
if (network.requestBodySize > InstabugConstants.MAX_NETWORK_BODY_SIZE_IN_BYTES) {
network.requestBody = InstabugConstants.MAX_REQUEST_BODY_SIZE_EXCEEDED_MESSAGE;
console.warn('IBG-RN:', InstabugConstants.MAX_REQUEST_BODY_SIZE_EXCEEDED_MESSAGE);
Expand Down