feat(metrics): better metrics and support for failed and successful requests
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 1m26s

This commit is contained in:
Fijxu 2025-01-11 14:58:37 -03:00
parent 12c965ceaf
commit 5560d9f986
Signed by: Fijxu
GPG key ID: 32C1DDF333EDA6A4
3 changed files with 46 additions and 21 deletions

View file

@ -2,7 +2,7 @@ import { ApiResponse, Innertube, YT } from "youtubei.js";
import { generateRandomString } from "youtubei.js/Utils";
import { compress, decompress } from "https://deno.land/x/brotli@0.1.7/mod.ts";
import { Store } from "@willsoto/node-konfig-core";
import { cachedEntries } from "../../routes/index.ts";
import { failedRequests, successfulRequests } from "../../routes/index.ts";
let youtubePlayerReqLocation = "youtubePlayerReq";
if (Deno.env.get("YT_PLAYER_REQ_LOCATION")) {
if (Deno.env.has("DENO_COMPILED")) {
@ -18,7 +18,7 @@ const { youtubePlayerReq } = await import(youtubePlayerReqLocation);
const videoCachePath = Deno.env.get("VIDEO_CACHE_PATH") as string || "/var/tmp/youtubei.js/video_cache"
let kv : Deno.Kv
export let kv : Deno.Kv
if ((Deno.env.get("VIDEO_CACHE_ON_DISK")?.toLowerCase() ?? false) == "true") {
console.log("[INFO] Storing video cache on disk")
kv = await Deno.openKv(videoCachePath);
@ -138,23 +138,27 @@ export const youtubePlayerParsing = async (
},
}))(videoData);
if (
cacheEnabled == true && videoData.playabilityStatus?.status == "OK"
) {
(async () => {
await kv.set(
["video_cache", videoId],
compress(
new TextEncoder().encode(
JSON.stringify(videoOnlyNecessaryInfo),
if (videoData.playabilityStatus?.status == "OK") {
successfulRequests.inc()
if (
cacheEnabled == true
) {
(async () => {
await kv.set(
["video_cache", videoId],
compress(
new TextEncoder().encode(
JSON.stringify(videoOnlyNecessaryInfo),
),
),
),
{
expireIn: 1000 * 60 * 60,
},
);
cachedEntries.inc()
})();
{
expireIn: 1000 * 60 * 60,
},
);
})();
}
} else {
failedRequests.inc()
}
return videoOnlyNecessaryInfo;

View file

@ -2,7 +2,7 @@ import { Hono } from "hono";
import { logger } from "hono/logger";
import { Store } from "@willsoto/node-konfig-core";
import { bearerAuth } from "hono/bearer-auth";
import { Registry, Counter } from "prom-client"
import { Registry, Counter, Gauge } from "prom-client"
import youtubeApiPlayer from "./youtube_api_routes/player.ts";
import invidiousRouteLatestVersion from "./invidious_routes/latestVersion.ts";
@ -19,7 +19,7 @@ export const externalTokenGeneratorFail = new Counter({
registers: [register]
});
export const cachedEntries = new Counter({
export const cachedEntries = new Gauge({
name: `${METRICS_PREFIX}cached_entries`,
help: 'TODO',
registers: [register]
@ -54,6 +54,20 @@ export const videoRestricted = new Counter({
help: 'TODO',
registers: [register]
});
export const failedRequests = new Counter({
name: `${METRICS_PREFIX}failed_requests`,
help: 'TODO',
registers: [register]
});
export const successfulRequests = new Counter({
name: `${METRICS_PREFIX}successful_requests`,
help: 'TODO',
registers: [register]
});
import health from "./health.ts";
export const routes = (app: Hono, konfigStore: Store<Record<string, unknown>>) => {

View file

@ -1,9 +1,16 @@
import { Hono } from "hono";
import { register } from "./index.ts";
import { register, cachedEntries } from "./index.ts";
import { kv } from "../lib/helpers/youtubePlayerHandling.ts"
const metrics = new Hono();
metrics.get("/", async () => {
let i = 0;
const entries = kv.list({ prefix: ["video_cache"] })
for await (const _ of entries) {
i += 1
}
cachedEntries.set(i)
return new Response(await register.metrics(), {
headers: { "Content-Type": "text/plain" },
});