implement counters and gauges

This commit is contained in:
Holly Stubbs 2024-07-02 23:49:10 +01:00
parent 23f3899e1d
commit ec7c14401b
Signed by: tgpholly
GPG Key ID: B8583C4B7D18119E
6 changed files with 110 additions and 22 deletions

View File

@ -1,9 +1,10 @@
// Copyright (c) Holly Stubbs (tgpholly) - Licensed under MIT
// Copyright (c) Catgirl Enterprises - Licensed under MIT
// Check LICENSE in repository root for more information.
import { createServer, Server } from "http";
import IConfig from "./interfaces/IConfig";
import IMetric from "./interfaces/IMetric";
import FunkyArray from "funky-array";
export default class SimpleProm {
static instance?:SimpleProm;
@ -11,18 +12,22 @@ export default class SimpleProm {
public config:IConfig;
public selfHostServer?:Server;
private metrics:FunkyArray<string, IMetric>;
private constructor(config:IConfig) {
SimpleProm.instance?.end();
SimpleProm.instance = this;
this.config = config;
this.metrics = new FunkyArray<string, IMetric>();
if (this.config.selfHost) {
this.config.selfHostPort = this.config.selfHostPort ?? 9100;
this.selfHostServer = createServer((req, res) => {
this.selfHostServer = createServer(async (req, res) => {
if (req.url?.startsWith("/metrics")) {
return res.end(this.getMetricText());
return res.end(await this.getMetricText());
}
res.end("SimpleProm exporter");
});
@ -36,15 +41,17 @@ export default class SimpleProm {
}
public addMetric(metric:IMetric) {
return this.metrics.set(metric.name, metric);
}
public removeMetric(metric:IMetric) {
this.metrics.remove(metric.name);
}
public getMetricText() {
return "TODO";
public async getMetricText() {
let metricText = "";
await this.metrics.forEach(metric => metricText += metric);
return metricText;
}
public end() {

View File

@ -1,3 +1,6 @@
// Copyright (c) Catgirl Enterprises - Licensed under MIT
// Check LICENSE in repository root for more information.
export default interface IConfig {
selfHost: boolean,
selfHostPort?: number

View File

@ -1,8 +1,12 @@
export default interface IMetric {
get helpText(): string,
set helpText(value:string)
// Copyright (c) Catgirl Enterprises - Licensed under MIT
// Check LICENSE in repository root for more information.
get Value(): number,
export default interface IMetric {
setHelpText(value:string) : void,
get name(): string,
Value: number,
add(value:number) : void,
toString(): string
}

View File

@ -0,0 +1,40 @@
// Copyright (c) Catgirl Enterprises - Licensed under MIT
// Check LICENSE in repository root for more information.
import IMetric from "../interfaces/IMetric";
export default class Counter implements IMetric {
private _name:string;
private _helpText:string;
private _value:number;
public constructor(name:string) {
this._name = name;
this._helpText = "";
this._value = 0;
}
public get name() {
return this._name;
}
public get Value() {
return this._value;
}
public set Value(value:number) {
throw "Cannot set the Value of a counter. Counters can only added to with the add method.";
}
public add(value:number) {
this._value += value;
}
public setHelpText(value:string) {
this._helpText = value;
}
public toString() {
return `${this._helpText.length > 0 ? `# HELP ${this._name} ${this._helpText}\n` : ""}# TYPE ${this._name} counter\n${this._name} ${this.Value}\n`;
}
}

View File

@ -1,17 +1,32 @@
// Copyright (c) Catgirl Enterprises - Licensed under MIT
// Check LICENSE in repository root for more information.
import IMetric from "../interfaces/IMetric";
export default class Gauge implements IMetric {
private name:string;
private value:number;
public readonly helpText:string;
private _name:string;
private _helpText:string;
public Value:number;
public constructor(name:string) {
this.name = name;
this.value = 0;
this.helpText = "";
this._name = name;
this._helpText = "";
this.Value = 0;
}
public get Value() {
return this.value;
public get name() {
return this._name;
}
public add(value:number) {
this.Value += value;
}
public setHelpText(value:string) {
this._helpText = value;
}
public toString() {
return `${this._helpText.length > 0 ? `# HELP ${this._name} ${this._helpText}\n` : ""}# TYPE ${this._name} gauge\n${this._name} ${this.Value}\n`;
}
}

View File

@ -1,5 +1,24 @@
import SimpleProm from "../index";
// Copyright (c) Catgirl Enterprises - Licensed under MIT
// Check LICENSE in repository root for more information.
SimpleProm.init({
import SimpleProm from "../index";
import Counter from "../objects/Counter";
import Gauge from "../objects/Guage";
const instance = SimpleProm.init({
selfHost: true
});
});
const testGaugeNoHelp = instance.addMetric(new Gauge("test_gauge_no_help"));
testGaugeNoHelp.Value = 1337;
const testGaugeHelp = instance.addMetric(new Gauge("test_gauge"));
testGaugeHelp.setHelpText("Test gauge help");
testGaugeHelp.Value = 1337;
const testCounterNoHelp = instance.addMetric(new Counter("test_counter_no_help"));
testCounterNoHelp.add(1337);
const testCounterHelp = instance.addMetric(new Counter("test_counter"));
testCounterHelp.setHelpText("Test counter help");
testCounterHelp.add(1337);