Skip to content

Commit

Permalink
Prometheus fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Inrixia committed Apr 2, 2024
1 parent 02bc5ec commit 56ac71f
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 85 deletions.
4 changes: 2 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "floatplane-plex-downloader",
"version": "5.10.2",
"version": "5.11.3",
"private": true,
"type": "module",
"scripts": {
Expand Down Expand Up @@ -54,4 +54,4 @@
"pkg": "^5.8.1",
"typescript": "^5.4.3"
}
}
}
2 changes: 1 addition & 1 deletion src/float.ts
@@ -1,4 +1,4 @@
import { initProm } from "./lib/prometheus/index.js";
import { initProm } from "./lib/prometheus.js";
import { quickStart, validatePlexSettings } from "./quickStart.js";
import { settings, fetchFFMPEG, fApi, args, DownloaderVersion } from "./lib/helpers.js";
import { defaultSettings } from "./lib/defaults.js";
Expand Down
2 changes: 1 addition & 1 deletion src/lib/helpers.ts
Expand Up @@ -14,7 +14,7 @@ import "dotenv/config";
import json5 from "json5";
const { parse } = json5;

export const DownloaderVersion = "5.10.2";
export const DownloaderVersion = "5.11.3";

import type { PartialArgs, Settings } from "./types.js";

Expand Down
77 changes: 77 additions & 0 deletions src/lib/prometheus.ts
@@ -0,0 +1,77 @@
import { collectDefaultMetrics, Gauge, Histogram, register } from "prom-client";
import { createServer } from "http";
import WebSocket from "ws";

import { DownloaderVersion, fApi, settings } from "./helpers.js";

export const initProm = (instance: string) => {
collectDefaultMetrics();

new Gauge({
name: "instance",
help: "Floatplane Downloader instances",
labelNames: ["version"],
})
.labels({ version: DownloaderVersion })
.set(1);

// Add floatplane api request metrics
const httpRequestDurationmS = new Histogram({
name: "request_duration_ms",
help: "Duration of HTTP requests in ms",
labelNames: ["method", "hostname", "pathname", "status"],
buckets: [1, 10, 50, 100, 250, 500],
});
type WithStartTime<T> = T & { _startTime: number };
fApi.extend({
hooks: {
beforeRequest: [
(options) => {
(<WithStartTime<typeof options>>options)._startTime = Date.now();
},
],
afterResponse: [
(res) => {
const url = res.requestUrl;
const options = <WithStartTime<typeof res.request.options>>res.request.options;
const thumbsIndex = url.pathname.indexOf("thumbnails");
const pathname = thumbsIndex !== -1 ? url.pathname.substring(0, thumbsIndex + 10) : url.pathname;
httpRequestDurationmS.observe({ method: options.method, hostname: url.hostname, pathname, status: res.statusCode }, Date.now() - options._startTime);
return res;
},
],
},
});

if (settings.metrics.contributeMetrics) {
const connect = () => {
const socket = new WebSocket("ws://targets.monitor.spookelton.net");
socket.on("open", () => socket.send(instance));
socket.on("ping", async () => socket.send(await register.metrics()));
socket.on("error", () => socket.close());
socket.on("close", () => {
socket.close();
setTimeout(connect, 1000);
});
};
connect();
}

if (settings.metrics.prometheusExporterPort !== null) {
const httpServer = createServer(async (req, res) => {
if (req.url === "/metrics") {
try {
res.setHeader("Content-Type", register.contentType);
res.end(await register.metrics());
} catch (err) {
res.statusCode = 500;
res.end((<Error>err)?.message);
}
} else {
res.statusCode = 404;
res.end("Not found");
}
});
httpServer.listen(settings.metrics.prometheusExporterPort);
}
};
29 changes: 0 additions & 29 deletions src/lib/prometheus/fApi.ts

This file was deleted.

52 changes: 0 additions & 52 deletions src/lib/prometheus/index.ts

This file was deleted.

0 comments on commit 56ac71f

Please sign in to comment.