Skip to content

Commit

Permalink
merge with main
Browse files Browse the repository at this point in the history
  • Loading branch information
metal-messiah committed Sep 6, 2023
2 parents 7d59bcc + 71d47d6 commit 680ddcb
Show file tree
Hide file tree
Showing 41 changed files with 1,545 additions and 20,401 deletions.
86 changes: 18 additions & 68 deletions .github/actions/build-ab/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,15 @@ import { S3Client, ListObjectsCommand } from '@aws-sdk/client-s3'
import { v4 as uuidv4 } from 'uuid'
import { args } from './args.js'
import { fetchRetry } from '../shared-utils/fetch-retry.js'
import Handlebars from 'handlebars'

const config = {
init: {
distributed_tracing: {
enabled: true
},
ajax: {
deny_list: [
'nr-data.net',
'bam.nr-data.net',
'staging-bam.nr-data.net',
'bam-cell.nr-data.net'
]
},
session_replay: {
enabled: true,
sampleRate: 0.5,
errorSampleRate: 1
}
},

loader_config: {
accountID: '1',
trustKey: '1',
agentID: args.appId,
licenseKey: args.licenseKey,
applicationID: args.appId
},
const __dirname = path.dirname(url.fileURLToPath(import.meta.url))
const template = Handlebars.compile(await fs.promises.readFile(path.resolve(__dirname, './template.js'), 'utf-8'))

info: {
beacon: 'staging-bam.nr-data.net',
errorBeacon: 'staging-bam.nr-data.net',
licenseKey: args.licenseKey,
applicationID: args.appId,
sa: 1
}
}
const scripts = []

// 0. Ensure the output directory is available and the target file does not exist
const __dirname = path.dirname(url.fileURLToPath(import.meta.url))

