feat: setup opentel

This commit is contained in:
Henry
2026-03-23 22:12:31 +00:00
parent 596f346181
commit a0a2047305
6 changed files with 1561 additions and 31 deletions

View File

@@ -66,10 +66,15 @@ const config = {
},
},
},
serverExternalPackages: ["pino"],
serverExternalPackages: [
"pino",
"@opentelemetry/sdk-node",
"@opentelemetry/auto-instrumentations-node",
"@opentelemetry/exporter-trace-otlp-http",
],
experimental: {
// instrumentationHook: true,
instrumentationHook: true,
swcPlugins: [["@lingui/swc-plugin", {}]],
},

View File

@@ -34,6 +34,10 @@
"@lingui/macro": "^5.3.2",
"@lingui/react": "^5.3.2",
"@novu/api": "^3.11.0",
"@opentelemetry/api": "^1.9.0",
"@opentelemetry/auto-instrumentations-node": "^0.71.0",
"@opentelemetry/exporter-trace-otlp-http": "^0.213.0",
"@opentelemetry/sdk-node": "^0.213.0",
"@t3-oss/env-nextjs": "^0.11.1",
"@tailwindcss/typography": "^0.5.16",
"@tanstack/react-query": "catalog:",

View File

@@ -0,0 +1,32 @@
export async function register() {
if (
process.env.NEXT_RUNTIME !== "nodejs" ||
process.env.NEXT_PUBLIC_KAN_ENV !== "cloud" ||
!process.env.AXIOM_TOKEN ||
!process.env.AXIOM_DATASET
) {
return;
}
const { NodeSDK } = await import("@opentelemetry/sdk-node");
const { OTLPTraceExporter } = await import("@opentelemetry/exporter-trace-otlp-http");
const { getNodeAutoInstrumentations } = await import("@opentelemetry/auto-instrumentations-node");
const sdk = new NodeSDK({
serviceName: "kan",
traceExporter: new OTLPTraceExporter({
url: "https://api.axiom.co/v1/traces",
headers: {
Authorization: `Bearer ${process.env.AXIOM_TOKEN}`,
"X-Axiom-Dataset": process.env.AXIOM_DATASET,
},
}),
instrumentations: [
getNodeAutoInstrumentations({
"@opentelemetry/instrumentation-fs": { enabled: false },
}),
],
});
sdk.start();
}

View File

@@ -20,6 +20,7 @@
},
"dependencies": {
"@axiomhq/js": "^1.4.0",
"@opentelemetry/api": "^1.9.0",
"pino": "^9.14.0",
"pino-pretty": "^13.1.3"
},

View File

@@ -1,4 +1,5 @@
import { Axiom } from "@axiomhq/js";
import { trace } from "@opentelemetry/api";
import pino from "pino";
const isDev = process.env.NODE_ENV !== "production";
@@ -9,7 +10,10 @@ const axiomToken = process.env.AXIOM_TOKEN;
const axiomDataset = process.env.AXIOM_DATASET;
const useAxiom = isCloud && !!axiomToken && !!axiomDataset;
function createAxiomStream(token: string, dataset: string): pino.DestinationStream {
function createAxiomStream(
token: string,
dataset: string,
): pino.DestinationStream {
const client = new Axiom({ token });
return {
write(msg: string) {
@@ -35,9 +39,25 @@ export const logger = useAxiom
...(isDev && {
transport: {
target: "pino-pretty",
options: { colorize: true, ignore: "pid,hostname", translateTime: "HH:MM:ss" },
options: {
colorize: true,
ignore: "pid,hostname",
translateTime: "HH:MM:ss",
},
},
}),
});
export const createLogger = (module: string) => logger.child({ module });
function getTraceContext(): { traceId?: string; spanId?: string } {
try {
const span = trace.getActiveSpan();
if (!span) return {};
const ctx = span.spanContext();
return { traceId: ctx.traceId, spanId: ctx.spanId };
} catch {
return {};
}
}
export const createLogger = (module: string) =>
logger.child({ module, ...getTraceContext() });

1520
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff