@nestjs/observe 0.3.25 → 0.3.26
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/README.md +17 -0
- package/dist/agent/detached-observe-worker.js +50 -6
- package/dist/agent/observe-agent.shared-buffer.d.ts +44 -0
- package/dist/agent/observe-agent.shared-buffer.js +196 -19
- package/dist/agent/observe-agent.worker.d.ts +22 -2
- package/dist/agent/observe-agent.worker.js +43 -7
- package/dist/agent/telemetry-wire-contract.js +7 -1
- package/dist/cli/agent-notes.js +16 -4
- package/dist/cli/link.js +28 -3
- package/dist/cli/observe-cli.js +1 -19
- package/dist/cli/open-url.d.ts +27 -0
- package/dist/cli/open-url.js +63 -0
- package/dist/cli/project-files.d.ts +42 -1
- package/dist/cli/project-files.js +146 -7
- package/dist/cli/project-setup.d.ts +6 -0
- package/dist/cli/project-setup.js +14 -1
- package/dist/cli/wire-application.js +6 -0
- package/dist/custom-metrics/counter.d.ts +13 -2
- package/dist/custom-metrics/counter.js +33 -16
- package/dist/custom-metrics/finite-value.util.d.ts +14 -0
- package/dist/custom-metrics/finite-value.util.js +33 -1
- package/dist/custom-metrics/gauge.d.ts +2 -0
- package/dist/custom-metrics/gauge.js +41 -17
- package/dist/custom-metrics/label-values.util.d.ts +37 -0
- package/dist/custom-metrics/label-values.util.js +51 -0
- package/dist/custom-metrics/summary.d.ts +23 -0
- package/dist/custom-metrics/summary.js +73 -2
- package/dist/instrument/create-instance-decorator.instrument.js +158 -96
- package/dist/interfaces/observe-options.interface.d.ts +3 -1
- package/dist/objectives/objectives.registry.js +12 -0
- package/dist/observe.module.js +119 -3
- package/dist/otlp/otlp-encoders.js +22 -5
- package/dist/otlp/otlp-span.mapper.js +7 -3
- package/dist/otlp/otlp-trace-exporter.service.js +63 -5
- package/dist/outgoing/http.integration.d.ts +8 -4
- package/dist/outgoing/http.integration.js +13 -6
- package/dist/outgoing/llm/llm-fetch.integration.js +62 -1
- package/dist/outgoing/llm/llm-providers.d.ts +16 -4
- package/dist/outgoing/llm/llm-providers.js +72 -22
- package/dist/outgoing/llm/llm-usage.d.ts +11 -6
- package/dist/outgoing/llm/llm-usage.js +72 -25
- package/dist/outgoing/mssql.integration.js +16 -2
- package/dist/outgoing/mysql2.integration.js +17 -1
- package/dist/outgoing/outgoing-observe-agent.service.js +15 -3
- package/dist/outgoing/outgoing-span.recorder.js +17 -3
- package/dist/outgoing/pg.integration.d.ts +3 -2
- package/dist/outgoing/pg.integration.js +39 -11
- package/dist/outgoing/sql-statement.util.js +106 -14
- package/dist/outgoing/undici.integration.d.ts +13 -0
- package/dist/outgoing/undici.integration.js +68 -0
- package/dist/protocols/bull-observe-agent.service.js +2 -12
- package/dist/protocols/deferred-trace-completion.d.ts +9 -1
- package/dist/protocols/deferred-trace-completion.js +36 -6
- package/dist/protocols/graphql-observe-agent.service.d.ts +36 -5
- package/dist/protocols/graphql-observe-agent.service.js +166 -5
- package/dist/protocols/graphql-operation-parser.js +72 -18
- package/dist/protocols/http-observe-agent.service.d.ts +43 -9
- package/dist/protocols/http-observe-agent.service.js +177 -59
- package/dist/protocols/job-trace-runner.d.ts +35 -3
- package/dist/protocols/job-trace-runner.js +126 -29
- package/dist/protocols/queue-observe-agent.service.js +44 -20
- package/dist/protocols/rpc-observe-agent.service.d.ts +50 -8
- package/dist/protocols/rpc-observe-agent.service.js +187 -47
- package/dist/protocols/ws-observe-agent.service.d.ts +55 -5
- package/dist/protocols/ws-observe-agent.service.js +226 -20
- package/dist/services/index.d.ts +1 -1
- package/dist/services/index.js +1 -1
- package/dist/services/logger-patcher.service.js +3 -4
- package/dist/services/operation-trace.registry.d.ts +45 -1
- package/dist/services/operation-trace.registry.js +418 -35
- package/dist/services/stdout-forwarder.service.d.ts +69 -0
- package/dist/services/stdout-forwarder.service.js +288 -51
- package/dist/services/tracer.service.d.ts +23 -0
- package/dist/services/tracer.service.js +91 -5
- package/dist/utils/capture-request.util.d.ts +3 -1
- package/dist/utils/capture-request.util.js +100 -12
- package/dist/utils/fit-to-length.util.js +33 -10
- package/dist/utils/log-line.parser.d.ts +14 -0
- package/dist/utils/log-line.parser.js +7 -3
- package/dist/utils/log-redactor.d.ts +14 -0
- package/dist/utils/log-redactor.js +512 -61
- package/dist/utils/operation-state.util.d.ts +39 -0
- package/dist/utils/operation-state.util.js +50 -0
- package/dist/utils/redact-url-query.d.ts +26 -4
- package/dist/utils/redact-url-query.js +184 -32
- package/dist/utils/sensitive-keys.d.ts +4 -1
- package/dist/utils/sensitive-keys.js +48 -10
- package/dist/utils/shared-hook-slot.util.d.ts +48 -0
- package/dist/utils/shared-hook-slot.util.js +103 -0
- package/dist/utils/source-context.util.d.ts +8 -0
- package/dist/utils/source-context.util.js +88 -24
- package/dist/utils/source-map-resolver.util.js +8 -2
- package/dist/utils/trace-context.util.js +21 -6
- package/package.json +2 -2
package/dist/cli/link.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { writeAgentNotes } from "./agent-notes.js";
|
|
2
|
+
import { webAddress } from "./open-url.js";
|
|
2
3
|
import { addDependencyCommand, detectPackageManager, } from "./package-manager.js";
|
|
3
|
-
import { addEnvFileToStartScripts, ensureEnvIgnored, readPackageName, writeEnvFile, } from "./project-files.js";
|
|
4
|
+
import { addEnvFileToStartScripts, ensureEnvIgnored, isEnvFileValue, isProjectFile, LINK_FILES, readPackageName, trackedEnvFile, writeEnvFile, } from "./project-files.js";
|
|
4
5
|
import { findEntryFiles, inspectProject, QUICK_START_URL, SDK_PACKAGE, serviceIdFor, } from "./project-setup.js";
|
|
5
6
|
import { wireApplication } from "./wire-application.js";
|
|
6
7
|
/** Extra seconds `slow_down` adds to the interval, as RFC 8628 says. */
|
|
@@ -30,6 +31,18 @@ export async function link(options, deps) {
|
|
|
30
31
|
deps.log(`${setup.unsupported}. Nothing was written.`);
|
|
31
32
|
return 1;
|
|
32
33
|
}
|
|
34
|
+
// Before a code is asked for: no pair is issued for a project whose own
|
|
35
|
+
// files lead somewhere else, nor for one it cannot be kept out of git in.
|
|
36
|
+
const outside = LINK_FILES.find((file) => !isProjectFile(options.cwd, file));
|
|
37
|
+
if (outside) {
|
|
38
|
+
deps.log(`${outside} leads out of ${options.cwd} - a link, or one that leads nowhere - so link will not write to it. Nothing was written.`);
|
|
39
|
+
return 1;
|
|
40
|
+
}
|
|
41
|
+
const tracked = trackedEnvFile(options.cwd);
|
|
42
|
+
if (tracked) {
|
|
43
|
+
deps.log(`${tracked} is tracked by git, so the secret link writes into it would be in your next commit, whatever .gitignore says. Run git rm --cached ${tracked}, which keeps the file, and link again. Nothing was written.`);
|
|
44
|
+
return 1;
|
|
45
|
+
}
|
|
33
46
|
const manager = detectPackageManager(options.cwd);
|
|
34
47
|
let started;
|
|
35
48
|
try {
|
|
@@ -42,8 +55,15 @@ export async function link(options, deps) {
|
|
|
42
55
|
deps.log(`Could not reach NestJS Observe at ${options.endpoint}: ${describe(err)}`);
|
|
43
56
|
return 1;
|
|
44
57
|
}
|
|
58
|
+
// Printed and opened as parsed, and only as a web address: the page is
|
|
59
|
+
// whatever the service answered, and it is opened before any approval.
|
|
60
|
+
const page = webAddress(started.verificationUri);
|
|
61
|
+
if (!page) {
|
|
62
|
+
deps.log(`NestJS Observe at ${options.endpoint} answered with a link page that is not a web address. Nothing was written.`);
|
|
63
|
+
return 1;
|
|
64
|
+
}
|
|
45
65
|
deps.log("");
|
|
46
|
-
deps.log(` Open ${
|
|
66
|
+
deps.log(` Open ${page}`);
|
|
47
67
|
deps.log(` and type ${started.userCode}`);
|
|
48
68
|
deps.log("");
|
|
49
69
|
deps.log(`Waiting for you to approve it in the dashboard. The code expires in ${Math.round(started.expiresIn / 60)} minutes.`);
|
|
@@ -54,7 +74,7 @@ export async function link(options, deps) {
|
|
|
54
74
|
if (options.openBrowser) {
|
|
55
75
|
// The page only, never the code: the code is typed, so a link that
|
|
56
76
|
// carries it cannot approve anything on its own.
|
|
57
|
-
deps.openUrl(
|
|
77
|
+
deps.openUrl(page);
|
|
58
78
|
}
|
|
59
79
|
const credentials = await waitForApproval(deps, options.endpoint, started);
|
|
60
80
|
if (typeof credentials === "string") {
|
|
@@ -188,6 +208,11 @@ async function waitForApproval(deps, endpoint, started) {
|
|
|
188
208
|
}
|
|
189
209
|
const body = (await response.json().catch(() => ({})));
|
|
190
210
|
if (response.ok && body.appKey && body.appSecret) {
|
|
211
|
+
// Checked before anything is written: the pair goes into .env, which
|
|
212
|
+
// every start of the app loads.
|
|
213
|
+
if (!isEnvFileValue(body.appKey) || !isEnvFileValue(body.appSecret)) {
|
|
214
|
+
return `${endpoint} answered with something other than a key pair. Nothing was written.`;
|
|
215
|
+
}
|
|
191
216
|
return body;
|
|
192
217
|
}
|
|
193
218
|
switch (body.error) {
|
package/dist/cli/observe-cli.js
CHANGED
|
@@ -4,6 +4,7 @@ import { createRequire } from "node:module";
|
|
|
4
4
|
import { hostname } from "node:os";
|
|
5
5
|
import { resolve } from "node:path";
|
|
6
6
|
import { link } from "./link.js";
|
|
7
|
+
import { openUrl } from "./open-url.js";
|
|
7
8
|
import { loadTypeScript } from "./project-setup.js";
|
|
8
9
|
const { version } = createRequire(import.meta.url)("../../package.json");
|
|
9
10
|
const HOSTED_COLLECTOR_ENDPOINT = "https://observe-api.nestjs.com";
|
|
@@ -16,25 +17,6 @@ const USAGE = `Usage: npx @nestjs/observe link [--no-browser] [--cwd <dir>]
|
|
|
16
17
|
Options:
|
|
17
18
|
--no-browser Print the link page instead of opening it.
|
|
18
19
|
--cwd <dir> The project directory (default: the current one).`;
|
|
19
|
-
/**
|
|
20
|
-
* Opens the link page in the default browser, best effort: a headless box or
|
|
21
|
-
* a missing opener just means the person opens the printed URL themselves.
|
|
22
|
-
*/
|
|
23
|
-
function openUrl(url) {
|
|
24
|
-
const [command, args] = process.platform === "darwin"
|
|
25
|
-
? ["open", [url]]
|
|
26
|
-
: process.platform === "win32"
|
|
27
|
-
? ["cmd", ["/c", "start", "", url]]
|
|
28
|
-
: ["xdg-open", [url]];
|
|
29
|
-
try {
|
|
30
|
-
const child = spawn(command, args, { stdio: "ignore", detached: true });
|
|
31
|
-
child.on("error", () => undefined);
|
|
32
|
-
child.unref();
|
|
33
|
-
}
|
|
34
|
-
catch {
|
|
35
|
-
// Printed above; nothing else to do.
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
20
|
async function main(argv) {
|
|
39
21
|
const [command, ...rest] = argv;
|
|
40
22
|
if (command !== "link") {
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `value` as an http(s) URL, parsed and written back out - or undefined for
|
|
3
|
+
* anything else.
|
|
4
|
+
*
|
|
5
|
+
* The link page comes from whoever answers the link request, and is handed
|
|
6
|
+
* to the desktop's opener before anything has been approved: a local file, a
|
|
7
|
+
* script, or another scheme's handler is not that party's to open on the
|
|
8
|
+
* person's machine. Written back out, whitespace and control characters are
|
|
9
|
+
* percent-encoded, so what is printed and opened is one plain address.
|
|
10
|
+
*/
|
|
11
|
+
export declare function webAddress(value: unknown): string | undefined;
|
|
12
|
+
/**
|
|
13
|
+
* The command that opens `url` in the default browser on `platform` - never
|
|
14
|
+
* through a shell.
|
|
15
|
+
*
|
|
16
|
+
* On Windows not `cmd /c start`: cmd.exe reads `&`, `|`, `^` and `%` in the
|
|
17
|
+
* URL as its own syntax, and a valid https URL can hold every one of them.
|
|
18
|
+
* `rundll32 url.dll,FileProtocolHandler` hands it to the same handler `start`
|
|
19
|
+
* does, as one plain argument.
|
|
20
|
+
*/
|
|
21
|
+
export declare function browserCommand(url: string, platform: NodeJS.Platform): [string, string[]];
|
|
22
|
+
/**
|
|
23
|
+
* Opens the link page in the default browser, best effort: a headless box or
|
|
24
|
+
* a missing opener just means the person opens the printed URL themselves.
|
|
25
|
+
* Only a web address is opened.
|
|
26
|
+
*/
|
|
27
|
+
export declare function openUrl(url: string): void;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
/**
|
|
3
|
+
* `value` as an http(s) URL, parsed and written back out - or undefined for
|
|
4
|
+
* anything else.
|
|
5
|
+
*
|
|
6
|
+
* The link page comes from whoever answers the link request, and is handed
|
|
7
|
+
* to the desktop's opener before anything has been approved: a local file, a
|
|
8
|
+
* script, or another scheme's handler is not that party's to open on the
|
|
9
|
+
* person's machine. Written back out, whitespace and control characters are
|
|
10
|
+
* percent-encoded, so what is printed and opened is one plain address.
|
|
11
|
+
*/
|
|
12
|
+
export function webAddress(value) {
|
|
13
|
+
if (typeof value !== "string") {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
try {
|
|
17
|
+
const url = new URL(value);
|
|
18
|
+
return url.protocol === "https:" || url.protocol === "http:"
|
|
19
|
+
? url.href
|
|
20
|
+
: undefined;
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The command that opens `url` in the default browser on `platform` - never
|
|
28
|
+
* through a shell.
|
|
29
|
+
*
|
|
30
|
+
* On Windows not `cmd /c start`: cmd.exe reads `&`, `|`, `^` and `%` in the
|
|
31
|
+
* URL as its own syntax, and a valid https URL can hold every one of them.
|
|
32
|
+
* `rundll32 url.dll,FileProtocolHandler` hands it to the same handler `start`
|
|
33
|
+
* does, as one plain argument.
|
|
34
|
+
*/
|
|
35
|
+
export function browserCommand(url, platform) {
|
|
36
|
+
if (platform === "darwin") {
|
|
37
|
+
return ["open", [url]];
|
|
38
|
+
}
|
|
39
|
+
if (platform === "win32") {
|
|
40
|
+
return ["rundll32", ["url.dll,FileProtocolHandler", url]];
|
|
41
|
+
}
|
|
42
|
+
return ["xdg-open", [url]];
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Opens the link page in the default browser, best effort: a headless box or
|
|
46
|
+
* a missing opener just means the person opens the printed URL themselves.
|
|
47
|
+
* Only a web address is opened.
|
|
48
|
+
*/
|
|
49
|
+
export function openUrl(url) {
|
|
50
|
+
const address = webAddress(url);
|
|
51
|
+
if (!address) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const [command, args] = browserCommand(address, process.platform);
|
|
55
|
+
try {
|
|
56
|
+
const child = spawn(command, args, { stdio: "ignore", detached: true });
|
|
57
|
+
child.on("error", () => undefined);
|
|
58
|
+
child.unref();
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
// Printed already; nothing else to do.
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -1,6 +1,32 @@
|
|
|
1
|
+
/** The files `link` writes in every project, whatever else it does. */
|
|
2
|
+
export declare const LINK_FILES: string[];
|
|
3
|
+
/**
|
|
4
|
+
* Whether `path`, relative to `cwd`, is a file of the project's own once
|
|
5
|
+
* links are followed - where `link` may write.
|
|
6
|
+
*
|
|
7
|
+
* A checkout's files can be links to anywhere the person can write, and a
|
|
8
|
+
* project's own paths can climb out with `../`: written through, the key
|
|
9
|
+
* pair, the notes or the wiring would land in someone else's file, reported
|
|
10
|
+
* under an in-project name. A file still to be created is the project's when
|
|
11
|
+
* its directory is; a link that leads nowhere is not, because writing to it
|
|
12
|
+
* would create the file it points at.
|
|
13
|
+
*/
|
|
14
|
+
export declare function isProjectFile(cwd: string, path: string): boolean;
|
|
1
15
|
/** The variables `ObserveModule` falls back to for its key pair. */
|
|
2
16
|
export declare const KEY_VARIABLE = "OBSERVE_APP_KEY";
|
|
3
17
|
export declare const SECRET_VARIABLE = "OBSERVE_APP_SECRET";
|
|
18
|
+
/**
|
|
19
|
+
* Whether `value` can be written into `.env` as `writeEnvFile` writes it.
|
|
20
|
+
*
|
|
21
|
+
* The pair comes from the link service's reply, and `.env` is loaded into
|
|
22
|
+
* the app on every start: a quote and a line break would end the value and
|
|
23
|
+
* add a line of the reply's choosing - `NODE_OPTIONS=--import ...`, or a
|
|
24
|
+
* `DATABASE_URL` that wins over the project's own. Refused rather than
|
|
25
|
+
* escaped: no escape for a quote or a line break reads the same in Node's
|
|
26
|
+
* `--env-file`, `dotenv` and a shell, and `dotenv` reads `\'` as an escaped
|
|
27
|
+
* quote where Node ends the value.
|
|
28
|
+
*/
|
|
29
|
+
export declare function isEnvFileValue(value: unknown): value is string;
|
|
4
30
|
/**
|
|
5
31
|
* Writes the key pair into `<cwd>/.env`, replacing an earlier pair and keeping
|
|
6
32
|
* every other line as it was. Created owner-only when it does not exist.
|
|
@@ -10,9 +36,24 @@ export declare const SECRET_VARIABLE = "OBSERVE_APP_SECRET";
|
|
|
10
36
|
* variables (`dotenv-expand`, `ConfigModule` with `expandVariables`) rewrites
|
|
11
37
|
* everything after a `$`, and a shell sourcing the file stops at the `&`.
|
|
12
38
|
* Node's own `--env-file` reads either form literally. Single quotes are
|
|
13
|
-
* literal in all of them, and
|
|
39
|
+
* literal in all of them, and a value with a quote - or anything else
|
|
40
|
+
* `isEnvFileValue` refuses - is not written at all.
|
|
14
41
|
*/
|
|
15
42
|
export declare function writeEnvFile(cwd: string, appKey: string, appSecret: string): string;
|
|
43
|
+
/**
|
|
44
|
+
* `.env` as a path relative to `cwd` when git already tracks the file it is -
|
|
45
|
+
* itself, or what it links to - or undefined: not tracked, not in a
|
|
46
|
+
* repository, or no git to ask.
|
|
47
|
+
*
|
|
48
|
+
* An ignore rule does not apply to a file in the index, so the secret written
|
|
49
|
+
* into a tracked `.env` would be in the next `git commit -a` whatever
|
|
50
|
+
* `.gitignore` says. Git is asked with its arguments fixed, from Node's own
|
|
51
|
+
* directory and pointed at the project with `-C`, and with the two settings
|
|
52
|
+
* a project's own `.git/config` could use to run a program turned off:
|
|
53
|
+
* `core.fsmonitor`, which `ls-files` would start, and the discovery of a bare
|
|
54
|
+
* repository that only sits in the checkout.
|
|
55
|
+
*/
|
|
56
|
+
export declare function trackedEnvFile(cwd: string): string | undefined;
|
|
16
57
|
/**
|
|
17
58
|
* Makes sure `.env` is git-ignored, so the secret cannot be committed by
|
|
18
59
|
* accident. `nest new` already ignores it; a project from elsewhere may not.
|
|
@@ -1,5 +1,44 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { existsSync, lstatSync, readFileSync, realpathSync, writeFileSync, } from "node:fs";
|
|
3
|
+
import { basename, dirname, isAbsolute, join, relative, sep } from "node:path";
|
|
4
|
+
/** The files `link` writes in every project, whatever else it does. */
|
|
5
|
+
export const LINK_FILES = [".env", ".gitignore", "package.json"];
|
|
6
|
+
/**
|
|
7
|
+
* Whether `path`, relative to `cwd`, is a file of the project's own once
|
|
8
|
+
* links are followed - where `link` may write.
|
|
9
|
+
*
|
|
10
|
+
* A checkout's files can be links to anywhere the person can write, and a
|
|
11
|
+
* project's own paths can climb out with `../`: written through, the key
|
|
12
|
+
* pair, the notes or the wiring would land in someone else's file, reported
|
|
13
|
+
* under an in-project name. A file still to be created is the project's when
|
|
14
|
+
* its directory is; a link that leads nowhere is not, because writing to it
|
|
15
|
+
* would create the file it points at.
|
|
16
|
+
*/
|
|
17
|
+
export function isProjectFile(cwd, path) {
|
|
18
|
+
let root;
|
|
19
|
+
try {
|
|
20
|
+
root = realpathSync.native(cwd);
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
const file = join(cwd, path);
|
|
26
|
+
let real;
|
|
27
|
+
try {
|
|
28
|
+
real = realpathSync.native(file);
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
if (lstatSync(file, { throwIfNoEntry: false }) !== undefined) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
const parent = dirname(path);
|
|
35
|
+
return parent === path ? false : isProjectFile(cwd, parent);
|
|
36
|
+
}
|
|
37
|
+
const fromRoot = relative(root, real);
|
|
38
|
+
return (fromRoot !== ".." &&
|
|
39
|
+
!fromRoot.startsWith(`..${sep}`) &&
|
|
40
|
+
!isAbsolute(fromRoot));
|
|
41
|
+
}
|
|
3
42
|
/** The variables `ObserveModule` falls back to for its key pair. */
|
|
4
43
|
export const KEY_VARIABLE = "OBSERVE_APP_KEY";
|
|
5
44
|
export const SECRET_VARIABLE = "OBSERVE_APP_SECRET";
|
|
@@ -9,6 +48,26 @@ export const SECRET_VARIABLE = "OBSERVE_APP_SECRET";
|
|
|
9
48
|
* load `.env` instead.
|
|
10
49
|
*/
|
|
11
50
|
const CONVENTIONAL_START_SCRIPTS = new Set(["start", "start:dev"]);
|
|
51
|
+
/**
|
|
52
|
+
* Printable ASCII without whitespace, a single quote or a backslash: what
|
|
53
|
+
* stands between single quotes in `.env` and reads the same to every loader.
|
|
54
|
+
* Key and secret are drawn from letters, digits and `!$%^&`, well inside it.
|
|
55
|
+
*/
|
|
56
|
+
const ENV_FILE_VALUE = /^[\x21-\x26\x28-\x5b\x5d-\x7e]+$/;
|
|
57
|
+
/**
|
|
58
|
+
* Whether `value` can be written into `.env` as `writeEnvFile` writes it.
|
|
59
|
+
*
|
|
60
|
+
* The pair comes from the link service's reply, and `.env` is loaded into
|
|
61
|
+
* the app on every start: a quote and a line break would end the value and
|
|
62
|
+
* add a line of the reply's choosing - `NODE_OPTIONS=--import ...`, or a
|
|
63
|
+
* `DATABASE_URL` that wins over the project's own. Refused rather than
|
|
64
|
+
* escaped: no escape for a quote or a line break reads the same in Node's
|
|
65
|
+
* `--env-file`, `dotenv` and a shell, and `dotenv` reads `\'` as an escaped
|
|
66
|
+
* quote where Node ends the value.
|
|
67
|
+
*/
|
|
68
|
+
export function isEnvFileValue(value) {
|
|
69
|
+
return typeof value === "string" && ENV_FILE_VALUE.test(value);
|
|
70
|
+
}
|
|
12
71
|
/**
|
|
13
72
|
* Writes the key pair into `<cwd>/.env`, replacing an earlier pair and keeping
|
|
14
73
|
* every other line as it was. Created owner-only when it does not exist.
|
|
@@ -18,9 +77,13 @@ const CONVENTIONAL_START_SCRIPTS = new Set(["start", "start:dev"]);
|
|
|
18
77
|
* variables (`dotenv-expand`, `ConfigModule` with `expandVariables`) rewrites
|
|
19
78
|
* everything after a `$`, and a shell sourcing the file stops at the `&`.
|
|
20
79
|
* Node's own `--env-file` reads either form literally. Single quotes are
|
|
21
|
-
* literal in all of them, and
|
|
80
|
+
* literal in all of them, and a value with a quote - or anything else
|
|
81
|
+
* `isEnvFileValue` refuses - is not written at all.
|
|
22
82
|
*/
|
|
23
83
|
export function writeEnvFile(cwd, appKey, appSecret) {
|
|
84
|
+
if (!isEnvFileValue(appKey) || !isEnvFileValue(appSecret)) {
|
|
85
|
+
throw new TypeError("The key pair can hold only printable ASCII without whitespace, quotes or backslashes.");
|
|
86
|
+
}
|
|
24
87
|
const path = join(cwd, ".env");
|
|
25
88
|
const existing = existsSync(path) ? readFileSync(path, "utf8") : "";
|
|
26
89
|
const kept = existing
|
|
@@ -39,6 +102,49 @@ export function writeEnvFile(cwd, appKey, appSecret) {
|
|
|
39
102
|
writeFileSync(path, lines.join("\n"), existing ? undefined : { mode: 0o600 });
|
|
40
103
|
return path;
|
|
41
104
|
}
|
|
105
|
+
/**
|
|
106
|
+
* `.env` as a path relative to `cwd` when git already tracks the file it is -
|
|
107
|
+
* itself, or what it links to - or undefined: not tracked, not in a
|
|
108
|
+
* repository, or no git to ask.
|
|
109
|
+
*
|
|
110
|
+
* An ignore rule does not apply to a file in the index, so the secret written
|
|
111
|
+
* into a tracked `.env` would be in the next `git commit -a` whatever
|
|
112
|
+
* `.gitignore` says. Git is asked with its arguments fixed, from Node's own
|
|
113
|
+
* directory and pointed at the project with `-C`, and with the two settings
|
|
114
|
+
* a project's own `.git/config` could use to run a program turned off:
|
|
115
|
+
* `core.fsmonitor`, which `ls-files` would start, and the discovery of a bare
|
|
116
|
+
* repository that only sits in the checkout.
|
|
117
|
+
*/
|
|
118
|
+
export function trackedEnvFile(cwd) {
|
|
119
|
+
let root;
|
|
120
|
+
try {
|
|
121
|
+
root = realpathSync(cwd);
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
return undefined;
|
|
125
|
+
}
|
|
126
|
+
let path = join(root, ".env");
|
|
127
|
+
try {
|
|
128
|
+
path = realpathSync(path);
|
|
129
|
+
}
|
|
130
|
+
catch {
|
|
131
|
+
// Not there yet, though it may still be in the index.
|
|
132
|
+
}
|
|
133
|
+
const { status } = spawnSync("git", [
|
|
134
|
+
"-c",
|
|
135
|
+
"core.fsmonitor=false",
|
|
136
|
+
"-c",
|
|
137
|
+
"safe.bareRepository=explicit",
|
|
138
|
+
"--literal-pathspecs",
|
|
139
|
+
"-C",
|
|
140
|
+
dirname(path),
|
|
141
|
+
"ls-files",
|
|
142
|
+
"--error-unmatch",
|
|
143
|
+
"--",
|
|
144
|
+
basename(path),
|
|
145
|
+
], { cwd: dirname(process.execPath), stdio: "ignore", timeout: 10_000 });
|
|
146
|
+
return status === 0 ? relative(root, path) || ".env" : undefined;
|
|
147
|
+
}
|
|
42
148
|
/**
|
|
43
149
|
* Makes sure `.env` is git-ignored, so the secret cannot be committed by
|
|
44
150
|
* accident. `nest new` already ignores it; a project from elsewhere may not.
|
|
@@ -47,10 +153,20 @@ export function writeEnvFile(cwd, appKey, appSecret) {
|
|
|
47
153
|
export function ensureEnvIgnored(cwd) {
|
|
48
154
|
const path = join(cwd, ".gitignore");
|
|
49
155
|
const existing = existsSync(path) ? readFileSync(path, "utf8") : "";
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
156
|
+
// Read the way git reads it. The last rule that matches decides, so a
|
|
157
|
+
// `!.env` further down takes `.env` back out. Only trailing spaces are
|
|
158
|
+
// dropped from a rule: an indented ` .env` names a file that starts with
|
|
159
|
+
// a space, and `.env` followed by a tab one that ends with a tab.
|
|
160
|
+
let ignored = false;
|
|
161
|
+
for (const line of existing.replace(/^\uFEFF/, "").split(/\r?\n/)) {
|
|
162
|
+
const rule = line.replace(/ +$/, "");
|
|
163
|
+
if (rule === ".env" || rule === "/.env" || rule === ".env*") {
|
|
164
|
+
ignored = true;
|
|
165
|
+
}
|
|
166
|
+
else if (rule.startsWith("!") && mayMatchEnv(rule.slice(1))) {
|
|
167
|
+
ignored = false;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
54
170
|
if (ignored) {
|
|
55
171
|
return false;
|
|
56
172
|
}
|
|
@@ -58,6 +174,29 @@ export function ensureEnvIgnored(cwd) {
|
|
|
58
174
|
writeFileSync(path, `${existing}${separator}\n# NestJS Observe key pair\n.env\n`);
|
|
59
175
|
return true;
|
|
60
176
|
}
|
|
177
|
+
/**
|
|
178
|
+
* Whether a `.gitignore` pattern can match `<cwd>/.env`. Asked only of rules
|
|
179
|
+
* that take files back out, where a wrong yes costs one more `.env` line at
|
|
180
|
+
* the end of the file and a wrong no costs the secret - so whatever is not
|
|
181
|
+
* read exactly counts as a match: a `[class]`, a `\` escape, and a name in
|
|
182
|
+
* another case, for a checkout on a case-insensitive file system.
|
|
183
|
+
*/
|
|
184
|
+
function mayMatchEnv(pattern) {
|
|
185
|
+
if (/[[\\]/.test(pattern)) {
|
|
186
|
+
return true;
|
|
187
|
+
}
|
|
188
|
+
// `/` in front anchors it to this directory, where `.env` is anyway; one
|
|
189
|
+
// at the end means directories only, and `.env` is a file.
|
|
190
|
+
const name = pattern.replace(/^\//, "").replace(/^(\*\*\/)+/, "");
|
|
191
|
+
if (!name || name.includes("/")) {
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
const glob = name
|
|
195
|
+
.replace(/[.+^${}()|]/g, "\\$&")
|
|
196
|
+
.replace(/\*/g, "[^/]*")
|
|
197
|
+
.replace(/\?/g, "[^/]");
|
|
198
|
+
return new RegExp(`^${glob}$`, "i").test(".env");
|
|
199
|
+
}
|
|
61
200
|
/**
|
|
62
201
|
* Adds `--env-file .env` to every script that runs `nest start` - `start`,
|
|
63
202
|
* `start:dev` and `start:debug` in a `nest new` project, and any of a
|
|
@@ -26,6 +26,12 @@ export declare function inspectProject(cwd: string, version: string, ts: TypeScr
|
|
|
26
26
|
* The project's own TypeScript, which the code is edited with - parsed by the
|
|
27
27
|
* compiler that will build it, so whatever syntax the project uses, it reads.
|
|
28
28
|
* Undefined without one; the CLI does not bring a compiler of its own.
|
|
29
|
+
*
|
|
30
|
+
* Found in the `node_modules` directories the project resolves from, and
|
|
31
|
+
* loaded by its absolute path from the CLI's own module. Not asked of the
|
|
32
|
+
* project as `require("typescript")`: Node answers that with package
|
|
33
|
+
* self-reference first, and in a project named `typescript` with `exports`
|
|
34
|
+
* it would load and run the project's own file instead, on every link.
|
|
29
35
|
*/
|
|
30
36
|
export declare function loadTypeScript(cwd: string): TypeScript | undefined;
|
|
31
37
|
/**
|
|
@@ -53,10 +53,23 @@ export function inspectProject(cwd, version, ts) {
|
|
|
53
53
|
* The project's own TypeScript, which the code is edited with - parsed by the
|
|
54
54
|
* compiler that will build it, so whatever syntax the project uses, it reads.
|
|
55
55
|
* Undefined without one; the CLI does not bring a compiler of its own.
|
|
56
|
+
*
|
|
57
|
+
* Found in the `node_modules` directories the project resolves from, and
|
|
58
|
+
* loaded by its absolute path from the CLI's own module. Not asked of the
|
|
59
|
+
* project as `require("typescript")`: Node answers that with package
|
|
60
|
+
* self-reference first, and in a project named `typescript` with `exports`
|
|
61
|
+
* it would load and run the project's own file instead, on every link.
|
|
56
62
|
*/
|
|
57
63
|
export function loadTypeScript(cwd) {
|
|
58
64
|
try {
|
|
59
|
-
const
|
|
65
|
+
const installed = createRequire(join(cwd, "package.json"))
|
|
66
|
+
.resolve.paths("typescript")
|
|
67
|
+
?.map((dir) => join(dir, "typescript"))
|
|
68
|
+
.find((dir) => existsSync(join(dir, "package.json")));
|
|
69
|
+
if (installed === undefined) {
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
const ts = createRequire(import.meta.url)(installed);
|
|
60
73
|
return typeof ts.createSourceFile === "function" ? ts : undefined;
|
|
61
74
|
}
|
|
62
75
|
catch {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
2
|
import { join, posix } from "node:path";
|
|
3
|
+
import { isProjectFile } from "./project-files.js";
|
|
3
4
|
import { SDK_PACKAGE } from "./project-setup.js";
|
|
4
5
|
/** What a step could not do, as the reason the CLI prints. */
|
|
5
6
|
class ManualStep {
|
|
@@ -26,6 +27,11 @@ export function wireApplication(ts, cwd, mainPath, serviceId) {
|
|
|
26
27
|
try {
|
|
27
28
|
const files = planWiring(ts, cwd, mainPath, serviceId);
|
|
28
29
|
for (const file of files) {
|
|
30
|
+
// A link out of the tree, or a `../` path: some other application's
|
|
31
|
+
// file, which nobody asked to have rewritten.
|
|
32
|
+
if (!isProjectFile(cwd, file.path)) {
|
|
33
|
+
return manual(`${file.path} leads out of the project`);
|
|
34
|
+
}
|
|
29
35
|
if (syntaxErrors(ts, file.path, file.text) >
|
|
30
36
|
syntaxErrors(ts, file.path, file.original)) {
|
|
31
37
|
return manual(`editing ${file.path} would not have left it valid TypeScript`);
|
|
@@ -15,8 +15,10 @@ export declare function stringifyLabel(label: Record<string, string | number>):
|
|
|
15
15
|
* flushed for the life of the process. That is correct for a counter and fatal
|
|
16
16
|
* for one labelled with something unbounded - a user id, a request id, a raw
|
|
17
17
|
* URL - which grows this object forever inside the *host application's* memory
|
|
18
|
-
* and adds a row per series to every flush.
|
|
19
|
-
*
|
|
18
|
+
* and adds a row per series to every flush. A summary's labels go when its
|
|
19
|
+
* window does, on each flush written - but a flush that is never written, by
|
|
20
|
+
* an agent switched off or without credentials, never clears one, so a
|
|
21
|
+
* summary that declared no labels is held to the same number per window.
|
|
20
22
|
*
|
|
21
23
|
* A thousand is far above a sensible label set and far below a leak.
|
|
22
24
|
*/
|
|
@@ -44,6 +46,8 @@ export declare class Counter<TLabel extends string = typeof DEFAULT_LABEL> imple
|
|
|
44
46
|
private readonly seriesLimit;
|
|
45
47
|
/** Whether a non-finite value has already been reported, likewise. */
|
|
46
48
|
private readonly nonFinite;
|
|
49
|
+
/** Whether a label value no key can be made from has been reported. */
|
|
50
|
+
private readonly labelValues;
|
|
47
51
|
/**
|
|
48
52
|
* The name of the counter.
|
|
49
53
|
* This should be a descriptive name that identifies the counter.
|
|
@@ -162,6 +166,13 @@ export declare class Counter<TLabel extends string = typeof DEFAULT_LABEL> imple
|
|
|
162
166
|
* @param value Value to increment the counter by.
|
|
163
167
|
*/
|
|
164
168
|
increment(label: Record<TLabel, string | number>, value: number): void;
|
|
169
|
+
/**
|
|
170
|
+
* Whether `key` may move to `reading`. The reading and its increase since
|
|
171
|
+
* the last flush both go on the wire, and the increase can leave the
|
|
172
|
+
* doubles while the reading stays in them: a counter taken down to -1e308,
|
|
173
|
+
* flushed, then brought back up to 1e308 is 2e308 above its baseline.
|
|
174
|
+
*/
|
|
175
|
+
private admitsNewReading;
|
|
165
176
|
private validateLabels;
|
|
166
177
|
}
|
|
167
178
|
export {};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { admitsFinite } from "./finite-value.util.js";
|
|
1
|
+
import { admitsFinite, admitsReading } from "./finite-value.util.js";
|
|
2
|
+
import { admitsLabelValues, isLabelSet, MAX_SERIES_KEY_LENGTH, } from "./label-values.util.js";
|
|
2
3
|
const DEFAULT_LABEL = "default";
|
|
3
4
|
/**
|
|
4
5
|
* Serialises a label object into the map key that identifies its series.
|
|
@@ -21,18 +22,14 @@ export function stringifyLabel(label) {
|
|
|
21
22
|
* flushed for the life of the process. That is correct for a counter and fatal
|
|
22
23
|
* for one labelled with something unbounded - a user id, a request id, a raw
|
|
23
24
|
* URL - which grows this object forever inside the *host application's* memory
|
|
24
|
-
* and adds a row per series to every flush.
|
|
25
|
-
*
|
|
25
|
+
* and adds a row per series to every flush. A summary's labels go when its
|
|
26
|
+
* window does, on each flush written - but a flush that is never written, by
|
|
27
|
+
* an agent switched off or without credentials, never clears one, so a
|
|
28
|
+
* summary that declared no labels is held to the same number per window.
|
|
26
29
|
*
|
|
27
30
|
* A thousand is far above a sensible label set and far below a leak.
|
|
28
31
|
*/
|
|
29
32
|
export const MAX_SERIES_PER_METRIC = 1_000;
|
|
30
|
-
/**
|
|
31
|
-
* The longest series key - the serialised label combination - the collector
|
|
32
|
-
* accepts. A longer one is not truncated there: the whole batch it rides in is
|
|
33
|
-
* refused, logs and spans included.
|
|
34
|
-
*/
|
|
35
|
-
const MAX_SERIES_KEY_LENGTH = 255;
|
|
36
33
|
/**
|
|
37
34
|
* Whether a metric may record under `key`: a series it already has, or a new
|
|
38
35
|
* one that is within the cap and short enough for the collector.
|
|
@@ -80,6 +77,8 @@ export class Counter {
|
|
|
80
77
|
seriesLimit = { warned: false };
|
|
81
78
|
/** Whether a non-finite value has already been reported, likewise. */
|
|
82
79
|
nonFinite = { warned: false };
|
|
80
|
+
/** Whether a label value no key can be made from has been reported. */
|
|
81
|
+
labelValues = { warned: false };
|
|
83
82
|
/**
|
|
84
83
|
* The name of the counter.
|
|
85
84
|
* This should be a descriptive name that identifies the counter.
|
|
@@ -209,33 +208,51 @@ export class Counter {
|
|
|
209
208
|
}
|
|
210
209
|
}
|
|
211
210
|
increment(labelOrValue, value) {
|
|
212
|
-
if (this.labels &&
|
|
211
|
+
if (this.labels && !isLabelSet(labelOrValue)) {
|
|
213
212
|
throw new Error("Cannot increment a counter with labels without specifying a label. For example, use `counter.increment({ route: '/login' })`.");
|
|
214
213
|
}
|
|
215
|
-
if (
|
|
214
|
+
if (isLabelSet(labelOrValue)) {
|
|
216
215
|
this.validateLabels(labelOrValue);
|
|
216
|
+
if (!admitsLabelValues(this.name, labelOrValue, this.labelValues)) {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
217
219
|
const stringifiedLabel = stringifyLabel(labelOrValue);
|
|
218
220
|
const incrementValue = value ?? 1;
|
|
219
|
-
if (!admitsFinite(this.name, incrementValue, this.nonFinite)
|
|
221
|
+
if (!admitsFinite(this.name, incrementValue, this.nonFinite)) {
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
const reading = (this.value[stringifiedLabel] ?? this._initialValue) + incrementValue;
|
|
225
|
+
if (!this.admitsNewReading(stringifiedLabel, reading) ||
|
|
220
226
|
!admitsSeries(this.name, this.value, stringifiedLabel, this.seriesLimit)) {
|
|
221
227
|
return;
|
|
222
228
|
}
|
|
223
|
-
this.value[stringifiedLabel] =
|
|
224
|
-
(this.value[stringifiedLabel] ?? this._initialValue) + incrementValue;
|
|
229
|
+
this.value[stringifiedLabel] = reading;
|
|
225
230
|
}
|
|
226
231
|
else {
|
|
227
232
|
const incrementValue = labelOrValue ?? 1;
|
|
228
233
|
if (!admitsFinite(this.name, incrementValue, this.nonFinite)) {
|
|
229
234
|
return;
|
|
230
235
|
}
|
|
231
|
-
this.value[DEFAULT_LABEL]
|
|
232
|
-
|
|
236
|
+
const reading = (this.value[DEFAULT_LABEL] ?? this._initialValue) + incrementValue;
|
|
237
|
+
if (!this.admitsNewReading(DEFAULT_LABEL, reading)) {
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
this.value[DEFAULT_LABEL] = reading;
|
|
233
241
|
}
|
|
234
242
|
this._lastUpdated = Date.now();
|
|
235
243
|
if (this._onChange) {
|
|
236
244
|
this._onChange(this);
|
|
237
245
|
}
|
|
238
246
|
}
|
|
247
|
+
/**
|
|
248
|
+
* Whether `key` may move to `reading`. The reading and its increase since
|
|
249
|
+
* the last flush both go on the wire, and the increase can leave the
|
|
250
|
+
* doubles while the reading stays in them: a counter taken down to -1e308,
|
|
251
|
+
* flushed, then brought back up to 1e308 is 2e308 above its baseline.
|
|
252
|
+
*/
|
|
253
|
+
admitsNewReading(key, reading) {
|
|
254
|
+
return admitsReading(this.name, reading, this.nonFinite, this._lastFlushedValue[key] ?? 0);
|
|
255
|
+
}
|
|
239
256
|
validateLabels(labeledValue) {
|
|
240
257
|
const keys = Object.keys(labeledValue);
|
|
241
258
|
const declared = this.labels;
|
|
@@ -18,3 +18,17 @@
|
|
|
18
18
|
export declare function admitsFinite(metricName: string, value: number, state: {
|
|
19
19
|
warned: boolean;
|
|
20
20
|
}): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Whether a metric may move to `reading`, which an admitted value takes it to,
|
|
23
|
+
* when what goes on the wire is its difference from `baseline` - a counter's
|
|
24
|
+
* increase since the last flush - or the reading itself.
|
|
25
|
+
*
|
|
26
|
+
* Asked after `admitsFinite`, about the result rather than the argument: two
|
|
27
|
+
* finite numbers can add up to an Infinity (`1e308 + 1e308`), which is sent
|
|
28
|
+
* as null just the same, and which nothing added later would bring back. The
|
|
29
|
+
* step is ignored and the reading kept, under the same once-per-metric
|
|
30
|
+
* warning as a non-finite argument.
|
|
31
|
+
*/
|
|
32
|
+
export declare function admitsReading(metricName: string, reading: number, state: {
|
|
33
|
+
warned: boolean;
|
|
34
|
+
}, baseline?: number): boolean;
|