Skip to content

Commit

Permalink
Fix logger level baking issue
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastien-comeau committed Nov 16, 2023
1 parent dbd0067 commit a52df22
Show file tree
Hide file tree
Showing 21 changed files with 144 additions and 81 deletions.
19 changes: 10 additions & 9 deletions frontend/.env.example
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
LOGGING_LEVEL=debug
# Content-Security-Policy response header settings
# These settings have default values within the application, so should only be changed if the Adobe Analytics scripts change
ADOBE_ANALYTICS_CSP_DOMAINS=["*.demdex.net", "assets.adobedtm.com", "cm.everesttech.net", "code.jquery.com"]

# Adobe Analytics Script Source
NEXT_PUBLIC_ADOBE_ANALYTICS_SCRIPT_SRC=https://assets.adobedtm.com/be5dfd287373/1e84b99f81fb/launch-ffa1e01dbeab-staging.min.js
ADOBE_ANALYTICS_SCRIPT_SRC=https://assets.adobedtm.com/be5dfd287373/1e84b99f81fb/launch-ffa1e01dbeab-staging.min.js

# Analytics beacon execution delay in milliseconds (Default: 250)
NEXT_PUBLIC_ANALYTICS_BEACON_DELAY=250
ANALYTICS_BEACON_DELAY=250

# [REQUIRED] Base URI for the Application
NEXT_PUBLIC_APP_BASE_URI=https://example.com
APP_BASE_URI=https://example.com

# Variable to specify what environment app is currently deployed to (dev, staging, test, prod, etc.)
NEXT_PUBLIC_ENVIRONMENT=dev
ENVIRONMENT=dev

# Logging level (Default: info)
LOGGING_LEVEL=debug

# OpenTelemetry/Dynatrace settings
# Note: to disable metrics and/or tracing exporting, leave the respective endpoint undefined
Expand All @@ -20,7 +25,3 @@ OTEL_METRICS_ENDPOINT=https://example.com/e/00000000-0000-0000-0000-000000000000
OTEL_METRICS_EXPORT_INTERVAL_MILLIS=30000 # optional -- default: 60000
OTEL_METRICS_EXPORT_TIMEOUT_MILLIS=5000 # optional -- default: 30000
OTEL_TRACES_ENDPOINT=https://example.com/e/00000000-0000-0000-0000-000000000000/api/v2/otlp/v1/traces

# Content-Security-Policy response header settings
# These settings have default values within the application, so should only be changed if the Adobe Analytics scripts change
ADOBE_ANALYTICS_CSP_DOMAINS=["*.demdex.net", "assets.adobedtm.com", "cm.everesttech.net", "code.jquery.com"]
2 changes: 1 addition & 1 deletion frontend/__mocks__/next/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const getConfigMock = () => ({
publicRuntimeConfig: {
NEXT_PUBLIC_APP_BASE_URI: 'https://example.com',
APP_BASE_URI: 'https://example.com',
},
})

