Skip to content

Commit

Permalink
feat(lib-prom): metric improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
awlayton committed Sep 26, 2024
1 parent cf3df10 commit 71e07aa
Show file tree
Hide file tree
Showing 25 changed files with 526 additions and 342 deletions.
3 changes: 0 additions & 3 deletions oada/.yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ packageExtensions:
peerDependencies:
"@types/ws": "*"
fastify: "*"
eslint-plugin-sonarjs@*:
dependencies:
"@typescript-eslint/utils": "*"
fastify-graceful-shutdown@*:
peerDependencies:
fastify: "*"
Expand Down
8 changes: 4 additions & 4 deletions oada/eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import typescript from 'typescript-eslint';

import _import from 'eslint-plugin-import';
import ava from 'eslint-plugin-ava';
import github from 'eslint-plugin-github';
import noConstructorBind from 'eslint-plugin-no-constructor-bind';
import noSecrets from 'eslint-plugin-no-secrets';
import node from 'eslint-plugin-n';
Expand Down Expand Up @@ -67,7 +66,7 @@ export default typescript.config(
regexp.configs['flat/recommended'],
...fixupConfigRules(
compat.extends(
'plugin:github/recommended',
// 'plugin:github/recommended',
'plugin:promise/recommended',
'plugin:optimize-regex/recommended',
'plugin:import/recommended',
Expand Down Expand Up @@ -104,13 +103,13 @@ export default typescript.config(
},
{
plugins: {
'github': fixupPluginRules(github),
// 'github': fixupPluginRules(github),
'promise': fixupPluginRules(promise),
'optimize-regex': fixupPluginRules(optimizeRegex),
'no-constructor-bind': noConstructorBind,
'import': fixupPluginRules(_import),
'no-secrets': noSecrets,
// Sonarjs,
// sonarjs,
'ava': fixupPluginRules(ava),
notice,
},
Expand Down Expand Up @@ -323,6 +322,7 @@ export default typescript.config(
'no-dupe-class-members': 'off',
'no-useless-constructor': 'off',
'no-invalid-this': 'off',
'sonarjs/sonar-no-fallthrough': 'off',
'filenames/match-regex': 'off',
'i18n-text/no-en': 'off',
'github/no-implicit-buggy-globals': 'off',
Expand Down
4 changes: 2 additions & 2 deletions oada/libs/lib-arangodb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"@oada/models": "workspace:^",
"@oada/oadaify": "^2.1.0",
"@oada/types": "^4.0.0",
"arangojs": "^9.0.0",
"arangojs": "^9.1.0",
"bcryptjs": "^2.4.3",
"debug": "^4.3.7",
"deep-equal": "^2.2.3",
Expand All @@ -75,7 +75,7 @@
"@types/deep-equal": "^1.0.4",
"@types/flat": "^5.0.5",
"@types/json-pointer": "^1.0.34",
"@types/node": "^22.5.5",
"@types/node": "^22.7.3",
"ava": "6.1.3",
"type-fest": "^4.26.1"
},
Expand Down
2 changes: 1 addition & 1 deletion oada/libs/lib-kafka/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"@ava/typescript": "^5.0.0",
"@types/convict": "^6.1.6",
"@types/debug": "^4.1.12",
"@types/node": "^22.5.5",
"@types/node": "^22.7.3",
"@types/uuid": "^10.0.0",
"ava": "6.1.3"
},
Expand Down
4 changes: 2 additions & 2 deletions oada/libs/lib-prom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@
"devDependencies": {
"@ava/typescript": "^5.0.0",
"@types/convict": "^6.1.6",
"@types/node": "^22.5.5",
"@types/node": "^22.7.3",
"@types/ws": "^8.5.12",
"ava": "6.1.3",
"fastify-plugin": "^5.0.0"
"fastify-plugin": "^5.0.1"
},
"volta": {
"node": "22.5.1"
Expand Down
52 changes: 51 additions & 1 deletion oada/libs/lib-prom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ import { config } from './config.js';

import { createServer } from 'node:http';

import { collectDefaultMetrics, register } from 'prom-client';
import {
Gauge,
type MetricConfiguration,
collectDefaultMetrics,
register,
} from 'prom-client';
import type NStats from 'nstats';

collectDefaultMetrics({ register });
Expand All @@ -37,6 +42,7 @@ export const nstats: typeof NStats = (...parameters) => {
*
* *Starts automatically, don't try to start manually.*
*/
// eslint-disable-next-line sonarjs/no-misused-promises
export const server = createServer(async (_, response) => {
try {
const metrics = await register.metrics();
Expand All @@ -60,3 +66,47 @@ const { port, host } = config.get('prometheus');
server.listen({ host, port });

export * from 'prom-client';

export interface PseudoMetricConfiguration<T extends string> {
name: `${string}_info`;
help: string;
labels?: Record<T, string>;
collect?: (this: PseudoMetric<T>) => void | Promise<void>;
registers: MetricConfiguration<T>['registers'];
}

/**
* A pseudo-metric that provides metadata about the process to prometheus
*
* The lables are the reported metadata
*
* @see {@link https://www.robustperception.io/exposing-the-software-version-to-prometheus/}
*/
export class PseudoMetric<T extends string = string> {
readonly #gauge;

constructor({
name,
help,
labels,
registers,
collect = () => {
this.set(labels!);
},
}: PseudoMetricConfiguration<T>) {
this.#gauge = new Gauge<T>({
name,
help,
aggregator: 'first',
registers,
collect: () => collect.call(this),
});
}

/**
* !! ***You should only call this from within a collect callback***
*/
public set(labels: Record<T, string>) {
this.#gauge.set(labels, 1);
}
}
4 changes: 2 additions & 2 deletions oada/libs/models/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"node": "22.5.1"
},
"devDependencies": {
"@types/node": "^22.5.5",
"jose": "^5.9.2"
"@types/node": "^22.7.3",
"jose": "^5.9.3"
}
}
4 changes: 2 additions & 2 deletions oada/libs/pino-debug/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
"pino": "^9.4.0",
"pino-caller": "^3.4.0",
"pino-debug": "^2.0.0",
"pino-loki": "^2.3.0",
"pino-loki": "^2.3.1",
"pino-pretty": "^11.2.2",
"tslib": "2.7.0"
},
"devDependencies": {
"@types/debug": "^4.1.12",
"@types/node": "^22.5.5"
"@types/node": "^22.7.3"
},
"peerDependencies": {
"debug": "*"
Expand Down
2 changes: 1 addition & 1 deletion oada/libs/pino-debug/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

/* eslint-disable unicorn/prefer-module */

/* eslint-disable import/no-dynamic-require */

import { resolve } from 'node:path';

Expand Down
20 changes: 10 additions & 10 deletions oada/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,26 @@
"@eslint/compat": "^1.1.1",
"@eslint/config-inspector": "^0.5.4",
"@eslint/eslintrc": "^3.1.0",
"@eslint/js": "^9.10.0",
"@eslint/js": "^9.11.1",
"@tsconfig/node20": "^20.1.4",
"@types/eslint": "^9.6.1",
"@types/mocha": "^10.0.8",
"@types/node": "^22.5.5",
"@typescript-eslint/eslint-plugin": "^8.6.0",
"@typescript-eslint/parser": "^8.6.0",
"@types/node": "^22.7.3",
"@typescript-eslint/eslint-plugin": "^8.7.0",
"@typescript-eslint/parser": "^8.7.0",
"@yarnpkg/sdks": "^3.2.0",
"browserslist": "^4.23.3",
"browserslist": "^4.24.0",
"c8": "^10.1.2",
"eslint": "^9.10.0",
"eslint": "^9.11.1",
"eslint-config-prettier": "^9.1.0",
"eslint-config-xo": "^0.46.0",
"eslint-config-xo-typescript": "^6.0.0",
"eslint-config-xo-typescript": "^7.0.0",
"eslint-formatter-pretty": "^6.0.1",
"eslint-import-resolver-node": "^0.3.9",
"eslint-import-resolver-typescript": "^3.6.3",
"eslint-plugin-array-func": "^5.0.2",
"eslint-plugin-ava": "^15.0.1",
"eslint-plugin-escompat": "^3.11.1",
"eslint-plugin-escompat": "^3.11.3",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-filenames": "^1.3.2",
"eslint-plugin-github": "^5.0.2",
Expand All @@ -59,9 +59,9 @@
"prettier": "^3.3.3",
"tslib": "2.7.0",
"typescript": "5.6.2",
"typescript-eslint": "^8.6.0",
"typescript-eslint": "^8.7.0",
"update-browserslist-db": "^1.1.0",
"zx": "^8.1.7"
"zx": "^8.1.8"
},
"dependencies": {
"pino-pretty": "^11.2.2"
Expand Down
30 changes: 15 additions & 15 deletions oada/services/auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,18 @@
},
"homepage": "https:/oada/oada-ref-auth-js",
"dependencies": {
"@fastify/accepts": "^5.0.0",
"@fastify/cors": "^10.0.0",
"@fastify/formbody": "^8.0.0",
"@fastify/helmet": "^12.0.0",
"@fastify/jwt": "^9.0.0",
"@fastify/passport": "^3.0.0",
"@fastify/rate-limit": "^10.0.0",
"@fastify/request-context": "^6.0.0",
"@fastify/secure-session": "^8.0.0",
"@fastify/sensible": "^6.0.0",
"@fastify/static": "^8.0.0",
"@fastify/view": "^10.0.0",
"@fastify/accepts": "^5.0.1",
"@fastify/cors": "^10.0.1",
"@fastify/formbody": "^8.0.1",
"@fastify/helmet": "^12.0.1",
"@fastify/jwt": "^9.0.1",
"@fastify/passport": "^3.0.1",
"@fastify/rate-limit": "^10.1.0",
"@fastify/request-context": "^6.0.1",
"@fastify/secure-session": "^8.1.0",
"@fastify/sensible": "^6.0.1",
"@fastify/static": "^8.0.1",
"@fastify/view": "^10.0.1",
"@oada/certs": "^4.1.1",
"@oada/error": "^2.0.1",
"@oada/formats": "^4.1.0",
Expand All @@ -74,7 +74,7 @@
"@oada/types": "^4.0.0",
"@oada/well-known-json": "^4.0.2",
"@qlever-llc/interface2class": "^1.1.0",
"arangojs": "^9.0.0",
"arangojs": "^9.1.0",
"bcryptjs": "^2.4.3",
"chalk": "^5.3.0",
"cmd-ts": "^0.13.0",
Expand All @@ -83,9 +83,9 @@
"es-main": "^1.3.0",
"fastify": "^5.0.0",
"fastify-graceful-shutdown": "^4.0.1",
"fastify-healthcheck": "^4.4.0",
"fastify-healthcheck": "^5.0.0",
"ioredis": "^5.4.1",
"jose": "^5.9.2",
"jose": "^5.9.3",
"oauth2orize": "^1.12.0",
"oauth2orize-device-code": "^0.1.0",
"oauth2orize-openid": "^0.4.1",
Expand Down
4 changes: 2 additions & 2 deletions oada/services/auth/src/cli/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

/* eslint-disable no-console -- This is a CLI */

import '@oada/pino-debug';

Expand Down Expand Up @@ -82,7 +82,7 @@ export const cmd = command({
const issuer = await Issuer.discover(
iss ? `${iss}` : `${config.get('oidc.issuer')}`,
);
// eslint-disable-next-line security/detect-non-literal-fs-filename
const f = dataFile
? (YAML.parse(`${await readFile(dataFile)}`) as Partial<Metadata>)
: undefined;
Expand Down
2 changes: 1 addition & 1 deletion oada/services/auth/src/cli/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

/* eslint-disable no-console -- This is a CLI */

import '@oada/pino-debug';

Expand Down
6 changes: 2 additions & 4 deletions oada/services/auth/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import {
requestContext,
} from '@fastify/request-context';
import type FastifyRateLimit from '@fastify/rate-limit';
import type { JsonSchemaToTsProvider } from '@fastify/type-provider-json-schema-to-ts';
import _fastifyGracefulShutdown from 'fastify-graceful-shutdown';
import cors from '@fastify/cors';
import { createServer } from 'oauth2orize';
Expand Down Expand Up @@ -101,8 +100,7 @@ async function makeRedis(uri: string) {
/**
* Fastify plugin implementing the OADA auth server
*/
const plugin: FastifyPluginAsync = async (f) => {
const fastify = f.withTypeProvider<JsonSchemaToTsProvider>();
const plugin: FastifyPluginAsync = async (fastify) => {
fastify.log.debug('start');
const {
/**
Expand Down Expand Up @@ -416,7 +414,7 @@ if (esMain(import.meta)) {
try {
await start();
} catch (error: unknown) {
// eslint-disable-next-line no-console
console.error(error);
// eslint-disable-next-line unicorn/no-process-exit, n/no-process-exit
process.exit(1);
Expand Down
Loading

0 comments on commit 71e07aa

Please sign in to comment.