Guages and Counters

This commit is contained in:
Holly Stubbs 2024-07-02 23:51:53 +01:00
parent e5491413a8
commit 9c3dc2e152
Signed by: tgpholly
GPG Key ID: B8583C4B7D18119E
11 changed files with 2203 additions and 0 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
lib/*
# ---> Node
# Logs
logs

134
.npmignore Normal file
View File

@ -0,0 +1,134 @@
.github/*
index.ts
tsconfig.json
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional stylelint cache
.stylelintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# vuepress v2.x temp and cache directory
.temp
.cache
# Docusaurus cache and generated files
.docusaurus
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

63
index.ts Normal file
View File

@ -0,0 +1,63 @@
// 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;
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(async (req, res) => {
if (req.url?.startsWith("/metrics")) {
return res.end(await this.getMetricText());
}
res.end("SimpleProm exporter");
});
this.selfHostServer.listen(this.config.selfHostPort);
}
}
static init(config:IConfig) {
return new SimpleProm(config);
}
public addMetric(metric:IMetric) {
return this.metrics.set(metric.name, metric);
}
public removeMetric(metric:IMetric) {
this.metrics.remove(metric.name);
}
public async getMetricText() {
let metricText = "";
await this.metrics.forEach(metric => metricText += metric);
return metricText;
}
public end() {
if (this.selfHostServer) {
this.selfHostServer.close();
this.selfHostServer.closeAllConnections();
}
}
}

7
interfaces/IConfig.ts Normal file
View File

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

12
interfaces/IMetric.ts Normal file
View File

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

40
objects/Counter.ts Normal file
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`;
}
}

32
objects/Guage.ts Normal file
View File

@ -0,0 +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 _helpText:string;
public Value:number;
public constructor(name:string) {
this._name = name;
this._helpText = "";
this.Value = 0;
}
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`;
}
}

1842
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

34
package.json Normal file
View File

@ -0,0 +1,34 @@
{
"name": "simple-prom",
"version": "1.0.0",
"description": "A simple and easy to use module for Prometheus metric exporting.",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"scripts": {
"updateCheck": "check-outdated",
"_clean": "tsc --build --clean",
"build": "tsc --build"
},
"repository": {
"type": "git",
"url": "https://git.eusv.net/Catgirl.Enterprises/simple-prom.git"
},
"keywords": [
"prometheus",
"prom",
"expoter",
"typescript",
"types"
],
"author": "Catgirl.Enterprises",
"license": "MIT",
"dependencies": {
"funky-array": "^1.0.0"
},
"devDependencies": {
"check-outdated": "^2.12.0",
"npm-run-all": "^4.1.5",
"ts-node": "^10.9.2",
"typescript": "^5.5.3"
}
}

24
test/testServer.ts Normal file
View File

@ -0,0 +1,24 @@
// Copyright (c) Catgirl Enterprises - Licensed under MIT
// Check LICENSE in repository root for more information.
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);

13
tsconfig.json Normal file
View File

@ -0,0 +1,13 @@
{
"compilerOptions": {
"module": "CommonJS",
"moduleResolution": "node",
"target": "ES2020",
"esModuleInterop": true,
"resolveJsonModule": true,
"rootDir": "./",
"outDir": "./lib/",
"strict": true,
"declaration": true
}
}