Expand Down
10 changes: 5 additions & 5 deletions frontend/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const nextConfig = {
NEXT_PUBLIC_BUILD_REVISION: process.env.BUILD_REVISION ?? '00000000',
NEXT_PUBLIC_BUILD_TIMESTAMP: new Date(process.env.BUILD_DATE ?? statSync('package.json').mtime).toISOString(),
NEXT_PUBLIC_BUILD_VERSION: process.env.BUILD_VERSION ?? '00000000-0000-00000000',
LOGGING_LEVEL: process.env.LOGGING_LEVEL ?? 'info',
},
experimental: {
instrumentationHook: true
Expand All @@ -32,10 +31,11 @@ const nextConfig = {
i18n: { ...i18n, localeDetection: false },
poweredByHeader: false,
publicRuntimeConfig: {
NEXT_PUBLIC_ADOBE_ANALYTICS_SCRIPT_SRC: process.env.NEXT_PUBLIC_ADOBE_ANALYTICS_SCRIPT_SRC,
NEXT_PUBLIC_ANALYTICS_BEACON_DELAY: process.env.NEXT_PUBLIC_ANALYTICS_BEACON_DELAY,
NEXT_PUBLIC_APP_BASE_URI: process.env.NEXT_PUBLIC_APP_BASE_URI,
NEXT_PUBLIC_ENVIRONMENT: process.env.NEXT_PUBLIC_ENVIRONMENT,
ADOBE_ANALYTICS_SCRIPT_SRC: process.env.ADOBE_ANALYTICS_SCRIPT_SRC,
ANALYTICS_BEACON_DELAY: process.env.ANALYTICS_BEACON_DELAY,
APP_BASE_URI: process.env.APP_BASE_URI,
ENVIRONMENT: process.env.ENVIRONMENT,
LOGGING_LEVEL: process.env.LOGGING_LEVEL ?? 'info',
},
reactStrictMode: true,
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/BreadcrumbStructuredData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const BreadcrumbStructuredData = ({ items }: BreadcrumbProps) => {
'@type': 'ListItem',
'position': index + 2,
'name': text,
'item': urlcat(publicRuntimeConfig.NEXT_PUBLIC_APP_BASE_URI, `/${locale ?? 'en'}${link}`),
'item': urlcat(publicRuntimeConfig.APP_BASE_URI, `/${locale ?? 'en'}${link}`),
})) ?? []),
]

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const Header = ({ gocLink, skipToMainText, breadcrumbItems, hideChecklist, class
const langSelectorLocale = locale === 'en' ? 'fr' : 'en'
const langSelectorAbbreviation = langSelectorLocale === 'fr' ? 'FR' : 'EN'
const langSelectorText = langSelectorLocale === 'fr' ? 'Français' : 'English'
const showBanner = publicRuntimeConfig.NEXT_PUBLIC_ENVIRONMENT !== 'prod'
const showBanner = publicRuntimeConfig.ENVIRONMENT !== 'prod'

return (
<>
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/pages/HomePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ export const HomePage = () => {
'@context': 'https://schema.org',
'@type': 'WebSite',
'name': t('common:application-name'),
'url': urlcat(publicRuntimeConfig.NEXT_PUBLIC_APP_BASE_URI, `/${locale ?? 'en'}`),
'url': urlcat(publicRuntimeConfig.APP_BASE_URI, `/${locale ?? 'en'}`),
}),
}),
[publicRuntimeConfig.NEXT_PUBLIC_APP_BASE_URI, locale, t],
[publicRuntimeConfig.APP_BASE_URI, locale, t],
)

