Skip to content
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

PXP-11183 feat dd filter bot on intake #1455

Merged
merged 4 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"brace": "^0.11.1",
"clipboard-plus": "^1.1.0",
"core-js": "^3.31.1",
"crawler-user-agents": "^1.0.115",
"css-loader": "^3.4.2",
"d3-array": "^1.2.0",
"d3-axis": "^3.0.0",
Expand Down
5 changes: 3 additions & 2 deletions src/ecosystemIndex.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import ReduxQueryNode, { submitSearchForm } from './QueryNode/ReduxQueryNode';
import {
basename, gaTrackingId, workspaceUrl, workspaceErrorUrl, Error403Url,
explorerPublic, enableResourceBrowser, resourceBrowserPublic, enableDAPTracker,
discoveryConfig, ddApplicationId, ddClientToken, ddEnv, ddUrl, ddSampleRate,
discoveryConfig, ddApplicationId, ddClientToken, ddEnv, ddUrl, ddSampleRate, ddKnownBotRegex,
} from './localconf';
import { portalVersion } from './versions';
import Analysis from './Analysis/Analysis';
Expand Down Expand Up @@ -83,14 +83,15 @@ async function init() {
// eslint-disable-next-line no-console
console.warn('Datadog clientToken is set, but applicationId is missing');
} else if (ddApplicationId && ddClientToken) {
const conditionalSampleRate = ddKnownBotRegex.test(navigator.userAgent) ? 0 : ddSampleRate;
datadogRum.init({
applicationId: ddApplicationId,
clientToken: ddClientToken,
site: ddUrl,
service: 'portal',
env: ddEnv,
version: portalVersion,
sampleRate: ddSampleRate,
sampleRate: conditionalSampleRate,
trackInteractions: true,
});
}
Expand Down
5 changes: 3 additions & 2 deletions src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import ReduxQueryNode, { submitSearchForm } from './QueryNode/ReduxQueryNode';
import {
basename, gaTrackingId, workspaceUrl, workspaceErrorUrl, Error403Url,
indexPublic, explorerPublic, enableResourceBrowser, resourceBrowserPublic, enableDAPTracker,
discoveryConfig, commonsWideAltText, ddApplicationId, ddClientToken, ddEnv, ddUrl, ddSampleRate,
discoveryConfig, commonsWideAltText, ddApplicationId, ddClientToken, ddEnv, ddUrl, ddSampleRate, ddKnownBotRegex,
} from './localconf';
import { portalVersion } from './versions';
import Analysis from './Analysis/Analysis';
Expand Down Expand Up @@ -83,14 +83,15 @@ async function init() {
// eslint-disable-next-line no-console
console.warn('Datadog clientToken is set, but applicationId is missing');
} else if (ddApplicationId && ddClientToken) {
const conditionalSampleRate = ddKnownBotRegex.test(navigator.userAgent) ? 0 : ddSampleRate;
datadogRum.init({
applicationId: ddApplicationId,
clientToken: ddClientToken,
site: ddUrl,
service: 'portal',
env: ddEnv,
version: portalVersion,
sampleRate: ddSampleRate,
sampleRate: conditionalSampleRate,
trackInteractions: true,
});
}
Expand Down
5 changes: 5 additions & 0 deletions src/localconf.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import crawlers from 'crawler-user-agents';

/* eslint-disable prefer-destructuring */
const { components, requiredCerts, config } = require('./params');

Expand Down Expand Up @@ -160,6 +162,8 @@ function buildConfig(opts) {
ddSampleRate = config.ddSampleRate;
}
}
const ddKnownBotPattern = crawlers.map((c) => c.pattern).join('|');
const ddKnownBotRegex = new RegExp(ddKnownBotPattern, 'i');
k-burt-uch marked this conversation as resolved.
Show resolved Hide resolved

