Skip to content

Commit

Permalink
feat: add datadog js monitoring to app js
Browse files Browse the repository at this point in the history
The `APP_VERSION` needs to be the same for in the javascript initialisation AND the command line tool that uploads the sourcemaps into datadog.  Using an evironment var allows us to set the `APP_VERSION` variable in the build process, use it in the command line and pass it into the app js.
  • Loading branch information
davidsauntson committed Feb 5, 2025
1 parent bbd79ab commit 11293af
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 1 deletion.
4 changes: 4 additions & 0 deletions app/javascript/appliance-calculator.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import initErrorSummary from '@citizensadvice/design-system/lib/error-summary';
import initDismissNotice from "./modules/last_added_appliance";
import initDatadog from "./modules/datadog";

try {
// Initialise datadog monitoring
initDatadog();

initErrorSummary();
window.parent.postMessage(
{
Expand Down
8 changes: 7 additions & 1 deletion app/javascript/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
import initHeader from '@citizensadvice/design-system/lib/header';
import initGreedyNav from "./modules/greedy-nav";
import initSupplierTableButton from "./modules/supplier-table";
import initDatadog from "./modules/datadog";

try {
// Initialise design-system modules first
// Initialise datadog monitoring
initDatadog();

// Initialise design-system modules
initHeader();
initGreedyNav();

Expand All @@ -14,3 +18,5 @@ try {
document.querySelector("html").classList.add("no-js");
throw error;
}

throw new Error("This is a deliberate error from the energy apps javascript")
20 changes: 20 additions & 0 deletions app/javascript/modules/datadog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { datadogLogs } from '@datadog/browser-logs'

const initDatadog = () => {
const ddEnv = window.location.hostname === 'www.citizensadvice.org.uk' ? "prod" : "dev";
const appVersion = document.querySelector('meta[name="app-version"]').getAttribute('content');

if (ddEnv && appVersion) {
datadogLogs.init({
clientToken: 'pub32a7538113d7910e4304b8aa13ff4521',
site: 'datadoghq.eu',
forwardErrorsToLogs: true,
sessionSampleRate: 100,
service: 'energy-apps',
env: ddEnv,
version: appVersion
});
}
}

export default initDatadog;
2 changes: 2 additions & 0 deletions app/views/layouts/appliance_calculator.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
document.querySelector('html').classList.remove('no-js');
= stylesheet_link_tag "appliance-calculator"
= render "shared/head_datadog"
%body
= yield
Expand Down
1 change: 1 addition & 0 deletions app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
= render "shared/head_analytics"
= stylesheet_link_tag "energy-csr-table"
= render "shared/head_datadog"
%body{ class: ("cas" if scotland?) }
= render "shared/google_tag_manager_no_script"
Expand Down
1 change: 1 addition & 0 deletions app/views/shared/_head_datadog.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
%meta{ name: "app-version", content: ENV.fetch("APP_VERSION", nil) }
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"private": "true",
"dependencies": {
"@citizensadvice/design-system": "^6.2.0",
"@datadog/browser-logs": "^6.1.0",
"esbuild": "^0.24.2",
"eslint-plugin-import": "^2.31.0",
"sass": "^1.83.4"
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@
resolved "https://registry.yarnpkg.com/@csstools/selector-specificity/-/selector-specificity-5.0.0.tgz#037817b574262134cabd68fc4ec1a454f168407b"
integrity sha512-PCqQV3c4CoVm3kdPhyeZ07VmBRdH2EpMFA/pd9OASpOEC3aXNGoqPDAZ80D0cLpMBxnmk0+yNhGsEx31hq7Gtw==

"@datadog/browser-core@6.1.0":
version "6.1.0"
resolved "https://registry.yarnpkg.com/@datadog/browser-core/-/browser-core-6.1.0.tgz#789e367b4a5a692d5fc462d868031ca6f371cbe0"
integrity sha512-GQV4zkT1k6vdlFaSqRQ9oh4nPJ8aE47mw9gpD1bGIaxdnEDNtyJXo/KI17arwJBEznhcGf7pHI+2wLWx5SfQ8w==

"@datadog/browser-logs@^6.1.0":
version "6.1.0"
resolved "https://registry.yarnpkg.com/@datadog/browser-logs/-/browser-logs-6.1.0.tgz#40f7255f992e31f0eb9832ed584cd92159fc98b5"
integrity sha512-yk8eNrdhaU2BuD6ffYpjgJxGbdazA06mWqzsFvzJuQUu65E9jjv7154IRN2HCAqR4TCjJ3o8cfB11TyEuHIUpg==
dependencies:
"@datadog/browser-core" "6.1.0"

"@dual-bundle/import-meta-resolve@^4.1.0":
version "4.1.0"
resolved "https://registry.yarnpkg.com/@dual-bundle/import-meta-resolve/-/import-meta-resolve-4.1.0.tgz#519c1549b0e147759e7825701ecffd25e5819f7b"
Expand Down

0 comments on commit 11293af

Please sign in to comment.