diff --git a/index.ts b/index.ts index 340d554..da7ddd3 100644 --- a/index.ts +++ b/index.ts @@ -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; + private constructor(config:IConfig) { SimpleProm.instance?.end(); SimpleProm.instance = this; this.config = config; + this.metrics = new FunkyArray(); + 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() { diff --git a/interfaces/IConfig.ts b/interfaces/IConfig.ts index ae83fb0..2940cb8 100644 --- a/interfaces/IConfig.ts +++ b/interfaces/IConfig.ts @@ -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 diff --git a/interfaces/IMetric.ts b/interfaces/IMetric.ts index 62c6d07..3edd4f2 100644 --- a/interfaces/IMetric.ts +++ b/interfaces/IMetric.ts @@ -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 } \ No newline at end of file diff --git a/objects/Counter.ts b/objects/Counter.ts index e69de29..9f16ad1 100644 --- a/objects/Counter.ts +++ b/objects/Counter.ts @@ -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`; + } +} \ No newline at end of file diff --git a/objects/Guage.ts b/objects/Guage.ts index 6efabbf..ae7eaef 100644 --- a/objects/Guage.ts +++ b/objects/Guage.ts @@ -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`; } } \ No newline at end of file diff --git a/test/testServer.ts b/test/testServer.ts index ec6d9a8..073e0e6 100644 --- a/test/testServer.ts +++ b/test/testServer.ts @@ -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 -}); \ No newline at end of file +}); + +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); \ No newline at end of file