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

Port lighthouse-docker to TS in New Repo #1

Merged
merged 15 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ node_modules/

# VSCode Misc.
.vscode

# Config Files
urls.json
metrics-config.json
13 changes: 13 additions & 0 deletions metrics-config.example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"audits": [
"largest-contentful-paint",
"first-contentful-paint",
"cumulative-layout-shift",
"total-blocking-time",
"speed-index",
"total-byte-weight",
"server-response-time",
"is-crawlable",
"screenshot-thumbnails"
]
}
79 changes: 79 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import ChromeRunner from "./ChromeRunner.js"
import LighthouseRunner from "./LighthouseRunner.js"
import Datadog from "./DatadogClient.js"
import {v2} from '@datadog/datadog-api-client'
import fs from 'node:fs'
import type { Result } from 'lighthouse'
import os from 'node:os'
import dotenv from 'dotenv'
dotenv.config();

function getAuditNumericValue(results: Result, audit: string): number {
return results.audits[audit].numericValue || 0
mlahargou marked this conversation as resolved.
Show resolved Hide resolved
}

function getAuditScore(results: Result, audit: string): number {
return results.audits[audit].score || 0
}

function getFetchTime(results: Result): number {
return Math.round(new Date(results.fetchTime).getTime() / 1000);
}

function retrieveDataPointsForAudits(results: Result, audits: string[]) {
const timestamp = getFetchTime(results)
const values: Record<string, Record<string, v2.MetricPoint>> = {}

for (let audit of audits) {
values[audit] = {
"numericValue": {
timestamp: timestamp,
value: getAuditNumericValue(results, audit)
},
"score": {
timestamp: timestamp,
value: getAuditScore(results, audit)
}
}
}

return values
}
(async() => {
const dd = new Datadog({
api_key: process.env.DD_API_KEY || '',
app_key: process.env.DD_APP_KEY || ''
})

const inspectList: Record<string, string[]> = JSON.parse(await fs.promises.readFile('urls.json', 'utf8'));

const coreMetrics: Record<string, string[]> = JSON.parse(await fs.promises.readFile('metrics-config.json', 'utf8'));

for (let [pageType, urls] of Object.entries(inspectList)) {
for (let url of urls) {
const chromeRunner = new ChromeRunner(false)
const lighthouseRunner = new LighthouseRunner()
mlahargou marked this conversation as resolved.
Show resolved Hide resolved

const port = await chromeRunner.start()
const results = await lighthouseRunner.run(url, {port: port})

chromeRunner.stop()

const metrics = retrieveDataPointsForAudits(results, coreMetrics['audits'])

const tags = {
'url': url,
'page_type': pageType,
'lighthouse_version': results.lighthouseVersion,
'host': os.hostname()
}

const tagsArray = Object.entries(tags).map(([key, value]) => `${key}:${value}`)

for (let [audit, dataPoints] of Object.entries(metrics)) {
await dd.submitMetrics(`lighthouse.${audit}.value`, [dataPoints.numericValue], tagsArray)
await dd.submitMetrics(`lighthouse.${audit}.score`, [dataPoints.numericValue], tagsArray)
}
}
}
})()
3 changes: 3 additions & 0 deletions urls.example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"Test": ["https://www.google.com/", "https://www.youtube.com"]
}