@vercel/functions 3.11.4 → 3.12.0-snapshot-224301946cd2
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/CHANGELOG.md +6 -0
- package/docs/index/functions/metric.md +7 -2
- package/metric.d.ts +3 -1
- package/metric.js +56 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @vercel/functions
|
|
2
2
|
|
|
3
|
+
## 3.12.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 3392aa8: `metric()` now works in a daemon. When the runtime IPC is not available, it writes the metric to the daemon metrics FIFO. In a daemon, `metric()` throws a `RangeError` when the metric is larger than 4096 bytes as a JSON line.
|
|
8
|
+
|
|
3
9
|
## 3.11.4
|
|
4
10
|
|
|
5
11
|
### Patch Changes
|
|
@@ -6,9 +6,9 @@
|
|
|
6
6
|
|
|
7
7
|
> **metric**(`name`, `value`, `tags?`): `void`
|
|
8
8
|
|
|
9
|
-
Defined in: [packages/functions/src/metric.ts:
|
|
9
|
+
Defined in: [packages/functions/src/metric.ts:49](https://github.com/vercel/vercel/blob/main/packages/functions/src/metric.ts#L49)
|
|
10
10
|
|
|
11
|
-
Reports a custom metric for the current Vercel Function invocation.
|
|
11
|
+
Reports a custom metric for the current Vercel Function invocation or daemon.
|
|
12
12
|
|
|
13
13
|
## Parameters
|
|
14
14
|
|
|
@@ -34,6 +34,11 @@ Optional tags to attach to the metric.
|
|
|
34
34
|
|
|
35
35
|
`void`
|
|
36
36
|
|
|
37
|
+
## Throws
|
|
38
|
+
|
|
39
|
+
In a daemon, when the metric is larger than 4096 bytes
|
|
40
|
+
as a JSON line.
|
|
41
|
+
|
|
37
42
|
## Example
|
|
38
43
|
|
|
39
44
|
```js
|
package/metric.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Reports a custom metric for the current Vercel Function invocation.
|
|
2
|
+
* Reports a custom metric for the current Vercel Function invocation or daemon.
|
|
3
3
|
*
|
|
4
4
|
* @param name The name of the metric.
|
|
5
5
|
* @param value The numeric value of the metric.
|
|
6
6
|
* @param tags Optional tags to attach to the metric.
|
|
7
|
+
* @throws {RangeError} In a daemon, when the metric is larger than 4096 bytes
|
|
8
|
+
* as a JSON line.
|
|
7
9
|
*
|
|
8
10
|
* @example
|
|
9
11
|
*
|
package/metric.js
CHANGED
|
@@ -22,9 +22,64 @@ __export(metric_exports, {
|
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(metric_exports);
|
|
24
24
|
const RUSTY_RUNTIME_IPC_SYMBOL = Symbol.for("@vercel/rusty-runtime-ipc");
|
|
25
|
+
const PIPE_BUF = 4096;
|
|
26
|
+
const DROP_METRIC_ERRORS = /* @__PURE__ */ new Set([
|
|
27
|
+
// The FIFO does not exist.
|
|
28
|
+
"ENOENT",
|
|
29
|
+
// The FIFO has no reader.
|
|
30
|
+
"ENXIO",
|
|
31
|
+
"EPIPE",
|
|
32
|
+
// The pipe is full.
|
|
33
|
+
"EAGAIN"
|
|
34
|
+
]);
|
|
25
35
|
function metric(name, value, tags) {
|
|
26
36
|
const ipc = globalThis[RUSTY_RUNTIME_IPC_SYMBOL];
|
|
27
|
-
ipc?.sendMetric
|
|
37
|
+
if (ipc?.sendMetric) {
|
|
38
|
+
ipc.sendMetric(name, value, tags);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
writeToFifo(name, value, tags);
|
|
42
|
+
}
|
|
43
|
+
function writeToFifo(name, value, tags) {
|
|
44
|
+
const path = globalThis.process?.env?.VERCEL_CUSTOM_METRICS_PATH;
|
|
45
|
+
if (!path) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
const line = new TextEncoder().encode(
|
|
49
|
+
`${JSON.stringify({ name, value, tags })}
|
|
50
|
+
`
|
|
51
|
+
);
|
|
52
|
+
if (line.byteLength > PIPE_BUF) {
|
|
53
|
+
throw new RangeError(
|
|
54
|
+
`metric(): "${name}" is ${line.byteLength} bytes as a JSON line. The limit in a daemon is ${PIPE_BUF} bytes.`
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
const fs = loadFs();
|
|
58
|
+
if (!fs) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
try {
|
|
62
|
+
const fd = fs.openSync(
|
|
63
|
+
path,
|
|
64
|
+
fs.constants.O_WRONLY | fs.constants.O_NONBLOCK
|
|
65
|
+
);
|
|
66
|
+
try {
|
|
67
|
+
fs.writeSync(fd, line);
|
|
68
|
+
} finally {
|
|
69
|
+
fs.closeSync(fd);
|
|
70
|
+
}
|
|
71
|
+
} catch (err) {
|
|
72
|
+
if (!DROP_METRIC_ERRORS.has(err.code ?? "")) {
|
|
73
|
+
throw err;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function loadFs() {
|
|
78
|
+
try {
|
|
79
|
+
return globalThis.process.getBuiltinModule?.("node:fs");
|
|
80
|
+
} catch {
|
|
81
|
+
return void 0;
|
|
82
|
+
}
|
|
28
83
|
}
|
|
29
84
|
// Annotate the CommonJS export names for ESM import in node:
|
|
30
85
|
0 && (module.exports = {
|