Skip to content

Commit

Permalink
Add better loggering in webservice
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbroks committed Sep 21, 2024
1 parent b201df4 commit 15cb008
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 14 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/apps-event-worker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ on:
paths:
- apps/event-worker/**
- packages/db/**
- packages/job-dispatch/**
- packages/validators/**
- .github/workflows/apps-event-worker.yaml
- pnpm-lock.yaml
- packages/job-dispatch/**
push:
branches: ["main"]
paths:
- apps/event-worker/**
- packages/db/**
- packages/job-dispatch/**
- packages/validators/**
- .github/workflows/apps-event-worker.yaml
- pnpm-lock.yaml
- packages/job-dispatch/**


jobs:
build:
runs-on: ubuntu-latest
Expand Down
6 changes: 2 additions & 4 deletions apps/event-worker/src/target-scan/gke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,13 @@ export const getGkeTargets = async (
cluster,
);
return namespaces
.filter((n) => n.metadata != null)
.filter((n) => n.metadata?.name != null)
.map((n) =>
_.merge(clusterTarget, {
name: `${cluster.name ?? cluster.id ?? ""}/${n.metadata!.name}`,
kind: "Namespace",
identifier: `${project}/${cluster.name}/${n.metadata!.name}`,
config: {
namespace: n.metadata!.name,
},
config: { namespace: n.metadata!.name },
metadata: {
[ReservedMetadataKey.ParentTargetIdentifier]:
clusterTarget.identifier,
Expand Down
1 change: 1 addition & 0 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@ctrlplane/auth": "workspace:*",
"@ctrlplane/db": "workspace:*",
"@ctrlplane/job-dispatch": "workspace:*",
"@ctrlplane/logger": "workspace:*",
"@ctrlplane/validators": "workspace:*",
"@octokit/auth-app": "^7.1.0",
"@octokit/rest": "catalog:",
Expand Down
41 changes: 34 additions & 7 deletions packages/api/src/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ import { ZodError } from "zod";

import { can } from "@ctrlplane/auth/utils";
import { db } from "@ctrlplane/db/client";
import { logger } from "@ctrlplane/logger";

export const createTRPCContext = (opts: {
headers: Headers;
session: Session | null;
}) => {
const session = opts.session;
const source = opts.headers.get("x-trpc-source") ?? "unknown";

console.log(">>> tRPC Request from", source, "by", session?.user.email);

return { session, db };
const trpcSource = opts.headers.get("x-trpc-source") ?? "unknown";
return { trpcSource, session, db };
};

export type Context = ReturnType<typeof createTRPCContext>;
Expand Down Expand Up @@ -54,9 +52,37 @@ export const createCallerFactory = t.createCallerFactory;
*/
export const createTRPCRouter = t.router;

export const publicProcedure = t.procedure;
export const loggedProcedure = t.procedure.use(async (opts) => {
const start = Date.now();

const result = await opts.next();

const durationMs = Date.now() - start;

const session = opts.ctx.session;
const email = session?.user.email ?? "unknown";
const source = opts.ctx.trpcSource;

const meta = {
label: "trpc",
path: opts.path,
type: opts.type,
durationMs,
ok: result.ok,
};

const authnProcedure = t.procedure.use(({ ctx, next }) => {
const log = durationMs > 100 || !result.ok ? logger.warning : logger.info;
log(
`${result.ok ? "OK" : "NOT OK"} - request from ${source} by ${email}`,
meta,
);

return result;
});

export const publicProcedure = loggedProcedure;

const authnProcedure = loggedProcedure.use(({ ctx, next }) => {
if (!ctx.session?.user) throw new TRPCError({ code: "UNAUTHORIZED" });
return next({
ctx: {
Expand All @@ -65,6 +91,7 @@ const authnProcedure = t.procedure.use(({ ctx, next }) => {
},
});
});

const authzProcedure = authnProcedure.use(
async ({ ctx, meta, path, getRawInput, next }) => {
const { authorizationCheck } = meta ?? {};
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 15cb008

Please sign in to comment.