api: logtape, otel, & logging improvements (#1230)

This commit is contained in:
acelinkio
2025-12-22 07:22:02 -08:00
committed by GitHub
parent 739ad96d66
commit 651d721669
9 changed files with 421 additions and 88 deletions
+20 -3
View File
@@ -1,5 +1,6 @@
import path from "node:path";
import { getCurrentSpan, setAttributes } from "@elysiajs/opentelemetry";
import { getLogger } from "@logtape/logtape";
import { SpanStatusCode } from "@opentelemetry/api";
import { encode } from "blurhash";
import { and, eq, is, lt, ne, type SQL, sql } from "drizzle-orm";
@@ -14,6 +15,8 @@ import type { Image } from "~/models/utils";
import { record } from "~/otel";
import { getFile } from "~/utils";
const logger = getLogger();
export const imageDir = process.env.IMAGES_PATH ?? "/images";
export const defaultBlurhash = "000000";
@@ -134,7 +137,16 @@ export const processImages = record(
const client = (await db.$client.connect()) as PoolClient;
client.on("notification", (evt) => {
if (evt.channel !== "kyoo_image") return;
processAll();
try {
processAll();
} catch (e) {
logger.error(
"Failed to process images. Aborting images downloading. error={error}",
{
error: e,
},
);
}
});
await client.query("listen kyoo_image");
@@ -193,14 +205,19 @@ const processOne = record("download", async () => {
span.recordException(err);
span.setStatus({ code: SpanStatusCode.ERROR });
}
console.error("Failed to download image", img.url, err);
logger.error("Failed to download image. imageurl={url}, error={error}", {
url: img.url,
error: err,
});
try {
await tx
.update(images)
.set({ attempt: sql`${images.attempt}+1` })
.where(eq(images.pk, img.pk));
} catch (e) {
console.error("Failed to mark download as failed", e);
logger.error("Failed to mark download as failed. error={error}", {
error: e,
});
}
}
return true;