Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(nodejs): don't evaluate plugin-server config at logger import (#70895)

Arno Hillen2585d9c333d2
diff --git a/nodejs/src/common/utils/logger.test.ts b/nodejs/src/common/utils/logger.test.ts
index fd37529c7c65..9cb0dd96dc08 100644
--- a/nodejs/src/common/utils/logger.test.ts
+++ b/nodejs/src/common/utils/logger.test.ts
@@ -60,4 +60,38 @@ describe('Logger', () => {
]
`)
})
+
+ // Entrypoints without plugin-server database env (e.g. the recording rasterizer)
+ // import this module transitively. Guards against reintroducing a defaultConfig
+ // import, which throws at module load when that env is missing. NODE_ENV must be
+ // production here: in the test env, config falls back to a default DATABASE_URL,
+ // so the throw this test guards against would never fire.
+ it('loads without plugin-server database env', async () => {
+ const saved: Record<string, string | undefined> = {
+ NODE_ENV: process.env.NODE_ENV,
+ DEBUG: process.env.DEBUG,
+ DATABASE_URL: process.env.DATABASE_URL,
+ POSTHOG_DB_NAME: process.env.POSTHOG_DB_NAME,
+ }
+ process.env.NODE_ENV = 'production'
+ delete process.env.DEBUG
+ delete process.env.DATABASE_URL
+ delete process.env.POSTHOG_DB_NAME
+ let isolated: typeof import('./logger') | undefined
+ try {
+ jest.isolateModules(() => {
+ isolated = require('./logger')
+ })
+ } finally {
+ for (const [key, value] of Object.entries(saved)) {
+ if (value === undefined) {
+ delete process.env[key]
+ } else {
+ process.env[key] = value
+ }
+ }
+ }
+ expect(isolated!.logger).toBeInstanceOf(isolated!.Logger)
+ await isolated!.shutdownLogger()
+ })
})
diff --git a/nodejs/src/common/utils/logger.ts b/nodejs/src/common/utils/logger.ts
index 3b00611b662e..2332225f8894 100644
--- a/nodejs/src/common/utils/logger.ts
+++ b/nodejs/src/common/utils/logger.ts
@@ -1,9 +1,13 @@
import pino from 'pino'
-import { LogLevel } from '~/types'
+import type { LogLevel } from '~/types'
-import { defaultConfig } from '../config/config'
-import { isProdEnv } from './env-utils'
+import { isProdEnv, isTestEnv } from './env-utils'
+
+// Read from env directly instead of defaultConfig: that module throws at import when
+// Postgres env vars are missing, and entrypoints without them (e.g. the recording
+// rasterizer) reach this logger transitively.
+const logLevel: LogLevel = (process.env.LOG_LEVEL as LogLevel) || (isTestEnv() ? 'warn' : 'info')
export class Logger {
private pino: ReturnType<typeof pino>
@@ -13,7 +17,6 @@ export class Logger {
constructor(name: string) {
this.prefix = `[${name.toUpperCase()}]`
- const logLevel: LogLevel = defaultConfig.LOG_LEVEL
if (isProdEnv()) {
this.pino = pino({
// By default pino will log the level number. So we can easily unify
@@ -108,9 +111,7 @@ export class Logger {
}
}
-// TODO: remove defaultConfig import — it creates circular imports when config files
-// import anything that transitively reaches this module. The logger should not depend on config.
-export const logger = new Logger(defaultConfig.PLUGIN_SERVER_MODE ?? 'MAIN')
+export const logger = new Logger(process.env.PLUGIN_SERVER_MODE ?? 'MAIN')
export function serializeError(error: unknown): Record<string, unknown> | unknown {
if (error instanceof Error) {
diff --git a/nodejs/src/session-replay/recording-rasterizer/__tests__/otel-metrics.test.ts b/nodejs/src/session-replay/recording-rasterizer/__tests__/otel-metrics.test.ts
new file mode 100644
index 000000000000..c67dfeaf93b3
--- /dev/null
+++ b/nodejs/src/session-replay/recording-rasterizer/__tests__/otel-metrics.test.ts
@@ -0,0 +1,35 @@
+// The rasterizer deployment has no plugin-server database env, so its whole import
+// graph must load without it. Guards the otlp metrics chain (shared instruments,
+// provider, exporter, logger) against reintroducing a module that evaluates
+// defaultConfig at load, which crashloops the worker at boot. NODE_ENV must be
+// production here: in the test env, config falls back to a default DATABASE_URL,
+// so the throw this test guards against would never fire.
+describe('otel-metrics import chain', () => {
+ it('loads without plugin-server database env', () => {
+ const saved: Record<string, string | undefined> = {
+ NODE_ENV: process.env.NODE_ENV,
+ DEBUG: process.env.DEBUG,
+ DATABASE_URL: process.env.DATABASE_URL,
+ POSTHOG_DB_NAME: process.env.POSTHOG_DB_NAME,
+ }
+ process.env.NODE_ENV = 'production'
+ delete process.env.DEBUG
+ delete process.env.DATABASE_URL
+ delete process.env.POSTHOG_DB_NAME
+ let mod: typeof import('../otel-metrics') | undefined
+ try {
+ jest.isolateModules(() => {
+ mod = require('../otel-metrics')
+ })
+ } finally {
+ for (const [key, value] of Object.entries(saved)) {
+ if (value === undefined) {
+ delete process.env[key]
+ } else {
+ process.env[key] = value
+ }
+ }
+ }
+ expect(mod!.initMetrics).toBeInstanceOf(Function)
+ })
+})