encore.dev 1.52.4 → 1.53.0
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.
- package/dist/internal/appinit/mod.js +10 -2
- package/dist/internal/appinit/mod.js.map +1 -1
- package/dist/internal/metrics/mod.d.ts +28 -0
- package/dist/internal/metrics/mod.js +53 -0
- package/dist/internal/metrics/mod.js.map +1 -0
- package/dist/internal/metrics/registry.d.ts +13 -0
- package/dist/internal/metrics/registry.js +58 -0
- package/dist/internal/metrics/registry.js.map +1 -0
- package/dist/internal/runtime/mod.d.ts +8 -0
- package/dist/internal/runtime/mod.js +10 -0
- package/dist/internal/runtime/mod.js.map +1 -1
- package/dist/internal/runtime/napi/napi.cjs +5 -1
- package/dist/internal/runtime/napi/napi.d.cts +35 -0
- package/dist/metrics/mod.d.ts +95 -0
- package/dist/metrics/mod.js +205 -0
- package/dist/metrics/mod.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/internal/appinit/mod.ts +15 -2
- package/internal/metrics/mod.ts +63 -0
- package/internal/metrics/registry.ts +70 -0
- package/internal/runtime/mod.ts +20 -0
- package/internal/runtime/napi/napi.cjs +5 -1
- package/internal/runtime/napi/napi.d.cts +35 -0
- package/metrics/mod.ts +258 -0
- package/package.json +6 -1
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { isMainThread, Worker } from "node:worker_threads";
|
|
1
|
+
import { isMainThread, Worker, workerData } from "node:worker_threads";
|
|
2
2
|
import { MiddlewareRequest, HandlerResponse } from "../../api/mod.js";
|
|
3
3
|
import { IterableSocket, IterableStream, Sink } from "../../api/stream.js";
|
|
4
4
|
import { RawRequest, RawResponse } from "../api/node_http.js";
|
|
5
5
|
import { setCurrentRequest } from "../reqtrack/mod.js";
|
|
6
6
|
import * as runtime from "../runtime/mod.js";
|
|
7
7
|
import { fileURLToPath } from "node:url";
|
|
8
|
+
import { initGlobalMetricsBuffer, setGlobalMetricsBuffer } from "../metrics/registry.js";
|
|
8
9
|
export function registerHandlers(handlers) {
|
|
9
10
|
runtime.RT.registerHandlers(handlers.map((h) => transformHandler(h)));
|
|
10
11
|
}
|
|
@@ -17,15 +18,22 @@ export function registerGateways(gateways) {
|
|
|
17
18
|
}
|
|
18
19
|
export async function run(entrypoint) {
|
|
19
20
|
if (isMainThread) {
|
|
21
|
+
const metricsBuffer = initGlobalMetricsBuffer();
|
|
20
22
|
const extraWorkers = runtime.RT.numWorkerThreads() - 1;
|
|
21
23
|
if (extraWorkers > 0) {
|
|
22
24
|
const path = fileURLToPath(entrypoint);
|
|
23
25
|
for (let i = 0; i < extraWorkers; i++) {
|
|
24
|
-
new Worker(path
|
|
26
|
+
new Worker(path, {
|
|
27
|
+
workerData: { metricsBuffer }
|
|
28
|
+
});
|
|
25
29
|
}
|
|
26
30
|
}
|
|
27
31
|
return runtime.RT.runForever();
|
|
28
32
|
}
|
|
33
|
+
// Worker thread: set metrics buffer from workerData
|
|
34
|
+
if (workerData && workerData.metricsBuffer) {
|
|
35
|
+
setGlobalMetricsBuffer(workerData.metricsBuffer);
|
|
36
|
+
}
|
|
29
37
|
// This is a worker thread. The runtime is already initialized, so block forever.
|
|
30
38
|
await new Promise(() => { });
|
|
31
39
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mod.js","sourceRoot":"","sources":["../../../internal/appinit/mod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"mod.js","sourceRoot":"","sources":["../../../internal/appinit/mod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAEvE,OAAO,EAAc,iBAAiB,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAC/E,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACxE,O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CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC;YAC3B,CAAC,CAAC;YAEF,MAAM,SAAS,GAAG,IAAI,iBAAiB,CAAC,SAAS,EAAE,SAAS,EAAE,SAAS,CAAC,CAAC;YACzE,OAAO,qBAAqB,CAAC,GAAG,EAAE,SAAS,EAAE,WAAW,EAAE,OAAO,CAAC,CAAC;QACrE,CAAC;KACF,CAAC;AACJ,CAAC;AAED,SAAS,UAAU,CACjB,OAAY;IAEZ,IAAI,OAAO,YAAY,OAAO,EAAE,CAAC;QAC/B,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE;YAC9B,OAAO,IAAI,eAAe,CAAC,OAAO,CAAC,CAAC,oBAAoB,EAAE,CAAC;QAC7D,CAAC,CAAC,CAAC;IACL,CAAC;SAAM,CAAC;QACN,OAAO,IAAI,eAAe,CAAC,OAAO,CAAC,CAAC,oBAAoB,EAAE,CAAC;IAC7D,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal class that handles atomic counter operations on SharedArrayBuffer.
|
|
3
|
+
*/
|
|
4
|
+
export declare class AtomicCounter {
|
|
5
|
+
private view;
|
|
6
|
+
private slot;
|
|
7
|
+
constructor(buffer: SharedArrayBuffer, slot: number);
|
|
8
|
+
increment(value?: number): void;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Internal class that handles atomic gauge operations on SharedArrayBuffer.
|
|
12
|
+
*/
|
|
13
|
+
export declare class AtomicGauge {
|
|
14
|
+
private view;
|
|
15
|
+
private slot;
|
|
16
|
+
constructor(buffer: SharedArrayBuffer, slot: number);
|
|
17
|
+
set(value: number): void;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Serialize labels to a consistent string key for map lookups.
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
23
|
+
export declare function serializeLabels(labels: Record<string, string | number | boolean>): string;
|
|
24
|
+
/**
|
|
25
|
+
* Process labels into an array of key/value pairs, converting numbers to floored integers.
|
|
26
|
+
* @internal
|
|
27
|
+
*/
|
|
28
|
+
export declare function processLabelsToPairs(labels: Record<string, string | number | boolean>): [string, string][];
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal class that handles atomic counter operations on SharedArrayBuffer.
|
|
3
|
+
*/
|
|
4
|
+
export class AtomicCounter {
|
|
5
|
+
view;
|
|
6
|
+
slot;
|
|
7
|
+
constructor(buffer, slot) {
|
|
8
|
+
this.view = new BigUint64Array(buffer);
|
|
9
|
+
this.slot = slot;
|
|
10
|
+
}
|
|
11
|
+
increment(value = 1) {
|
|
12
|
+
const v = BigInt(Math.floor(value));
|
|
13
|
+
Atomics.add(this.view, this.slot, v);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Internal class that handles atomic gauge operations on SharedArrayBuffer.
|
|
18
|
+
*/
|
|
19
|
+
export class AtomicGauge {
|
|
20
|
+
view;
|
|
21
|
+
slot;
|
|
22
|
+
constructor(buffer, slot) {
|
|
23
|
+
this.view = new BigUint64Array(buffer);
|
|
24
|
+
this.slot = slot;
|
|
25
|
+
}
|
|
26
|
+
set(value) {
|
|
27
|
+
// For gauges, store f64 bits as u64
|
|
28
|
+
const float64 = new Float64Array(1);
|
|
29
|
+
float64[0] = value;
|
|
30
|
+
const uint64View = new BigUint64Array(float64.buffer);
|
|
31
|
+
const v = uint64View[0];
|
|
32
|
+
Atomics.store(this.view, this.slot, v);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Serialize labels to a consistent string key for map lookups.
|
|
37
|
+
* @internal
|
|
38
|
+
*/
|
|
39
|
+
export function serializeLabels(labels) {
|
|
40
|
+
const sorted = Object.entries(labels).sort(([a], [b]) => a.localeCompare(b));
|
|
41
|
+
return JSON.stringify(sorted);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Process labels into an array of key/value pairs, converting numbers to floored integers.
|
|
45
|
+
* @internal
|
|
46
|
+
*/
|
|
47
|
+
export function processLabelsToPairs(labels) {
|
|
48
|
+
return Object.entries(labels).map(([key, value]) => [
|
|
49
|
+
key,
|
|
50
|
+
typeof value === "number" ? String(Math.floor(value)) : String(value)
|
|
51
|
+
]);
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=mod.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.js","sourceRoot":"","sources":["../../../internal/metrics/mod.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,OAAO,aAAa;IAChB,IAAI,CAAiB;IACrB,IAAI,CAAS;IAErB,YAAY,MAAyB,EAAE,IAAY;QACjD,IAAI,CAAC,IAAI,GAAG,IAAI,cAAc,CAAC,MAAM,CAAC,CAAC;QACvC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;IAED,SAAS,CAAC,QAAgB,CAAC;QACzB,MAAM,CAAC,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC;QACpC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;IACvC,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,WAAW;IACd,IAAI,CAAiB;IACrB,IAAI,CAAS;IAErB,YAAY,MAAyB,EAAE,IAAY;QACjD,IAAI,CAAC,IAAI,GAAG,IAAI,cAAc,CAAC,MAAM,CAAC,CAAC;QACvC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;IAED,GAAG,CAAC,KAAa;QACf,oCAAoC;QACpC,MAAM,OAAO,GAAG,IAAI,YAAY,CAAC,CAAC,CAAC,CAAC;QACpC,OAAO,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC;QACnB,MAAM,UAAU,GAAG,IAAI,cAAc,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QACtD,MAAM,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC;QACxB,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;IACzC,CAAC;CACF;AAED;;;GAGG;AACH,MAAM,UAAU,eAAe,CAC7B,MAAiD;IAEjD,MAAM,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC;IAC7E,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;AAChC,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,oBAAoB,CAClC,MAAiD;IAEjD,OAAO,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC;QAClD,GAAG;QACH,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;KACtE,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import * as runtime from "../runtime/mod.js";
|
|
2
|
+
/**
|
|
3
|
+
* Called during encores initialization
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
export declare function setGlobalMetricsBuffer(buffer: SharedArrayBuffer): void;
|
|
7
|
+
/**
|
|
8
|
+
* Called during encores initialization, should be called on the main thread
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
export declare function initGlobalMetricsBuffer(): SharedArrayBuffer;
|
|
12
|
+
export declare function getRegistry(): runtime.MetricsRegistry | undefined;
|
|
13
|
+
export declare function getBuffer(): SharedArrayBuffer | undefined;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import * as runtime from "../runtime/mod.js";
|
|
2
|
+
import { RT } from "../runtime/mod.js";
|
|
3
|
+
import log from "../../log/mod.js";
|
|
4
|
+
let globalRegistry;
|
|
5
|
+
let globalBuffer;
|
|
6
|
+
let initErrorLogged = false;
|
|
7
|
+
const testMode = process.env.NODE_ENV === "test";
|
|
8
|
+
/**
|
|
9
|
+
* Called during encores initialization
|
|
10
|
+
* @internal
|
|
11
|
+
*/
|
|
12
|
+
export function setGlobalMetricsBuffer(buffer) {
|
|
13
|
+
globalBuffer = buffer;
|
|
14
|
+
globalRegistry = RT.getMetricsRegistry();
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Called during encores initialization, should be called on the main thread
|
|
18
|
+
* @internal
|
|
19
|
+
*/
|
|
20
|
+
export function initGlobalMetricsBuffer() {
|
|
21
|
+
const INITIAL_METRICS_SLOTS = 10000;
|
|
22
|
+
const metricsBuffer = new SharedArrayBuffer(INITIAL_METRICS_SLOTS * 8);
|
|
23
|
+
const view = new BigUint64Array(metricsBuffer);
|
|
24
|
+
runtime.RT.createMetricsRegistry(view);
|
|
25
|
+
setGlobalMetricsBuffer(metricsBuffer);
|
|
26
|
+
return metricsBuffer;
|
|
27
|
+
}
|
|
28
|
+
export function getRegistry() {
|
|
29
|
+
if (!globalRegistry) {
|
|
30
|
+
// In test mode, silently return undefined (no-op)
|
|
31
|
+
if (testMode) {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
// In non-test mode, log error once and return undefined
|
|
35
|
+
if (!initErrorLogged) {
|
|
36
|
+
initErrorLogged = true;
|
|
37
|
+
log.error("Metrics registry not initialized. Metrics will not be collected. ");
|
|
38
|
+
}
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
return globalRegistry;
|
|
42
|
+
}
|
|
43
|
+
export function getBuffer() {
|
|
44
|
+
if (!globalBuffer) {
|
|
45
|
+
// In test mode, silently return undefined (no-op)
|
|
46
|
+
if (testMode) {
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
// In non-test mode, log error once and return undefined
|
|
50
|
+
if (!initErrorLogged) {
|
|
51
|
+
initErrorLogged = true;
|
|
52
|
+
log.error("Metrics buffer not initialized. Metrics will not be collected. ");
|
|
53
|
+
}
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
return globalBuffer;
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.js","sourceRoot":"","sources":["../../../internal/metrics/registry.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,EAAE,EAAE,MAAM,gBAAgB,CAAC;AACpC,OAAO,GAAG,MAAM,eAAe,CAAC;AAEhC,IAAI,cAAmD,CAAC;AACxD,IAAI,YAA2C,CAAC;AAChD,IAAI,eAAe,GAAG,KAAK,CAAC;AAE5B,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,MAAM,CAAC;AAEjD;;;GAGG;AACH,MAAM,UAAU,sBAAsB,CAAC,MAAyB;IAC9D,YAAY,GAAG,MAAM,CAAC;IACtB,cAAc,GAAG,EAAE,CAAC,kBAAkB,EAAE,CAAC;AAC3C,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,uBAAuB;IACrC,MAAM,qBAAqB,GAAG,KAAM,CAAC;IACrC,MAAM,aAAa,GAAG,IAAI,iBAAiB,CAAC,qBAAqB,GAAG,CAAC,CAAC,CAAC;IACvE,MAAM,IAAI,GAAG,IAAI,cAAc,CAAC,aAAa,CAAC,CAAC;IAC/C,OAAO,CAAC,EAAE,CAAC,qBAAqB,CAAC,IAAI,CAAC,CAAC;IACvC,sBAAsB,CAAC,aAAa,CAAC,CAAC;IAEtC,OAAO,aAAa,CAAC;AACvB,CAAC;AAED,MAAM,UAAU,WAAW;IACzB,IAAI,CAAC,cAAc,EAAE,CAAC;QACpB,kDAAkD;QAClD,IAAI,QAAQ,EAAE,CAAC;YACb,OAAO,SAAS,CAAC;QACnB,CAAC;QAED,wDAAwD;QACxD,IAAI,CAAC,eAAe,EAAE,CAAC;YACrB,eAAe,GAAG,IAAI,CAAC;YACvB,GAAG,CAAC,KAAK,CACP,mEAAmE,CACpE,CAAC;QACJ,CAAC;QACD,OAAO,SAAS,CAAC;IACnB,CAAC;IACD,OAAO,cAAc,CAAC;AACxB,CAAC;AAED,MAAM,UAAU,SAAS;IACvB,IAAI,CAAC,YAAY,EAAE,CAAC;QAClB,kDAAkD;QAClD,IAAI,QAAQ,EAAE,CAAC;YACb,OAAO,SAAS,CAAC;QACnB,CAAC;QAED,wDAAwD;QACxD,IAAI,CAAC,eAAe,EAAE,CAAC;YACrB,eAAe,GAAG,IAAI,CAAC;YACvB,GAAG,CAAC,KAAK,CACP,iEAAiE,CAClE,CAAC;QACJ,CAAC;QACD,OAAO,SAAS,CAAC;IACnB,CAAC;IACD,OAAO,YAAY,CAAC;AACtB,CAAC"}
|
|
@@ -1,3 +1,11 @@
|
|
|
1
1
|
import { Runtime } from "./napi/napi.cjs";
|
|
2
2
|
export * from "./napi/napi.cjs";
|
|
3
3
|
export declare const RT: Runtime;
|
|
4
|
+
export interface Metric {
|
|
5
|
+
name: string;
|
|
6
|
+
services: string[];
|
|
7
|
+
}
|
|
8
|
+
export interface RuntimeConfig {
|
|
9
|
+
metrics: Record<string, Metric>;
|
|
10
|
+
}
|
|
11
|
+
export declare function runtimeConfig(): RuntimeConfig;
|
|
@@ -8,4 +8,14 @@ export const RT = new Runtime({
|
|
|
8
8
|
decimal: (val) => new Decimal(val)
|
|
9
9
|
}
|
|
10
10
|
});
|
|
11
|
+
let cached = null;
|
|
12
|
+
export function runtimeConfig() {
|
|
13
|
+
if (cached === null) {
|
|
14
|
+
let cfg = RT.runtimeConfig();
|
|
15
|
+
cached = {
|
|
16
|
+
metrics: cfg.metrics
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
return cached;
|
|
20
|
+
}
|
|
11
21
|
//# sourceMappingURL=mod.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mod.js","sourceRoot":"","sources":["../../../internal/runtime/mod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC1C,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAE1C,cAAc,iBAAiB,CAAC;AAEhC,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,MAAM,CAAC;AAEjD,MAAM,CAAC,MAAM,EAAE,GAAG,IAAI,OAAO,CAAC;IAC5B,QAAQ;IACR,gBAAgB,EAAE;QAChB,OAAO,EAAE,CAAC,GAA6B,EAAE,EAAE,CAAC,IAAI,OAAO,CAAC,GAAG,CAAC;KAC7D;CACF,CAAC,CAAC"}
|
|
1
|
+
{"version":3,"file":"mod.js","sourceRoot":"","sources":["../../../internal/runtime/mod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC1C,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAE1C,cAAc,iBAAiB,CAAC;AAEhC,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,MAAM,CAAC;AAEjD,MAAM,CAAC,MAAM,EAAE,GAAG,IAAI,OAAO,CAAC;IAC5B,QAAQ;IACR,gBAAgB,EAAE;QAChB,OAAO,EAAE,CAAC,GAA6B,EAAE,EAAE,CAAC,IAAI,OAAO,CAAC,GAAG,CAAC;KAC7D;CACF,CAAC,CAAC;AAWH,IAAI,MAAM,GAAyB,IAAI,CAAC;AACxC,MAAM,UAAU,aAAa;IAC3B,IAAI,MAAM,KAAK,IAAI,EAAE,CAAC;QACpB,IAAI,GAAG,GAAG,EAAE,CAAC,aAAa,EAAE,CAAC;QAC7B,MAAM,GAAG;YACP,OAAO,EAAE,GAAG,CAAC,OAAO;SACrB,CAAC;IACJ,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// The version of the runtime this JS bundle was generated for.
|
|
2
|
-
const version = "v1.
|
|
2
|
+
const version = "v1.53.0";
|
|
3
3
|
|
|
4
4
|
// Load the native module.
|
|
5
5
|
const nativeModulePath = process.env.ENCORE_RUNTIME_LIB;
|
|
@@ -26,6 +26,8 @@ const {
|
|
|
26
26
|
ListIterator,
|
|
27
27
|
LogLevel,
|
|
28
28
|
Logger,
|
|
29
|
+
MetricType,
|
|
30
|
+
MetricsRegistry,
|
|
29
31
|
ObjectAttrs,
|
|
30
32
|
ObjectErrorKind,
|
|
31
33
|
PubSubSubscription,
|
|
@@ -64,6 +66,8 @@ module.exports = {
|
|
|
64
66
|
ListIterator,
|
|
65
67
|
LogLevel,
|
|
66
68
|
Logger,
|
|
69
|
+
MetricType,
|
|
70
|
+
MetricsRegistry,
|
|
67
71
|
ObjectAttrs,
|
|
68
72
|
ObjectErrorKind,
|
|
69
73
|
PubSubSubscription,
|
|
@@ -182,6 +182,29 @@ export class Logger {
|
|
|
182
182
|
with(fields: Record<string, unknown>): Logger
|
|
183
183
|
}
|
|
184
184
|
|
|
185
|
+
export interface Metric {
|
|
186
|
+
name: string
|
|
187
|
+
services: Array<string>
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export enum MetricType {
|
|
191
|
+
Counter = 'Counter',
|
|
192
|
+
Gauge = 'Gauge'
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* MetricsRegistry manages the SharedArrayBuffer and slot allocation
|
|
197
|
+
* for custom application metrics.
|
|
198
|
+
* This is a lightweight wrapper around Arc<MetricsRegistryInner> for NAPI.
|
|
199
|
+
*/
|
|
200
|
+
export class MetricsRegistry {
|
|
201
|
+
constructor(typedArray: object)
|
|
202
|
+
/** Allocate (or get) a slot for a unique metric */
|
|
203
|
+
allocateSlot(name: string, labels: Array<[string, string]>, serviceName: string | undefined | null, metricType: MetricType): number
|
|
204
|
+
/** Get the number of allocated slots */
|
|
205
|
+
slotCount(): number
|
|
206
|
+
}
|
|
207
|
+
|
|
185
208
|
export class ObjectAttrs {
|
|
186
209
|
name: string
|
|
187
210
|
version?: string
|
|
@@ -271,12 +294,24 @@ export class Runtime {
|
|
|
271
294
|
/** Returns the git commit hash used to build the Encore runtime */
|
|
272
295
|
static buildCommit(): string
|
|
273
296
|
appMeta(): AppMeta
|
|
297
|
+
runtimeConfig(): RuntimeConfig
|
|
274
298
|
/**
|
|
275
299
|
* Reports the total number of worker threads,
|
|
276
300
|
* including the main thread.
|
|
277
301
|
*/
|
|
278
302
|
numWorkerThreads(): number
|
|
303
|
+
/**
|
|
304
|
+
* Register the shared metrics buffer (called once by main thread)
|
|
305
|
+
* All worker threads will use the same registry
|
|
306
|
+
*/
|
|
307
|
+
createMetricsRegistry(buffer: object): void
|
|
308
|
+
/** Get the shared metrics registry (all worker threads use this) */
|
|
309
|
+
getMetricsRegistry(): MetricsRegistry
|
|
310
|
+
|
|
311
|
+
}
|
|
279
312
|
|
|
313
|
+
export interface RuntimeConfig {
|
|
314
|
+
metrics: Record<string, Metric>
|
|
280
315
|
}
|
|
281
316
|
|
|
282
317
|
export interface RuntimeOptions {
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom metrics for Encore applications.
|
|
3
|
+
*
|
|
4
|
+
* This module provides counters and gauges that can be statically analyzed
|
|
5
|
+
* by the Encore compiler and automatically exported to observability backends.
|
|
6
|
+
*
|
|
7
|
+
* @example Simple counter
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { Counter } from 'encore.dev/metrics';
|
|
10
|
+
*
|
|
11
|
+
* export const ordersProcessed = new Counter("orders_processed");
|
|
12
|
+
*
|
|
13
|
+
* ordersProcessed.increment();
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @example Counter with labels
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import { CounterGroup } from 'encore.dev/metrics';
|
|
19
|
+
*
|
|
20
|
+
* interface Labels {
|
|
21
|
+
* success: boolean;
|
|
22
|
+
* }
|
|
23
|
+
*
|
|
24
|
+
* export const ordersProcessed = new CounterGroup<Labels>("orders_processed");
|
|
25
|
+
*
|
|
26
|
+
* ordersProcessed.with({ success: true }).increment();
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export interface MetricConfig {
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* A Counter tracks cumulative values that only increase.
|
|
33
|
+
* Use counters for metrics like request counts, errors, etc.
|
|
34
|
+
*/
|
|
35
|
+
export declare class Counter {
|
|
36
|
+
private name;
|
|
37
|
+
private cache;
|
|
38
|
+
private labelPairs;
|
|
39
|
+
private cfg;
|
|
40
|
+
constructor(name: string, cfg?: MetricConfig);
|
|
41
|
+
/**
|
|
42
|
+
* Increment the counter by the given value (default 1).
|
|
43
|
+
*/
|
|
44
|
+
increment(value?: number): void;
|
|
45
|
+
ref(): Counter;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* A CounterGroup tracks counters with labels.
|
|
49
|
+
* Each unique combination of label values creates a separate counter time series.
|
|
50
|
+
*
|
|
51
|
+
* @typeParam L - The label interface (must have string/number/boolean fields)
|
|
52
|
+
* Note: Number values in labels are converted to integers using Math.floor().
|
|
53
|
+
*/
|
|
54
|
+
export declare class CounterGroup<L extends Record<keyof L, string | number | boolean>> {
|
|
55
|
+
private name;
|
|
56
|
+
private labelCache;
|
|
57
|
+
private cfg;
|
|
58
|
+
constructor(name: string, cfg?: MetricConfig);
|
|
59
|
+
/**
|
|
60
|
+
* Get a counter for the given label values.
|
|
61
|
+
*
|
|
62
|
+
* Note: Number values in labels are converted to integers using Math.floor().
|
|
63
|
+
*/
|
|
64
|
+
with(labels: L): Counter;
|
|
65
|
+
ref(): CounterGroup<L>;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* A Gauge tracks values that can go up or down.
|
|
69
|
+
* Use gauges for metrics like memory usage, active connections, temperature, etc.
|
|
70
|
+
*/
|
|
71
|
+
export declare class Gauge {
|
|
72
|
+
private name;
|
|
73
|
+
private cache;
|
|
74
|
+
private labelPairs;
|
|
75
|
+
private cfg;
|
|
76
|
+
constructor(name: string, cfg?: MetricConfig);
|
|
77
|
+
/**
|
|
78
|
+
* Set the gauge to the given value.
|
|
79
|
+
*/
|
|
80
|
+
set(value: number): void;
|
|
81
|
+
ref(): Gauge;
|
|
82
|
+
}
|
|
83
|
+
export declare class GaugeGroup<L extends Record<keyof L, string | number | boolean>> {
|
|
84
|
+
private name;
|
|
85
|
+
private labelCache;
|
|
86
|
+
private cfg;
|
|
87
|
+
constructor(name: string, cfg?: MetricConfig);
|
|
88
|
+
/**
|
|
89
|
+
* Get a gauge for the given label values.
|
|
90
|
+
*
|
|
91
|
+
* Note: Number values in labels are converted to integers using Math.floor().
|
|
92
|
+
*/
|
|
93
|
+
with(labels: L): Gauge;
|
|
94
|
+
ref(): GaugeGroup<L>;
|
|
95
|
+
}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom metrics for Encore applications.
|
|
3
|
+
*
|
|
4
|
+
* This module provides counters and gauges that can be statically analyzed
|
|
5
|
+
* by the Encore compiler and automatically exported to observability backends.
|
|
6
|
+
*
|
|
7
|
+
* @example Simple counter
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { Counter } from 'encore.dev/metrics';
|
|
10
|
+
*
|
|
11
|
+
* export const ordersProcessed = new Counter("orders_processed");
|
|
12
|
+
*
|
|
13
|
+
* ordersProcessed.increment();
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @example Counter with labels
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import { CounterGroup } from 'encore.dev/metrics';
|
|
19
|
+
*
|
|
20
|
+
* interface Labels {
|
|
21
|
+
* success: boolean;
|
|
22
|
+
* }
|
|
23
|
+
*
|
|
24
|
+
* export const ordersProcessed = new CounterGroup<Labels>("orders_processed");
|
|
25
|
+
*
|
|
26
|
+
* ordersProcessed.with({ success: true }).increment();
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
import * as runtime from "../internal/runtime/mod.js";
|
|
30
|
+
import { MetricType } from "../internal/runtime/mod.js";
|
|
31
|
+
import { getRegistry, getBuffer } from "../internal/metrics/registry.js";
|
|
32
|
+
import { AtomicCounter, AtomicGauge, processLabelsToPairs, serializeLabels } from "../internal/metrics/mod.js";
|
|
33
|
+
import { currentRequest } from "../req_meta.js";
|
|
34
|
+
/**
|
|
35
|
+
* Resolves the service name for a metric by checking:
|
|
36
|
+
* 1. If there's only one service using this metric in the runtime config
|
|
37
|
+
* 2. Otherwise, looks at the current request context
|
|
38
|
+
*/
|
|
39
|
+
function resolveServiceName(metricName) {
|
|
40
|
+
const rtConfig = runtime.runtimeConfig();
|
|
41
|
+
const rtSvcs = rtConfig.metrics[metricName]?.services ?? [];
|
|
42
|
+
if (rtSvcs.length === 1) {
|
|
43
|
+
return rtSvcs[0];
|
|
44
|
+
}
|
|
45
|
+
const currReq = currentRequest();
|
|
46
|
+
if (currReq) {
|
|
47
|
+
if (currReq.type === "api-call") {
|
|
48
|
+
return currReq.api.service;
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
return currReq.service;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* A Counter tracks cumulative values that only increase.
|
|
58
|
+
* Use counters for metrics like request counts, errors, etc.
|
|
59
|
+
*/
|
|
60
|
+
export class Counter {
|
|
61
|
+
name;
|
|
62
|
+
cache;
|
|
63
|
+
labelPairs;
|
|
64
|
+
cfg;
|
|
65
|
+
constructor(name, cfg) {
|
|
66
|
+
this.name = name;
|
|
67
|
+
this.cfg = cfg ?? {};
|
|
68
|
+
this.labelPairs = [];
|
|
69
|
+
this.cache = new Map();
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Increment the counter by the given value (default 1).
|
|
73
|
+
*/
|
|
74
|
+
increment(value = 1) {
|
|
75
|
+
const serviceName = resolveServiceName(this.name);
|
|
76
|
+
if (!serviceName) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
let metric = this.cache.get(serviceName);
|
|
80
|
+
if (!metric) {
|
|
81
|
+
const registry = getRegistry();
|
|
82
|
+
const buffer = getBuffer();
|
|
83
|
+
// If registry or buffer are not initialized, silently skip
|
|
84
|
+
if (!registry || !buffer) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
const slot = registry.allocateSlot(this.name, this.labelPairs, serviceName, MetricType.Counter);
|
|
88
|
+
metric = new AtomicCounter(buffer, slot);
|
|
89
|
+
this.cache.set(serviceName, metric);
|
|
90
|
+
}
|
|
91
|
+
metric.increment(value);
|
|
92
|
+
}
|
|
93
|
+
ref() {
|
|
94
|
+
return this;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* A CounterGroup tracks counters with labels.
|
|
99
|
+
* Each unique combination of label values creates a separate counter time series.
|
|
100
|
+
*
|
|
101
|
+
* @typeParam L - The label interface (must have string/number/boolean fields)
|
|
102
|
+
* Note: Number values in labels are converted to integers using Math.floor().
|
|
103
|
+
*/
|
|
104
|
+
export class CounterGroup {
|
|
105
|
+
name;
|
|
106
|
+
labelCache;
|
|
107
|
+
cfg;
|
|
108
|
+
constructor(name, cfg) {
|
|
109
|
+
this.name = name;
|
|
110
|
+
this.labelCache = new Map();
|
|
111
|
+
this.cfg = cfg ?? {};
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Get a counter for the given label values.
|
|
115
|
+
*
|
|
116
|
+
* Note: Number values in labels are converted to integers using Math.floor().
|
|
117
|
+
*/
|
|
118
|
+
with(labels) {
|
|
119
|
+
const labelKey = serializeLabels(labels);
|
|
120
|
+
let cached = this.labelCache.get(labelKey);
|
|
121
|
+
if (!cached) {
|
|
122
|
+
// Create counter instance
|
|
123
|
+
cached = new Counter(this.name, this.cfg);
|
|
124
|
+
const labelPairs = processLabelsToPairs(labels);
|
|
125
|
+
cached.labelPairs = labelPairs;
|
|
126
|
+
this.labelCache.set(labelKey, cached);
|
|
127
|
+
}
|
|
128
|
+
return cached;
|
|
129
|
+
}
|
|
130
|
+
ref() {
|
|
131
|
+
return this;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* A Gauge tracks values that can go up or down.
|
|
136
|
+
* Use gauges for metrics like memory usage, active connections, temperature, etc.
|
|
137
|
+
*/
|
|
138
|
+
export class Gauge {
|
|
139
|
+
name;
|
|
140
|
+
cache;
|
|
141
|
+
labelPairs;
|
|
142
|
+
cfg;
|
|
143
|
+
constructor(name, cfg) {
|
|
144
|
+
this.name = name;
|
|
145
|
+
this.cfg = cfg ?? {};
|
|
146
|
+
this.labelPairs = [];
|
|
147
|
+
this.cache = new Map();
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Set the gauge to the given value.
|
|
151
|
+
*/
|
|
152
|
+
set(value) {
|
|
153
|
+
const serviceName = resolveServiceName(this.name);
|
|
154
|
+
if (!serviceName) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
let metric = this.cache.get(serviceName);
|
|
158
|
+
if (!metric) {
|
|
159
|
+
const registry = getRegistry();
|
|
160
|
+
const buffer = getBuffer();
|
|
161
|
+
// If registry or buffer are not initialized, silently skip
|
|
162
|
+
if (!registry || !buffer) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
const slot = registry.allocateSlot(this.name, this.labelPairs, serviceName, MetricType.Gauge);
|
|
166
|
+
metric = new AtomicGauge(buffer, slot);
|
|
167
|
+
this.cache.set(serviceName, metric);
|
|
168
|
+
}
|
|
169
|
+
metric.set(value);
|
|
170
|
+
}
|
|
171
|
+
ref() {
|
|
172
|
+
return this;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
export class GaugeGroup {
|
|
176
|
+
name;
|
|
177
|
+
labelCache;
|
|
178
|
+
cfg;
|
|
179
|
+
constructor(name, cfg) {
|
|
180
|
+
this.name = name;
|
|
181
|
+
this.labelCache = new Map();
|
|
182
|
+
this.cfg = cfg ?? {};
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Get a gauge for the given label values.
|
|
186
|
+
*
|
|
187
|
+
* Note: Number values in labels are converted to integers using Math.floor().
|
|
188
|
+
*/
|
|
189
|
+
with(labels) {
|
|
190
|
+
const labelKey = serializeLabels(labels);
|
|
191
|
+
let cached = this.labelCache.get(labelKey);
|
|
192
|
+
if (!cached) {
|
|
193
|
+
// Create gauge instance
|
|
194
|
+
cached = new Gauge(this.name, this.cfg);
|
|
195
|
+
const labelPairs = processLabelsToPairs(labels);
|
|
196
|
+
cached.labelPairs = labelPairs;
|
|
197
|
+
this.labelCache.set(labelKey, cached);
|
|
198
|
+
}
|
|
199
|
+
return cached;
|
|
200
|
+
}
|
|
201
|
+
ref() {
|
|
202
|
+
return this;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
//# sourceMappingURL=mod.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.js","sourceRoot":"","sources":["../../metrics/mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH,OAAO,KAAK,OAAO,MAAM,yBAAyB,CAAC;AACnD,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;AACtE,OAAO,EACL,aAAa,EACb,WAAW,EACX,oBAAoB,EACpB,eAAe,EAChB,MAAM,yBAAyB,CAAC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