Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

feat(observability): initialize HyperDX tracing SDK in api and worker

Carl-Gerhard Lindesvärdd19c5298d04e
diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts
index fd47a327c..903e1401a 100644
--- a/apps/api/src/index.ts
+++ b/apps/api/src/index.ts
@@ -1,7 +1,7 @@
/** biome-ignore-all lint/suspicious/useAwait: fastify need async or done callbacks */
process.env.TZ = 'UTC';
-import './utils/intercept-output';
+import './utils/observability';
import { rawStderrWrite } from '@openpanel/logger';
import sourceMapSupport from 'source-map-support';
diff --git a/apps/api/src/utils/intercept-output.ts b/apps/api/src/utils/intercept-output.ts
deleted file mode 100644
index f6b225429..000000000
--- a/apps/api/src/utils/intercept-output.ts
+++ /dev/null
@@ -1,6 +0,0 @@
-import { interceptProcessOutput } from '@openpanel/logger';
-import { logger } from './logger';
-
-// Side-effect module — must stay the first import in index.ts so output from
-// the rest of the boot sequence is captured too.
-interceptProcessOutput(logger);
diff --git a/apps/api/src/utils/observability.ts b/apps/api/src/utils/observability.ts
new file mode 100644
index 000000000..82a468596
--- /dev/null
+++ b/apps/api/src/utils/observability.ts
@@ -0,0 +1,27 @@
+import { init } from '@hyperdx/node-opentelemetry';
+import { getServiceName, interceptProcessOutput } from '@openpanel/logger';
+import { logger } from './logger';
+
+// Side-effect module — must stay the first import in index.ts.
+//
+// Traces only: logs ship via the pino transport and the output interceptor,
+// so the SDK's log pipeline and console capture stay off — one shipping path
+// per signal, nothing lands twice (see logging-capture-plan.md). Metrics stay
+// on prom-client/Grafana.
+//
+// tsdown bundles most deps (and hoists external imports above this code), so
+// require-hook auto-instrumentation only reaches diagnostics_channel-based
+// instrumentation (undici/fetch). To get spans from ioredis/bullmq/pg, move
+// them to `external` in tsdown.config.ts.
+if (process.env.HYPERDX_API_KEY) {
+ init({
+ apiKey: process.env.HYPERDX_API_KEY,
+ service: getServiceName('api'),
+ consoleCapture: false,
+ disableLogs: true,
+ disableMetrics: true,
+ disableStartupLogs: true,
+ });
+}
+
+interceptProcessOutput(logger);
diff --git a/apps/worker/src/index.ts b/apps/worker/src/index.ts
index c22d891c8..0a38bffa2 100644
--- a/apps/worker/src/index.ts
+++ b/apps/worker/src/index.ts
@@ -1,4 +1,4 @@
-import './utils/intercept-output';
+import './utils/observability';
import { createBullBoard } from '@bull-board/api';
import { BullMQAdapter } from '@bull-board/api/bullMQAdapter';
diff --git a/apps/worker/src/utils/intercept-output.ts b/apps/worker/src/utils/intercept-output.ts
deleted file mode 100644
index f6b225429..000000000
--- a/apps/worker/src/utils/intercept-output.ts
+++ /dev/null
@@ -1,6 +0,0 @@
-import { interceptProcessOutput } from '@openpanel/logger';
-import { logger } from './logger';
-
-// Side-effect module — must stay the first import in index.ts so output from
-// the rest of the boot sequence is captured too.
-interceptProcessOutput(logger);
diff --git a/apps/worker/src/utils/observability.ts b/apps/worker/src/utils/observability.ts
new file mode 100644
index 000000000..627b13c82
--- /dev/null
+++ b/apps/worker/src/utils/observability.ts
@@ -0,0 +1,27 @@
+import { init } from '@hyperdx/node-opentelemetry';
+import { getServiceName, interceptProcessOutput } from '@openpanel/logger';
+import { logger } from './logger';
+
+// Side-effect module — must stay the first import in index.ts.
+//
+// Traces only: logs ship via the pino transport and the output interceptor,
+// so the SDK's log pipeline and console capture stay off — one shipping path
+// per signal, nothing lands twice (see logging-capture-plan.md). Metrics stay
+// on prom-client/Grafana.
+//
+// tsdown bundles most deps (and hoists external imports above this code), so
+// require-hook auto-instrumentation only reaches diagnostics_channel-based
+// instrumentation (undici/fetch). To get spans from ioredis/bullmq/pg, move
+// them to `external` in tsdown.config.ts.
+if (process.env.HYPERDX_API_KEY) {
+ init({
+ apiKey: process.env.HYPERDX_API_KEY,
+ service: getServiceName('worker'),
+ consoleCapture: false,
+ disableLogs: true,
+ disableMetrics: true,
+ disableStartupLogs: true,
+ });
+}
+
+interceptProcessOutput(logger);
diff --git a/packages/logger/index.ts b/packages/logger/index.ts
index 6e7df04a4..fc75ecf0d 100644
--- a/packages/logger/index.ts
+++ b/packages/logger/index.ts
@@ -81,10 +81,16 @@ function redactSensitive(value: unknown, depth = 0): unknown {
return result;
}
-export function createLogger({ name }: { name: string }): ILogger {
- const service = [process.env.LOG_PREFIX, name, process.env.NODE_ENV ?? 'dev']
+// Shared by logs and traces so both signals land under the same service
+// name in ClickStack (e.g. new-api-production).
+export function getServiceName(name: string): string {
+ return [process.env.LOG_PREFIX, name, process.env.NODE_ENV ?? 'dev']
.filter(Boolean)
.join('-');
+}
+
+export function createLogger({ name }: { name: string }): ILogger {
+ const service = getServiceName(name);
const useHyperDX = logExporter === 'otlp' && !!process.env.HYPERDX_API_KEY;
const usePretty = !useHyperDX && process.env.NODE_ENV !== 'production';