2024-07-02 00:29:30 +01:00
|
|
|
// Copyright (c) Holly Stubbs (tgpholly) - Licensed under MIT
|
|
|
|
// Check LICENSE in repository root for more information.
|
|
|
|
|
|
|
|
import { createServer, Server } from "http";
|
|
|
|
import IConfig from "./interfaces/IConfig";
|
2024-07-02 10:20:16 +01:00
|
|
|
import IMetric from "./interfaces/IMetric";
|
2024-07-02 00:29:30 +01:00
|
|
|
|
2024-07-02 10:20:16 +01:00
|
|
|
export default class SimpleProm {
|
2024-07-02 00:29:30 +01:00
|
|
|
static instance?:SimpleProm;
|
|
|
|
|
|
|
|
public config:IConfig;
|
|
|
|
public selfHostServer?:Server;
|
|
|
|
|
|
|
|
private constructor(config:IConfig) {
|
|
|
|
SimpleProm.instance?.end();
|
|
|
|
SimpleProm.instance = this;
|
|
|
|
|
|
|
|
this.config = config;
|
|
|
|
|
2024-07-02 10:20:16 +01:00
|
|
|
if (this.config.selfHost) {
|
|
|
|
this.config.selfHostPort = this.config.selfHostPort ?? 9100;
|
|
|
|
|
|
|
|
this.selfHostServer = createServer((req, res) => {
|
2024-07-02 16:46:38 +01:00
|
|
|
if (req.url?.startsWith("/metrics")) {
|
|
|
|
return res.end(this.getMetricText());
|
|
|
|
}
|
2024-07-02 10:20:16 +01:00
|
|
|
res.end("SimpleProm exporter");
|
|
|
|
});
|
|
|
|
|
|
|
|
this.selfHostServer.listen(this.config.selfHostPort);
|
|
|
|
}
|
2024-07-02 00:29:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static init(config:IConfig) {
|
2024-07-02 10:20:16 +01:00
|
|
|
return new SimpleProm(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
public addMetric(metric:IMetric) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public removeMetric(metric:IMetric) {
|
2024-07-02 00:29:30 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-07-02 16:46:38 +01:00
|
|
|
public getMetricText() {
|
|
|
|
return "TODO";
|
|
|
|
}
|
|
|
|
|
2024-07-02 00:29:30 +01:00
|
|
|
public end() {
|
|
|
|
if (this.selfHostServer) {
|
|
|
|
this.selfHostServer.close();
|
|
|
|
this.selfHostServer.closeAllConnections();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|