diff --git a/CHANGELOG-FRONTIER.md b/CHANGELOG-FRONTIER.md index 1efd7199aa9..5205499f0db 100644 --- a/CHANGELOG-FRONTIER.md +++ b/CHANGELOG-FRONTIER.md @@ -1,3 +1,15 @@ +## 8.17.0 + +- Migrate to the Dynatrace New RUM Experience (RUM on Grail) and load the agent `async` + - **Default:** `_complete.js` from the Dynatrace global CDN, loaded `async`, so pages no longer block rendering on the agent download (~181 KB gzipped with New RUM and Session Replay). It follows the tenant's agent version and configuration with no react-scripts release + - **`enableManualPageDetection` and `window.dtinfo.appName` now work under async loading.** `layout.ejs` called them only if `window.dtrum` already existed, which silently skipped them once the agent loaded async. A new `dtWhenReady(which, fn)` helper runs them when the agent arrives + - **Early-error buffer:** an inline listener catches errors thrown before the async agent installs its handlers and replays them through `dtrum.reportError`. Measured on int, capture at 250 ms rose from 2/10 to 6/10 and at 400 ms from 5/10 to 9/10; it cannot reach t=0. Emits nothing under `off` + - **Removed the `frontier_snow_dynatraceNewRUM` flag.** It never controlled New RUM — that is the tenant's `enabledOnGrail` setting — and only toggled `async` + - `frontier_snow_dynatraceRUM` has four states: `off` (kill switch), two SRI arms — `asyncCS-inline` (versioned tag + SRI, `sync`) and `asyncCS-script` (the same tag, `async`) — and a fail-open default for anything else, including a Split outage returning `control` + - **Both SRI arms currently send no data to Grail** — 0 of 12 runs on int, against 4 of 6 for the default; sampling cannot explain it and the cause is unknown. Classic RUM keeps working on them, so nothing looks wrong from the page. Keep them limited to their test keys. Their values are baked in at publish time: Snow does not yet supply fresh ones through `locals.dynatrace` (pending in fs-webdev/snow#446) + - **Removed all old-RUM code paths:** `edgeUrls`, the old `cdnUrls`, and the three `_inline_*.ejs` bootstrap files + - Maintainer tooling and docs moved to `packages/react-scripts/tools/dynatrace/` and excluded from the published package. Published package: 119 files / 392 KB → 104 files / 254 KB. Rationale and known limits: `tools/dynatrace/DYNATRACE_RUM_MECHANISMS.md` + ## 8.16.4 - Widen the `typescript` peer range from `^3.2.1 || ^4 || ^5` to `^3.2.1 || ^4 || ^5 || ^6` so consuming apps can take `typescript@6` diff --git a/package-lock.json b/package-lock.json index a713dbf1bdf..ebd31e8ccd5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18398,13 +18398,13 @@ "license": "MIT" }, "node_modules/handlebars": { - "version": "4.7.9", - "resolved": "https://familysearch.jfrog.io/artifactory/api/npm/fs-npm-prod-virtual/handlebars/-/handlebars-4.7.9.tgz", - "integrity": "sha512-4E71E0rpOaQuJR2A3xDZ+GM1HyWYv1clR58tC8emQNeQe3RH7MAzSbat+V0wG78LQBo6m6bzSG/L4pBuCsgnUQ==", + "version": "4.7.10", + "resolved": "https://familysearch.jfrog.io/artifactory/api/npm/fs-npm-prod-virtual/handlebars/-/handlebars-4.7.10.tgz", + "integrity": "sha512-P5VJMVM7qgBn6vjXMw8WG9uVI+ncf2pi72j4de4yz5ZULLj2RGqLYaKOYGsgyrViQ0tePOVlN1tDCCXXtFqXKg==", "dev": true, "license": "MIT", "dependencies": { - "minimist": "^1.2.5", + "minimist": "^1.2.8", "neo-async": "^2.6.2", "source-map": "^0.6.1", "wordwrap": "^1.0.0" @@ -28432,9 +28432,9 @@ "license": "MIT" }, "node_modules/proxy-addr": { - "version": "2.0.7", - "resolved": "https://familysearch.jfrog.io/artifactory/api/npm/fs-npm-prod-virtual/proxy-addr/-/proxy-addr-2.0.7.tgz", - "integrity": "sha512-llQsMLSUDUPT44jdrU/O37qlnifitDP+ZwrmmZcoSKyLKvtZxpyV0n2/bD/N4tBAAZ/gJEdZU7KMraoK1+XYAg==", + "version": "2.0.8", + "resolved": "https://familysearch.jfrog.io/artifactory/api/npm/fs-npm-prod-virtual/proxy-addr/-/proxy-addr-2.0.8.tgz", + "integrity": "sha512-5nnx0yGyVUcY6t9RnWcARWtwT9F1D8O9rt08htPvnd49W1IgZtmLkhu9WfMzQj1cFxjHIO6connUNVW5k7AVyQ==", "license": "MIT", "dependencies": { "forwarded": "0.2.0", @@ -28442,6 +28442,10 @@ }, "engines": { "node": ">= 0.10" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" } }, "node_modules/proxy-addr/node_modules/ipaddr.js": { @@ -33040,22 +33044,6 @@ } } }, - "node_modules/tailwindcss/node_modules/yaml": { - "version": "2.9.0", - "resolved": "https://familysearch.jfrog.io/artifactory/api/npm/fs-npm-prod-virtual/yaml/-/yaml-2.9.0.tgz", - "integrity": "sha512-2AvhNX3mb8zd6Zy7INTtSpl1F15HW6Wnqj0srWlkKLcpYl/gMIMJiyuGq2KeI2YFxUPjdlB+3Lc10seMLtL4cA==", - "extraneous": true, - "license": "ISC", - "bin": { - "yaml": "bin.mjs" - }, - "engines": { - "node": ">= 14.6" - }, - "funding": { - "url": "https://github.com/sponsors/eemeli" - } - }, "node_modules/tapable": { "version": "2.3.3", "resolved": "https://familysearch.jfrog.io/artifactory/api/npm/fs-npm-prod-virtual/tapable/-/tapable-2.3.3.tgz", @@ -37152,7 +37140,7 @@ }, "packages/react-scripts": { "name": "@fs/react-scripts", - "version": "8.16.2", + "version": "8.17.0", "license": "MIT", "dependencies": { "@babel/core": "^7.16.0", @@ -37249,7 +37237,7 @@ "typescript": "^4.4.4" }, "engines": { - "node": ">=20.0.0" + "node": ">=24.0.0" }, "optionalDependencies": { "fsevents": "^2.3.2" @@ -37257,7 +37245,7 @@ "peerDependencies": { "@fs/eslint-config-frontier-react": ">=10.0.0", "react": ">= 16", - "typescript": "^3.2.1 || ^4 || ^5" + "typescript": "^3.2.1 || ^4 || ^5 || ^6" }, "peerDependenciesMeta": { "typescript": { diff --git a/packages/react-scripts/layout/views/layout.ejs b/packages/react-scripts/layout/views/layout.ejs index 3c5e76ad5f2..ba4c9ef91f5 100644 --- a/packages/react-scripts/layout/views/layout.ejs +++ b/packages/react-scripts/layout/views/layout.ejs @@ -64,10 +64,25 @@ <%- include('partials/experiments') %> <%- include('partials/sentry') %> <%- include('partials/clientAppConfig') %> - if (window.dtrum) { + // window.dtinfo is a plain global read by Dynatrace metadata capture rules, so set it + // unconditionally and as early as possible. Both RUM Classic and the New RUM Experience can + // read it via their own (separately configured) capture rules; gating it on the agent being + // loaded meant the async treatments never set it at all. + if (!window.dtinfo) window.dtinfo = {} + window.dtinfo.appName = SERVER_DATA.appName + + // enableManualPageDetection is a RUM Classic API with no New RUM equivalent — New RUM models + // navigations / page summaries / view summaries instead. Defer it until dtrum exists rather + // than firing once: under async loading the old `if (window.dtrum)` guard was always false, + // so Classic silently fell back to automatic page detection on those treatments. + if (window.dtWhenReady) { + window.dtWhenReady('classic', function () { + if (typeof window.dtrum.enableManualPageDetection === 'function') { + window.dtrum.enableManualPageDetection() + } + }) + } else if (window.dtrum) { window.dtrum.enableManualPageDetection() - if (!window.dtinfo) window.dtinfo = {} - window.dtinfo.appName = SERVER_DATA.appName } diff --git a/packages/react-scripts/layout/views/partials/dynatrace.ejs b/packages/react-scripts/layout/views/partials/dynatrace.ejs index 6805ef274fc..c1a008682bb 100644 --- a/packages/react-scripts/layout/views/partials/dynatrace.ejs +++ b/packages/react-scripts/layout/views/partials/dynatrace.ejs @@ -2,27 +2,216 @@ /* SETS up dynatrace for the various environments */ if (typeof getFeatureFlag !== 'undefined') { + /* One flag, four states — see tools/dynatrace/DYNATRACE_RUM_MECHANISMS.md for the full rationale. + * + * 'off' no agent at all (kill switch) + * 'asyncCS-inline' versioned OneAgent JS tag + SRI, loaded SYNCHRONOUSLY + * 'asyncCS-script' the same versioned + SRI tag, loaded ASYNC + * anything else combined _complete.js from the Dynatrace global CDN, loaded ASYNC + * + * The two asyncCS-* names read backwards and are kept only because they are live in Split: + * '-inline' is the SYNC arm (it replaced an inline-bootstrap approach that was abandoned) and + * '-script' is the ASYNC one. Trust this table over the names. + * + * The default is deliberately the fallthrough rather than an explicit 'global-cdn' match: a + * retired treatment name, a renamed flag, or a Split outage returning 'control' must still load + * RUM. Failing closed would produce a silent monitoring gap, which is the one failure mode we + * cannot detect from the data itself. + * + * The SRI arms are the hedge for BOTH documented revisit triggers, split so each can be measured + * on its own. Measured on the wire, _complete.js and the versioned file are the same size to + * within ~750 bytes; the ONLY material difference is the cache header — _complete.js is + * max-age=86400 with no validator, so it is re-downloaded in full every 24 hours, while the + * versioned URL is max-age=31536000, immutable. That is the whole case for these arms. + * + * KNOWN BROKEN — both SRI arms send NOTHING to Grail. Measured on int over two rounds: Grail data + * in 0 of 12 SRI runs vs 4 of 6 global-cdn runs. Int records 33% of sessions, so a single empty + * run means nothing; 12 in a row is <1% by chance. Classic RUM keeps working, so there is no + * visible symptom; only the Grail side goes silent. Not the agent code, not the D module, not an + * SRI or CORS failure — the leading suspect is the `auto` key, which _complete.js carries and the + * baked data-dtconfig does not. Keep these treatments limited to their test keys until it is + * triaged. See tools/dynatrace/DYNATRACE_RUM_MECHANISMS.md, "Open: the SRI arms send no data to + * Grail". + * + * Sync vs async is the second, independent question: sync has no blind window, async never + * blocks rendering. Because the versioned URL is cached for a year, the sync parse-block is + * paid only on the first uncached load — which is why sync is worth measuring here even though + * it would be indefensible for _complete.js. + * + * Whether the New RUM Experience is active is NOT a code concern — it is the tenant's + * `enabledOnGrail` setting, toggled per application in the Dynatrace UI with no deploy. + * The old frontier_snow_dynatraceNewRUM flag never controlled it; it only toggled `async`. + */ const dynatraceFlag = getFeatureFlag('frontier_snow_dynatraceRUM', {appName: process.env.APP_NAME}); const env = envType(); + const treatment = dynatraceFlag.treatment; + const rumDisabled = treatment === 'off'; + const useSriSync = treatment === 'asyncCS-inline'; // versioned + SRI, render-blocking + const useSriAsync = treatment === 'asyncCS-script'; // versioned + SRI, non-blocking + const useSri = useSriSync || useSriAsync; - const cdnUrls = { + // locals.dynatrace would supply fresh values from the S3 copy of dynatrace-rum-config.json, but + // no released Snow populates it yet (pending in fs-webdev/snow#446). Until then the values below, + // written by tools/dynatrace/fetch-dynatrace-scripts.js at react-scripts publish time, are what + // actually render. + const cdnUrlsNew = (locals && locals.dynatrace && locals.dynatrace.cdnUrls) || { + int: 'https://js-cdn.dynatrace.com/jstag/15c157a40ab/sri/ruxitagent_ICA15789MNPQRTUVXfghqrux_10347260902154543.js', + beta: 'https://js-cdn.dynatrace.com/jstag/15c157a40ab/sri/ruxitagent_ICA15789MNPQRTUVXfghqrux_10347260902154543.js', + prod: 'https://js-cdn.dynatrace.com/jstag/15c157a40ab/sri/ruxitagent_ICA15789MNPQRTUVXfghqrux_10347260902154543.js' + } + + const cdnIntegrityNew = (locals && locals.dynatrace && locals.dynatrace.cdnIntegrity) || { + int: 'sha256-1MMBOB8sNstRMOWL02a1m3Ksn1qVkP/F6KewuBFvwtk=', + beta: 'sha256-1MMBOB8sNstRMOWL02a1m3Ksn1qVkP/F6KewuBFvwtk=', + prod: 'sha256-1MMBOB8sNstRMOWL02a1m3Ksn1qVkP/F6KewuBFvwtk=' + } + + const cdnConfigNew = (locals && locals.dynatrace && locals.dynatrace.cdnConfig) || { + int: 'app=3faf90e849295814|cors=1|srtbv=4|owasp=1|featureHash=ICA15789MNPQRTUVXfghqrux|msl=153600|srsr=10000|nsfnv=1|reportUrl=https://bf99293tkn.bf.dynatrace.com/bf|srvr=%5C%2Fidentity%5C%2Fsettings|rdnt=2|uxrgce=1|cuc=t0rtn87t|srms=2,1,0,0%2Ftextarea%2Cinput%2Cselect%2Coption;0%2Fdatalist;0%2Fform%20button;0%2F%5Bdata-dtrum-input%5D;0%2F.data-dtrum-input;0%2F%5Bcontenteditable%5D%3Anot%28%5Bcontenteditable%3D%22false%22%5D%29;0%2F%5Brole%3D%22textbox%22%5D%2C%5Brole%3D%22checkbox%22%5D%2C%5Brole%3D%22radio%22%5D%2C%5Brole%3D%22option%22%5D%2C%5Brole%3D%22combobox%22%5D%2C%5Brole%3D%22slider%22%5D%2C%5Brole%3D%22searchbox%22%5D%2C%5Brole%3D%22switch%22%5D%2C%5Brole%3D%22spinbutton%22%5D;1%2F%5Edata%28%28%5C-.%2B%24%29%7C%24%29|mel=100000|dpvc=1|md=mdcc1=cfs_anid,mdcc2=bFS.User.profile.cisId,mdcc3=bs.pageName,mdcc4=bFS.attrs.anonId,mdcc5=bFS.attrs.country,mdcc6=bFS.attrs.cisId|lastModification=1793030880628|mdp=mdcc4,mdcc5|tp=500,50,0|srbbv=3|agentUri=https://js-cdn.dynatrace.com/jstag/15c157a40ab/sri/ruxitagent_ICA15789MNPQRTUVXfghqrux_10347260902154543.js', + beta: 'app=c4242bb1eb216374|cors=1|srtbv=4|owasp=1|featureHash=ICA15789MNPQRTUVXfghqrux|msl=153600|srsr=5000|nsfnv=1|reportUrl=https://bf99293tkn.bf.dynatrace.com/bf|srvr=%5C%2Fidentity%5C%2Fsettings|rdnt=2|uxrgce=1|cuc=t0rtn87t|srms=2,1,0,0%2Ftextarea%2Cinput%2Cselect%2Coption;0%2Fdatalist;0%2Fform%20button;0%2F%5Bdata-dtrum-input%5D;0%2F.data-dtrum-input;0%2F%5Bcontenteditable%5D%3Anot%28%5Bcontenteditable%3D%22false%22%5D%29;0%2F%5Brole%3D%22textbox%22%5D%2C%5Brole%3D%22checkbox%22%5D%2C%5Brole%3D%22radio%22%5D%2C%5Brole%3D%22option%22%5D%2C%5Brole%3D%22combobox%22%5D%2C%5Brole%3D%22slider%22%5D%2C%5Brole%3D%22searchbox%22%5D%2C%5Brole%3D%22switch%22%5D%2C%5Brole%3D%22spinbutton%22%5D;1%2F%5Edata%28%28%5C-.%2B%24%29%7C%24%29|mel=100000|dpvc=1|md=mdcc1=cfs_anid,mdcc2=bFS.attrs.cisId,mdcc3=bFS.User.profile.cisId,mdcc4=fx-ratelimit-remaining,mdcc5=bs.campaign,mdcc6=bs.channel,mdcc7=bs.pageName,mdcc8=bs.pageURL,mdcc9=bs.pageType,mdcc10=bs.visitorID|lastModification=1793030880628|tp=500,50,0|srbbv=3|agentUri=https://js-cdn.dynatrace.com/jstag/15c157a40ab/sri/ruxitagent_ICA15789MNPQRTUVXfghqrux_10347260902154543.js', + prod: 'app=a8e5edd77f861ace|cors=1|srtbv=4|owasp=1|featureHash=ICA15789MNPQRTUVXfghqrux|msl=153600|srsr=5000|vcx=1500|nsfnv=1|reportUrl=https://bf99293tkn.bf.dynatrace.com/bf|srvr=%5C%2Fidentity%5C%2Fsettings|rdnt=2|uxrgce=1|vcit=8000|cuc=t0rtn87t|srms=2,2,1,|mdl=mdcc11=20|mel=100000|dpvc=1|md=mdcc1=cfs_anid,mdcc2=bFS.attrs.cisId,mdcc3=bFS.User.profile.cisId,mdcc4=bs.pageName,mdcc5=bs.pageType,mdcc6=bs.pageURL,mdcc7=bs.visitorID,mdcc8=bs.campaign,mdcc9=bs.channel,mdcc10=fx-ratelimit-remaining,mdcc11=bdocument.referrer,mdcc12=dutm_campaign,mdcc13=dutm_source,mdcc14=dutm_medium|lastModification=1793030880628|tp=500,50,0|srbbv=3|agentUri=https://js-cdn.dynatrace.com/jstag/15c157a40ab/sri/ruxitagent_ICA15789MNPQRTUVXfghqrux_10347260902154543.js' + } + + // New RUM JavaScript tag (combined code+config _complete.js) from the Dynatrace global CDN. + // The URL is stable and Dynatrace updates the file behind it, so this fallback does not go stale. + const cdnCompleteUrlsNew = (locals && locals.dynatrace && locals.dynatrace.cdnCompleteUrls) || { int: 'https://js-cdn.dynatrace.com/jstag/15c157a40ab/bf99293tkn/3faf90e849295814_complete.js', beta: 'https://js-cdn.dynatrace.com/jstag/15c157a40ab/bf99293tkn/c4242bb1eb216374_complete.js', prod: 'https://js-cdn.dynatrace.com/jstag/15c157a40ab/bf99293tkn/a8e5edd77f861ace_complete.js' } - const edgeUrls = { - int: 'https://edge.fscdn.org/assets/components/hf/assets/js/monitoring/dynatrace-20221104-int.min-9aebd229f83ef7c845ae898f451bb617.js', - beta: 'https://edge.fscdn.org/assets/components/hf/assets/js/monitoring/dynatrace-20221104-beta.min-e859eab722487cc71d4f659447d7ed6d.js', - prod: 'https://edge.fscdn.org/assets/components/hf/assets/js/monitoring/dynatrace-20221104-prod.min-44bb9345beec8a984c2fca40385b4f41.js' - } + // The agent is never inlined: EJS compiles included files as templates, and the minified agent + // contains the EJS open-delimiter sequence (a less-than sign immediately followed by a percent + // sign) inside a character-class string, which EJS misreads as an unterminated scriptlet and + // fails with "Could not find matching close tag". It would also re-ship 300-460 KB per response. + if (!rumDisabled) { + if (useSri) { %> + + + <% } else { %> + + + <% } %> + - <% } else if (dynatraceFlag.treatment === 'asyncCS-inline') { %> - <%- include(`./partials/dynatrace/_inline_${env}`) %> - <% } else if (dynatraceFlag.treatment === 'global-cdn') { %> - - <% } %> + poll(); // synchronous treatments have already loaded by now + if (!(fired.classic && fired.grail)) { + var iv = setInterval(function () { + if (poll() || ++attempts > 50) clearInterval(iv); // up to ~10s for an async agent + }, 200); + } + })(); -<% } %> + // Replay whatever the buffer caught before the agent's own handlers existed. + // + // Routed through RUM Classic on purpose: dtrum.reportError is available, dynatrace.reportError + // is NOT (New RUM exposes sendEvent instead, whose error event shape we have not verified). + // Classic runs alongside New RUM and cannot be disabled, so this path always exists. Whether a + // Classic-reported error also surfaces in Grail is unconfirmed — see the mechanisms doc. + // + // Known imprecision, accepted deliberately: the agent installs its handlers slightly before + // window.dtrum becomes visible (measured — errors captured from ~550ms while dtrum appeared at + // ~660ms), so an error thrown in that ~100ms sliver can be reported twice. Duplicating a + // handful of errors is the better failure than dropping them, and the "[dt-early]" prefix makes + // replayed ones identifiable if they ever need excluding. + window.dtWhenReady && window.dtWhenReady('classic', function () { + var buf = window.__dtEarlyErrors || []; + if (window.__dtEarlyErrorsStop) { try { window.__dtEarlyErrorsStop(); } catch (e) {} } + for (var i = 0; i < buf.length; i++) { + try { + var rec = buf[i]; + var ev = rec.e || {}; + // Pass the Error object where we have one so the real stack survives; a bare string makes + // the agent synthesise a stack from the reporting site, which points here instead of at + // the throw. Check exception.is_stack_trace_generated in user.events to tell them apart. + var err = ev.error || ev.reason; + var at = '[dt-early +' + rec.t + 'ms] '; + if (typeof window.dtrum.reportError === 'function') { + var reported = new Error(at + (err instanceof Error ? err.message : (ev.message || err || 'unknown early error'))); + if (err instanceof Error) { + reported.name = err.name; + if (err.stack) reported.stack = err.stack; + } + window.dtrum.reportError(reported); + } + } catch (e) {} + } + buf.length = 0; + }); + + <% } +} %> diff --git a/packages/react-scripts/layout/views/partials/dynatrace/_inline_beta.ejs b/packages/react-scripts/layout/views/partials/dynatrace/_inline_beta.ejs deleted file mode 100644 index 34f8961ae23..00000000000 --- a/packages/react-scripts/layout/views/partials/dynatrace/_inline_beta.ejs +++ /dev/null @@ -1,73 +0,0 @@ - \ No newline at end of file diff --git a/packages/react-scripts/layout/views/partials/dynatrace/_inline_int.ejs b/packages/react-scripts/layout/views/partials/dynatrace/_inline_int.ejs deleted file mode 100644 index 731df536373..00000000000 --- a/packages/react-scripts/layout/views/partials/dynatrace/_inline_int.ejs +++ /dev/null @@ -1,73 +0,0 @@ - \ No newline at end of file diff --git a/packages/react-scripts/layout/views/partials/dynatrace/_inline_prod.ejs b/packages/react-scripts/layout/views/partials/dynatrace/_inline_prod.ejs deleted file mode 100644 index 204607df5bf..00000000000 --- a/packages/react-scripts/layout/views/partials/dynatrace/_inline_prod.ejs +++ /dev/null @@ -1,73 +0,0 @@ - diff --git a/packages/react-scripts/package.json b/packages/react-scripts/package.json index 18f880ce3ab..588c4b1238f 100644 --- a/packages/react-scripts/package.json +++ b/packages/react-scripts/package.json @@ -1,6 +1,6 @@ { "name": "@fs/react-scripts", - "version": "8.16.4", + "version": "8.17.0", "upstreamVersion": "5.0.1", "description": "Configuration and scripts for Create React App.", "repository": { diff --git a/packages/react-scripts/tools/dynatrace/DYNATRACE_RUM_MECHANISMS.md b/packages/react-scripts/tools/dynatrace/DYNATRACE_RUM_MECHANISMS.md new file mode 100644 index 00000000000..5d7d6d46ee4 --- /dev/null +++ b/packages/react-scripts/tools/dynatrace/DYNATRACE_RUM_MECHANISMS.md @@ -0,0 +1,286 @@ +# Dynatrace RUM: how the agent is loaded, and why + +Implementation reference for [dynatrace.ejs](../../layout/views/partials/dynatrace.ejs): the rules +a maintainer must keep when changing it, what is known broken, and the error-capture limits that +come from this package's own configuration. + +* **Full analysis** — the option matrix, payload and beacon measurements, the rejected + candidates, the maintenance model — is the system of record in Confluence: + [Dynatrace RUM: Loading Mechanisms & Agent Management (In-Depth Analysis)](https://icseng.atlassian.net/wiki/spaces/FRDOCS/pages/2496266249/Dynatrace+RUM+Loading+Mechanisms+Agent+Management+In-Depth+Analysis). +* **How to refresh the agent and test each flag state:** [DYNATRACE_RUM_UPDATE.md](./DYNATRACE_RUM_UPDATE.md). +* **The measurement harnesses, raw data and research notes** are at tag + `rum-research-archive-2026-10-27`. + +**Current decision:** serve the combined `_complete.js` tag from the Dynatrace global CDN, +loaded `async`. Provisional — see [Revisit triggers](#revisit-triggers). + +--- + +## What is implemented + +One flag, `frontier_snow_dynatraceRUM`, with four states: `off`, two SRI arms +(`asyncCS-inline` sync, `asyncCS-script` async) and a fallthrough default of `_complete.js` +loaded `async`. The table, and the cookies that force each state, are in +[DYNATRACE_RUM_UPDATE.md, Step 3](./DYNATRACE_RUM_UPDATE.md#step-3-verify-what-renders). + +Rules that must survive any refactoring: + +1. **The default is a fallthrough, not an equality check.** Retired treatment names, a renamed + flag, and a Split outage returning `control` must all still load RUM. Failing closed produces a + silent monitoring gap — the one failure mode invisible in the data itself. +2. **`async`, never `defer`.** `defer` holds execution until parsing finishes, which strictly + widens the window in which early errors are lost, for a negligible page-speed gain. +3. **Never inline the agent.** EJS compiles included files as templates, and the minified agent + contains the EJS open-delimiter sequence inside a string, so it fails with "Could not find + matching close tag" — this caused a production 500. It would also re-ship 300–460 KB in every + HTML response. +4. **`off` emits zero bytes.** The early-error buffer sits inside the `!rumDisabled` branch so a + kill switch thrown because RUM is causing trouble leaves nothing of ours installed either. +5. **New RUM is not a code concern.** Whether it is active is the tenant's `enabledOnGrail` + setting. The former `frontier_snow_dynatraceNewRUM` flag never controlled it; it only toggled + `async`, and was removed in 8.17.0. +6. **The SRI arms render baked values.** `locals.dynatrace` would supply fresh ones, but no + released Snow populates it yet (pending in fs-webdev/snow#446). The default arm is unaffected: + its URL is stable and Dynatrace updates the file behind it. + +### Why this choice, in brief + +* **Only `_complete.js` stays current without a release.** The alternatives couple the agent to + the react-scripts release pipeline, and that coupling demonstrably failed: two of the three + previous loading treatments were serving an agent built on **2022-11-04** to production. +* **Payload, not mechanism, is the performance lever.** An agent's FCP cost is essentially its + download time; mechanism choice moved FCP by tens of milliseconds, payload by hundreds. +* **`async` removes a render-path dependency** on `js-cdn.dynatrace.com` — a ~181 KB gzipped + download with New RUM and Session Replay enabled. +* **What we gave up:** Subresource Integrity, and immutable caching. `_complete.js` is + `max-age=86400` with no `ETag` or `Last-Modified`, so a daily visitor re-downloads it in full + every day. A request for a cache validator was filed with Dynatrace on 2026-08-25; if they add + one, this trade disappears. + +--- + +## Known broken: the SRI arms send no data to Grail + +**Measured on int on 2026-08-27 and again on 2026-10-27 (alpha.20, 1.347 config). +Unexplained. Both SRI arms are unusable for New RUM until this is triaged.** + +With `asyncCS-inline` or `asyncCS-script` selected, the agent loads and runs, but no New RUM +beacon ever reaches Grail: + +| | runs with Grail data | beacons when present | +|---|---|---| +| `global-cdn` | **4 of 6** (3/3 in August, 1/3 in October) | 2–8, 14–42 KB | +| SRI arms, both | **0 of 12** (6 in each round) | — | + +Read this against sampling. Int records 33% of sessions, and a session that is not recorded +sends nothing to Grail on *any* arm, so an empty run on its own proves nothing — `global-cdn` +itself came back empty in 2 of its 6 runs. The evidence is the comparison: if the SRI arms were +recorded at even int's configured 33%, twelve empty runs in a row would happen by chance about +0.8% of the time (0.67¹²). Each run is a fresh browser context with a 20-second dwell. + +Grail's sampling appears to have changed between the rounds. In August `global-cdn` sent Grail +data on every run, even alongside a ~1 KB Classic stub; in October it did so only in the run that +also sent a full Classic payload. So a session's Classic payload size does not reliably say whether +it was recorded on the Grail side. The August half of the SRI evidence is the stronger half: an +unsampled channel should have reported on every run. + +Classic RUM keeps working on the SRI arms. That is what makes this dangerous rather than merely +broken: the page loads, the agent reports, Classic dashboards keep moving, and only the Grail +side is silent. There is no symptom visible from the application. + +Ruled out: + +* **Not the agent code.** The SRI-served file contains all four Gen3 API methods. +* **Not the `ruxitagent_D_` runtime module.** Byte-identical and HTTP 200 on both paths. +* **Not SRI or CORS.** The script executes; no console errors; no integrity failures. + +The one observable difference: `window.dynatrace` exposes nine methods on `global-cdn` and two +(`sendBizEvent`, `sendEvent`) on the SRI arms. Diffing the two configurations, four keys appear +in `_complete.js` and are absent from the baked `data-dtconfig` — `auto`, `domain`, `rid`, +`rpid`. **`auto=1` is the leading hypothesis** for why the Grail channel never initialises, but +it has not been tested; the mechanism is genuinely unknown. + +**This likely extends to a self-hosted versioned agent.** That design also serves a baked +`data-dtconfig` rather than `_complete.js`, so it probably inherits the same failure. Mirroring +`_complete.js` itself onto our CDN is a different variant — its configuration is built in — see +[If we revive SRI or self-hosting](#if-we-revive-sri-or-self-hosting). + +Next step is to intercept the response and inject the missing keys one at a time to find which +one restores the Grail channel. Until then, keep both SRI treatments limited to their test keys. + +--- + +## Early-error buffer + +A ~20-line inline listener in `dynatrace.ejs`, directly above `dtWhenReady`, that catches errors +thrown before the async agent exists and replays them once it arrives. + +**Why.** Requests are never lost in the async window — the agent recovers them retroactively from +Resource Timing. Exceptions are: there is no buffered API for an error thrown with no listener +attached. Before the buffer, errors thrown before ~250 ms were never captured. Sentry does not fill +the gap; it installs from the app bundle, later than the agent. + +**How.** `error` and `unhandledrejection` listeners in the capture phase push to +`window.__dtEarlyErrors` (bounded at 50). When the agent appears, the `dtWhenReady('classic', …)` +callback removes the listeners and replays each entry through `dtrum.reportError`, prefixed +`[dt-early +ms]`. + +**Constraints for anyone editing the layout:** + +* It must stay **first** in its script block, and the partial must stay early in `` — + anything thrown before it runs is unrecoverable. +* It currently renders *after* a render-blocking stylesheet in `layout.ejs`, which delays it. + Moving it above that link should widen its coverage. Suggestive measurements only; test before + changing the layout. +* It reports through RUM Classic because `dynatrace.reportError` does not exist. Whether a + Classic-reported error also reaches Grail is **unconfirmed**. To check, run a known early error + and query: + + ``` + fetch user.events, from:-2h + | filter characteristics.has_error == true + | filter contains(exception.message, "dt-early") + | fields timestamp, exception.message, exception.type, error.source, dt.rum.session.id + ``` + +**What it covers, measured on int (alpha.18, 2026-08-24):** a band from roughly **350 ms to +550 ms** — after the inline script parses, before the agent installs its own handlers. Capture at +250 ms rose from 2/10 to 6/10, at 400 ms from 5/10 to 9/10. It cannot reach t=0. + +**Accepted imprecisions:** an error in the ~100 ms between the agent installing its handlers and +`window.dtrum` appearing can be reported twice; replayed errors are timestamped when the agent +arrives (the original offset is in the prefix); and where only a message exists the replay builds +a new `Error`, so the stack points at the replay site. + +--- + +## Error-capture limits rooted in this package + +These come from react-scripts' own build configuration, not from Dynatrace, so they belong here. +The measurements behind them are in the archive tag. + +* **Application errors are opaque.** Chunk ` +``` + +Key parts: +- **`src`**: The CDN URL for the external script +- **`data-dtconfig`**: Configuration string (includes app ID, parameters, etc.) +- **`integrity`**: SRI hash for script verification +- **app ID**: The environment-specific application identifier (e.g., `app=3faf90e849295814`) + +## Files Modified During Update + +``` +packages/react-scripts/layout/views/partials/dynatrace.ejs (fallback values auto-updated) +packages/react-scripts/tools/dynatrace/dynatrace-rum-config.json (regenerated; also published to S3) +packages/react-scripts/package.json (version bump) +CHANGELOG-FRONTIER.md (add entry) +``` + +> Note: the script never generates inline agent files. The old-RUM `_inline_*.ejs` bootstrap files were deleted when the old-RUM code paths were removed. + +## Troubleshooting + +**API returns 401 Unauthorized** +- Check that `DYNATRACE_API_TOKEN` environment variable is set +- Verify the token has the correct scopes (RUM manual insertion tags read) + +**API returns 400 Bad Request** +- Ensure entity IDs include the `APPLICATION-` prefix +- Entity IDs must be uppercase + +**API returns 404 Not Found** +- Verify the API endpoint URL is correct: `/api/v2/rum/oneAgentJavaScriptTagWithSri/{entityId}` +- Check that you're using the `.live.dynatrace.com` domain, not `.apps.dynatrace.com` + +## Related Documentation + +- [DYNATRACE_RUM_MECHANISMS.md](./DYNATRACE_RUM_MECHANISMS.md) — why the template is built this way, what is known broken, and how to read the current tenant state +- [Dynatrace RUM: Loading Mechanisms & Agent Management (In-Depth Analysis)](https://icseng.atlassian.net/wiki/spaces/FRDOCS/pages/2496266249/Dynatrace+RUM+Loading+Mechanisms+Agent+Management+In-Depth+Analysis) — the full analysis and measurements (Confluence) +- Tag `rum-research-archive-2026-10-27` — the measurement harnesses, raw data and research notes, recoverable with `git show` +- [Dynatrace RUM API Documentation](https://docs.dynatrace.com/docs/dynatrace-api/environment-api/rum/rum-manual-insertion-tags) +- [dynatrace.ejs](../../layout/views/partials/dynatrace.ejs) - Main RUM configuration file +- [fetch-dynatrace-scripts.js](./fetch-dynatrace-scripts.js) - The fetch script itself + +Release history is in `CHANGELOG-FRONTIER.md` at the repository root. diff --git a/packages/react-scripts/tools/dynatrace/dynatrace-rum-config.json b/packages/react-scripts/tools/dynatrace/dynatrace-rum-config.json new file mode 100644 index 00000000000..0e51285d1b5 --- /dev/null +++ b/packages/react-scripts/tools/dynatrace/dynatrace-rum-config.json @@ -0,0 +1,23 @@ +{ + "generated": "2026-10-26T22:34:38.268Z", + "cdnUrls": { + "int": "https://js-cdn.dynatrace.com/jstag/15c157a40ab/sri/ruxitagent_ICA15789MNPQRTUVXfghqrux_10347260902154543.js", + "beta": "https://js-cdn.dynatrace.com/jstag/15c157a40ab/sri/ruxitagent_ICA15789MNPQRTUVXfghqrux_10347260902154543.js", + "prod": "https://js-cdn.dynatrace.com/jstag/15c157a40ab/sri/ruxitagent_ICA15789MNPQRTUVXfghqrux_10347260902154543.js" + }, + "cdnIntegrity": { + "int": "sha256-1MMBOB8sNstRMOWL02a1m3Ksn1qVkP/F6KewuBFvwtk=", + "beta": "sha256-1MMBOB8sNstRMOWL02a1m3Ksn1qVkP/F6KewuBFvwtk=", + "prod": "sha256-1MMBOB8sNstRMOWL02a1m3Ksn1qVkP/F6KewuBFvwtk=" + }, + "cdnConfig": { + "int": "app=3faf90e849295814|cors=1|srtbv=4|owasp=1|featureHash=ICA15789MNPQRTUVXfghqrux|msl=153600|srsr=10000|nsfnv=1|reportUrl=https://bf99293tkn.bf.dynatrace.com/bf|srvr=%5C%2Fidentity%5C%2Fsettings|rdnt=2|uxrgce=1|cuc=t0rtn87t|srms=2,1,0,0%2Ftextarea%2Cinput%2Cselect%2Coption;0%2Fdatalist;0%2Fform%20button;0%2F%5Bdata-dtrum-input%5D;0%2F.data-dtrum-input;0%2F%5Bcontenteditable%5D%3Anot%28%5Bcontenteditable%3D%22false%22%5D%29;0%2F%5Brole%3D%22textbox%22%5D%2C%5Brole%3D%22checkbox%22%5D%2C%5Brole%3D%22radio%22%5D%2C%5Brole%3D%22option%22%5D%2C%5Brole%3D%22combobox%22%5D%2C%5Brole%3D%22slider%22%5D%2C%5Brole%3D%22searchbox%22%5D%2C%5Brole%3D%22switch%22%5D%2C%5Brole%3D%22spinbutton%22%5D;1%2F%5Edata%28%28%5C-.%2B%24%29%7C%24%29|mel=100000|dpvc=1|md=mdcc1=cfs_anid,mdcc2=bFS.User.profile.cisId,mdcc3=bs.pageName,mdcc4=bFS.attrs.anonId,mdcc5=bFS.attrs.country,mdcc6=bFS.attrs.cisId|lastModification=1793030880628|mdp=mdcc4,mdcc5|tp=500,50,0|srbbv=3|agentUri=https://js-cdn.dynatrace.com/jstag/15c157a40ab/sri/ruxitagent_ICA15789MNPQRTUVXfghqrux_10347260902154543.js", + "beta": "app=c4242bb1eb216374|cors=1|srtbv=4|owasp=1|featureHash=ICA15789MNPQRTUVXfghqrux|msl=153600|srsr=5000|nsfnv=1|reportUrl=https://bf99293tkn.bf.dynatrace.com/bf|srvr=%5C%2Fidentity%5C%2Fsettings|rdnt=2|uxrgce=1|cuc=t0rtn87t|srms=2,1,0,0%2Ftextarea%2Cinput%2Cselect%2Coption;0%2Fdatalist;0%2Fform%20button;0%2F%5Bdata-dtrum-input%5D;0%2F.data-dtrum-input;0%2F%5Bcontenteditable%5D%3Anot%28%5Bcontenteditable%3D%22false%22%5D%29;0%2F%5Brole%3D%22textbox%22%5D%2C%5Brole%3D%22checkbox%22%5D%2C%5Brole%3D%22radio%22%5D%2C%5Brole%3D%22option%22%5D%2C%5Brole%3D%22combobox%22%5D%2C%5Brole%3D%22slider%22%5D%2C%5Brole%3D%22searchbox%22%5D%2C%5Brole%3D%22switch%22%5D%2C%5Brole%3D%22spinbutton%22%5D;1%2F%5Edata%28%28%5C-.%2B%24%29%7C%24%29|mel=100000|dpvc=1|md=mdcc1=cfs_anid,mdcc2=bFS.attrs.cisId,mdcc3=bFS.User.profile.cisId,mdcc4=fx-ratelimit-remaining,mdcc5=bs.campaign,mdcc6=bs.channel,mdcc7=bs.pageName,mdcc8=bs.pageURL,mdcc9=bs.pageType,mdcc10=bs.visitorID|lastModification=1793030880628|tp=500,50,0|srbbv=3|agentUri=https://js-cdn.dynatrace.com/jstag/15c157a40ab/sri/ruxitagent_ICA15789MNPQRTUVXfghqrux_10347260902154543.js", + "prod": "app=a8e5edd77f861ace|cors=1|srtbv=4|owasp=1|featureHash=ICA15789MNPQRTUVXfghqrux|msl=153600|srsr=5000|vcx=1500|nsfnv=1|reportUrl=https://bf99293tkn.bf.dynatrace.com/bf|srvr=%5C%2Fidentity%5C%2Fsettings|rdnt=2|uxrgce=1|vcit=8000|cuc=t0rtn87t|srms=2,2,1,|mdl=mdcc11=20|mel=100000|dpvc=1|md=mdcc1=cfs_anid,mdcc2=bFS.attrs.cisId,mdcc3=bFS.User.profile.cisId,mdcc4=bs.pageName,mdcc5=bs.pageType,mdcc6=bs.pageURL,mdcc7=bs.visitorID,mdcc8=bs.campaign,mdcc9=bs.channel,mdcc10=fx-ratelimit-remaining,mdcc11=bdocument.referrer,mdcc12=dutm_campaign,mdcc13=dutm_source,mdcc14=dutm_medium|lastModification=1793030880628|tp=500,50,0|srbbv=3|agentUri=https://js-cdn.dynatrace.com/jstag/15c157a40ab/sri/ruxitagent_ICA15789MNPQRTUVXfghqrux_10347260902154543.js" + }, + "cdnCompleteUrls": { + "int": "https://js-cdn.dynatrace.com/jstag/15c157a40ab/bf99293tkn/3faf90e849295814_complete.js", + "beta": "https://js-cdn.dynatrace.com/jstag/15c157a40ab/bf99293tkn/c4242bb1eb216374_complete.js", + "prod": "https://js-cdn.dynatrace.com/jstag/15c157a40ab/bf99293tkn/a8e5edd77f861ace_complete.js" + } +} \ No newline at end of file diff --git a/packages/react-scripts/tools/dynatrace/fetch-dynatrace-scripts.js b/packages/react-scripts/tools/dynatrace/fetch-dynatrace-scripts.js new file mode 100755 index 00000000000..292a2cf7c17 --- /dev/null +++ b/packages/react-scripts/tools/dynatrace/fetch-dynatrace-scripts.js @@ -0,0 +1,415 @@ +#!/usr/bin/env node +/** + * Dynatrace RUM Script Fetcher + * + * Fetches the latest RUM tags from Dynatrace using the API. + * Automatically updates the dynatrace.ejs fallbacks, and publishes dynatrace-rum-config.json + * to S3 for Snow to consume. No released Snow reads it yet (pending in fs-webdev/snow#446), so + * the baked fallbacks are what render today. + * + * Requires: + * - Dynatrace API token with "Read RUM manual insertion tags" scope + * - AWS CLI configured with S3 write access to the CDN bucket + * + * IMPORTANT: Dynatrace API may not be available on all instances. + * If unavailable, fetch scripts manually from: + * Dynatrace UI → Settings → Real User Monitoring → Managed JavaScript + * + * Usage: + * node fetch-dynatrace-scripts.js # reads token from keychain + * DYNATRACE_API_TOKEN="xxx" node fetch-dynatrace-scripts.js # override for one run + * + * Setup (one-time): + * 1. Store Dynatrace API token in keychain: + * security add-generic-password -a dynatrace-rum-fetch -s dynatrace-rum-fetch -w + * + * 2. Configure AWS CLI (uses standard AWS credential chain): + * aws configure # or set AWS_ACCESS_KEY_ID + AWS_SECRET_ACCESS_KEY env vars + * aws s3 ls # verify you have S3 access + * + * Configuration: + * - Dynatrace token: auto-read from keychain; override with DYNATRACE_API_TOKEN + * - AWS credentials: read via AWS CLI (no keychain needed; uses ~/.aws/credentials or env vars) + * - S3 bucket/region: set via S3_PUBLISH_BUCKET and S3_PUBLISH_REGION env vars + * - Dynatrace/Entity IDs: set via DYNATRACE_ENV_URL and *_ENTITY_ID env vars + * + * API Documentation: + * https://docs.dynatrace.com/docs/dynatrace-api/environment-api/rum/rum-manual-insertion-tags + */ + +const https = require('https'); +const fs = require('fs'); +const path = require('path'); +const { execSync } = require('child_process'); + +const KEYCHAIN_ACCOUNT = 'dynatrace-rum-fetch'; +const KEYCHAIN_SERVICE_DYNATRACE = 'dynatrace-rum-fetch'; +const KEYCHAIN_SERVICE_S3 = 's3-publish-key'; + +function getKeychainSecret(service) { + try { + const secret = execSync( + `security find-generic-password -a "${KEYCHAIN_ACCOUNT}" -s "${service}" -w`, + { stdio: ['pipe', 'pipe', 'pipe'] } + ).toString().trim(); + return secret || null; + } catch (error) { + return null; + } +} + +function printKeychainSetupInstructions() { + console.error(` +ERROR: No Dynatrace API token found in keychain. + +To store your Dynatrace token (one-time setup): + + security add-generic-password \\ + -a "${KEYCHAIN_ACCOUNT}" \\ + -s "${KEYCHAIN_SERVICE_DYNATRACE}" \\ + -w + +You'll be prompted to enter the token (won't appear in shell history). + +To get a Dynatrace token: + 1. Log into https://bjm35087.live.dynatrace.com + 2. Go to Account → Access Tokens → Generate new token + 3. Add scope: "Read RUM manual insertion tags" + 4. Paste the token when prompted above + +To update an existing token: + security delete-generic-password -a "${KEYCHAIN_ACCOUNT}" -s "${KEYCHAIN_SERVICE_DYNATRACE}" + security add-generic-password -a "${KEYCHAIN_ACCOUNT}" -s "${KEYCHAIN_SERVICE_DYNATRACE}" -w + +For AWS S3 access (used for publishing config): + Ensure AWS CLI is configured with credentials: + aws configure + Or set environment variables: + export AWS_ACCESS_KEY_ID="your-key" + export AWS_SECRET_ACCESS_KEY="your-secret" + +Or override Dynatrace token for a single run: + DYNATRACE_API_TOKEN="dt0c01.xxx" node fetch-dynatrace-scripts.js +`); +} + +// Configuration +const DYNATRACE_ENVIRONMENT_URL = process.env.DYNATRACE_ENV_URL || "https://bjm35087.live.dynatrace.com"; +const DYNATRACE_API_TOKEN = process.env.DYNATRACE_API_TOKEN || getKeychainSecret(KEYCHAIN_SERVICE_DYNATRACE); + +// S3 CDN configuration (for publishing dynatrace-rum-config.json via AWS CLI) +const S3_PUBLISH_BUCKET = process.env.S3_PUBLISH_BUCKET || "fs-cdn2-origin/assets/dynatrace"; +const S3_PUBLISH_REGION = process.env.S3_PUBLISH_REGION || "us-east-1"; +// AWS auth uses the standard credential chain. The `aws` child processes below inherit this +// process's env, so select a profile the normal AWS way — no script-specific env var needed: +// aws sso login --profile frontier-admin +// export AWS_PROFILE=frontier-admin (or run inline: AWS_PROFILE=frontier-admin node ...) + +// Entity IDs for your RUM applications in each environment +// Get from Dynatrace UI: Applications → select app → Settings → note the entity ID +const ENTITY_IDS = { + int: process.env.INT_ENTITY_ID || "APPLICATION-3FAF90E849295814", + beta: process.env.BETA_ENTITY_ID || "APPLICATION-C4242BB1EB216374", + prod: process.env.PROD_ENTITY_ID || "APPLICATION-A8E5EDD77F861ACE", +}; + +function parseUrl(urlString) { + const url = new URL(urlString); + return { + hostname: url.hostname, + path: url.pathname.replace(/\/$/, ''), + }; +} + +async function makeRequest(hostname, path) { + return new Promise((resolve, reject) => { + const options = { + hostname, + path, + method: 'GET', + headers: { + 'Authorization': `Api-Token ${DYNATRACE_API_TOKEN}`, + 'Content-Type': 'application/json', + }, + }; + + https.request(options, (res) => { + let data = ''; + res.on('data', chunk => data += chunk); + res.on('end', () => { + if (res.statusCode >= 200 && res.statusCode < 300) { + resolve(data); + } else { + reject(`HTTP ${res.statusCode}: ${data}`); + } + }); + }).on('error', reject).end(); + }); +} + +function extractEnvValues(results) { + const envs = Object.keys(results); + return { + cdnUrls: Object.fromEntries(envs.map(env => [ + env, results[env].completeTag.match(/src="([^"]+)"/)?.[1] || '' + ])), + cdnIntegrity: Object.fromEntries(envs.map(env => [ + env, results[env].completeTag.match(/integrity="([^"]+)"/)?.[1] || '' + ])), + cdnConfig: Object.fromEntries(envs.map(env => [ + env, results[env].completeTag.match(/data-dtconfig="([^"]+)"/)?.[1] || '' + ])), + // Combined code+config file (the "JavaScript tag" _complete.js) used by the global-cdn treatment. + cdnCompleteUrls: Object.fromEntries(envs.map(env => [ + env, results[env].simpleTag.match(/src="([^"]+)"/)?.[1] || '' + ])), + }; +} + +// Parse the Dynatrace agent version — the trailing number in ruxitagent__.js — +// from each environment's tag, e.g. ...ruxitagent_ICA7NQVfghqrux_10341260622154106.js → 10341260622154106. +function extractAgentVersions(results) { + return Object.fromEntries(Object.keys(results).map(env => { + const src = results[env].completeTag.match(/src="([^"]+)"/)?.[1] || ''; + const version = src.match(/ruxitagent_[A-Za-z0-9]+_(\d+)\.js/)?.[1] || 'unknown'; + return [env, version]; + })); +} + +function updateDynatraceEjs(results) { + const ejsPath = path.join(__dirname, '../../layout/views/partials/dynatrace.ejs'); + let content = fs.readFileSync(ejsPath, 'utf8'); + const values = extractEnvValues(results); + + // Rewrite the hardcoded fallback object inside a + // const = (locals && locals.dynatrace && locals.dynatrace.) || { ... } + // block, preserving the locals-first progressive-enhancement guard. + const replaceFallback = (name, localsKey, map) => { + const lines = Object.entries(map).map(([env, v]) => ` ${env}: '${v}'`).join(',\n'); + const re = new RegExp( + `const ${name} = \\(locals && locals\\.dynatrace && locals\\.dynatrace\\.${localsKey}\\) \\|\\| \\{[^}]+\\}`, + 's' + ); + if (!re.test(content)) { + console.warn(` ⚠️ Could not find ${name} fallback block in dynatrace.ejs — skipped`); + return; + } + const replacement = `const ${name} = (locals && locals.dynatrace && locals.dynatrace.${localsKey}) || {\n${lines}\n }`; + // Use a function replacement so '$' in values is not treated as a substitution token. + content = content.replace(re, () => replacement); + }; + + replaceFallback('cdnUrlsNew', 'cdnUrls', values.cdnUrls); + replaceFallback('cdnIntegrityNew', 'cdnIntegrity', values.cdnIntegrity); + replaceFallback('cdnConfigNew', 'cdnConfig', values.cdnConfig); + replaceFallback('cdnCompleteUrlsNew', 'cdnCompleteUrls', values.cdnCompleteUrls); + + fs.writeFileSync(ejsPath, content, 'utf8'); + console.log(" ✅ Updated dynatrace.ejs fallback values"); +} + +function writeCdnConfigJson(results) { + const values = extractEnvValues(results); + const config = { + generated: new Date().toISOString(), + ...values, + }; + const configPath = path.join(__dirname, 'dynatrace-rum-config.json'); + fs.writeFileSync(configPath, JSON.stringify(config, null, 2), 'utf8'); + console.log(" ✅ Wrote dynatrace-rum-config.json"); + return config; +} + +function printAwsSetupInstructions() { + const active = process.env.AWS_PROFILE || ""; + console.error(` +AWS credentials are missing or expired${active ? ` for profile "${active}"` : " (no profile set — using the default credential chain)"}. + +This script publishes dynatrace-rum-config.json to s3://${S3_PUBLISH_BUCKET} and needs S3 write access. + +If your org uses AWS SSO (FamilySearch does), run: + export AWS_PROFILE=frontier-admin # so this script's aws calls use that profile + aws sso login # refresh the SSO session (opens a browser) + # ...then re-run this script + +Or pin the profile for a single run without exporting it: + AWS_PROFILE=frontier-admin node packages/react-scripts/tools/dynatrace/fetch-dynatrace-scripts.js + +Verify your identity any time: + aws sts get-caller-identity${active ? ` --profile ${active}` : " --profile frontier-admin"} + +(For static keys instead of SSO: run 'aws configure', or set AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY.) +`); +} + +// Preflight: confirm AWS credentials resolve before doing any Dynatrace work, so we fail +// fast with actionable guidance instead of after fetching everything (publish is the last step). +function checkAwsCredentials() { + if (!process.env.AWS_PROFILE) { + console.warn( + " ⚠️ AWS_PROFILE is not set — falling back to the default credential chain.\n" + + " In this setup the default profile has no credentials, so the S3 publish will\n" + + " likely fail. Set it first: export AWS_PROFILE=frontier-admin\n" + + " (after: aws sso login --profile frontier-admin)" + ); + } + try { + const out = execSync(`aws sts get-caller-identity --output json`, { + stdio: ['pipe', 'pipe', 'pipe'], + }).toString(); + const id = JSON.parse(out); + const profileNote = process.env.AWS_PROFILE || ""; + console.log(` ✅ AWS credentials OK — account ${id.Account}${profileNote ? `, profile ${profileNote}` : ""}`); + return true; + } catch (error) { + printAwsSetupInstructions(); + return false; + } +} + +async function publishToS3(config) { + const tempFile = path.join(__dirname, '.dynatrace-rum-config-temp.json'); + try { + fs.writeFileSync(tempFile, JSON.stringify(config), 'utf8'); + + // Use AWS CLI for S3 upload (inherits this process's env → honors AWS_PROFILE / default chain) + const awsCmd = `aws s3 cp "${tempFile}" s3://${S3_PUBLISH_BUCKET}/dynatrace-rum-config.json --region ${S3_PUBLISH_REGION} --metadata "generated=$(date +%s)" --cache-control "max-age=300" --acl public-read`; + + execSync(awsCmd, { stdio: 'inherit' }); + + console.log(` ✅ Published dynatrace-rum-config.json to S3 (s3://${S3_PUBLISH_BUCKET}/dynatrace-rum-config.json)`); + } catch (error) { + console.error(` ❌ Failed to publish to S3: ${error.message}`); + printAwsSetupInstructions(); + throw error; + } finally { + // Always remove the temp file, even if the upload failed. + try { fs.unlinkSync(tempFile); } catch (_) { /* already gone */ } + } +} + +async function fetchScripts() { + if (!DYNATRACE_API_TOKEN) { + printKeychainSetupInstructions(); + process.exit(1); + } + + const missingIds = Object.entries(ENTITY_IDS) + .filter(([, id]) => !id) + .map(([env]) => env); + + if (missingIds.length > 0) { + console.error(`ERROR: Missing entity IDs for: ${missingIds.join(", ")}`); + console.error("Set environment variables: INT_ENTITY_ID, BETA_ENTITY_ID, PROD_ENTITY_ID"); + process.exit(1); + } + + // Preflight AWS creds up front — publishing to S3 is the last step, and the Dynatrace + // fetch is wasted work if we can't publish. Fail fast with setup guidance instead. + console.log("Checking AWS credentials..."); + if (!checkAwsCredentials()) { + process.exit(1); + } + + try { + const { hostname, path: basePath } = parseUrl(DYNATRACE_ENVIRONMENT_URL); + console.log("Fetching Dynatrace RUM scripts...\n"); + + const results = {}; + + for (const [env, entityId] of Object.entries(ENTITY_IDS)) { + console.log(`📥 Fetching ${env.toUpperCase()} environment (entity: ${entityId})...`); + + try { + // Fetch simple JavaScript tag (basic, no SRI) + const simplePath = `${basePath}/api/v2/rum/javaScriptTag/${entityId}`; + const simpleResponse = await makeRequest(hostname, simplePath); + + // Fetch OneAgent JavaScript tag with SRI (for CDN URL, config, integrity hash) + const tagPath = `${basePath}/api/v2/rum/oneAgentJavaScriptTagWithSri/${entityId}`; + const tagResponse = await makeRequest(hostname, tagPath); + + // NOTE: we intentionally no longer fetch /inlineCode. The new RUM agent is loaded via + //