Skip to content

Commit aeaf70c

Browse files
authored
refactor: logs and tracings (#313)
1 parent d061599 commit aeaf70c

File tree

5 files changed

+28
-17
lines changed

5 files changed

+28
-17
lines changed

src/authn/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,6 @@ export const upsertUserFromRequest = async ({
140140
throw new Error("Could not parse profile info", profileInfo.error);
141141
}
142142

143-
logger.info(`Updating profile Info for user ID: ${sub}`);
144-
145143
return upsertUserProfileInfo(DB, profileInfo.data, logger);
146144
};
147145

src/builder.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import SchemaBuilder from "@pothos/core";
22
import AuthzPlugin from "@pothos/plugin-authz";
33
import DataloaderPlugin from "@pothos/plugin-dataloader";
4-
import TracingPlugin, { wrapResolver } from "@pothos/plugin-tracing";
4+
import TracingPlugin, { isRootField } from "@pothos/plugin-tracing";
55
import { DateResolver, DateTimeResolver } from "graphql-scalars";
66

77
import * as rules from "~/authz";
8-
import { createLogger } from "~/logging";
98
import { Context } from "~/types";
109

11-
const tracingLogger = createLogger("tracing");
12-
1310
export const builder = new SchemaBuilder<{
1411
Context: Context;
1512
AuthZRule: keyof typeof rules;
@@ -26,13 +23,22 @@ export const builder = new SchemaBuilder<{
2623
}>({
2724
plugins: [TracingPlugin, AuthzPlugin, DataloaderPlugin],
2825
tracing: {
29-
default: () => true,
30-
wrap: (resolver, options, config) =>
31-
wrapResolver(resolver, (error, duration) => {
32-
tracingLogger.debug(
33-
`[TRACING] ${config.parentType}.${config.name} in ${duration}ms`,
34-
);
35-
}),
26+
default: (config) => isRootField(config),
27+
wrap: (resolver) => {
28+
return async (parent, args, context, info) => {
29+
const start = Date.now();
30+
const result = await resolver(parent, args, context, info);
31+
const duration = Date.now() - start;
32+
33+
context.logger.debug("[TRACING]", {
34+
path: info.path,
35+
duration,
36+
operation: info.operation?.operation,
37+
});
38+
39+
return result;
40+
};
41+
},
3642
},
3743
});
3844

src/context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const createGraphqlContext = async ({
3434
Env & {
3535
logger: Logger;
3636
}): Promise<Context> => {
37-
logger.info("graphql-params", params);
37+
logger.info("graphql-params", JSON.stringify(params, null, 2));
3838

3939
if (!MAIL_QUEUE) {
4040
throw new Error("Missing MAIL_QUEUE");

src/datasources/queries/users.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ export const upsertUserProfileInfo = async (
6363
throw new Error("User operation failed");
6464
}
6565

66-
logger.info(result.length > 1 ? "User updated" : "New user created");
67-
6866
return selectUsersSchema.parse(updatedUser);
6967
} catch (error) {
7068
logger.error("Error in user operation", { error });

src/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useMaskedErrors } from "@envelop/core";
22
import { useImmediateIntrospection } from "@envelop/immediate-introspection";
33
import { authZEnvelopPlugin } from "@graphql-authz/envelop-plugin";
4+
import { format } from "date-fns";
45
import { createYoga, maskError } from "graphql-yoga";
56

67
import { Env } from "worker-configuration";
@@ -78,6 +79,10 @@ export default {
7879
traceId,
7980
});
8081

82+
const start = Date.now();
83+
84+
logger.info(`⛳️ — Start Request ${format(start, "HH:mm:ss.SSS")}`);
85+
8186
logTraceId(req, logger);
8287

8388
logPossibleUserIdFromJWT(req, logger);
@@ -94,7 +99,11 @@ export default {
9499

95100
response.headers.set("x-api-trace-id", traceId);
96101

97-
logger.info("🏁 — End Request");
102+
const end = Date.now();
103+
104+
logger.info(
105+
`🏁 — End Request ${format(end, "HH:mm:ss.SSS")} (${end - start}ms)`,
106+
);
98107

99108
return response;
100109
},

0 commit comments

Comments
 (0)