Skip to content

Commit

Permalink
refactor(core): only modify urls when capturing
Browse files Browse the repository at this point in the history
Prior to this were were creating a new list of urls to audit and appending a
random parameter to each url. This is necessary to ensure that we are not
hitting a CDN cache when we are trying to audit the urls. However, when we send
the metrics to DataDog, we send the modified url. This causes the metrics to be
unique and harder to aggregate or create trends on.

We can't drop the group by 'url' in DataDog because we might be sending multiple
urls for the same page type which would cause the metrics to be aggregated
incorrectly.

Instead, let's only modify the urls when we are starting the lighthouse runner
which will allow us to send the original url to DataDog.
  • Loading branch information
ardelato committed Feb 12, 2024
1 parent f4d0e36 commit df081cf
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,10 @@ function retrieveDataPointsForAudits(results: Result, audits: string[]) {
/**
* Adds `rand={123..}` param to the urls to ensure we never hit a CDN cache.
*/
function addRandomParamToUrl(inspectList: Record<string, string[]>): Record<string, string[]> {
const updatedInspectList: Record<string, string[]> = structuredClone(inspectList);

for (const pageType in updatedInspectList) {
const urls = updatedInspectList[pageType];

updatedInspectList[pageType] = urls.map(url => {
const urlObject = new URL(url);
urlObject.searchParams.append('rand', Math.floor(Math.random() * 1000000).toString());
return urlObject.toString();
});
}
return updatedInspectList;
function addRandomParamToUrl(url: string): string {
const urlObject = new URL(url);
urlObject.searchParams.append('rand', Math.floor(Math.random() * 1000000).toString());
return urlObject.toString();
}

async function sendMetricsToDatadog(metricName: string, dataPoints: v2.MetricPoint[], tags: Record<string, string>, metadata?: v1.MetricMetadata) {
Expand All @@ -100,8 +91,9 @@ async function captureLighthouseMetrics(pageType: string, url: string, audits: s

const formFactor = config.settings?.formFactor || 'mobile'
console.log(`Running Lighthouse for ${url} with form factor: ${formFactor}`);
const randUrl = addRandomParamToUrl(url);
const page = await browserRunner.start()
const results = await lighthouseRunner.run(url, {...options}, config, page)
const results = await lighthouseRunner.run(randUrl, {...options}, config, page)

await browserRunner.stop()

Expand Down Expand Up @@ -134,7 +126,6 @@ async function captureLighthouseMetrics(pageType: string, url: string, audits: s
}
(async() => {
let inspectList: Record<string, string[]> = URLS;
inspectList = addRandomParamToUrl(inspectList);

const audits = lhConfig.settings.onlyAudits || []

Expand Down

0 comments on commit df081cf

Please sign in to comment.