@adalo/metrics 0.1.114 → 0.1.115

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.
@@ -1,98 +1,17 @@
1
1
  /**
2
2
  * MetricsClient handles Prometheus metrics collection and push.
3
3
  * Supports gauges, counters, default metrics, and custom metrics.
4
+ * Extends BaseMetricsClient for common functionality.
4
5
  */
5
- export class MetricsClient {
6
- /**
7
- * @param {Object} config
8
- * @param {string} [config.appName] Name of the application
9
- * @param {string} [config.dynoId] Dyno/instance ID
10
- * @param {string} [config.processType] Process type (web, worker, etc.)
11
- * @param {boolean} [config.enabled] Enable metrics collection
12
- * @param {boolean} [config.logValues] Log metrics values to console
13
- * @param {string} [config.pushgatewayUrl] PushGateway URL
14
- * @param {string} [config.pushgatewaySecret] PushGateway secret token
15
- * @param {number} [config.intervalSec] Interval in seconds for pushing metrics
16
- * @param {boolean} [config.removeOldMetrics] Enable to clear metrics by service name
17
- * @param {boolean} [config.scripDefaultMetrics] Enable to scip default metrics creation
18
- * @param {function} [config.startupValidation] Add to validate on start push.
19
- */
20
- constructor(config?: {
21
- appName?: string | undefined;
22
- dynoId?: string | undefined;
23
- processType?: string | undefined;
24
- enabled?: boolean | undefined;
25
- logValues?: boolean | undefined;
26
- pushgatewayUrl?: string | undefined;
27
- pushgatewaySecret?: string | undefined;
28
- intervalSec?: number | undefined;
29
- removeOldMetrics?: boolean | undefined;
30
- scripDefaultMetrics?: boolean | undefined;
31
- startupValidation?: Function | undefined;
32
- });
33
- appName: string;
34
- dynoId: string;
35
- processType: string;
36
- enabled: boolean;
37
- logValues: boolean;
38
- pushgatewayUrl: string;
39
- authToken: string;
40
- intervalSec: number;
41
- startupValidation: Function | undefined;
42
- prefixLogs: string;
43
- _registry: client.Registry<"text/plain; version=0.0.4; charset=utf-8">;
44
- defaultLabels: {
45
- app: string;
46
- dyno_id: string;
47
- process_type: string;
48
- };
49
- gateway: client.Pushgateway<"text/plain; version=0.0.4; charset=utf-8">;
50
- gauges: {};
51
- counters: {};
52
- countersFunctions: {};
53
- /** @type {Object<string, function(): number | Promise<number>>} */
54
- gaugeUpdaters: {
55
- [x: string]: () => number | Promise<number>;
56
- };
6
+ export class MetricsClient extends BaseMetricsClient {
57
7
  _lastUsageMicros: number;
58
8
  _lastCheckTime: number;
9
+ _httpRequestBuffer: any[];
59
10
  /**
60
11
  * Register all built-in default Gauges and Counters.
61
12
  * @private
62
13
  */
63
14
  private _initDefaultMetrics;
64
- /**
65
- * Create a gauge metric.
66
- * @param {Object} options - Gauge configuration
67
- * @param {string} options.name - Name of the gauge
68
- * @param {string} options.help - Help text describing the gauge
69
- * @param {function(): number|Promise<number>} [options.updateFn] - Optional function returning the gauge value
70
- * @param {string[]} [options.labelNames] - Optional custom label names
71
- * @returns {import('prom-client').Gauge} The created Prometheus gauge
72
- */
73
- createGauge: ({ name, help, updateFn, labelNames, }: {
74
- name: string;
75
- help: string;
76
- updateFn?: (() => number | Promise<number>) | undefined;
77
- labelNames?: string[] | undefined;
78
- }) => import('prom-client').Gauge;
79
- /**
80
- * Create a Prometheus Counter metric.
81
- *
82
- * @param {Object} params - Counter configuration
83
- * @param {string} params.name - Metric name
84
- * @param {string} params.help - Metric description
85
- * @param {string[]} [params.labelNames] - Optional list of label names. Defaults to this.defaultLabels keys.
86
- *
87
- * @returns {(labels?: Object, incrementValue?: number) => void}
88
- * A function to increment the counter.
89
- * Usage: (labels?, incrementValue?)
90
- */
91
- createCounter({ name, help, labelNames }: {
92
- name: string;
93
- help: string;
94
- labelNames?: string[] | undefined;
95
- }): (labels?: Object, incrementValue?: number) => void;
96
15
  /**
97
16
  * Get CPU usage percent (cgroup-aware)
98
17
  * @returns {number}
@@ -119,9 +38,9 @@ export class MetricsClient {
119
38
  */
120
39
  measureLag(): Promise<number>;
121
40
  /**
122
- * Increment the HTTP requests counter with detailed request information.
41
+ * Track an HTTP request (adds to buffer for interval collection).
123
42
  *
124
- * @param {Object} params - The parameters for the request counter.
43
+ * @param {Object} params - The parameters for the request.
125
44
  * @param {string} params.method - HTTP method (GET, POST, etc.).
126
45
  * @param {string} params.route - The full requested URL or route.
127
46
  * @param {number} params.status_code - HTTP response status code.
@@ -138,92 +57,15 @@ export class MetricsClient {
138
57
  duration: number;
139
58
  }): void;
140
59
  /**
141
- * Express middleware to track HTTP requests.
142
- * Track the `app_requests_total` and `app_requests_total_duration` metric.
143
- */
144
- trackHttpRequestMiddleware: (req: any, res: any, next: any) => void;
145
- /**
146
- * Clear all collected counters
147
- */
148
- clearAllCounters: () => void;
149
- /**
150
- * Push all gauges and counters to PushGateway and optionally log.
151
- */
152
- pushMetrics: () => Promise<void>;
153
- _startPush: (interval?: number, customPushMetics?: undefined) => void;
154
- /**
155
- * Start periodic metrics collection and push.
156
- *
157
- * This method wraps the internal `_startPush` method.
158
- * If a `customPushMetrics` function is provided, it will be executed
159
- * at the given interval instead of the default `pushMetrics` behavior.
160
- *
161
- * @param {number} [interval=this.intervalSec] - Interval in seconds between pushes.
162
- * @param {() => void | Promise<void>} [customPushMetrics] - Optional custom push function. If provided, Prometheus push is skipped.
163
- */
164
- startPush: (interval?: number | undefined, customPushMetics?: undefined) => void;
165
- /**
166
- * Cleanup metrics and exit process.
167
- * @returns {Promise<void>}
168
- */
169
- cleanup: () => Promise<void>;
170
- /**
171
- * Remove old/stale dyno/instance metrics from PushGateway.
172
- *
173
- * Compares existing PushGateway metrics for this job and deletes any instances
174
- * that do not match the current dynoId.
175
- *
176
- * @param {boolean} removeOldMetrics If true, performs cleanup; otherwise does nothing
177
- * @returns {Promise<void>}
60
+ * Collect HTTP metrics from buffer, group by labels, and set gauges.
178
61
  * @private
179
62
  */
180
- private _clearOldWorkers;
181
- /**
182
- * Delete metrics for this job/instance from PushGateway.
183
- *
184
- * @param {Object} [params]
185
- * @param {string} [params.jobName] Job name (defaults to appName)
186
- * @param {Object} [params.groupings] Grouping labels
187
- * @param {string} [params.groupings.process_type] Process type label
188
- * @param {string} [params.groupings.instance] Instance/dyno ID
189
- * @returns {Promise<void>}
190
- */
191
- gatewayDelete: (params?: {
192
- jobName?: string | undefined;
193
- groupings?: {
194
- process_type?: string | undefined;
195
- instance?: string | undefined;
196
- } | undefined;
197
- } | undefined) => Promise<void>;
198
- /**
199
- * Push metrics to PushGateway.
200
- *
201
- * @param {object} [params]
202
- * @param {string} [params.jobName]
203
- * @param {object} [params.groupings]
204
- * @returns {Promise<void>}
205
- */
206
- gatewayPush: (params?: {
207
- jobName?: string | undefined;
208
- groupings?: object | undefined;
209
- } | undefined) => Promise<void>;
63
+ private _collectHttpMetrics;
210
64
  /**
211
- * Merge the default metric labels (`app`, `dyno_id`, `process_type`)
212
- * with custom label names.
213
- *
214
- * @param {string[]} labels Additional label names
215
- * @returns {string[]} Combined label names
65
+ * Express middleware to track HTTP requests.
66
+ * Track the `http_app_requests_total` and `http_app_requests_total_duration` metric.
216
67
  */
217
- withDefaultLabels: (labels?: string[]) => string[];
218
- getDefaultLabels: (labels?: any[]) => {
219
- app: string;
220
- dyno_id: string;
221
- process_type: string;
222
- };
223
- _setCleanupHandlers: () => void;
224
- get metricsEnabled(): boolean;
225
- get metricsLogValues(): boolean;
226
- get registry(): client.Registry<"text/plain; version=0.0.4; charset=utf-8">;
68
+ trackHttpRequestMiddleware: (req: any, res: any, next: any) => void;
227
69
  }
228
- import client = require("prom-client");
70
+ import { BaseMetricsClient } from "./baseMetricsClient";
229
71
  //# sourceMappingURL=metricsClient.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"metricsClient.d.ts","sourceRoot":"","sources":["../src/metricsClient.js"],"names":[],"mappings":"AAKA;;;GAGG;AACH;IACE;;;;;;;;;;;;;OAaG;IACH;QAZ2B,OAAO;QACP,MAAM;QACN,WAAW;QACV,OAAO;QACP,SAAS;QACV,cAAc;QACd,iBAAiB;QACjB,WAAW;QACV,gBAAgB;QAChB,mBAAmB;QAClB,iBAAiB;OAuD7C;IApDC,gBAA4E;IAC5E,eAAqE;IACrE,oBAG6B;IAC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,QAAQ,IAAI,CAAC,CAUzB;IAED;;;;;;;OAOG;IACH;;;sBAFa,QAAQ,IAAI,CAAC,CAYzB;IAED;;;;;;OAMG;IACH,6BAHW,MAAM,EAAE,KACN,MAAM,EAAE,CAIpB;IAED;;;;MAEC;IAED,gCAGC;IAID,8BAEC;IAED,gCAEC;IAED,4EAEC;CACF"}
1
+ {"version":3,"file":"metricsClient.d.ts","sourceRoot":"","sources":["../src/metricsClient.js"],"names":[],"mappings":"AAIA;;;;GAIG;AACH;IAiBI,yBAAyB;IACzB,uBAAgC;IAEhC,0BAA4B;IAK9B;;;OAGG;IACH,4BA4DC;IAED;;;OAGG;IACH,0BAFa,MAAM,CA2BlB;IAED;;;OAGG;IACH,oBAFa,MAAM,CAiBlB;IAED;;;OAGG;IACH,2BAFa,MAAM,CAWlB;IAED;;;OAGG;IACH,2BAFa,MAAM,CAgBlB;IAED;;;OAGG;IACH,cAFa,QAAQ,MAAM,CAAC,CAO3B;IAED;;;;;;;;;;OAUG;IACH;QAP0B,MAAM,EAArB,MAAM;QACS,KAAK,EAApB,MAAM;QACS,WAAW,EAA1B,MAAM;QACU,KAAK;QACL,UAAU;QACX,QAAQ,EAAvB,MAAM;aAoBhB;IAED;;;OAGG;IACH,4BA6CC;IAUD;;;OAGG;IACH,oEA+BC;CACF"}