Skip to content

Commit

Permalink
Integrate bc7f0c6 (#2605) from roman/SendBundleSizes into staging-07
Browse files Browse the repository at this point in the history
Co-authored-by: roman.gaignault <roman.gaignault@datadoghq.com>
  • Loading branch information
dd-mergequeue[bot] and RomanGaignault authored Feb 13, 2024
2 parents 2f18f28 + bc7f0c6 commit 335764e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 41 deletions.
43 changes: 10 additions & 33 deletions scripts/export-bundles-sizes.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
const fs = require('fs')
const path = require('path')
const { execSync } = require('child_process')
const { getBrowserSdkVersion } = require('./lib/browser-sdk-version')
const { getOrg2ApiKey } = require('./lib/secrets')
const { runMain, fetch: fetchWrapper } = 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 versionPath = path.join(__dirname, '../packages/rum/package.json')

const URL = 'https://http-intake.logs.datadoghq.com/api/v2/logs'
const HEADERS = {
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',
}
Expand All @@ -29,49 +28,27 @@ runMain(async () => {
rum_slim: getBundleSize(rumSlimPath),
worker: getBundleSize(workerPath),
},
version: getVersion(),
commit: executeGitCommand('git rev-parse HEAD'),
version: getBrowserSdkVersion(),
commit: process.env.CI_COMMIT_SHORT_SHA,
branch: process.env.CI_COMMIT_REF_NAME,
},
]
await postBundleSize(URL, logData)
await sendLogToOrg2(logData)
})

function executeGitCommand(command) {
try {
return execSync(command)
.toString('utf8')
.replace(/[\n\r\s]+$/, '')
} catch (error) {
console.error('Failed to execute git command:', error)
return null
}
}

function getVersion() {
try {
const versionJson = fs.readFileSync(versionPath, 'utf8')
return JSON.parse(versionJson).version
} catch (error) {
console.error('Failed to get version:', error)
return null
}
}

function getBundleSize(pathBundle) {
try {
const file = fs.statSync(pathBundle)
return file.size
} catch (error) {
console.error('Failed to get bundle size:', error)
return null
throw new Error('Failed to get bundle size', { cause: error })
}
}

async function postBundleSize(url = '', bundleData = {}) {
await fetchWrapper(url, {
async function sendLogToOrg2(bundleData = {}) {
await fetchWrapper(LOG_INTAKE_URL, {
method: 'POST',
headers: HEADERS,
headers: LOG_INTAKE_REQUEST_HEADERS,
body: JSON.stringify(bundleData),
})
}
9 changes: 9 additions & 0 deletions scripts/lib/browser-sdk-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const lernaJson = require('../../lerna.json')

function getBrowserSdkVersion() {
return lernaJson.version
}

module.exports = {
getBrowserSdkVersion,
}
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 { getBrowserSdkVersion } = 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 getBrowserSdkVersion()
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 `${getBrowserSdkVersion()}-${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 { getBrowserSdkVersion } = 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${getBrowserSdkVersion()}
${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 { getBrowserSdkVersion } = 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] = getBrowserSdkVersion()
})
return `${JSON.stringify(json, null, 2)}\n`
}

0 comments on commit 335764e

Please sign in to comment.