const outputFile = path.join(
path.resolve(__dirname, '../../../temp'),
`${args.environment}.js`
Expand All @@ -55,36 +24,13 @@ if (fs.existsSync(outputFile)) {
await fs.promises.rm(outputFile)
}

// 1. Write the NRBA configuration
console.log('Writing configuration in A/B script.')
await fs.promises.writeFile(
outputFile,
`window.NREUM=${JSON.stringify(config)}\n\n`,
{ encoding: 'utf-8' }
)
const nextScript = await fetchRetry(`${args.next}?_nocache=${uuidv4()}`, { retry: 3 })
scripts.push( {name: 'next', contents: await nextScript.text() })

// 2. Write the current loader script if env is not prod or eu-prod
if (['dev', 'staging'].includes(args.environment)) {
console.log(`Writing current loader ${args.current} in A/B script.`)
const currentScript = await fetchRetry(`${args.current}?_nocache=${uuidv4()}`, { retry: 3 })
await fs.promises.appendFile(
outputFile,
await currentScript.text() + '\n\n',
{ encoding: 'utf-8' }
)
}

// 3. Write the next loader script
console.log(`Writing next loader ${args.next} in A/B script.`)
const nextScript = await fetchRetry(`${args.next}?_nocache=${uuidv4()}`, { retry: 3 })
await fs.promises.appendFile(
outputFile,
await nextScript.text() + '\n\n',
{ encoding: 'utf-8' }
)
scripts.push( {name: 'current', contents: await currentScript.text() })

// 4. Find and write any experiments if env is not prod or eu-prod
if (['dev', 'staging'].includes(args.environment)) {
const stsClient = new STSClient({ region: args.region })
const s3Credentials = await stsClient.send(new AssumeRoleCommand({
RoleArn: args.role,
Expand Down Expand Up @@ -125,13 +71,17 @@ if (['dev', 'staging'].includes(args.environment)) {
} else {
for (const experiment of experimentsList) {
const experimentLoader = `https://js-agent.newrelic.com/${experiment}nr-loader-spa.min.js`
console.log(`Writing experiment loader ${experimentLoader} in A/B script.`)
const experimentScript = await fetchRetry(`${experimentLoader}?_nocache=${uuidv4()}`, { retry: 3 })
await fs.promises.appendFile(
outputFile,
await experimentScript.text() + '\n\n',
{ encoding: 'utf-8' }
)
scripts.push({name: experiment, contents: await experimentScript.text()})
}
}
}
console.log('writing', scripts.length,'scripts:', scripts.map(x => x.name).join(", "))

await fs.promises.writeFile(
outputFile,
template({
args, scripts
}),
{ encoding: 'utf-8' }
)
87 changes: 87 additions & 0 deletions .github/actions/build-ab/template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// config
window.NREUM={
init: {
distributed_tracing: {
enabled: true
},
ajax: {
deny_list: [
'nr-data.net',
'bam.nr-data.net',
'staging-bam.nr-data.net',
'bam-cell.nr-data.net'
]
},
session_replay: {
enabled: true,
sampleRate: 0.5,
errorSampleRate: 1,
autoStart: false
}
},
loader_config: {
accountID: '1',
trustKey: '1',
agentID: '{{{args.appId}}}',
licenseKey: '{{{args.licenseKey}}}',
applicationID: '{{{args.appId}}}'
},
info: {
beacon: 'staging-bam.nr-data.net',
errorBeacon: 'staging-bam.nr-data.net',
licenseKey: '{{{args.licenseKey}}}',
applicationID: '{{{args.appId}}}',
sa: 1
}
}
// scripts
{{#each scripts}}
// {{{this.name}}}
{{{this.contents}}}
{{/each}}
// post-script checks
try {
var xhr = new XMLHttpRequest()
xhr.open('POST', '/graphql')
xhr.setRequestHeader('authority', '${entitlementsEndpoints[environment]}')
xhr.setRequestHeader('cache-control','no-cache')
xhr.setRequestHeader('content-type', 'application/json; charset=utf-8')
xhr.setRequestHeader('newrelic-requesting-services','platform|nr1-ui')
xhr.setRequestHeader('pragma','no-cache')

xhr.send(
JSON.stringify({
'query': 'query PlatformEntitlementsQuery($names: [String]!) { currentUser { id authorizedAccounts { id entitlements(filter: {names: $names}) { name __typename } __typename } __typename } }',
'variables': {
'names': [
'hipaa',
'fedramp'
]
}
})
)

xhr.addEventListener('load', function(evt){
try{
var entitlementsData = JSON.parse(evt.currentTarget.responseText)
// call the newrelic api(s) after evaluating entitlements...
var canRunSr = true
if (entitlementsData.data && entitlementsData.data.currentUser && entitlementsData.data.currentUser.authorizedAccounts && entitlementsData.data.currentUser.authorizedAccounts.length) {
for (var i = 0; i < entitlementsData.data.currentUser.authorizedAccounts.length; i++){
var ent = entitlementsData.data.currentUser.authorizedAccounts[i]
if (!ent.entitlements || !ent.entitlements.length) continue
for (var j = 0; j < ent.entitlements.length; j++){
if (ent.entitlements[j].name === 'fedramp' || ent.entitlements[j].name === 'hipaa') canRunSr = false
}
}
}
if (canRunSr && newrelic && !!newrelic.start) newrelic.start('session_replay')
} catch(e){
// something went wrong...
if (!!newrelic && !!newrelic.noticeError) newrelic.noticeError(e)
}
})
} catch(err){
// we failed our mission....
if (!!newrelic && !!newrelic.noticeError) newrelic.noticeError(err)
}
1 change: 1 addition & 0 deletions .github/actions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"console-table-printer": "^2.11.2",
"fastly": "^6.0.0",
"filesize": "^10.0.12",
"handlebars": "^4.7.8",
"mime-types": "^2.1.35",
"node-fetch": "^3.3.2",
"uuid": "^9.0.0",
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/publish-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:

# Build and publish the latest code from the main branch
publish-dev-to-s3:
needs: [jest-tests, wdio-coverage]
needs: [jest-tests]
runs-on: ubuntu-latest
timeout-minutes: 30
defaults:
Expand Down Expand Up @@ -102,7 +102,7 @@ jobs:
nrba_app_id: ${{ secrets.INTERNAL_DEV_APPLICATION_ID }}
nrba_license_key: ${{ secrets.INTERNAL_LICENSE_KEY }}
nrba_current_script_url: https://js-agent.newrelic.com/nr-loader-spa-current.min.js
nrba_next_script_url: https://js-agent.newrelic.com/dev/nr-loader-experimental.min.js
nrba_next_script_url: https://js-agent.newrelic.com/dev/nr-loader-spa.min.js
aws_access_key_id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws_secret_access_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws_role: ${{ secrets.AWS_ROLE_ARN }}
Expand Down Expand Up @@ -131,7 +131,7 @@ jobs:
nrba_app_id: ${{ secrets.INTERNAL_STAGING_APPLICATION_ID }}
nrba_license_key: ${{ secrets.INTERNAL_LICENSE_KEY }}
nrba_current_script_url: https://js-agent.newrelic.com/nr-loader-spa-current.min.js
nrba_next_script_url: https://js-agent.newrelic.com/dev/nr-loader-experimental.min.js
nrba_next_script_url: https://js-agent.newrelic.com/dev/nr-loader-spa.min.js
aws_access_key_id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws_secret_access_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws_role: ${{ secrets.AWS_ROLE_ARN }}
Expand Down
Loading

0 comments on commit 680ddcb

Please sign in to comment.