Faro: Improve performance of TRACKING_URLS regex (#98022)

There have been reports of faro performing poorly when the URLs
generated are long (there was a 39KB one). The TRACKING_URLS we are
using leading wildcard characters, leading to excessive backtracking.

This commit uses a simpler regular expression, that ensures we are
blocking the appropriate URLs without the performance hit. To prevent
that from happening, a timed test is introduced. The timeout threshold
is long enough to be hardware independent.
This commit is contained in:
Kostas Pelelis
2024-12-16 17:27:29 +02:00
committed by GitHub
parent 59014dc924
commit 2879166426
2 changed files with 19 additions and 6 deletions
@@ -5,7 +5,11 @@ import * as faroWebSdkModule from '@grafana/faro-web-sdk';
import { BrowserConfig, FetchTransport } from '@grafana/faro-web-sdk';
import { EchoSrvTransport } from './EchoSrvTransport';
import { GrafanaJavascriptAgentBackend, GrafanaJavascriptAgentBackendOptions } from './GrafanaJavascriptAgentBackend';
import {
GrafanaJavascriptAgentBackend,
GrafanaJavascriptAgentBackendOptions,
TRACKING_URLS,
} from './GrafanaJavascriptAgentBackend';
describe('GrafanaJavascriptAgentEchoBackend', () => {
let mockedSetUser: jest.Mock;
@@ -93,8 +97,7 @@ describe('GrafanaJavascriptAgentEchoBackend', () => {
expect(initializeFaroMock.mock.calls[0][0].transports?.[0]).toBeInstanceOf(EchoSrvTransport);
expect(initializeFaroMock.mock.calls[0][0].transports?.[0].getIgnoreUrls()).toEqual([
/.*\/log-grafana-javascript-agent.*/,
/.*.google-analytics.com*.*/,
/.*.googletagmanager.com*.*/,
/\.(google-analytics|googletagmanager)\.com/,
/frontend-metrics/,
/\/collect(?:\/[\w]*)?$/,
]);
@@ -116,6 +119,17 @@ describe('GrafanaJavascriptAgentEchoBackend', () => {
});
});
test('will ensure the performance of TRACKING_URLS', async () => {
// 10e6 is based on true events
const longString = Array.from({ length: 10e6 }, () => Math.random().toString(36)[2]).join('');
const maxExecutionTime = 500;
const start = performance.now();
TRACKING_URLS.some((u) => u && longString.match(u) !== null);
const end = performance.now();
expect(end - start).toBeLessThanOrEqual(maxExecutionTime);
});
//@FIXME - make integration test work
// it('integration test with EchoSrv and GrafanaJavascriptAgent', async () => {
@@ -37,9 +37,8 @@ export interface GrafanaJavascriptAgentBackendOptions extends BrowserConfig {
ignoreUrls: RegExp[];
}
const TRACKING_URLS = [
/.*.google-analytics.com*.*/,
/.*.googletagmanager.com*.*/,
export const TRACKING_URLS = [
/\.(google-analytics|googletagmanager)\.com/,
/frontend-metrics/,
/\/collect(?:\/[\w]*)?$/,
];