-
Notifications
You must be signed in to change notification settings - Fork 299
chore(o11y): update server jobs to use structured logging #6387
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,9 +9,15 @@ | |
| * node src/scripts/syncBreaches.js | ||
| */ | ||
|
|
||
| import * as Sentry from "@sentry/node"; | ||
|
|
||
| Sentry.init({ | ||
| dsn: config.sentryDsn, | ||
| tracesSampleRate: 1.0, | ||
| }); | ||
|
|
||
| import { readdir } from "node:fs/promises"; | ||
| import os from "node:os"; | ||
| import Sentry from "@sentry/nextjs"; | ||
| import { fetchHibpBreaches, HibpGetBreachesResponse } from "../../utils/hibp"; | ||
| import { | ||
| getAllBreaches, | ||
|
|
@@ -25,23 +31,23 @@ import { | |
| } from "../../db/redis/client.js"; | ||
| import { uploadToS3 } from "../../utils/s3.js"; | ||
| import { config } from "../../config"; | ||
| import { logger as baseLogger } from "../../app/functions/server/logging"; | ||
|
|
||
| const SENTRY_SLUG = "cron-sync-breaches"; | ||
|
|
||
| Sentry.init({ | ||
| dsn: config.sentryDsn, | ||
| tracesSampleRate: 1.0, | ||
| }); | ||
|
|
||
| const checkInId = Sentry.captureCheckIn({ | ||
| monitorSlug: SENTRY_SLUG, | ||
| status: "in_progress", | ||
| }); | ||
|
|
||
| const logger = baseLogger.child({ module: "syncBreaches" }); | ||
|
|
||
| export async function getBreachIcons(breaches: HibpGetBreachesResponse) { | ||
| // make logofolder if it doesn't exist | ||
| // TODO: clean up unused folder and existing logo logic | ||
| // MNTOR-5166 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Praise: thanks for filing this one, good improvements there. |
||
| const logoFolder = os.tmpdir(); | ||
| console.log(`Logo folder: ${logoFolder}`); | ||
| logger.debug(`Logo folder: ${logoFolder}`); | ||
|
|
||
| // read existing logos | ||
| const existingLogos = await readdir(logoFolder); | ||
|
|
@@ -51,99 +57,112 @@ export async function getBreachIcons(breaches: HibpGetBreachesResponse) { | |
| const breachName = breach.Name; | ||
|
|
||
| if (!breachDomain || breachDomain.length === 0) { | ||
| console.log("empty domain: ", breachName); | ||
| logger.info("Breach has empty domain", { breachName }); | ||
| await updateBreachFaviconUrl(breachName, null); | ||
| continue; | ||
| } | ||
| const logoFilename = breachDomain.toLowerCase() + ".ico"; | ||
| // TODO: Check S3 bucket, not file system, for existing logos | ||
| // MNTOR-5166 | ||
| if (existingLogos.includes(logoFilename)) { | ||
| console.log("skipping ", logoFilename); | ||
| logger.info("skipping ", logoFilename); | ||
| await updateBreachFaviconUrl( | ||
| breachName, | ||
| `https://s3.amazonaws.com/${config.s3Bucket}/${logoFilename}`, | ||
| ); | ||
| continue; | ||
| } | ||
| console.log(`fetching: ${logoFilename}`); | ||
| logger.info("Fetching logo", { logoFilename }); | ||
| const res = await fetch( | ||
| `https://icons.duckduckgo.com/ip3/${breachDomain}.ico`, | ||
| ); | ||
| if (res.status !== 200) { | ||
| // update logo path with null | ||
| console.log(`Logo does not exist for: ${breachName} ${breachDomain}`); | ||
| logger.info("Logo does not exist", { breachName, breachDomain }); | ||
| await updateBreachFaviconUrl(breachName, null); | ||
| continue; | ||
| } | ||
|
|
||
| try { | ||
| // TODO: Do not reupload to S3 if logo already exists | ||
| // MNTOR-5166 | ||
| await uploadToS3(logoFilename, Buffer.from(await res.arrayBuffer())); | ||
| await updateBreachFaviconUrl( | ||
| breachName, | ||
| `https://s3.amazonaws.com/${config.s3Bucket}/${logoFilename}`, | ||
| ); | ||
| } catch (e) { | ||
| console.error(e); | ||
| logger.error(e); | ||
| continue; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Get breaches and upserts to DB | ||
| const breachesResponse = await fetchHibpBreaches(); | ||
| const seen = new Set(); | ||
| breachesResponse.forEach((breach) => { | ||
| seen.add(breach.Name + breach.BreachDate); | ||
|
|
||
| // sanity check: corrupt data structure | ||
| if (!isValidBreach(breach)) { | ||
| throw new Error( | ||
| "Breach data structure is not valid: " + JSON.stringify(breach), | ||
| async function run() { | ||
| // Get breaches and upserts to DB | ||
| const breachesResponse = await fetchHibpBreaches(); | ||
| const seen = new Set(); | ||
| breachesResponse.forEach((breach) => { | ||
| seen.add(breach.Name + breach.BreachDate); | ||
|
|
||
| // sanity check: corrupt data structure | ||
| if (!isValidBreach(breach)) { | ||
| const invalidMsg = "Breach data structure is not valid"; | ||
| logger.error(invalidMsg, { breach }); | ||
| throw new Error(`${invalidMsg}: ` + JSON.stringify(breach)); | ||
| } | ||
| }); | ||
|
|
||
| logger.info("Breaches found", { total: length, unique: seen.size }); | ||
|
|
||
| // sanity check: no duplicate breaches with Name + BreachDate | ||
| if (seen.size !== breachesResponse.length) { | ||
| const dupeMsg = "Breaches contain duplicates"; | ||
| logger.error(dupeMsg, { | ||
| unique: seen.size, | ||
| total: breachesResponse.length, | ||
| }); | ||
| throw new Error(`${dupeMsg}. Stopping script...`); | ||
| } else { | ||
| await upsertBreaches(breachesResponse); | ||
|
|
||
| // get | ||
| const result = await getAllBreaches(); | ||
| logger.info( | ||
| "Number of breaches in the database after upsert:", | ||
| result.length, | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| console.log("Breaches found: ", breachesResponse.length); | ||
| console.log("Unique breaches based on Name + BreachDate", seen.size); | ||
|
|
||
| // sanity check: no duplicate breaches with Name + BreachDate | ||
| if (seen.size !== breachesResponse.length) { | ||
| throw new Error("Breaches contain duplicates. Stopping script..."); | ||
| } else { | ||
| await upsertBreaches(breachesResponse); | ||
|
|
||
| // get | ||
| const result = await getAllBreaches(); | ||
| console.log( | ||
| "Number of breaches in the database after upsert:", | ||
| result.length, | ||
| ); | ||
|
|
||
| // try to refresh Redis cache of all breaches | ||
| try { | ||
| const rClient = redisClient(); | ||
| await rClient.set( | ||
| REDIS_ALL_BREACHES_KEY, | ||
| JSON.stringify(result), | ||
| "EX", | ||
| BREACHES_EXPIRY_SECONDS, | ||
| ); | ||
| } catch (e) { | ||
| Sentry.captureMessage( | ||
| `Update Redis failed for syncBreaches.ts: ${e as string}`, | ||
| ); | ||
| console.error( | ||
| `Update Redis failed for syncBreaches.ts: ${(e as Error).stack}`, | ||
| ); | ||
| // try to refresh Redis cache of all breaches | ||
| try { | ||
| const rClient = redisClient(); | ||
| await rClient.set( | ||
| REDIS_ALL_BREACHES_KEY, | ||
| JSON.stringify(result), | ||
| "EX", | ||
| BREACHES_EXPIRY_SECONDS, | ||
| ); | ||
| } catch (e) { | ||
| logger.error("Update Redis failed for syncBreaches.ts", { error: e }); | ||
| } | ||
| } | ||
| await getBreachIcons(breachesResponse); | ||
| } | ||
|
|
||
| await getBreachIcons(breachesResponse); | ||
|
|
||
| Sentry.captureCheckIn({ | ||
| checkInId, | ||
| monitorSlug: SENTRY_SLUG, | ||
| status: "ok", | ||
| }); | ||
| try { | ||
| await run(); | ||
| Sentry.captureCheckIn({ | ||
| checkInId, | ||
| monitorSlug: SENTRY_SLUG, | ||
| status: "ok", | ||
| }); | ||
| } catch (error) { | ||
| Sentry.captureCheckIn({ | ||
| checkInId, | ||
| monitorSlug: SENTRY_SLUG, | ||
| status: "error", | ||
| }); | ||
| throw error; | ||
| } | ||
| setTimeout(process.exit, 1000); | ||
|
|
||
| /** | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.