Skip to content

Commit

Permalink
✨[RUM-3151] Report bundle sizes to logs (#2605)
Browse files Browse the repository at this point in the history
* ✨[RUM-3151] Report bundle sizes to logs

* added ddsource in request and PostBundleSize is now async

* fix lint error

* added branch new method

* Changed name intake, source version file and commit retrieve process

* added browser-sdk-version function / added error.cause to run main function

* added function printErrorWithCause

* deleted getter and added a simple return of lerna version. Changed fetch wrapper into fetch in export bundle script. Deleted local preferences in gitignore

* build env getbrowserversion

* changed export name BrowserSdkVersion

* changed browserSdkVersion export name
  • Loading branch information
RomanGaignault authored Feb 15, 2024
1 parent f6d3465 commit 84cd328
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
54 changes: 54 additions & 0 deletions scripts/export-bundles-sizes.js
Original file line number Diff line number Diff line change
@@ -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),
})
}
5 changes: 5 additions & 0 deletions scripts/lib/browser-sdk-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const lernaJson = require('../../lerna.json')

module.exports = {
browserSdkVersion: lernaJson.version,
}
6 changes: 3 additions & 3 deletions scripts/lib/build-env.js
Original file line number Diff line number Diff line change
@@ -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')

/**
Expand All @@ -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'
Expand Down
10 changes: 9 additions & 1 deletion scripts/lib/execution-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function runMain(mainFunction) {
.then(() => mainFunction())
.catch((error) => {
printError('\nScript exited with error:')
printError(error)
printErrorWithCause(error)
process.exit(1)
})
}
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions scripts/release/generate-changelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -33,7 +33,7 @@ ${emojisLegend}
---
## v${lernaConfig.version}
## v${browserSdkVersion}
${changesList}
${content.slice(content.indexOf('\n##'))}`
Expand Down
4 changes: 2 additions & 2 deletions scripts/release/update-peer-dependency-versions.js
Original file line number Diff line number Diff line change
@@ -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`)

Expand All @@ -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`
}

0 comments on commit 84cd328

Please sign in to comment.