Compare commits
9 Commits
feat/log-d
...
feat/opent
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a0a2047305 | ||
|
|
596f346181 | ||
|
|
7b59e2ac07 | ||
|
|
2a9b1fd2b5 | ||
|
|
a2d4314e14 | ||
|
|
f94e4d6da2 | ||
|
|
97f4defc0e | ||
|
|
dd29dc89fd | ||
|
|
486cdf8313 |
@@ -28,6 +28,7 @@ const config = {
|
|||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
/** Enables hot reloading for local packages without a build step */
|
/** Enables hot reloading for local packages without a build step */
|
||||||
transpilePackages: [
|
transpilePackages: [
|
||||||
"@kan/api",
|
"@kan/api",
|
||||||
@@ -65,8 +66,15 @@ const config = {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
serverExternalPackages: [
|
||||||
|
"pino",
|
||||||
|
"@opentelemetry/sdk-node",
|
||||||
|
"@opentelemetry/auto-instrumentations-node",
|
||||||
|
"@opentelemetry/exporter-trace-otlp-http",
|
||||||
|
],
|
||||||
|
|
||||||
experimental: {
|
experimental: {
|
||||||
// instrumentationHook: true,
|
instrumentationHook: true,
|
||||||
swcPlugins: [["@lingui/swc-plugin", {}]],
|
swcPlugins: [["@lingui/swc-plugin", {}]],
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@@ -34,6 +34,10 @@
|
|||||||
"@lingui/macro": "^5.3.2",
|
"@lingui/macro": "^5.3.2",
|
||||||
"@lingui/react": "^5.3.2",
|
"@lingui/react": "^5.3.2",
|
||||||
"@novu/api": "^3.11.0",
|
"@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",
|
"@t3-oss/env-nextjs": "^0.11.1",
|
||||||
"@tailwindcss/typography": "^0.5.16",
|
"@tailwindcss/typography": "^0.5.16",
|
||||||
"@tanstack/react-query": "catalog:",
|
"@tanstack/react-query": "catalog:",
|
||||||
|
|||||||
32
apps/web/src/instrumentation.ts
Normal file
32
apps/web/src/instrumentation.ts
Normal 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();
|
||||||
|
}
|
||||||
@@ -227,7 +227,7 @@ export const boardRouter = createTRPCRouter({
|
|||||||
boardSlug: z
|
boardSlug: z
|
||||||
.string()
|
.string()
|
||||||
.min(3)
|
.min(3)
|
||||||
.max(24)
|
.max(60)
|
||||||
.regex(/^(?![-]+$)[a-zA-Z0-9-]+$/),
|
.regex(/^(?![-]+$)[a-zA-Z0-9-]+$/),
|
||||||
members: z.array(z.string().min(12)).optional(),
|
members: z.array(z.string().min(12)).optional(),
|
||||||
labels: z.array(z.string().min(12)).optional(),
|
labels: z.array(z.string().min(12)).optional(),
|
||||||
@@ -657,7 +657,7 @@ export const boardRouter = createTRPCRouter({
|
|||||||
boardSlug: z
|
boardSlug: z
|
||||||
.string()
|
.string()
|
||||||
.min(3)
|
.min(3)
|
||||||
.max(24)
|
.max(60)
|
||||||
.regex(/^(?![-]+$)[a-zA-Z0-9-]+$/),
|
.regex(/^(?![-]+$)[a-zA-Z0-9-]+$/),
|
||||||
boardPublicId: z.string().min(12),
|
boardPublicId: z.string().min(12),
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import type { CreateNextContextOptions } from "@trpc/server/adapters/next";
|
|||||||
import type { NextApiRequest } from "next";
|
import type { NextApiRequest } from "next";
|
||||||
import type { OpenApiMeta } from "trpc-to-openapi";
|
import type { OpenApiMeta } from "trpc-to-openapi";
|
||||||
import { initTRPC, TRPCError } from "@trpc/server";
|
import { initTRPC, TRPCError } from "@trpc/server";
|
||||||
import { getHTTPStatusCodeFromError } from "@trpc/server/http";
|
|
||||||
import { env } from "next-runtime-env";
|
import { env } from "next-runtime-env";
|
||||||
import superjson from "superjson";
|
import superjson from "superjson";
|
||||||
import { ZodError } from "zod";
|
import { ZodError } from "zod";
|
||||||
@@ -15,6 +14,27 @@ import { createLogger } from "@kan/logger";
|
|||||||
|
|
||||||
const log = createLogger("api");
|
const log = createLogger("api");
|
||||||
|
|
||||||
|
const TRPC_STATUS_MAP: Partial<Record<TRPCError["code"], number>> = {
|
||||||
|
PARSE_ERROR: 400,
|
||||||
|
BAD_REQUEST: 400,
|
||||||
|
UNAUTHORIZED: 401,
|
||||||
|
FORBIDDEN: 403,
|
||||||
|
NOT_FOUND: 404,
|
||||||
|
METHOD_NOT_SUPPORTED: 405,
|
||||||
|
TIMEOUT: 408,
|
||||||
|
CONFLICT: 409,
|
||||||
|
PRECONDITION_FAILED: 412,
|
||||||
|
PAYLOAD_TOO_LARGE: 413,
|
||||||
|
UNPROCESSABLE_CONTENT: 422,
|
||||||
|
TOO_MANY_REQUESTS: 429,
|
||||||
|
CLIENT_CLOSED_REQUEST: 499,
|
||||||
|
INTERNAL_SERVER_ERROR: 500,
|
||||||
|
NOT_IMPLEMENTED: 501,
|
||||||
|
BAD_GATEWAY: 502,
|
||||||
|
SERVICE_UNAVAILABLE: 503,
|
||||||
|
GATEWAY_TIMEOUT: 504,
|
||||||
|
};
|
||||||
|
|
||||||
export interface User {
|
export interface User {
|
||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
@@ -110,8 +130,7 @@ export const createRESTContext = async ({ req }: CreateNextContextOptions) => {
|
|||||||
try {
|
try {
|
||||||
session = await auth.api.getSession();
|
session = await auth.api.getSession();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log.error({ err: error }, "Error getting session");
|
log.warn({ err: error }, "Failed to get session, treating as unauthenticated");
|
||||||
throw error;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return createInnerTRPCContext({
|
return createInnerTRPCContext({
|
||||||
@@ -170,7 +189,7 @@ const loggingMiddleware = t.middleware(async ({ path, type, next, ctx }) => {
|
|||||||
if (result.ok) {
|
if (result.ok) {
|
||||||
log.info({ ...meta, status: 200 }, `${label} OK`);
|
log.info({ ...meta, status: 200 }, `${label} OK`);
|
||||||
} else {
|
} else {
|
||||||
const status = getHTTPStatusCodeFromError(result.error);
|
const status = TRPC_STATUS_MAP[result.error.code] ?? 500;
|
||||||
const errorCode = result.error.code;
|
const errorCode = result.error.code;
|
||||||
log.error(
|
log.error(
|
||||||
{ ...meta, status, errorCode, err: result.error },
|
{ ...meta, status, errorCode, err: result.error },
|
||||||
|
|||||||
@@ -19,7 +19,8 @@
|
|||||||
"typecheck": "tsc --noEmit --emitDeclarationOnly false"
|
"typecheck": "tsc --noEmit --emitDeclarationOnly false"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@axiomhq/pino": "^1.4.0",
|
"@axiomhq/js": "^1.4.0",
|
||||||
|
"@opentelemetry/api": "^1.9.0",
|
||||||
"pino": "^9.14.0",
|
"pino": "^9.14.0",
|
||||||
"pino-pretty": "^13.1.3"
|
"pino-pretty": "^13.1.3"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import { Axiom } from "@axiomhq/js";
|
||||||
|
import { trace } from "@opentelemetry/api";
|
||||||
import pino from "pino";
|
import pino from "pino";
|
||||||
|
|
||||||
const isDev = process.env.NODE_ENV !== "production";
|
const isDev = process.env.NODE_ENV !== "production";
|
||||||
@@ -8,34 +10,54 @@ const axiomToken = process.env.AXIOM_TOKEN;
|
|||||||
const axiomDataset = process.env.AXIOM_DATASET;
|
const axiomDataset = process.env.AXIOM_DATASET;
|
||||||
const useAxiom = isCloud && !!axiomToken && !!axiomDataset;
|
const useAxiom = isCloud && !!axiomToken && !!axiomDataset;
|
||||||
|
|
||||||
const targets: pino.TransportTargetOptions[] = [];
|
function createAxiomStream(
|
||||||
|
token: string,
|
||||||
if (isDev) {
|
dataset: string,
|
||||||
targets.push({
|
): pino.DestinationStream {
|
||||||
target: "pino-pretty",
|
const client = new Axiom({ token });
|
||||||
options: {
|
return {
|
||||||
colorize: true,
|
write(msg: string) {
|
||||||
ignore: "pid,hostname",
|
try {
|
||||||
translateTime: "HH:MM:ss",
|
client.ingest(dataset, [JSON.parse(msg) as Record<string, unknown>]);
|
||||||
|
} catch {
|
||||||
|
// ignore malformed log lines
|
||||||
|
}
|
||||||
},
|
},
|
||||||
});
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (useAxiom) {
|
export const logger = useAxiom
|
||||||
targets.push({
|
? pino(
|
||||||
target: "@axiomhq/pino",
|
{ level },
|
||||||
options: {
|
pino.multistream([
|
||||||
dataset: axiomDataset,
|
{ stream: process.stdout, level },
|
||||||
token: axiomToken,
|
{ stream: createAxiomStream(axiomToken, axiomDataset), level },
|
||||||
},
|
]),
|
||||||
});
|
)
|
||||||
|
: pino({
|
||||||
|
level,
|
||||||
|
...(isDev && {
|
||||||
|
transport: {
|
||||||
|
target: "pino-pretty",
|
||||||
|
options: {
|
||||||
|
colorize: true,
|
||||||
|
ignore: "pid,hostname",
|
||||||
|
translateTime: "HH:MM:ss",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
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 logger = pino({
|
export const createLogger = (module: string) =>
|
||||||
level,
|
logger.child({ module, ...getTraceContext() });
|
||||||
...(targets.length > 0 && {
|
|
||||||
transport: { targets },
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
export const createLogger = (module: string) => logger.child({ module });
|
|
||||||
|
|||||||
1571
pnpm-lock.yaml
generated
1571
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user