return (
Expand Down
23 changes: 11 additions & 12 deletions frontend/src/lib/hooks/usePublicRuntimeConfig.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import { useMemo } from 'react'

import { NextConfig } from 'next'
import getConfig from 'next/config'

import { publicRuntimeConfigSchema } from '../schemas/public-runtime-config-schema'

export const usePublicRuntimeConfig = () => {
const config = getConfig() as NextConfig
const config = getConfig()

const { ADOBE_ANALYTICS_SCRIPT_SRC, ANALYTICS_BEACON_DELAY, APP_BASE_URI, ENVIRONMENT, LOGGING_LEVEL } =
config?.publicRuntimeConfig ?? {}

return useMemo(
() =>
publicRuntimeConfigSchema.validateSync({
NEXT_PUBLIC_ADOBE_ANALYTICS_SCRIPT_SRC: config.publicRuntimeConfig?.NEXT_PUBLIC_ADOBE_ANALYTICS_SCRIPT_SRC,
NEXT_PUBLIC_ANALYTICS_BEACON_DELAY: config.publicRuntimeConfig?.NEXT_PUBLIC_ANALYTICS_BEACON_DELAY,
NEXT_PUBLIC_APP_BASE_URI: config.publicRuntimeConfig?.NEXT_PUBLIC_APP_BASE_URI,
NEXT_PUBLIC_ENVIRONMENT: config.publicRuntimeConfig?.NEXT_PUBLIC_ENVIRONMENT,
ADOBE_ANALYTICS_SCRIPT_SRC,
ANALYTICS_BEACON_DELAY,
APP_BASE_URI,
ENVIRONMENT,
LOGGING_LEVEL,
}),
[
config.publicRuntimeConfig?.NEXT_PUBLIC_ADOBE_ANALYTICS_SCRIPT_SRC,
config.publicRuntimeConfig?.NEXT_PUBLIC_ANALYTICS_BEACON_DELAY,
config.publicRuntimeConfig?.NEXT_PUBLIC_APP_BASE_URI,
config.publicRuntimeConfig?.NEXT_PUBLIC_ENVIRONMENT,
]
[ADOBE_ANALYTICS_SCRIPT_SRC, ANALYTICS_BEACON_DELAY, APP_BASE_URI, ENVIRONMENT, LOGGING_LEVEL],
)
}
32 changes: 22 additions & 10 deletions frontend/src/lib/schemas/public-runtime-config-schema.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
import { LevelWithSilent } from 'pino'
import * as yup from 'yup'

export const levelsWithSilent: ReadonlyArray<LevelWithSilent> = [
'debug',
'error',
'fatal',
'info',
'silent',
'trace',
'warn',
]

export const publicRuntimeConfigSchema = yup.object({
NEXT_PUBLIC_ADOBE_ANALYTICS_SCRIPT_SRC: yup
.string()
.url('Environment variable NEXT_PUBLIC_ADOBE_ANALYTICS_SCRIPT_SRC must be a valid URL'),
NEXT_PUBLIC_ANALYTICS_BEACON_DELAY: yup
ADOBE_ANALYTICS_SCRIPT_SRC: yup.string().url('Env. variable ${path} must be a valid URL'),
ANALYTICS_BEACON_DELAY: yup
.number()
.integer('Environment variable NEXT_PUBLIC_ANALYTICS_BEACON_DELAY must be an integer')
.min(0, 'Environment variable NEXT_PUBLIC_ANALYTICS_BEACON_DELAY must be greater than or equal to 0')
.integer('Env. variable ${path} must be an integer')
.min(0, 'Env. variable ${path} must be greater than or equal to ${min}')
.default(() => 250),
NEXT_PUBLIC_APP_BASE_URI: yup
APP_BASE_URI: yup
.string()
.required('Env. variable ${path} is required')
.url('Env. variable ${path} must be a valid URL'),
ENVIRONMENT: yup.string(),
LOGGING_LEVEL: yup
.string()
.required('Environment variable NEXT_PUBLIC_APP_BASE_URI is required')
.url('Environment variable NEXT_PUBLIC_APP_BASE_URI must be a valid URL'),
NEXT_PUBLIC_ENVIRONMENT: yup.string(),
.oneOf(levelsWithSilent, 'Env. variable ${path} must be one of the following values: ${values}'),
})
13 changes: 10 additions & 3 deletions frontend/src/logging/log-level.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
const logLevelData = {
'*': process.env.LOGGING_LEVEL,
import getConfig from 'next/config'

export const getLoggingLevelConfig = (): string | undefined => {
// middleware can only read from process.env
if (process.env.LOGGING_LEVEL) return process.env.LOGGING_LEVEL
return getConfig()?.publicRuntimeConfig?.LOGGING_LEVEL
}

export const logLevelData = {
'*': getLoggingLevelConfig(),
// 'middleware': ''
// 'home': 'info',
// 'app': 'debug',
}
export default logLevelData
15 changes: 6 additions & 9 deletions frontend/src/logging/log-util.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import pino, { Logger, stdTimeFunctions } from 'pino'
import pino, { stdTimeFunctions } from 'pino'

import logLevelData from './log-level'
import { logLevelData } from './log-level'

const logLevels = new Map<string, string | undefined>(
Object.entries(logLevelData)
)

export function getLogLevel(logger: string): string {
return logLevels.get(logger) || logLevels.get('*') || 'info'
export const getLogLevel = (logger: string) => {
const logLevels = new Map(Object.entries(logLevelData))
return logLevels.get(logger) ?? logLevels.get('*') ?? 'info'
}

export function getLogger(name: string): Logger {
export const getLogger = (name: string) => {
return pino({
name,
level: getLogLevel(name),
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export interface MyAppProps extends AppProps {

const MyApp = ({ Component, emotionCache = clientSideEmotionCache, pageProps, router }: MyAppProps) => {
const publicRuntimeConfig = usePublicRuntimeConfig()
const nextSEOConfig = getNextSEOConfig(publicRuntimeConfig.NEXT_PUBLIC_APP_BASE_URI, router)
const nextSEOConfig = getNextSEOConfig(publicRuntimeConfig.APP_BASE_URI, router)

// XXX :: GjB :: this is just a sample metric!
requestCounter.add(1)
Expand All @@ -63,14 +63,14 @@ const MyApp = ({ Component, emotionCache = clientSideEmotionCache, pageProps, ro
logger.debug(
{
appPreviousLocationPathname,
beaconDelay: publicRuntimeConfig.NEXT_PUBLIC_ANALYTICS_BEACON_DELAY,
beaconDelay: publicRuntimeConfig.ANALYTICS_BEACON_DELAY,
documentTitle: document.title,
windowLocationPathname: window.location.pathname,
},
'Analytics beacon execution',
)
;(window as AppWindow).adobeDataLayer?.push?.({ event: 'pageLoad' })
}, publicRuntimeConfig.NEXT_PUBLIC_ANALYTICS_BEACON_DELAY)
}, publicRuntimeConfig.ANALYTICS_BEACON_DELAY)
}
}

Expand All @@ -82,7 +82,7 @@ const MyApp = ({ Component, emotionCache = clientSideEmotionCache, pageProps, ro
router.events.off('routeChangeComplete', handleRouteChange)
router.events.off('hashChangeComplete', handleRouteChange)
}
}, [publicRuntimeConfig.NEXT_PUBLIC_ANALYTICS_BEACON_DELAY, router.events])
}, [publicRuntimeConfig.ANALYTICS_BEACON_DELAY, router.events])

return (
<CacheProvider value={emotionCache}>
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { MyAppProps } from './_app'

const log = getLogger('_document.tsx')

const adobeAnalyticsConfigured = process.env.NEXT_PUBLIC_ADOBE_ANALYTICS_SCRIPT_SRC !== undefined
const adobeAnalyticsConfigured = process.env.ADOBE_ANALYTICS_SCRIPT_SRC !== undefined
const devmodeEnabled = process.env.NODE_ENV !== 'production'

// see https://experienceleague.adobe.com/docs/id-service/using/reference/csp.html
Expand Down Expand Up @@ -125,7 +125,7 @@ export default function MyDocument({ emotionStyleTags, locale, nonce }: MyDocume
<Script strategy="beforeInteractive" src="https://code.jquery.com/jquery-3.6.3.min.js" />
)}
{adobeAnalyticsConfigured && (
<Script strategy="beforeInteractive" src={process.env.NEXT_PUBLIC_ADOBE_ANALYTICS_SCRIPT_SRC} />
<Script strategy="beforeInteractive" src={process.env.ADOBE_ANALYTICS_SCRIPT_SRC} />
)}
</body>
</Html>
Expand Down
1 change: 0 additions & 1 deletion frontend/src/pages/api/readyz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export default function handler(_req: NextApiRequest, res: NextApiResponse<Ready
try {
// validate public runtime configuration
const publicRuntimeConfig = publicRuntimeConfigSchema.validateSync(process.env, { stripUnknown: true })

res.status(200).json({
publicRuntimeConfig,
status: 'UP',
Expand Down
4 changes: 2 additions & 2 deletions gitops/environments/dev/deployments.patch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ spec:
containers:
- name: seniors-journey-frontend
env:
- name: NEXT_PUBLIC_APP_BASE_URI
- name: APP_BASE_URI
valueFrom:
configMapKeyRef:
name: seniors-journey-frontend
key: NEXT_PUBLIC_APP_BASE_URI
key: APP_BASE_URI
- name: OTEL_API_KEY
valueFrom:
secretKeyRef:
Expand Down
2 changes: 1 addition & 1 deletion gitops/environments/dev/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ configMapGenerator:
labels:
app.kubernetes.io/name: seniors-journey-frontend
literals:
- NEXT_PUBLIC_APP_BASE_URI=https://seniors-journey-dev.dev-dp.dts-stn.com/
- APP_BASE_URI=https://seniors-journey-dev.dev-dp.dts-stn.com/
- OTEL_ENVIRONMENT=dev
- OTEL_METRICS_ENDPOINT=https://dynatrace.dev-dp.admin.dts-stn.com/e/21a07aef-852b-4d9b-aa87-ee5f8b79f8c9/api/v2/otlp/v1/metrics
- OTEL_TRACES_ENDPOINT=https://dynatrace.dev-dp.admin.dts-stn.com/e/21a07aef-852b-4d9b-aa87-ee5f8b79f8c9/api/v2/otlp/v1/traces
Expand Down
29 changes: 25 additions & 4 deletions gitops/environments/production/deployments.patch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,47 @@ spec:
- name: seniors-journey-frontend
image: dtsrhpprodscedspokeacr.azurecr.io/seniors-journey/seniors-journey-frontend:1.0.0
env:
# TODO: REMOVE NEXT_PUBLIC_* ON NEXT RELEASE (REMOVED SINCE 2023-11-16)
- name: APP_BASE_URI
valueFrom:
configMapKeyRef:
name: seniors-journey-frontend
key: APP_BASE_URI
- name: NEXT_PUBLIC_APP_BASE_URI
valueFrom:
configMapKeyRef:
name: seniors-journey-frontend
key: NEXT_PUBLIC_APP_BASE_URI
key: APP_BASE_URI
- name: ANALYTICS_BEACON_DELAY
valueFrom:
configMapKeyRef:
name: seniors-journey-frontend
key: ANALYTICS_BEACON_DELAY
- name: NEXT_PUBLIC_ANALYTICS_BEACON_DELAY
valueFrom:
configMapKeyRef:
name: seniors-journey-frontend
key: NEXT_PUBLIC_ANALYTICS_BEACON_DELAY
key: ANALYTICS_BEACON_DELAY
- name: ADOBE_ANALYTICS_SCRIPT_SRC
valueFrom:
configMapKeyRef:
name: seniors-journey-frontend
key: ADOBE_ANALYTICS_SCRIPT_SRC
- name: NEXT_PUBLIC_ADOBE_ANALYTICS_SCRIPT_SRC
valueFrom:
configMapKeyRef:
name: seniors-journey-frontend
key: NEXT_PUBLIC_ADOBE_ANALYTICS_SCRIPT_SRC
key: ADOBE_ANALYTICS_SCRIPT_SRC
- name: ENVIRONMENT
valueFrom:
configMapKeyRef:
name: seniors-journey-frontend
key: ENVIRONMENT
- name: NEXT_PUBLIC_ENVIRONMENT
valueFrom:
configMapKeyRef:
name: seniors-journey-frontend
key: NEXT_PUBLIC_ENVIRONMENT
key: ENVIRONMENT
- name: OTEL_API_KEY
valueFrom:
secretKeyRef:
Expand Down
8 changes: 4 additions & 4 deletions gitops/environments/production/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ configMapGenerator:
labels:
app.kubernetes.io/name: seniors-journey-frontend
literals:
- NEXT_PUBLIC_ADOBE_ANALYTICS_SCRIPT_SRC=https://assets.adobedtm.com/be5dfd287373/9b9cb7867b5b/launch-59d77766b86a.min.js
- NEXT_PUBLIC_ANALYTICS_BEACON_DELAY=500
- NEXT_PUBLIC_APP_BASE_URI=https://retraite-retirement.service.canada.ca/
- NEXT_PUBLIC_ENVIRONMENT=prod
- ADOBE_ANALYTICS_SCRIPT_SRC=https://assets.adobedtm.com/be5dfd287373/9b9cb7867b5b/launch-59d77766b86a.min.js
- ANALYTICS_BEACON_DELAY=500
- APP_BASE_URI=https://retraite-retirement.service.canada.ca/
- ENVIRONMENT=prod
- OTEL_ENVIRONMENT=production
- OTEL_METRICS_ENDPOINT=https://dynatrace.prod-dp.admin.dts-stn.com/e/676a0299-9802-4933-99d4-481318a557db/api/v2/otlp/v1/metrics
- OTEL_TRACES_ENDPOINT=https://dynatrace.prod-dp.admin.dts-stn.com/e/676a0299-9802-4933-99d4-481318a557db/api/v2/otlp/v1/traces
Expand Down
Loading

0 comments on commit a52df22

Please sign in to comment.