// backward compatible: homepageChartNodes not set means using graphql query,
// which will return 401 UNAUTHORIZED if not logged in, thus not making public
Expand Down Expand Up @@ -589,6 +593,7 @@ function buildConfig(opts) {
ddEnv,
ddUrl,
ddSampleRate,
ddKnownBotRegex,
showSystemUse,
showSystemUseOnlyOnLogin,
Error403Url,
Expand Down
15 changes: 12 additions & 3 deletions src/localconf.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ describe('The localconf', () => {

it('Defines a bunch of configuration variables', () => {
expectedKeys.forEach((key) => {
// if (conf[key] === undefined) {
// console.log(`conf[${key}] === ${conf[key]}`);
// }
expect(conf[key]).toBeDefined();
});
});
Expand All @@ -38,4 +35,16 @@ describe('The localconf', () => {
expect(newConf[key]).toEqual(conf[key]);
});
});

it('can identify bot by user-agent value', () => {
global.userAgent.mockReturnValue('Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)');
const newConf = conf.buildConfig(conf.app);
expect(global.navigator.userAgent).toMatch(newConf.ddKnownBotRegex);
});

it('can ignore normal browser event by user-agent value', () => {
global.userAgent.mockReturnValue('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36');
const newConf = conf.buildConfig(conf.app);
expect(global.navigator.userAgent).not.toMatch(newConf.ddKnownBotRegex);
});
});
5 changes: 3 additions & 2 deletions src/nctIndex.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import ReduxQueryNode, { submitSearchForm } from './QueryNode/ReduxQueryNode';
import {
basename, gaTrackingId, workspaceUrl, workspaceErrorUrl,
indexPublic, explorerPublic, enableResourceBrowser, resourceBrowserPublic, enableDAPTracker,
discoveryConfig, ddApplicationId, ddClientToken, ddEnv, ddUrl, ddSampleRate,
discoveryConfig, ddApplicationId, ddClientToken, ddEnv, ddUrl, ddSampleRate, ddKnownBotRegex,
} from './localconf';
import { portalVersion } from './versions';
import Analysis from './Analysis/Analysis';
Expand Down Expand Up @@ -96,14 +96,15 @@ async function init() {
// eslint-disable-next-line no-console
console.warn('Datadog clientToken is set, but applicationId is missing');
} else if (ddApplicationId && ddClientToken) {
const conditionalSampleRate = ddKnownBotRegex.test(navigator.userAgent) ? 0 : ddSampleRate;
datadogRum.init({
applicationId: ddApplicationId,
clientToken: ddClientToken,
site: ddUrl,
service: 'portal',
env: ddEnv,
version: portalVersion,
sampleRate: ddSampleRate,
sampleRate: conditionalSampleRate,
trackInteractions: true,
});
}
Expand Down
2 changes: 2 additions & 0 deletions src/setupJest.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ Object.defineProperty(window, 'matchMedia', {
dispatchEvent: jest.fn(),
})),
});

global.userAgent = jest.spyOn(navigator, 'userAgent', 'get');
5 changes: 3 additions & 2 deletions src/workspaceIndex.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import ReduxLogin, { fetchLogin } from './Login/ReduxLogin';
import { ReduxNavBar, ReduxTopBar, ReduxFooter } from './Layout/reduxer';
import {
basename, gaTrackingId, workspaceUrl, workspaceErrorUrl, enableDAPTracker,
ddApplicationId, ddClientToken, ddEnv, ddUrl, ddSampleRate,
ddApplicationId, ddClientToken, ddEnv, ddUrl, ddSampleRate, ddKnownBotRegex,
} from './localconf';
import { portalVersion } from './versions';
import { components } from './params';
Expand Down Expand Up @@ -58,14 +58,15 @@ async function init() {
// eslint-disable-next-line no-console
console.warn('Datadog clientToken is set, but applicationId is missing');
} else if (ddApplicationId && ddClientToken) {
const conditionalSampleRate = ddKnownBotRegex.test(navigator.userAgent) ? 0 : ddSampleRate;
datadogRum.init({
applicationId: ddApplicationId,
clientToken: ddClientToken,
site: ddUrl,
service: 'portal',
env: ddEnv,
version: portalVersion,
sampleRate: ddSampleRate,
sampleRate: conditionalSampleRate,
trackInteractions: true,
});
}
Expand Down
Loading