@cripty2001/utils 0.0.53 → 0.0.55

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,7 +17,9 @@ export declare class AppserverHandledError extends AppserverError {
17
17
  export declare class Appserver<U extends AppserverData> {
18
18
  private app;
19
19
  private parseUser;
20
- constructor(port: number, parseUser: AppserverUsergetter<U>);
20
+ private getMetrics;
21
+ constructor(port: number, parseUser: AppserverUsergetter<U>, getMetrics: () => Record<string, number>);
22
+ private handleMetricsRequest;
21
23
  private parseInput;
22
24
  register<ISchema extends TSchema, O extends AppserverData, I extends Static<ISchema> & AppserverData = Static<ISchema> & AppserverData>(action: string, inputSchema: ISchema, auth: boolean, handler: AppserverHandler<I, U, O>): void;
23
25
  }
@@ -34,10 +34,43 @@ exports.AppserverHandledError = AppserverHandledError;
34
34
  class Appserver {
35
35
  app;
36
36
  parseUser;
37
- constructor(port, parseUser) {
37
+ getMetrics;
38
+ constructor(port, parseUser, getMetrics) {
38
39
  this.parseUser = parseUser;
40
+ this.getMetrics = getMetrics;
39
41
  this.app = (0, express_1.default)();
40
42
  this.app.listen(port);
43
+ this.app.get('/metrics', async (req, res) => {
44
+ await this.handleMetricsRequest(req, res);
45
+ });
46
+ }
47
+ async handleMetricsRequest(req, res) {
48
+ try {
49
+ const metrics = this.getMetrics();
50
+ const toReturn = Object.entries(metrics)
51
+ .map(([name, value]) => {
52
+ if (typeof value !== 'number' || !isFinite(value))
53
+ throw new Error(`Metric value for "${name}" is not a number`);
54
+ const prometheusName = 'app_' + name
55
+ .toLowerCase()
56
+ .replace(/[^a-z0-9_]/g, '_')
57
+ .replace(/_+/g, '_')
58
+ .replace(/^_+|_+$/g, '');
59
+ return `# TYPE ${prometheusName} gauge\n${prometheusName} ${value}\n`;
60
+ })
61
+ .join('');
62
+ res
63
+ .status(200)
64
+ .type('text/plain')
65
+ .send(toReturn);
66
+ }
67
+ catch (e) {
68
+ console.log("Error generating metrics:", e);
69
+ res
70
+ .status(500)
71
+ .type('text/plain')
72
+ .send('# Error generating metrics\n');
73
+ }
41
74
  }
42
75
  async parseInput(req) {
43
76
  if (req.headers['content-type'] !== 'application/vnd.msgpack')
package/dist/index.d.ts CHANGED
@@ -85,3 +85,4 @@ export declare function randBetween(min: number, max: number): number;
85
85
  export declare function getEnv(key: string, defaultValue?: string): string;
86
86
  export declare const CURRENT_TS_MS: Whispr<number>;
87
87
  export declare function timediff2HumanReadable(diffMs: number): string;
88
+ export declare function fn2promise<T>(fn: () => T | Promise<T>): Promise<T>;
package/dist/index.js CHANGED
@@ -16,6 +16,7 @@ exports.parseQuery = parseQuery;
16
16
  exports.randBetween = randBetween;
17
17
  exports.getEnv = getEnv;
18
18
  exports.timediff2HumanReadable = timediff2HumanReadable;
19
+ exports.fn2promise = fn2promise;
19
20
  const whispr_1 = require("@cripty2001/whispr");
20
21
  const lodash_1 = require("lodash");
21
22
  /**
@@ -233,3 +234,9 @@ function timediff2HumanReadable(diffMs) {
233
234
  const rtf = new Intl.RelativeTimeFormat();
234
235
  return rtf.format(Math.round(diff), unit);
235
236
  }
237
+ function fn2promise(fn) {
238
+ const result = fn();
239
+ return result instanceof Promise ?
240
+ result :
241
+ Promise.resolve(result);
242
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cripty2001/utils",
3
- "version": "0.0.53",
3
+ "version": "0.0.55",
4
4
  "description": "Internal Set of utils. If you need them use them, otherwise go to the next package ;)",
5
5
  "homepage": "https://github.com/cripty2001/utils#readme",
6
6
  "bugs": {