Skip to content

Commit

Permalink
chore: add userId and sessionId to logs, update meilisearch version t…
Browse files Browse the repository at this point in the history
…o v1.12.0
  • Loading branch information
Shunseii committed Jan 8, 2025
1 parent 15a5847 commit 7f168e3
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 16 deletions.
33 changes: 21 additions & 12 deletions apps/api/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,23 @@ export const auth = betterAuth({
// TODO: add trace ids to these logs
// Upstash client will automatically deserialize JSON strings
get: async (key) => {
logger.debug({ key, category: LogCategory.DATABASE, event: "redis_get" });
logger.debug(
{ key, category: LogCategory.DATABASE, event: "redis_get" },
"Better auth get from redis.",
);

return await redisClient.get(key);
},
set: async (key, value, ttl) => {
logger.debug({
key,
ttl,
category: LogCategory.DATABASE,
event: "redis_set",
});
logger.debug(
{
key,
ttl,
category: LogCategory.DATABASE,
event: "redis_set",
},
"Better auth set to redis.",
);

if (ttl) {
await redisClient.set(key, JSON.stringify(value), { ex: ttl });
Expand All @@ -199,11 +205,14 @@ export const auth = betterAuth({
}
},
delete: async (key) => {
logger.debug({
key,
category: LogCategory.DATABASE,
event: "redis_delete",
});
logger.debug(
{
key,
category: LogCategory.DATABASE,
event: "redis_delete",
},
"Beter auth delete from redis.",
);

await redisClient.del(key);
},
Expand Down
5 changes: 4 additions & 1 deletion apps/api/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { AsyncLocalStorage } from "async_hooks";

interface TraceContext {
traceId?: string;
userId?: string;
sessionId?: string;
seqNum: number;
}

Expand All @@ -15,7 +17,6 @@ export const getTraceContext = (): TraceContext => {

if (!context) {
return {
traceId: undefined,
seqNum: 0,
};
}
Expand Down Expand Up @@ -45,6 +46,8 @@ export const logger = pino({
return {
traceId: context.traceId,
seqNum: context.seqNum,
userId: context.userId,
sessionId: context.sessionId,
};
},
serializers: {
Expand Down
7 changes: 6 additions & 1 deletion apps/api/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NextFunction, RequestHandler, Request, Response } from "express";
import { fromNodeHeaders } from "better-auth/node";
import { Session, User, auth as authClient } from "./auth";
import crypto from "crypto";
import { traceContext } from "./logger";
import { getTraceContext, traceContext } from "./logger";

export const auth: RequestHandler = async (req, res, next) => {
const { api } = authClient;
Expand All @@ -15,6 +15,11 @@ export const auth: RequestHandler = async (req, res, next) => {
return res.status(401).json({ message: "Unauthorized" });
}

const context = getTraceContext();

context.userId = user.id;
context.sessionId = session.id;

req.user = user;
req.session = session;

Expand Down
6 changes: 5 additions & 1 deletion apps/api/src/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TRPCError, initTRPC } from "@trpc/server";
import * as trpcExpress from "@trpc/server/adapters/express";
import { fromNodeHeaders } from "better-auth/node";
import { Session, User, auth } from "./auth";
import { logger } from "./logger";
import { getTraceContext, logger } from "./logger";

// Created for each request
export const createContext = async ({
Expand All @@ -26,8 +26,12 @@ export const createContext = async ({
};
}

const context = getTraceContext();
const { session, user } = data;

context.userId = user.id;
context.sessionId = session.id;

return {
session,
user,
Expand Down
2 changes: 1 addition & 1 deletion apps/search/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM getmeili/meilisearch:v1.3.4
FROM getmeili/meilisearch:v1.12.0

COPY ./apps/search/config.toml /etc/meilisearch/config.toml

Expand Down

0 comments on commit 7f168e3

Please sign in to comment.