Skip to content

Commit b86be19

Browse files
authored
logs (#98)
1 parent 25a3732 commit b86be19

6 files changed

Lines changed: 101 additions & 51 deletions

File tree

.eslintrc.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module.exports = {
3232
// No nos sirve esta regla realmente y causa varios falsos positivos
3333
// https://typescript-eslint.io/rules/no-non-null-assertion/
3434
"@typescript-eslint/no-unsafe-arguments": "off",
35-
"no-console": "error",
35+
// "no-console": "error", // We capture console.log on our logger system, so we allow it
3636
curly: ["error", "all"],
3737
},
3838
},

package-lock.json

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
"@pothos/plugin-authz": "^3.5.8",
9090
"@pothos/plugin-tracing": "^0.5.8",
9191
"@sanity/client": "^6.7.0",
92+
"@tsndr/cloudflare-worker-jwt": "^2.3.2",
9293
"@types/react": "^18.2.22",
9394
"cookie": "^0.5.0",
9495
"dbdocs": "^0.8.2",

src/index.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { initContextCache } from "@pothos/core";
1010
import { useOpenTelemetry } from "@envelop/opentelemetry";
1111
import { provider } from "~/obs/exporter";
1212
import { verifyToken } from "@clerk/backend";
13+
import jwt from "@tsndr/cloudflare-worker-jwt";
1314
import {
1415
ProfileInfoSchema,
1516
updateUserProfileInfo,
@@ -71,6 +72,24 @@ const getUser = async ({
7172
return updateUserProfileInfo(DB, profileInfo);
7273
};
7374

75+
const attachPossibleUserIdFromJWT = (request: Request) => {
76+
const JWT_TOKEN = (request.headers.get("Authorization") ?? "").split(" ")[1];
77+
if (!JWT_TOKEN) {
78+
console.info("No token present");
79+
return null;
80+
}
81+
try {
82+
const { payload } = jwt.decode(JWT_TOKEN);
83+
const userId = (payload as { id: string })?.id ?? "ANONYMOUS";
84+
H.setAttributes({
85+
userId: userId,
86+
});
87+
} catch (error) {
88+
console.error("Could not parse token", error);
89+
return null;
90+
}
91+
};
92+
7493
export const yoga = createYoga<Env>({
7594
landingPage: APP_ENV !== "production",
7695
graphqlEndpoint: "/graphql",
@@ -177,6 +196,8 @@ export default {
177196
H.setAttributes({
178197
APP_ENV: APP_ENV ?? "none",
179198
});
199+
200+
attachPossibleUserIdFromJWT(req);
180201
// eslint-disable-next-line no-console
181202
console.log("🏁 — Initialize Request");
182203
const response = await yoga.fetch(
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
RESEND_EMAIL_KEY=""
1+
SANITY_PROJECT_ID=
2+
SANITY_DATASET=
3+
SANITY_API_VERSION=
4+
SANITY_SECRET_TOKEN=
5+
HIGHLIGHT_PROJECT_ID=
Lines changed: 69 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
/* eslint-disable no-console */
2+
import { H } from "@highlight-run/cloudflare";
23
import { createClient } from "@sanity/client";
34
import { ensureKeys } from "../utils";
45
import { GoogleImportQueueElement } from "../../src/datasources/queues/google_import";
56
import { v5 } from "uuid";
7+
import { APP_ENV } from "../../src/env";
68

79
type ENV = {
810
SANITY_PROJECT_ID: string;
11+
HIGHLIGHT_PROJECT_ID: string;
912
SANITY_DATASET: string;
1013
SANITY_API_VERSION: string;
1114
SANITY_SECRET_TOKEN: string;
@@ -23,55 +26,75 @@ const getSanityClient = (env: ENV) =>
2326
export const queueConsumer: ExportedHandlerQueueHandler<
2427
ENV,
2528
GoogleImportQueueElement
26-
> = async (batch, env) => {
27-
ensureKeys(env, [
28-
"SANITY_PROJECT_ID",
29-
"SANITY_DATASET",
30-
"SANITY_API_VERSION",
31-
"SANITY_SECRET_TOKEN",
32-
]);
33-
const sanityClient = getSanityClient(env);
34-
console.log("Processing queue", batch.queue);
35-
for await (const msg of batch.messages) {
36-
console.log("Processing message", msg);
37-
const { googleMedia, sanityEventInstanceId } = msg.body;
38-
const eventInstance = await sanityClient.getDocument(sanityEventInstanceId);
39-
if (!eventInstance) {
40-
throw new Error(`Event instance ${sanityEventInstanceId} not found`);
41-
}
42-
const response = await fetch(googleMedia.baseUrl + "=w4096");
43-
const blob = await response.blob();
44-
45-
const createdAsset = await sanityClient.assets.upload("image", blob, {
46-
contentType: googleMedia.mimeType,
47-
title: googleMedia.id,
48-
preserveFilename: true,
49-
creditLine: "JavaScript Chile",
50-
extract: ["blurhash", "exif", "image", "location", "lqip", "palette"],
29+
> = async (batch, env, ctx) => {
30+
try {
31+
const r = new Request("cloudflare:workers:google_import_queue_consumer");
32+
H.init(r, { HIGHLIGHT_PROJECT_ID: env.HIGHLIGHT_PROJECT_ID ?? "" }, ctx);
33+
H.setAttributes({
34+
APP_ENV: APP_ENV ?? "none",
5135
});
52-
console.log("Created asset", createdAsset, createdAsset.metadata);
36+
ensureKeys(env, [
37+
"SANITY_PROJECT_ID",
38+
"SANITY_DATASET",
39+
"SANITY_API_VERSION",
40+
"SANITY_SECRET_TOKEN",
41+
"HIGHLIGHT_PROJECT_ID",
42+
]);
43+
const sanityClient = getSanityClient(env);
44+
console.log("Processing queue", batch.queue);
45+
for await (const msg of batch.messages) {
46+
try {
47+
console.log("Processing message", msg);
48+
const { googleMedia, sanityEventInstanceId } = msg.body;
49+
const eventInstance = await sanityClient.getDocument(
50+
sanityEventInstanceId,
51+
);
52+
if (!eventInstance) {
53+
throw new Error(`Event instance ${sanityEventInstanceId} not found`);
54+
}
55+
const response = await fetch(googleMedia.baseUrl + "=w4096");
56+
const blob = await response.blob();
5357

54-
const createdImage = await sanityClient.createOrReplace({
55-
_id: v5(googleMedia.id, v5.URL),
56-
_type: "eventImage",
57-
externalId: googleMedia.id,
58-
externalURL: googleMedia.baseUrl,
59-
eventInstance: {
60-
_type: "reference",
61-
_ref: sanityEventInstanceId,
62-
},
63-
title: googleMedia.filename,
64-
image: {
65-
_type: "image",
66-
asset: {
67-
_type: "reference",
68-
_ref: createdAsset._id,
69-
},
70-
},
71-
});
58+
const createdAsset = await sanityClient.assets.upload("image", blob, {
59+
contentType: googleMedia.mimeType,
60+
title: googleMedia.id,
61+
preserveFilename: true,
62+
creditLine: "JavaScript Chile",
63+
extract: ["blurhash", "exif", "image", "location", "lqip", "palette"],
64+
});
65+
console.log("Created asset", createdAsset, createdAsset.metadata);
7266

73-
console.log("Created image", createdImage);
67+
const createdImage = await sanityClient.createOrReplace({
68+
_id: v5(googleMedia.id, v5.URL),
69+
_type: "eventImage",
70+
externalId: googleMedia.id,
71+
externalURL: googleMedia.baseUrl,
72+
eventInstance: {
73+
_type: "reference",
74+
_ref: sanityEventInstanceId,
75+
},
76+
title: googleMedia.filename,
77+
image: {
78+
_type: "image",
79+
asset: {
80+
_type: "reference",
81+
_ref: createdAsset._id,
82+
},
83+
},
84+
});
7485

75-
msg.ack();
86+
console.log("Created image", createdImage);
87+
88+
msg.ack();
89+
} catch (e) {
90+
console.error(e);
91+
H.consumeError(e as Error);
92+
throw e;
93+
}
94+
}
95+
} catch (e) {
96+
console.error(e);
97+
H.consumeError(e as Error);
98+
throw e;
7699
}
77100
};

0 commit comments

Comments
 (0)