-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: add job to send test results to gcp
- Loading branch information
1 parent
604ac38
commit 6ac15c4
Showing
6 changed files
with
409 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#! /usr/bin/env node | ||
const fs = require('node:fs'); | ||
const process = require('node:process'); | ||
const { sendMetricsToGCP, makeTimeSeries } = require('./gcp-monitoring.cjs'); | ||
|
||
const resultFiles = process.argv.slice(2); | ||
|
||
const tapResultRegex = new RegExp( | ||
`(^(?<status>not )?ok (?<num>[0-9]+) - (?<name>.+?)(?: %ava-dur=(?<duration>[0-9]+)ms)?(?:# (?<comments>.+?))?$(?<output>(\n^#.+?$)*)(?<failure>(\n^(?:(?!(?:not|ok) ))[^\n\r]+?$)*))`, | ||
'gms', | ||
); | ||
let timeSeriesData = []; | ||
|
||
function processTAP(packageName, tapbody) { | ||
let m; | ||
const returnValue = []; | ||
// eslint-disable-next-line no-cond-assign | ||
while ((m = tapResultRegex.exec(tapbody))) { | ||
if (m.groups.name) { | ||
const testCaseName = `${m.groups.name}`.replace(/["<>]/g, '').trim(); | ||
|
||
let skipped = false; | ||
let succeeded = true; | ||
let todo = false; | ||
if (m.groups.status) { | ||
succeeded = false; | ||
} | ||
if (m.groups.comments) { | ||
if (m.groups.comments.match(/SKIP/gi)) { | ||
skipped = true; | ||
} | ||
if (m.groups.comments.match(/TODO/gi)) { | ||
todo = true; | ||
skipped = true; | ||
succeeded = true; | ||
} | ||
} | ||
returnValue.push({ | ||
labels: { | ||
test_name: testCaseName, | ||
package: packageName, | ||
test_status: | ||
succeeded && !(todo || skipped) | ||
? 'succeeded' | ||
: !succeeded | ||
? 'failed' | ||
: 'skipped', | ||
}, | ||
value: Number(succeeded && !(todo || skipped)), | ||
}); | ||
} | ||
} | ||
return returnValue; | ||
} | ||
|
||
for (const file of resultFiles) { | ||
const resultsBody = fs.readFileSync(file, 'utf-8'); | ||
const packageName = file.split('/').at(-2); | ||
|
||
const response = processTAP(packageName, resultsBody); | ||
timeSeriesData.push(...response); | ||
} | ||
|
||
const timeSeries = makeTimeSeries(timeSeriesData); | ||
sendMetricsToGCP(timeSeries); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const Monitoring = require('@google-cloud/monitoring'); | ||
|
||
const gcpCredentials = JSON.parse(process.env.GCP_CREDENTIALS); | ||
const projectId = gcpCredentials.project_id; | ||
|
||
const monitoring = new Monitoring.MetricServiceClient({ | ||
projectId: gcpCredentials.project_id, | ||
credentials: { | ||
client_email: gcpCredentials.client_email, | ||
private_key: gcpCredentials.private_key, | ||
}, | ||
}); | ||
|
||
async function sendMetricsToGCP(timeSeries) { | ||
const batchSize = 200; | ||
for (let i = 0; i < timeSeries.length; i += batchSize) { | ||
const batch = timeSeries.slice(i, i + batchSize); | ||
const request = { | ||
name: monitoring.projectPath(projectId), | ||
timeSeries: batch, | ||
}; | ||
|
||
try { | ||
await monitoring.createTimeSeries(request); | ||
console.log( | ||
`Batch starting with metric ${batch[0].metric.type} sent successfully.`, | ||
); | ||
} catch (error) { | ||
console.error('Error sending batch:', error); | ||
} | ||
} | ||
} | ||
|
||
function makeTimeSeries(testData) { | ||
const timeSeries = testData.map(({ labels, value }) => ({ | ||
metric: { | ||
type: `custom.googleapis.com/github/test-results`, | ||
labels, | ||
}, | ||
resource: { | ||
type: 'global', | ||
labels: { | ||
project_id: projectId, | ||
}, | ||
}, | ||
points: [ | ||
{ | ||
interval: { | ||
endTime: { | ||
seconds: Math.floor(Date.now() / 1000), | ||
}, | ||
}, | ||
value: { | ||
doubleValue: value, | ||
}, | ||
}, | ||
], | ||
})); | ||
return timeSeries; | ||
} | ||
|
||
module.exports = { sendMetricsToGCP, makeTimeSeries }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
"packageManager": "[email protected]", | ||
"devDependencies": { | ||
"@endo/eslint-plugin": "^2.2.2", | ||
"@google-cloud/monitoring": "^4.1.0", | ||
"@jessie.js/eslint-plugin": "^0.4.1", | ||
"@types/express": "^4.17.17", | ||
"@types/node": "^22.0.0", | ||
|
Oops, something went wrong.