From 21ba1aa9e781afe7b059986ad5ce468ba7c8e053 Mon Sep 17 00:00:00 2001 From: Dominik Broj Date: Thu, 20 Jun 2024 13:54:07 +0200 Subject: [PATCH] initialize faro only on prod (#4566) # What this PR does initialize faro only on prod ## Which issue(s) this PR closes Closes https://github.com/grafana/oncall-private/issues/2756 ## Checklist - [ ] Unit, integration, and e2e (if applicable) tests updated - [x] Documentation added (or `pr:no public docs` PR label added if not required) - [x] Added the relevant release notes label (see labels prefixed w/ `release:`). These labels dictate how your PR will show up in the autogenerated release notes. --- grafana-plugin/package.json | 6 +++--- grafana-plugin/src/utils/consts.ts | 13 ++++++++++++- grafana-plugin/src/utils/faro.ts | 3 ++- grafana-plugin/webpack.config.ts | 1 + 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/grafana-plugin/package.json b/grafana-plugin/package.json index e39bb446..e888a52c 100644 --- a/grafana-plugin/package.json +++ b/grafana-plugin/package.json @@ -7,8 +7,8 @@ "lint:fix": "eslint --max-warnings=0 --fix --cache --ext .js,.jsx,.ts,.tsx ./src ./e2e-tests", "stylelint": "stylelint ./src/**/*.{css,scss,module.css,module.scss}", "stylelint:fix": "stylelint --fix ./src/**/*.{css,scss,module.css,module.scss}", - "build": "webpack -c ./webpack.config.ts --env production", - "build:dev": "webpack -c ./webpack.config.ts --env development", + "build": "NODE_ENV=production webpack -c ./webpack.config.ts --env production", + "build:dev": "NODE_ENV=development webpack -c ./webpack.config.ts --env development", "labels:link": "yarn --cwd ../../gops-labels/frontend link && yarn link \"@grafana/labels\" && yarn --cwd ../../gops-labels/frontend watch", "labels:unlink": "yarn --cwd ../../gops-labels/frontend unlink", "test-utc": "TZ=UTC jest --verbose --testNamePattern '^((?!@london-tz).)*$'", @@ -23,7 +23,7 @@ "test:e2e:gen": "yarn playwright codegen http://localhost:3000", "e2e-show-report": "yarn playwright show-report", "generate-types": "cd ./src/network/oncall-api/types-generator && yarn generate", - "watch": "webpack -w -c ./webpack.config.ts --env development", + "watch": "NODE_ENV=development webpack -w -c ./webpack.config.ts --env development", "sign": "npx --yes @grafana/sign-plugin@latest", "start": "yarn watch", "plop": "plop", diff --git a/grafana-plugin/src/utils/consts.ts b/grafana-plugin/src/utils/consts.ts index ee691a70..7e26bae2 100644 --- a/grafana-plugin/src/utils/consts.ts +++ b/grafana-plugin/src/utils/consts.ts @@ -35,12 +35,23 @@ export const ONCALL_PROD = 'https://oncall-prod-us-central-0.grafana.net/oncall' export const ONCALL_OPS = 'https://oncall-ops-us-east-0.grafana.net/oncall'; export const ONCALL_DEV = 'https://oncall-dev-us-central-0.grafana.net/oncall'; +export const getProcessEnvVarSafely = (name: string) => { + try { + return process.env[name]; + } catch (error) { + console.error(error); + return undefined; + } +}; + +export const getIsDevelopmentEnv = () => getProcessEnvVarSafely['NODE_ENV'] === 'development'; + // Single source of truth on the frontend for OnCall API URL export const getOnCallApiUrl = (meta?: OnCallAppPluginMeta) => { if (meta?.jsonData?.onCallApiUrl) { return meta?.jsonData?.onCallApiUrl; } else if (typeof window === 'undefined') { - return process.env.ONCALL_API_URL; + return getProcessEnvVarSafely('ONCALL_API_URL'); } return undefined; }; diff --git a/grafana-plugin/src/utils/faro.ts b/grafana-plugin/src/utils/faro.ts index 782511aa..6b4c9df3 100644 --- a/grafana-plugin/src/utils/faro.ts +++ b/grafana-plugin/src/utils/faro.ts @@ -9,6 +9,7 @@ import { ONCALL_DEV, ONCALL_OPS, ONCALL_PROD, + getIsDevelopmentEnv, } from './consts'; import { safeJSONStringify } from './string'; @@ -31,7 +32,7 @@ class BaseFaroHelper { faro: Faro; initializeFaro(onCallApiUrl: string) { - if (this.faro) { + if (this.faro || getIsDevelopmentEnv()) { return undefined; } diff --git a/grafana-plugin/webpack.config.ts b/grafana-plugin/webpack.config.ts index 49c66ee9..cccf4a11 100644 --- a/grafana-plugin/webpack.config.ts +++ b/grafana-plugin/webpack.config.ts @@ -64,6 +64,7 @@ const config = async (env): Promise => { ...(env.development ? [new LiveReloadPlugin({ appendScriptTag: true, useSourceHash: true })] : []), new EnvironmentPlugin({ ONCALL_API_URL: null, + NODE_ENV: 'development', }), new DefinePlugin({ 'process.env': JSON.stringify(dotenv.config().parsed),