implement counters and gauges
This commit is contained in:
parent
23f3899e1d
commit
ec7c14401b
21
index.ts
21
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.
|
// Check LICENSE in repository root for more information.
|
||||||
|
|
||||||
import { createServer, Server } from "http";
|
import { createServer, Server } from "http";
|
||||||
import IConfig from "./interfaces/IConfig";
|
import IConfig from "./interfaces/IConfig";
|
||||||
import IMetric from "./interfaces/IMetric";
|
import IMetric from "./interfaces/IMetric";
|
||||||
|
import FunkyArray from "funky-array";
|
||||||
|
|
||||||
export default class SimpleProm {
|
export default class SimpleProm {
|
||||||
static instance?:SimpleProm;
|
static instance?:SimpleProm;
|
||||||
|
@ -11,18 +12,22 @@ export default class SimpleProm {
|
||||||
public config:IConfig;
|
public config:IConfig;
|
||||||
public selfHostServer?:Server;
|
public selfHostServer?:Server;
|
||||||
|
|
||||||
|
private metrics:FunkyArray<string, IMetric>;
|
||||||
|
|
||||||
private constructor(config:IConfig) {
|
private constructor(config:IConfig) {
|
||||||
SimpleProm.instance?.end();
|
SimpleProm.instance?.end();
|
||||||
SimpleProm.instance = this;
|
SimpleProm.instance = this;
|
||||||
|
|
||||||
this.config = config;
|
this.config = config;
|
||||||
|
|
||||||
|
this.metrics = new FunkyArray<string, IMetric>();
|
||||||
|
|
||||||
if (this.config.selfHost) {
|
if (this.config.selfHost) {
|
||||||
this.config.selfHostPort = this.config.selfHostPort ?? 9100;
|
this.config.selfHostPort = this.config.selfHostPort ?? 9100;
|
||||||
|
|
||||||
this.selfHostServer = createServer((req, res) => {
|
this.selfHostServer = createServer(async (req, res) => {
|
||||||
if (req.url?.startsWith("/metrics")) {
|
if (req.url?.startsWith("/metrics")) {
|
||||||
return res.end(this.getMetricText());
|
return res.end(await this.getMetricText());
|
||||||
}
|
}
|
||||||
res.end("SimpleProm exporter");
|
res.end("SimpleProm exporter");
|
||||||
});
|
});
|
||||||
|
@ -36,15 +41,17 @@ export default class SimpleProm {
|
||||||
}
|
}
|
||||||
|
|
||||||
public addMetric(metric:IMetric) {
|
public addMetric(metric:IMetric) {
|
||||||
|
return this.metrics.set(metric.name, metric);
|
||||||
}
|
}
|
||||||
|
|
||||||
public removeMetric(metric:IMetric) {
|
public removeMetric(metric:IMetric) {
|
||||||
|
this.metrics.remove(metric.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public getMetricText() {
|
public async getMetricText() {
|
||||||
return "TODO";
|
let metricText = "";
|
||||||
|
await this.metrics.forEach(metric => metricText += metric);
|
||||||
|
return metricText;
|
||||||
}
|
}
|
||||||
|
|
||||||
public end() {
|
public end() {
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
// Copyright (c) Catgirl Enterprises - Licensed under MIT
|
||||||
|
// Check LICENSE in repository root for more information.
|
||||||
|
|
||||||
export default interface IConfig {
|
export default interface IConfig {
|
||||||
selfHost: boolean,
|
selfHost: boolean,
|
||||||
selfHostPort?: number
|
selfHostPort?: number
|
||||||
|
|
|
@ -1,8 +1,12 @@
|
||||||
export default interface IMetric {
|
// Copyright (c) Catgirl Enterprises - Licensed under MIT
|
||||||
get helpText(): string,
|
// Check LICENSE in repository root for more information.
|
||||||
set helpText(value:string)
|
|
||||||
|
|
||||||
get Value(): number,
|
export default interface IMetric {
|
||||||
|
setHelpText(value:string) : void,
|
||||||
|
|
||||||
|
get name(): string,
|
||||||
|
Value: number,
|
||||||
|
add(value:number) : void,
|
||||||
|
|
||||||
toString(): string
|
toString(): string
|
||||||
}
|
}
|
|
@ -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`;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,17 +1,32 @@
|
||||||
|
// Copyright (c) Catgirl Enterprises - Licensed under MIT
|
||||||
|
// Check LICENSE in repository root for more information.
|
||||||
|
|
||||||
import IMetric from "../interfaces/IMetric";
|
import IMetric from "../interfaces/IMetric";
|
||||||
|
|
||||||
export default class Gauge implements IMetric {
|
export default class Gauge implements IMetric {
|
||||||
private name:string;
|
private _name:string;
|
||||||
private value:number;
|
private _helpText:string;
|
||||||
public readonly helpText:string;
|
public Value:number;
|
||||||
|
|
||||||
public constructor(name:string) {
|
public constructor(name:string) {
|
||||||
this.name = name;
|
this._name = name;
|
||||||
this.value = 0;
|
this._helpText = "";
|
||||||
this.helpText = "";
|
this.Value = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public get Value() {
|
public get name() {
|
||||||
return this.value;
|
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`;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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
|
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);
|
Loading…
Reference in New Issue