diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 857613e610..d52b36d351 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -140,6 +140,7 @@ build-and-lint: - yarn build - yarn lint - node scripts/check-packages.js + - node scripts/export-bundles-sizes.js build-bundle: extends: diff --git a/scripts/export-bundles-sizes.js b/scripts/export-bundles-sizes.js new file mode 100644 index 0000000000..d846e1e15b --- /dev/null +++ b/scripts/export-bundles-sizes.js @@ -0,0 +1,54 @@ +const fs = require('fs') +const path = require('path') +const { browserSdkVersion } = require('./lib/browser-sdk-version') +const { getOrg2ApiKey } = require('./lib/secrets') +const { runMain, fetch } = require('./lib/execution-utils') + +const rumPath = path.join(__dirname, '../packages/rum/bundle/datadog-rum.js') +const logsPath = path.join(__dirname, '../packages/logs/bundle/datadog-logs.js') +const rumSlimPath = path.join(__dirname, '../packages/rum-slim/bundle/datadog-rum-slim.js') +const workerPath = path.join(__dirname, '../packages/worker/bundle/worker.js') + +const LOG_INTAKE_URL = 'https://http-intake.logs.datadoghq.com/api/v2/logs' +const LOG_INTAKE_REQUEST_HEADERS = { + 'DD-API-KEY': getOrg2ApiKey(), + 'Content-Type': 'application/json', +} + +runMain(async () => { + const logData = [ + { + message: 'Browser SDK bundles sizes', + service: 'browser-sdk', + ddsource: 'browser-sdk', + env: 'ci', + bundle_sizes: { + rum: getBundleSize(rumPath), + logs: getBundleSize(logsPath), + rum_slim: getBundleSize(rumSlimPath), + worker: getBundleSize(workerPath), + }, + version: browserSdkVersion, + commit: process.env.CI_COMMIT_SHORT_SHA, + branch: process.env.CI_COMMIT_REF_NAME, + }, + ] + await sendLogToOrg2(logData) +}) + +function getBundleSize(pathBundle) { + try { + const file = fs.statSync(pathBundle) + return file.size + } catch (error) { + throw new Error('Failed to get bundle size', { cause: error }) + } +} + +async function sendLogToOrg2(bundleData = {}) { + await fetch(LOG_INTAKE_URL, { + method: 'POST', + headers: LOG_INTAKE_REQUEST_HEADERS, + body: JSON.stringify(bundleData), + }) +} diff --git a/scripts/lib/browser-sdk-version.js b/scripts/lib/browser-sdk-version.js new file mode 100644 index 0000000000..6bca8b9398 --- /dev/null +++ b/scripts/lib/browser-sdk-version.js @@ -0,0 +1,5 @@ +const lernaJson = require('../../lerna.json') + +module.exports = { + browserSdkVersion: lernaJson.version, +} diff --git a/scripts/lib/build-env.js b/scripts/lib/build-env.js index de9d6b532b..e3824b48c3 100644 --- a/scripts/lib/build-env.js +++ b/scripts/lib/build-env.js @@ -1,7 +1,7 @@ const { readFileSync } = require('fs') const path = require('path') const execSync = require('child_process').execSync -const lernaJson = require('../../lerna.json') +const { browserSdkVersion } = require('./browser-sdk-version') const { command } = require('./command') /** @@ -24,12 +24,12 @@ const buildEnvFactories = { SDK_VERSION: () => { switch (getBuildMode()) { case 'release': - return lernaJson.version + return browserSdkVersion case 'canary': { const commitSha1 = execSync('git rev-parse HEAD').toString().trim() // TODO when tags would allow '+' characters // use build separator (+) instead of prerelease separator (-) - return `${lernaJson.version}-${commitSha1}` + return `${browserSdkVersion}-${commitSha1}` } default: return 'dev' diff --git a/scripts/lib/execution-utils.js b/scripts/lib/execution-utils.js index 1c73a2794e..f1e7203453 100644 --- a/scripts/lib/execution-utils.js +++ b/scripts/lib/execution-utils.js @@ -26,7 +26,7 @@ function runMain(mainFunction) { .then(() => mainFunction()) .catch((error) => { printError('\nScript exited with error:') - printError(error) + printErrorWithCause(error) process.exit(1) }) } @@ -38,6 +38,14 @@ function printError(...params) { console.log(redColor, ...params, resetColor) } +function printErrorWithCause(error) { + printError(error) + if (error.cause) { + printError('Caused by:') + printErrorWithCause(error.cause) + } +} + function printLog(...params) { const greenColor = '\x1b[32;1m' console.log(greenColor, ...params, resetColor) diff --git a/scripts/release/generate-changelog.js b/scripts/release/generate-changelog.js index a0974da97f..9e128f148f 100644 --- a/scripts/release/generate-changelog.js +++ b/scripts/release/generate-changelog.js @@ -5,7 +5,7 @@ const readFile = util.promisify(require('fs').readFile) const emojiNameMap = require('emoji-name-map') -const lernaConfig = require('../../lerna.json') +const { browserSdkVersion } = require('../lib/browser-sdk-version') const { spawnCommand, printError, runMain } = require('../lib/execution-utils') const { command } = require('../lib/command') const { modifyFile } = require('../lib/files-utils') @@ -33,7 +33,7 @@ ${emojisLegend} --- -## v${lernaConfig.version} +## v${browserSdkVersion} ${changesList} ${content.slice(content.indexOf('\n##'))}` diff --git a/scripts/release/update-peer-dependency-versions.js b/scripts/release/update-peer-dependency-versions.js index d884281caf..4494af84da 100644 --- a/scripts/release/update-peer-dependency-versions.js +++ b/scripts/release/update-peer-dependency-versions.js @@ -1,7 +1,7 @@ -const lernaConfig = require('../../lerna.json') const { runMain } = require('../lib/execution-utils') const { modifyFile } = require('../lib/files-utils') const { command } = require('../lib/command') +const { browserSdkVersion } = require('../lib/browser-sdk-version') const JSON_FILES = ['rum', 'rum-slim', 'logs'].map((packageName) => `./packages/${packageName}/package.json`) @@ -25,7 +25,7 @@ runMain(async () => { function updateJsonPeerDependencies(content) { const json = JSON.parse(content) Object.keys(json.peerDependencies).forEach((key) => { - json.peerDependencies[key] = lernaConfig.version + json.peerDependencies[key] = browserSdkVersion }) return `${JSON.stringify(json, null, 2)}\n` }