@cedarjs/telemetry 8.0.0-canary.3600 → 8.0.0-canary.3603
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 +27 -40
- package/dist/cli.d.ts +28 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +87 -0
- package/dist/environment.d.ts +3 -0
- package/dist/environment.d.ts.map +1 -0
- package/dist/environment.js +48 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/payload.d.ts +77 -0
- package/dist/payload.d.ts.map +1 -0
- package/dist/payload.js +41 -0
- package/dist/record.d.ts +45 -0
- package/dist/record.d.ts.map +1 -0
- package/dist/record.js +62 -0
- package/dist/redact.d.ts +6 -4
- package/dist/redact.d.ts.map +1 -1
- package/dist/redact.js +4 -1
- package/dist/scripts/send.d.ts +2 -0
- package/dist/scripts/send.d.ts.map +1 -0
- package/dist/scripts/send.js +2 -0
- package/dist/send.d.ts +9 -0
- package/dist/send.d.ts.map +1 -0
- package/dist/send.js +105 -0
- package/dist/telemetry.d.ts +18 -8
- package/dist/telemetry.d.ts.map +1 -1
- package/dist/telemetry.js +19 -45
- package/dist/uid.d.ts +9 -0
- package/dist/uid.d.ts.map +1 -0
- package/dist/uid.js +25 -0
- package/package.json +18 -4
- package/dist/scripts/invoke.d.ts +0 -2
- package/dist/scripts/invoke.d.ts.map +0 -1
- package/dist/scripts/invoke.js +0 -4
- package/dist/sendTelemetry.d.ts +0 -19
- package/dist/sendTelemetry.d.ts.map +0 -1
- package/dist/sendTelemetry.js +0 -160
package/README.md
CHANGED
|
@@ -1,51 +1,38 @@
|
|
|
1
1
|
# Telemetry
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
Here is an example JSON packet containing the telemetry data for running yarn rw info:
|
|
6
|
-
|
|
7
|
-
```json
|
|
8
|
-
{
|
|
9
|
-
"type": "command",
|
|
10
|
-
"command": "info", // actual CLI command that was invoked, including flags
|
|
11
|
-
"ci": false, // whether or not this is running in a CI environment
|
|
12
|
-
"duration": 2353, // how long the process took, in milliseconds
|
|
13
|
-
"NODE_ENV": "development", // the value of NODE_ENV, if set
|
|
14
|
-
"complexity": "4.2.6.3", // a measure of how complex the app is (route, service, cell and page counts)
|
|
15
|
-
"system": "8.32", // cpu core count, memory in GB
|
|
16
|
-
"sides": "web,api", // sides that are in use
|
|
17
|
-
"shell": "zsh",
|
|
18
|
-
"nodeVersion": "14.17.1",
|
|
19
|
-
"yarnVersion": "14.17.1",
|
|
20
|
-
"npmVersion": "14.17.1",
|
|
21
|
-
"vsCodeVersion": "1.58.0",
|
|
22
|
-
"cedarVersion": "0.35.1",
|
|
23
|
-
"os": "macOS",
|
|
24
|
-
"osVersion": "11.4",
|
|
25
|
-
"system": "8.32" // number of cpu cores and system memory
|
|
26
|
-
}
|
|
27
|
-
```
|
|
3
|
+
Anonymous usage telemetry for the Cedar CLI and `create-cedar-app`.
|
|
28
4
|
|
|
29
|
-
|
|
5
|
+
See the [Telemetry docs](https://cedarjs.com/docs/telemetry) for what's sent,
|
|
6
|
+
when, and how to turn it off.
|
|
30
7
|
|
|
31
|
-
|
|
32
|
-
creates variables for your user space, like `.bashrc` or `.bash_profile`:
|
|
8
|
+
## Recording
|
|
33
9
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
```
|
|
10
|
+
CLI commands and plugins record attributes and errors for the current CLI
|
|
11
|
+
run:
|
|
37
12
|
|
|
38
|
-
|
|
13
|
+
```ts
|
|
14
|
+
import {
|
|
15
|
+
recordTelemetryAttributes,
|
|
16
|
+
recordTelemetryError,
|
|
17
|
+
} from '@cedarjs/cli-helpers/telemetry'
|
|
39
18
|
|
|
40
|
-
|
|
19
|
+
recordTelemetryAttributes({ command: 'generate page', force: true })
|
|
20
|
+
recordTelemetryError(error)
|
|
21
|
+
```
|
|
41
22
|
|
|
42
|
-
|
|
23
|
+
Records are published on the `cedar:telemetry` diagnostics channel. They do
|
|
24
|
+
nothing unless the CLI is collecting telemetry, which it does unless it's
|
|
25
|
+
been disabled. Only record values that can't identify a user or their
|
|
26
|
+
project: command names, flags and fixed option values, never names, paths or
|
|
27
|
+
hosts.
|
|
43
28
|
|
|
44
|
-
|
|
45
|
-
flag to help diagnose issues.
|
|
29
|
+
## Sending
|
|
46
30
|
|
|
47
|
-
|
|
31
|
+
When the CLI run finishes, `shutdownTelemetry()` (from
|
|
32
|
+
`@cedarjs/telemetry/cli`) writes one event to `.cedar/telemetry/` and starts
|
|
33
|
+
a background process that sends it as JSON to `https://telemetry.cedarjs.com`.
|
|
48
34
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
35
|
+
## Troubleshooting
|
|
36
|
+
|
|
37
|
+
`CEDAR_VERBOSE_TELEMETRY=1` prints each event as it's sent, and any error from
|
|
38
|
+
sending it. `CEDAR_REDIRECT_TELEMETRY=<url>` sends events to another server.
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { TelemetryPayload } from './payload.js';
|
|
2
|
+
/**
|
|
3
|
+
* A telemetry event as the CLI writes it to `.cedar/telemetry/`. The
|
|
4
|
+
* background sender adds the fields that are slow to compute before sending
|
|
5
|
+
*/
|
|
6
|
+
export type PendingTelemetryEvent = Omit<TelemetryPayload, 'environment' | 'project'>;
|
|
7
|
+
export declare function getTelemetryDir(): string;
|
|
8
|
+
/**
|
|
9
|
+
* Starts collecting telemetry for this CLI run. Everything recorded with
|
|
10
|
+
* `recordTelemetryAttributes()` and `recordTelemetryError()` until
|
|
11
|
+
* `shutdownTelemetry()` (or process exit) becomes one event.
|
|
12
|
+
*/
|
|
13
|
+
export declare function startTelemetry(options: {
|
|
14
|
+
cedarVersion: string;
|
|
15
|
+
}): void;
|
|
16
|
+
/**
|
|
17
|
+
* yargs middleware that describes the running command for the telemetry
|
|
18
|
+
* event, e.g. `generate page [name] [path] --force`. See
|
|
19
|
+
* `commandForTelemetry`.
|
|
20
|
+
*/
|
|
21
|
+
export declare function telemetryMiddleware(argv: Record<string, unknown>, yargs: unknown): void;
|
|
22
|
+
/**
|
|
23
|
+
* Stops collecting, writes the event to `.cedar/telemetry/` and starts a
|
|
24
|
+
* background process that sends it, so the CLI doesn't wait on the network.
|
|
25
|
+
* Synchronous, so it can run in an `exit` handler.
|
|
26
|
+
*/
|
|
27
|
+
export declare function shutdownTelemetry(): void;
|
|
28
|
+
//# sourceMappingURL=cli.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAA;AAMpD;;;GAGG;AACH,MAAM,MAAM,qBAAqB,GAAG,IAAI,CACtC,gBAAgB,EAChB,aAAa,GAAG,SAAS,CAC1B,CAAA;AAQD,wBAAgB,eAAe,WAE9B;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE;IAAE,YAAY,EAAE,MAAM,CAAA;CAAE,QAiC/D;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CACjC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,KAAK,EAAE,OAAO,QAGf;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,SA6ChC"}
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { hideBin } from "yargs/helpers";
|
|
4
|
+
import { getPaths } from "@cedarjs/project-config";
|
|
5
|
+
import { commandForTelemetry } from "./commandForTelemetry.js";
|
|
6
|
+
import { isVerboseTelemetry } from "./payload.js";
|
|
7
|
+
import { collectTelemetry } from "./record.js";
|
|
8
|
+
import { spawnSender } from "./telemetry.js";
|
|
9
|
+
import { getTelemetryUid } from "./uid.js";
|
|
10
|
+
let collector;
|
|
11
|
+
let startTime = 0;
|
|
12
|
+
let cedarVersion = "";
|
|
13
|
+
let isShutdown = false;
|
|
14
|
+
let describedCommand;
|
|
15
|
+
function getTelemetryDir() {
|
|
16
|
+
return path.join(getPaths().generated.base, "telemetry");
|
|
17
|
+
}
|
|
18
|
+
function startTelemetry(options) {
|
|
19
|
+
if (collector) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
collector = collectTelemetry();
|
|
23
|
+
startTime = Date.now();
|
|
24
|
+
cedarVersion = options.cedarVersion;
|
|
25
|
+
const cleanArgv = hideBin(process.argv);
|
|
26
|
+
if (!cleanArgv.includes("sb") && !cleanArgv.includes("storybook")) {
|
|
27
|
+
for (const signal of ["SIGTERM", "SIGINT", "SIGHUP"]) {
|
|
28
|
+
process.on(signal, () => {
|
|
29
|
+
if (process.listenerCount(signal) === 1) {
|
|
30
|
+
process.exit();
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
} else {
|
|
35
|
+
process.on("shutdown-telemetry", () => {
|
|
36
|
+
shutdownTelemetry();
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
process.on("exit", () => {
|
|
40
|
+
shutdownTelemetry();
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
function telemetryMiddleware(argv, yargs) {
|
|
44
|
+
describedCommand = commandForTelemetry(argv, yargs, hideBin(process.argv));
|
|
45
|
+
}
|
|
46
|
+
function shutdownTelemetry() {
|
|
47
|
+
if (!collector || isShutdown) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
isShutdown = true;
|
|
51
|
+
collector.stop();
|
|
52
|
+
try {
|
|
53
|
+
const { command, ...attributes } = collector.attributes;
|
|
54
|
+
const event = {
|
|
55
|
+
type: "command",
|
|
56
|
+
uid: getTelemetryUid(),
|
|
57
|
+
timestamp: new Date(startTime).toISOString(),
|
|
58
|
+
// The command a handler records is only used when the middleware
|
|
59
|
+
// didn't run, e.g. for `--version` and `--help`
|
|
60
|
+
command: describedCommand ?? (command === void 0 ? void 0 : String(command)),
|
|
61
|
+
durationMs: Date.now() - startTime,
|
|
62
|
+
attributes,
|
|
63
|
+
error: collector.error,
|
|
64
|
+
cedarVersion
|
|
65
|
+
};
|
|
66
|
+
const telemetryDir = getTelemetryDir();
|
|
67
|
+
fs.mkdirSync(telemetryDir, { recursive: true });
|
|
68
|
+
const eventPath = path.join(
|
|
69
|
+
telemetryDir,
|
|
70
|
+
`${startTime}-${process.pid}.json`
|
|
71
|
+
);
|
|
72
|
+
const tempPath = `${eventPath}.tmp`;
|
|
73
|
+
fs.writeFileSync(tempPath, JSON.stringify(event, null, 2));
|
|
74
|
+
fs.renameSync(tempPath, eventPath);
|
|
75
|
+
spawnSender();
|
|
76
|
+
} catch (error) {
|
|
77
|
+
if (isVerboseTelemetry()) {
|
|
78
|
+
console.error("Cedar telemetry error:", error);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
export {
|
|
83
|
+
getTelemetryDir,
|
|
84
|
+
shutdownTelemetry,
|
|
85
|
+
startTelemetry,
|
|
86
|
+
telemetryMiddleware
|
|
87
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"environment.d.ts","sourceRoot":"","sources":["../src/environment.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAA;AAiBxD,wBAAsB,uBAAuB,IAAI,OAAO,CAAC,oBAAoB,CAAC,CAyC7E"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import os from "node:os";
|
|
2
|
+
import ci from "ci-info";
|
|
3
|
+
import envinfo from "envinfo";
|
|
4
|
+
async function getTelemetryEnvironment() {
|
|
5
|
+
let info = {};
|
|
6
|
+
try {
|
|
7
|
+
info = JSON.parse(
|
|
8
|
+
await envinfo.run(
|
|
9
|
+
{
|
|
10
|
+
System: ["OS", "Shell"],
|
|
11
|
+
Binaries: ["Node", "Yarn", "npm"],
|
|
12
|
+
IDEs: ["VSCode"]
|
|
13
|
+
},
|
|
14
|
+
{ json: true }
|
|
15
|
+
)
|
|
16
|
+
);
|
|
17
|
+
} catch {
|
|
18
|
+
}
|
|
19
|
+
const [osType, osVersion] = info.System?.OS?.split(" ") ?? [];
|
|
20
|
+
return {
|
|
21
|
+
osType,
|
|
22
|
+
osVersion,
|
|
23
|
+
// Only the name of the shell, never the path to it
|
|
24
|
+
shell: info.System?.Shell?.path?.split(/[\\/]/).pop(),
|
|
25
|
+
nodeVersion: info.Binaries?.Node?.version,
|
|
26
|
+
yarnVersion: info.Binaries?.Yarn?.version,
|
|
27
|
+
npmVersion: info.Binaries?.npm?.version,
|
|
28
|
+
vscodeVersion: info.IDEs?.VSCode?.version,
|
|
29
|
+
cpuCount: os.availableParallelism(),
|
|
30
|
+
memoryGb: Math.round(os.totalmem() / 1073741824),
|
|
31
|
+
nodeEnv: describeNodeEnv(process.env.NODE_ENV),
|
|
32
|
+
ci: ci.isCI,
|
|
33
|
+
cedarCi: !!process.env.CEDAR_CI,
|
|
34
|
+
devEnvironment: Object.keys(process.env).some(
|
|
35
|
+
(key) => key.startsWith("GITPOD_")
|
|
36
|
+
) ? "gitpod" : void 0
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
const KNOWN_NODE_ENVS = /* @__PURE__ */ new Set(["development", "production", "test"]);
|
|
40
|
+
function describeNodeEnv(nodeEnv) {
|
|
41
|
+
if (!nodeEnv) {
|
|
42
|
+
return void 0;
|
|
43
|
+
}
|
|
44
|
+
return KNOWN_NODE_ENVS.has(nodeEnv) ? nodeEnv : "other";
|
|
45
|
+
}
|
|
46
|
+
export {
|
|
47
|
+
getTelemetryEnvironment
|
|
48
|
+
};
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,cAAc,kBAAkB,CAAA;AAChC,cAAc,gBAAgB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { TelemetryAttributeValue } from './record.js';
|
|
2
|
+
/** Where telemetry events are sent unless `CEDAR_REDIRECT_TELEMETRY` is set */
|
|
3
|
+
export declare const DEFAULT_TELEMETRY_URL = "https://telemetry.cedarjs.com";
|
|
4
|
+
export declare function getTelemetryUrl(): string;
|
|
5
|
+
export declare function isVerboseTelemetry(): boolean;
|
|
6
|
+
/**
|
|
7
|
+
* Information about the machine and tooling a telemetry event came from.
|
|
8
|
+
* Nothing in here identifies a user.
|
|
9
|
+
*/
|
|
10
|
+
export interface TelemetryEnvironment {
|
|
11
|
+
osType: string | undefined;
|
|
12
|
+
osVersion: string | undefined;
|
|
13
|
+
shell: string | undefined;
|
|
14
|
+
nodeVersion: string | undefined;
|
|
15
|
+
yarnVersion: string | undefined;
|
|
16
|
+
npmVersion: string | undefined;
|
|
17
|
+
vscodeVersion: string | undefined;
|
|
18
|
+
cpuCount: number;
|
|
19
|
+
memoryGb: number;
|
|
20
|
+
/** `development`, `production`, `test` or `other` */
|
|
21
|
+
nodeEnv: string | undefined;
|
|
22
|
+
/** Whether this ran in a CI environment */
|
|
23
|
+
ci: boolean;
|
|
24
|
+
/** Whether this ran in Cedar's own CI */
|
|
25
|
+
cedarCi: boolean;
|
|
26
|
+
/** A known cloud development environment, like `gitpod` */
|
|
27
|
+
devEnvironment: string | undefined;
|
|
28
|
+
}
|
|
29
|
+
/** Size and shape of the Cedar project an event came from */
|
|
30
|
+
export interface TelemetryProject {
|
|
31
|
+
/** Counts of routes, prerendered routes, services, cells and pages */
|
|
32
|
+
complexity: string;
|
|
33
|
+
sides: string[];
|
|
34
|
+
/** The keys of the `[experimental]` sections in `cedar.toml` */
|
|
35
|
+
experiments: string[];
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* One telemetry event, as sent to the telemetry endpoint. `command` events
|
|
39
|
+
* come from the `cedar` CLI, one per CLI run, and `create` events come from
|
|
40
|
+
* `create-cedar-app` and `create-cedar-rsc-app`.
|
|
41
|
+
*/
|
|
42
|
+
export interface TelemetryPayload {
|
|
43
|
+
type: 'command' | 'create';
|
|
44
|
+
/**
|
|
45
|
+
* A random ID that is replaced once it's 24 hours old. It only lets events
|
|
46
|
+
* from the same project within that 24 hour window be grouped together
|
|
47
|
+
*/
|
|
48
|
+
uid: string;
|
|
49
|
+
/** When the command started, as an ISO 8601 string */
|
|
50
|
+
timestamp: string;
|
|
51
|
+
/**
|
|
52
|
+
* The command that ran, described from its definition, like
|
|
53
|
+
* `generate page [name] [path] --force`. Values the user typed are
|
|
54
|
+
* replaced by placeholders named after their argument or option
|
|
55
|
+
*/
|
|
56
|
+
command: string | undefined;
|
|
57
|
+
durationMs: number;
|
|
58
|
+
/** Flags and option values the command recorded */
|
|
59
|
+
attributes: Record<string, TelemetryAttributeValue>;
|
|
60
|
+
/** The first line of the error message, with file paths removed */
|
|
61
|
+
error: string | undefined;
|
|
62
|
+
/** The version of the package that sent the event */
|
|
63
|
+
cedarVersion: string;
|
|
64
|
+
environment: TelemetryEnvironment;
|
|
65
|
+
/** Not included for `create` events */
|
|
66
|
+
project: TelemetryProject | undefined;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* POSTs a telemetry event as JSON. Never throws: telemetry must not break
|
|
70
|
+
* whatever it's measuring.
|
|
71
|
+
*
|
|
72
|
+
* @returns Whether the endpoint accepted the event
|
|
73
|
+
*/
|
|
74
|
+
export declare function postTelemetryPayload(payload: TelemetryPayload, { timeoutMs }?: {
|
|
75
|
+
timeoutMs?: number;
|
|
76
|
+
}): Promise<boolean>;
|
|
77
|
+
//# sourceMappingURL=payload.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"payload.d.ts","sourceRoot":"","sources":["../src/payload.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAA;AAE1D,+EAA+E;AAC/E,eAAO,MAAM,qBAAqB,kCAAkC,CAAA;AAEpE,wBAAgB,eAAe,WAM9B;AAED,wBAAgB,kBAAkB,YAKjC;AAED;;;GAGG;AACH,MAAM,WAAW,oBAAoB;IACnC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAA;IAC1B,SAAS,EAAE,MAAM,GAAG,SAAS,CAAA;IAC7B,KAAK,EAAE,MAAM,GAAG,SAAS,CAAA;IACzB,WAAW,EAAE,MAAM,GAAG,SAAS,CAAA;IAC/B,WAAW,EAAE,MAAM,GAAG,SAAS,CAAA;IAC/B,UAAU,EAAE,MAAM,GAAG,SAAS,CAAA;IAC9B,aAAa,EAAE,MAAM,GAAG,SAAS,CAAA;IACjC,QAAQ,EAAE,MAAM,CAAA;IAChB,QAAQ,EAAE,MAAM,CAAA;IAChB,qDAAqD;IACrD,OAAO,EAAE,MAAM,GAAG,SAAS,CAAA;IAC3B,2CAA2C;IAC3C,EAAE,EAAE,OAAO,CAAA;IACX,yCAAyC;IACzC,OAAO,EAAE,OAAO,CAAA;IAChB,2DAA2D;IAC3D,cAAc,EAAE,MAAM,GAAG,SAAS,CAAA;CACnC;AAED,6DAA6D;AAC7D,MAAM,WAAW,gBAAgB;IAC/B,sEAAsE;IACtE,UAAU,EAAE,MAAM,CAAA;IAClB,KAAK,EAAE,MAAM,EAAE,CAAA;IACf,gEAAgE;IAChE,WAAW,EAAE,MAAM,EAAE,CAAA;CACtB;AAED;;;;GAIG;AACH,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,SAAS,GAAG,QAAQ,CAAA;IAC1B;;;OAGG;IACH,GAAG,EAAE,MAAM,CAAA;IACX,sDAAsD;IACtD,SAAS,EAAE,MAAM,CAAA;IACjB;;;;OAIG;IACH,OAAO,EAAE,MAAM,GAAG,SAAS,CAAA;IAC3B,UAAU,EAAE,MAAM,CAAA;IAClB,mDAAmD;IACnD,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,uBAAuB,CAAC,CAAA;IACnD,mEAAmE;IACnE,KAAK,EAAE,MAAM,GAAG,SAAS,CAAA;IACzB,qDAAqD;IACrD,YAAY,EAAE,MAAM,CAAA;IACpB,WAAW,EAAE,oBAAoB,CAAA;IACjC,uCAAuC;IACvC,OAAO,EAAE,gBAAgB,GAAG,SAAS,CAAA;CACtC;AAED;;;;;GAKG;AACH,wBAAsB,oBAAoB,CACxC,OAAO,EAAE,gBAAgB,EACzB,EAAE,SAAgB,EAAE,GAAE;IAAE,SAAS,CAAC,EAAE,MAAM,CAAA;CAAO,oBA+BlD"}
|
package/dist/payload.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const DEFAULT_TELEMETRY_URL = "https://telemetry.cedarjs.com";
|
|
2
|
+
function getTelemetryUrl() {
|
|
3
|
+
return process.env.CEDAR_REDIRECT_TELEMETRY || process.env.REDWOOD_REDIRECT_TELEMETRY || DEFAULT_TELEMETRY_URL;
|
|
4
|
+
}
|
|
5
|
+
function isVerboseTelemetry() {
|
|
6
|
+
return Boolean(
|
|
7
|
+
process.env.CEDAR_VERBOSE_TELEMETRY || process.env.REDWOOD_VERBOSE_TELEMETRY
|
|
8
|
+
);
|
|
9
|
+
}
|
|
10
|
+
async function postTelemetryPayload(payload, { timeoutMs = 5e3 } = {}) {
|
|
11
|
+
const verbose = isVerboseTelemetry();
|
|
12
|
+
if (verbose) {
|
|
13
|
+
console.info("Cedar telemetry payload", payload);
|
|
14
|
+
}
|
|
15
|
+
try {
|
|
16
|
+
const response = await fetch(getTelemetryUrl(), {
|
|
17
|
+
method: "POST",
|
|
18
|
+
body: JSON.stringify(payload),
|
|
19
|
+
headers: { "Content-Type": "application/json" },
|
|
20
|
+
signal: AbortSignal.timeout(timeoutMs)
|
|
21
|
+
});
|
|
22
|
+
if (verbose && !response.ok) {
|
|
23
|
+
console.error(
|
|
24
|
+
`Cedar telemetry endpoint responded with ${response.status}:`,
|
|
25
|
+
await response.text()
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
return response.ok;
|
|
29
|
+
} catch (error) {
|
|
30
|
+
if (verbose) {
|
|
31
|
+
console.error("Cedar telemetry could not be sent:", error);
|
|
32
|
+
}
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
export {
|
|
37
|
+
DEFAULT_TELEMETRY_URL,
|
|
38
|
+
getTelemetryUrl,
|
|
39
|
+
isVerboseTelemetry,
|
|
40
|
+
postTelemetryPayload
|
|
41
|
+
};
|
package/dist/record.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export { redactErrorMessage } from './redact.js';
|
|
2
|
+
/**
|
|
3
|
+
* The diagnostics channel telemetry records are published on. Channels are
|
|
4
|
+
* looked up by name across the whole process, so records published through
|
|
5
|
+
* any copy of this package reach the one collector that's subscribed.
|
|
6
|
+
*/
|
|
7
|
+
export declare const TELEMETRY_CHANNEL_NAME = "cedar:telemetry";
|
|
8
|
+
export type TelemetryAttributeValue = string | number | boolean;
|
|
9
|
+
export type TelemetryAttributes = Record<string, TelemetryAttributeValue | undefined>;
|
|
10
|
+
export type TelemetryRecord = {
|
|
11
|
+
kind: 'attributes';
|
|
12
|
+
attributes: TelemetryAttributes;
|
|
13
|
+
} | {
|
|
14
|
+
kind: 'error';
|
|
15
|
+
error: unknown;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Records attributes for the telemetry event of the current process. Does
|
|
19
|
+
* nothing when telemetry is disabled.
|
|
20
|
+
*
|
|
21
|
+
* Only record values that can't identify a user or their project: command
|
|
22
|
+
* names, flags and enum-like option values, never names, paths or hosts.
|
|
23
|
+
*
|
|
24
|
+
* @param attributes Key-value pairs to record. `undefined` values are skipped
|
|
25
|
+
*/
|
|
26
|
+
export declare function recordTelemetryAttributes(attributes: TelemetryAttributes): void;
|
|
27
|
+
/**
|
|
28
|
+
* Records an error for the telemetry event of the current process. Only a
|
|
29
|
+
* redacted version of the error message is sent, see `redactErrorMessage`.
|
|
30
|
+
* Does nothing when telemetry is disabled.
|
|
31
|
+
*/
|
|
32
|
+
export declare function recordTelemetryError(error: unknown): void;
|
|
33
|
+
export interface TelemetryCollector {
|
|
34
|
+
attributes: Record<string, TelemetryAttributeValue>;
|
|
35
|
+
error: string | undefined;
|
|
36
|
+
/** Stops collecting. Records published afterwards are ignored */
|
|
37
|
+
stop: () => void;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Subscribes to the telemetry channel and collects everything recorded on it
|
|
41
|
+
* until `stop()` is called. Later attributes overwrite earlier ones with the
|
|
42
|
+
* same key, and the last recorded error wins.
|
|
43
|
+
*/
|
|
44
|
+
export declare function collectTelemetry(): TelemetryCollector;
|
|
45
|
+
//# sourceMappingURL=record.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"record.d.ts","sourceRoot":"","sources":["../src/record.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAEhD;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,oBAAoB,CAAA;AAEvD,MAAM,MAAM,uBAAuB,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAAA;AAE/D,MAAM,MAAM,mBAAmB,GAAG,MAAM,CACtC,MAAM,EACN,uBAAuB,GAAG,SAAS,CACpC,CAAA;AAED,MAAM,MAAM,eAAe,GACvB;IAAE,IAAI,EAAE,YAAY,CAAC;IAAC,UAAU,EAAE,mBAAmB,CAAA;CAAE,GACvD;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,OAAO,CAAA;CAAE,CAAA;AAIrC;;;;;;;;GAQG;AACH,wBAAgB,yBAAyB,CAAC,UAAU,EAAE,mBAAmB,QAOxE;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,OAAO,QAIlD;AAED,MAAM,WAAW,kBAAkB;IACjC,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,uBAAuB,CAAC,CAAA;IACnD,KAAK,EAAE,MAAM,GAAG,SAAS,CAAA;IACzB,iEAAiE;IACjE,IAAI,EAAE,MAAM,IAAI,CAAA;CACjB;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,IAAI,kBAAkB,CAsCrD"}
|
package/dist/record.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import diagnosticsChannel from "node:diagnostics_channel";
|
|
2
|
+
import { redactErrorMessage } from "./redact.js";
|
|
3
|
+
import { redactErrorMessage as redactErrorMessage2 } from "./redact.js";
|
|
4
|
+
const TELEMETRY_CHANNEL_NAME = "cedar:telemetry";
|
|
5
|
+
const channel = diagnosticsChannel.channel(TELEMETRY_CHANNEL_NAME);
|
|
6
|
+
function recordTelemetryAttributes(attributes) {
|
|
7
|
+
if (channel.hasSubscribers) {
|
|
8
|
+
channel.publish({
|
|
9
|
+
kind: "attributes",
|
|
10
|
+
attributes
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function recordTelemetryError(error) {
|
|
15
|
+
if (channel.hasSubscribers) {
|
|
16
|
+
channel.publish({ kind: "error", error });
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function collectTelemetry() {
|
|
20
|
+
const collector = {
|
|
21
|
+
attributes: {},
|
|
22
|
+
error: void 0,
|
|
23
|
+
stop: () => {
|
|
24
|
+
diagnosticsChannel.unsubscribe(TELEMETRY_CHANNEL_NAME, onRecord);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
function onRecord(message) {
|
|
28
|
+
try {
|
|
29
|
+
collect(message);
|
|
30
|
+
} catch {
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function collect(message) {
|
|
34
|
+
if (!isTelemetryRecord(message)) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
if (message.kind === "attributes") {
|
|
38
|
+
for (const [key, value] of Object.entries(message.attributes)) {
|
|
39
|
+
if (isAttributeValue(value)) {
|
|
40
|
+
collector.attributes[key] = value;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
} else {
|
|
44
|
+
collector.error = redactErrorMessage(message.error);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
diagnosticsChannel.subscribe(TELEMETRY_CHANNEL_NAME, onRecord);
|
|
48
|
+
return collector;
|
|
49
|
+
}
|
|
50
|
+
function isTelemetryRecord(message) {
|
|
51
|
+
return typeof message === "object" && message !== null && "kind" in message && (message.kind === "attributes" || message.kind === "error");
|
|
52
|
+
}
|
|
53
|
+
function isAttributeValue(value) {
|
|
54
|
+
return typeof value === "string" || typeof value === "boolean" || typeof value === "number" && Number.isFinite(value);
|
|
55
|
+
}
|
|
56
|
+
export {
|
|
57
|
+
TELEMETRY_CHANNEL_NAME,
|
|
58
|
+
collectTelemetry,
|
|
59
|
+
recordTelemetryAttributes,
|
|
60
|
+
recordTelemetryError,
|
|
61
|
+
redactErrorMessage2 as redactErrorMessage
|
|
62
|
+
};
|
package/dist/redact.d.ts
CHANGED
|
@@ -7,7 +7,10 @@
|
|
|
7
7
|
* project or their infrastructure is replaced: anything containing `/`, `\`,
|
|
8
8
|
* `@` or `:`, and IPv4 addresses. Replacements are labelled `[url]`, `[host]`,
|
|
9
9
|
* `[path]` or `[redacted]`. Quoted paths are replaced as a whole, since they
|
|
10
|
-
* can contain spaces, and so are hostnames in DNS errors.
|
|
10
|
+
* can contain spaces, and so are hostnames in DNS errors. The command line in
|
|
11
|
+
* a failed command's error, e.g. `Command failed with exit code 1: git commit
|
|
12
|
+
* -m 'Ada's app'`, is replaced by `[command]`, since its arguments can be
|
|
13
|
+
* anything the user typed.
|
|
11
14
|
*
|
|
12
15
|
* Words that describe the failure without identifying anyone are kept:
|
|
13
16
|
* scoped package names (`@cedarjs/vite`), known specifiers (`node:fs`),
|
|
@@ -15,9 +18,8 @@
|
|
|
15
18
|
* What is left is the shape of the error, e.g.
|
|
16
19
|
* `ENOENT: no such file or directory, open '[path]'`.
|
|
17
20
|
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
* own copy instead of importing it from there.
|
|
21
|
+
* This module has no imports so it can be bundled into create-cedar-app
|
|
22
|
+
* without pulling in anything else.
|
|
21
23
|
*/
|
|
22
24
|
export declare function redactErrorMessage(error: unknown): string;
|
|
23
25
|
//# sourceMappingURL=redact.d.ts.map
|
package/dist/redact.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redact.d.ts","sourceRoot":"","sources":["../src/redact.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"redact.d.ts","sourceRoot":"","sources":["../src/redact.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CA8BzD"}
|
package/dist/redact.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
function redactErrorMessage(error) {
|
|
2
|
-
const message = error instanceof Error ? error.message : typeof error === "string" ? error : String(error);
|
|
2
|
+
const message = error instanceof Error ? String(error.message) : typeof error === "string" ? error : String(error);
|
|
3
3
|
return (message.split("\n")[0] || "Unknown error").replace(
|
|
4
|
+
/^(Command (?:failed|was killed|timed out)\b[^:]*): .*$/,
|
|
5
|
+
"$1: [command]"
|
|
6
|
+
).replace(
|
|
4
7
|
/(['"`])([^'"`\\/\n]*[\\/][^'"`\n]*)\1/g,
|
|
5
8
|
(_match, quote, inner) => `${quote}${isSafeWord(inner) ? inner : placeholderFor(inner)}${quote}`
|
|
6
9
|
).replace(/\b(ENOTFOUND|EAI_AGAIN)\s+[^\s'"`]+/g, "$1 [host]").replace(/\S+/g, redactWord);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"send.d.ts","sourceRoot":"","sources":["../../src/scripts/send.ts"],"names":[],"mappings":""}
|
package/dist/send.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { TelemetryProject } from './payload.js';
|
|
2
|
+
export declare function getTelemetryProject(): TelemetryProject | undefined;
|
|
3
|
+
/**
|
|
4
|
+
* Sends every unsent event in `.cedar/telemetry/`. Sent files get a `_`
|
|
5
|
+
* prefix, and files that fail to send are retried next time. Only the
|
|
6
|
+
* newest event files are kept, so users can see what was sent.
|
|
7
|
+
*/
|
|
8
|
+
export declare function sendPendingTelemetry(): Promise<void>;
|
|
9
|
+
//# sourceMappingURL=send.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"send.d.ts","sourceRoot":"","sources":["../src/send.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAA;AAapD,wBAAgB,mBAAmB,IAAI,gBAAgB,GAAG,SAAS,CA2BlE;AAwBD;;;;GAIG;AACH,wBAAsB,oBAAoB,kBAyEzC"}
|
package/dist/send.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { getRawConfig } from "@cedarjs/project-config";
|
|
4
|
+
import { RWProject } from "@cedarjs/structure/dist/model/RWProject";
|
|
5
|
+
import { getTelemetryDir } from "./cli.js";
|
|
6
|
+
import { getTelemetryEnvironment } from "./environment.js";
|
|
7
|
+
import { postTelemetryPayload } from "./payload.js";
|
|
8
|
+
const KEPT_EVENT_FILES = 8;
|
|
9
|
+
const STALE_CLAIM_MS = 10 * 60 * 1e3;
|
|
10
|
+
const CLAIM_SUFFIX = ".sending";
|
|
11
|
+
function getTelemetryProject() {
|
|
12
|
+
try {
|
|
13
|
+
const project = new RWProject();
|
|
14
|
+
const routes = project.getRouter().routes;
|
|
15
|
+
const prerenderedRoutes = routes.filter((route) => route.hasPrerender);
|
|
16
|
+
const experimental = getRawConfig()["experimental"];
|
|
17
|
+
const experiments = typeof experimental === "object" && experimental !== null ? Object.keys(experimental) : [];
|
|
18
|
+
return {
|
|
19
|
+
complexity: [
|
|
20
|
+
routes.length,
|
|
21
|
+
prerenderedRoutes.length,
|
|
22
|
+
project.services.length,
|
|
23
|
+
project.cells.length,
|
|
24
|
+
project.pages.length
|
|
25
|
+
].join("."),
|
|
26
|
+
sides: project.sides,
|
|
27
|
+
experiments
|
|
28
|
+
};
|
|
29
|
+
} catch {
|
|
30
|
+
return void 0;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function isPendingEvent(value) {
|
|
34
|
+
return typeof value === "object" && value !== null && "type" in value && "uid" in value && typeof value.uid === "string" && "timestamp" in value && "durationMs" in value && "attributes" in value && "cedarVersion" in value;
|
|
35
|
+
}
|
|
36
|
+
function fileTime(file) {
|
|
37
|
+
return parseInt(file.replace(/^_/, ""), 10) || 0;
|
|
38
|
+
}
|
|
39
|
+
async function sendPendingTelemetry() {
|
|
40
|
+
const telemetryDir = getTelemetryDir();
|
|
41
|
+
if (!fs.existsSync(telemetryDir)) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
releaseStaleClaims(telemetryDir);
|
|
45
|
+
const pendingFiles = fs.readdirSync(telemetryDir).filter((file) => file.endsWith(".json") && !file.startsWith("_"));
|
|
46
|
+
if (pendingFiles.length > 0) {
|
|
47
|
+
const environment = await getTelemetryEnvironment();
|
|
48
|
+
const project = getTelemetryProject();
|
|
49
|
+
for (const file of pendingFiles) {
|
|
50
|
+
const filePath = path.join(telemetryDir, file);
|
|
51
|
+
const claimPath = filePath + CLAIM_SUFFIX;
|
|
52
|
+
try {
|
|
53
|
+
fs.renameSync(filePath, claimPath);
|
|
54
|
+
const now = /* @__PURE__ */ new Date();
|
|
55
|
+
fs.utimesSync(claimPath, now, now);
|
|
56
|
+
} catch {
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
let event;
|
|
60
|
+
try {
|
|
61
|
+
event = JSON.parse(fs.readFileSync(claimPath, "utf-8"));
|
|
62
|
+
} catch {
|
|
63
|
+
event = void 0;
|
|
64
|
+
}
|
|
65
|
+
if (!isPendingEvent(event)) {
|
|
66
|
+
fs.rmSync(claimPath, { force: true });
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
const payload = { ...event, environment, project };
|
|
70
|
+
const sent = await postTelemetryPayload(payload);
|
|
71
|
+
if (sent) {
|
|
72
|
+
try {
|
|
73
|
+
fs.writeFileSync(claimPath, JSON.stringify(payload, null, 2));
|
|
74
|
+
fs.renameSync(claimPath, path.join(telemetryDir, `_${file}`));
|
|
75
|
+
} catch {
|
|
76
|
+
fs.rmSync(claimPath, { force: true });
|
|
77
|
+
}
|
|
78
|
+
} else {
|
|
79
|
+
fs.renameSync(claimPath, filePath);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
const files = fs.readdirSync(telemetryDir).filter((file) => file.endsWith(".json")).sort((a, b) => fileTime(b) - fileTime(a));
|
|
84
|
+
for (const file of files.slice(KEPT_EVENT_FILES)) {
|
|
85
|
+
fs.rmSync(path.join(telemetryDir, file), { force: true });
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function releaseStaleClaims(telemetryDir) {
|
|
89
|
+
for (const file of fs.readdirSync(telemetryDir)) {
|
|
90
|
+
if (!file.endsWith(CLAIM_SUFFIX)) {
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
const claimPath = path.join(telemetryDir, file);
|
|
94
|
+
try {
|
|
95
|
+
if (fs.statSync(claimPath).mtimeMs < Date.now() - STALE_CLAIM_MS) {
|
|
96
|
+
fs.renameSync(claimPath, claimPath.slice(0, -CLAIM_SUFFIX.length));
|
|
97
|
+
}
|
|
98
|
+
} catch {
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
export {
|
|
103
|
+
getTelemetryProject,
|
|
104
|
+
sendPendingTelemetry
|
|
105
|
+
};
|
package/dist/telemetry.d.ts
CHANGED
|
@@ -12,16 +12,26 @@
|
|
|
12
12
|
* backslashes are doubled so they can't escape the closing quote.
|
|
13
13
|
*/
|
|
14
14
|
export declare function quoteForWindowsShell(arg: string): string;
|
|
15
|
-
export declare const timedTelemetry: (argv: string[], options: Record<string, unknown>, func: (...args: any[]) => any) => Promise<any>;
|
|
16
15
|
/**
|
|
17
|
-
*
|
|
18
|
-
*
|
|
16
|
+
* Starts the background process that sends the telemetry events in
|
|
17
|
+
* `.cedar/telemetry/`. It's detached, so the CLI can exit without waiting on
|
|
18
|
+
* the network.
|
|
19
19
|
*/
|
|
20
|
-
export declare
|
|
20
|
+
export declare function spawnSender(): void;
|
|
21
21
|
/**
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
22
|
+
* Runs `func` and records how long it took as the `<type>Duration` attribute
|
|
23
|
+
* (in milliseconds) of the current telemetry event
|
|
24
|
+
*
|
|
25
|
+
* @param _argv Unused. Only the command the CLI handler records is sent
|
|
26
|
+
*/
|
|
27
|
+
export declare const timedTelemetry: <T>(_argv: string[], options: {
|
|
28
|
+
type: string;
|
|
29
|
+
}, func: () => T | Promise<T>) => Promise<T>;
|
|
30
|
+
/**
|
|
31
|
+
* Records an error for the current telemetry event. Same as
|
|
32
|
+
* `recordTelemetryError()`
|
|
33
|
+
*
|
|
34
|
+
* @param _argv Unused. Only the command the CLI handler records is sent
|
|
25
35
|
*/
|
|
26
|
-
export declare const
|
|
36
|
+
export declare const errorTelemetry: (_argv: string[], error: unknown) => Promise<void>;
|
|
27
37
|
//# sourceMappingURL=telemetry.d.ts.map
|
package/dist/telemetry.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"telemetry.d.ts","sourceRoot":"","sources":["../src/telemetry.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"telemetry.d.ts","sourceRoot":"","sources":["../src/telemetry.ts"],"names":[],"mappings":"AAQA;;;;;;;;;;;;GAYG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,UAuB/C;AAED;;;;GAIG;AACH,wBAAgB,WAAW,SAsC1B;AAED;;;;;GAKG;AACH,eAAO,MAAM,cAAc,GAAU,CAAC,EACpC,OAAO,MAAM,EAAE,EACf,SAAS;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,EACzB,MAAM,MAAM,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,KACzB,OAAO,CAAC,CAAC,CASX,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,cAAc,GAAU,OAAO,MAAM,EAAE,EAAE,OAAO,OAAO,kBAEnE,CAAA"}
|
package/dist/telemetry.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { spawn } from "child_process";
|
|
2
2
|
import os from "os";
|
|
3
3
|
import path from "path";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import { redactErrorMessage } from "./redact.js";
|
|
4
|
+
import { isVerboseTelemetry } from "./payload.js";
|
|
5
|
+
import { recordTelemetryAttributes, recordTelemetryError } from "./record.js";
|
|
7
6
|
function quoteForWindowsShell(arg) {
|
|
8
7
|
let quoted = '"';
|
|
9
8
|
let backslashes = 0;
|
|
@@ -21,69 +20,44 @@ function quoteForWindowsShell(arg) {
|
|
|
21
20
|
}
|
|
22
21
|
return quoted + "\\".repeat(backslashes * 2) + '"';
|
|
23
22
|
}
|
|
24
|
-
|
|
23
|
+
function spawnSender() {
|
|
25
24
|
const isWindows = os.type() === "Windows_NT";
|
|
25
|
+
const verbose = isVerboseTelemetry();
|
|
26
|
+
const stdio = verbose ? ["ignore", "inherit", "inherit"] : "ignore";
|
|
26
27
|
const spawnOptions = isWindows ? {
|
|
27
|
-
stdio
|
|
28
|
+
stdio,
|
|
28
29
|
// The following options run the process in the background without a console window, even though they don't look like they would.
|
|
29
30
|
// See https://github.com/nodejs/node/issues/21825#issuecomment-503766781 for information
|
|
30
31
|
detached: false,
|
|
31
32
|
windowsHide: false,
|
|
32
33
|
shell: true
|
|
33
34
|
} : {
|
|
34
|
-
stdio
|
|
35
|
-
detached:
|
|
35
|
+
stdio,
|
|
36
|
+
detached: !verbose,
|
|
36
37
|
windowsHide: true
|
|
37
38
|
};
|
|
38
|
-
const scriptArgs = [
|
|
39
|
-
path.join(import.meta.dirname, "scripts", "invoke.js"),
|
|
40
|
-
...args
|
|
41
|
-
];
|
|
39
|
+
const scriptArgs = [path.join(import.meta.dirname, "scripts", "send.js")];
|
|
42
40
|
if (isWindows) {
|
|
43
41
|
const command = [process.execPath, ...scriptArgs].map(quoteForWindowsShell).join(" ");
|
|
44
42
|
spawn(command, [], spawnOptions).unref();
|
|
45
43
|
} else {
|
|
46
44
|
spawn(process.execPath, scriptArgs, spawnOptions).unref();
|
|
47
45
|
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
const start = /* @__PURE__ */ new Date();
|
|
56
|
-
const result = await func.call(void 0);
|
|
57
|
-
const duration = (/* @__PURE__ */ new Date()).getTime() - start.getTime();
|
|
58
|
-
spawnProcess(
|
|
59
|
-
...commandArgs(argv),
|
|
60
|
-
"--duration",
|
|
61
|
-
duration.toString(),
|
|
62
|
-
"--type",
|
|
63
|
-
JSON.stringify(options.type)
|
|
64
|
-
);
|
|
46
|
+
}
|
|
47
|
+
const timedTelemetry = async (_argv, options, func) => {
|
|
48
|
+
const start = Date.now();
|
|
49
|
+
const result = await func();
|
|
50
|
+
recordTelemetryAttributes({
|
|
51
|
+
[`${options.type}Duration`]: Date.now() - start
|
|
52
|
+
});
|
|
65
53
|
return result;
|
|
66
54
|
};
|
|
67
|
-
const errorTelemetry = async (
|
|
68
|
-
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
spawnProcess(
|
|
72
|
-
...commandArgs(argv),
|
|
73
|
-
"--error",
|
|
74
|
-
JSON.stringify(redactErrorMessage(error))
|
|
75
|
-
);
|
|
76
|
-
};
|
|
77
|
-
const telemetryMiddleware = async (argv, yargs) => {
|
|
78
|
-
if (process.env.CEDAR_DISABLE_TELEMETRY || process.env.REDWOOD_DISABLE_TELEMETRY) {
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
currentCommand = commandForTelemetry(argv, yargs, hideBin(process.argv));
|
|
82
|
-
spawnProcess(...commandArgs(process.argv));
|
|
55
|
+
const errorTelemetry = async (_argv, error) => {
|
|
56
|
+
recordTelemetryError(error);
|
|
83
57
|
};
|
|
84
58
|
export {
|
|
85
59
|
errorTelemetry,
|
|
86
60
|
quoteForWindowsShell,
|
|
87
|
-
|
|
61
|
+
spawnSender,
|
|
88
62
|
timedTelemetry
|
|
89
63
|
};
|
package/dist/uid.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The project's anonymous telemetry ID, stored in `.cedar/telemetry.txt`.
|
|
3
|
+
* It's replaced with a new random ID once it's more than 24 hours old, so
|
|
4
|
+
* events can only be linked within that 24 hour window. The CLI stores it in
|
|
5
|
+
* each event when the event is created, so an event that is sent later keeps
|
|
6
|
+
* the ID it was created with.
|
|
7
|
+
*/
|
|
8
|
+
export declare function getTelemetryUid(): string;
|
|
9
|
+
//# sourceMappingURL=uid.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"uid.d.ts","sourceRoot":"","sources":["../src/uid.ts"],"names":[],"mappings":"AASA;;;;;;GAMG;AACH,wBAAgB,eAAe,WAwB9B"}
|
package/dist/uid.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { v4 as uuidv4, validate as validateUUID } from "uuid";
|
|
4
|
+
import { getPaths } from "@cedarjs/project-config";
|
|
5
|
+
const ONE_DAY_MS = 24 * 60 * 60 * 1e3;
|
|
6
|
+
function getTelemetryUid() {
|
|
7
|
+
const uidFile = path.join(getPaths().generated.base, "telemetry.txt");
|
|
8
|
+
try {
|
|
9
|
+
if (fs.existsSync(uidFile) && fs.statSync(uidFile).mtimeMs >= Date.now() - ONE_DAY_MS) {
|
|
10
|
+
const storedUid = fs.readFileSync(uidFile, "utf-8").trim();
|
|
11
|
+
if (validateUUID(storedUid)) {
|
|
12
|
+
return storedUid;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
const uid = uuidv4();
|
|
16
|
+
fs.mkdirSync(path.dirname(uidFile), { recursive: true });
|
|
17
|
+
fs.writeFileSync(uidFile, uid);
|
|
18
|
+
return uid;
|
|
19
|
+
} catch {
|
|
20
|
+
return uuidv4();
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export {
|
|
24
|
+
getTelemetryUid
|
|
25
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cedarjs/telemetry",
|
|
3
|
-
"version": "8.0.0-canary.
|
|
3
|
+
"version": "8.0.0-canary.3603",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/cedarjs/cedar.git",
|
|
@@ -8,6 +8,21 @@
|
|
|
8
8
|
},
|
|
9
9
|
"license": "MIT",
|
|
10
10
|
"type": "module",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"default": "./dist/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./cli": {
|
|
17
|
+
"types": "./dist/cli.d.ts",
|
|
18
|
+
"default": "./dist/cli.js"
|
|
19
|
+
},
|
|
20
|
+
"./record": {
|
|
21
|
+
"types": "./dist/record.d.ts",
|
|
22
|
+
"default": "./dist/record.js"
|
|
23
|
+
},
|
|
24
|
+
"./package.json": "./package.json"
|
|
25
|
+
},
|
|
11
26
|
"main": "./dist/index.js",
|
|
12
27
|
"types": "./dist/index.d.ts",
|
|
13
28
|
"files": [
|
|
@@ -22,9 +37,8 @@
|
|
|
22
37
|
"test:watch": "vitest watch"
|
|
23
38
|
},
|
|
24
39
|
"dependencies": {
|
|
25
|
-
"@cedarjs/project-config": "8.0.0-canary.
|
|
26
|
-
"@cedarjs/structure": "8.0.0-canary.
|
|
27
|
-
"@whatwg-node/fetch": "0.12.1",
|
|
40
|
+
"@cedarjs/project-config": "8.0.0-canary.3603",
|
|
41
|
+
"@cedarjs/structure": "8.0.0-canary.3603",
|
|
28
42
|
"ci-info": "4.4.0",
|
|
29
43
|
"envinfo": "7.21.0",
|
|
30
44
|
"uuid": "11.1.1",
|
package/dist/scripts/invoke.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"invoke.d.ts","sourceRoot":"","sources":["../../src/scripts/invoke.ts"],"names":[],"mappings":""}
|
package/dist/scripts/invoke.js
DELETED
package/dist/sendTelemetry.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Turns `process.argv` into the `command` string sent with telemetry, for
|
|
3
|
-
* events sent before the CLI has described the running command (see
|
|
4
|
-
* `commandForTelemetry`).
|
|
5
|
-
*
|
|
6
|
-
* Only the command and the names of the options are kept, e.g.
|
|
7
|
-
* `generate --force --typescript`. Option values, positional arguments and
|
|
8
|
-
* everything after `--` can contain names, paths, hosts and other values
|
|
9
|
-
* that identify a user or their project, so they are left out.
|
|
10
|
-
*
|
|
11
|
-
* The command is the first argument after the script path. It is only kept
|
|
12
|
-
* if it looks like a command name, like `build` or `data-migrate`.
|
|
13
|
-
*
|
|
14
|
-
* `exec` passes its options on to the user's script, so for `exec` only the
|
|
15
|
-
* command is kept.
|
|
16
|
-
*/
|
|
17
|
-
export declare const sanitizeArgv: (argv: string[]) => string;
|
|
18
|
-
export declare const sendTelemetry: () => Promise<void>;
|
|
19
|
-
//# sourceMappingURL=sendTelemetry.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"sendTelemetry.d.ts","sourceRoot":"","sources":["../src/sendTelemetry.ts"],"names":[],"mappings":"AAsEA;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,YAAY,GAAI,MAAM,MAAM,EAAE,WAsB1C,CAAA;AAgHD,eAAO,MAAM,aAAa,qBAsCzB,CAAA"}
|
package/dist/sendTelemetry.js
DELETED
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
import fs from "node:fs";
|
|
2
|
-
import os from "os";
|
|
3
|
-
import path from "path";
|
|
4
|
-
import { fetch } from "@whatwg-node/fetch";
|
|
5
|
-
import ci from "ci-info";
|
|
6
|
-
import envinfo from "envinfo";
|
|
7
|
-
import { v4 as uuidv4 } from "uuid";
|
|
8
|
-
import yargsParser from "yargs/yargs";
|
|
9
|
-
import { getRawConfig, getPaths } from "@cedarjs/project-config";
|
|
10
|
-
import { RWProject } from "@cedarjs/structure/dist/model/RWProject";
|
|
11
|
-
import { redactErrorMessage } from "./redact.js";
|
|
12
|
-
const getInfo = async (presets = {}) => {
|
|
13
|
-
const info = JSON.parse(
|
|
14
|
-
await envinfo.run(
|
|
15
|
-
{
|
|
16
|
-
System: ["OS", "Shell"],
|
|
17
|
-
Binaries: ["Node", "Yarn", "npm"],
|
|
18
|
-
npmPackages: "@cedarjs/*",
|
|
19
|
-
IDEs: ["VSCode"]
|
|
20
|
-
},
|
|
21
|
-
{ json: true }
|
|
22
|
-
)
|
|
23
|
-
);
|
|
24
|
-
const shell = info.System?.Shell;
|
|
25
|
-
if (shell?.path?.match("/")) {
|
|
26
|
-
info.System.Shell.name = info.System.Shell.path.split("/").pop();
|
|
27
|
-
} else if (shell?.path.match("\\")) {
|
|
28
|
-
info.System.Shell.name = info.System.Shell.path.split("\\").pop();
|
|
29
|
-
}
|
|
30
|
-
const cpuCount = os.availableParallelism();
|
|
31
|
-
const memoryGb = Math.round(os.totalmem() / 1073741824);
|
|
32
|
-
const experiments = Object.keys(getRawConfig()["experimental"] || {});
|
|
33
|
-
return {
|
|
34
|
-
os: info.System?.OS?.split(" ")[0],
|
|
35
|
-
osVersion: info.System?.OS?.split(" ")[1],
|
|
36
|
-
shell: info.System?.Shell?.name,
|
|
37
|
-
nodeVersion: info.Binaries?.Node?.version,
|
|
38
|
-
yarnVersion: info.Binaries?.Yarn?.version,
|
|
39
|
-
npmVersion: info.Binaries?.npm?.version,
|
|
40
|
-
vsCodeVersion: info.IDEs?.VSCode?.version,
|
|
41
|
-
cedarVersion: presets.cedarVersion || info.npmPackages?.["@cedarjs/core"]?.installed,
|
|
42
|
-
system: `${cpuCount}.${memoryGb}`,
|
|
43
|
-
experiments
|
|
44
|
-
};
|
|
45
|
-
};
|
|
46
|
-
const LONG_OPTION_NAME = /^--[a-zA-Z][\w-]*/;
|
|
47
|
-
const SHORT_OPTION_NAME = /^-[a-zA-Z]/;
|
|
48
|
-
const COMMAND_NAME = /^[a-z][\w:-]*$/i;
|
|
49
|
-
const sanitizeArgv = (argv) => {
|
|
50
|
-
const args = argv.slice(2);
|
|
51
|
-
const passthroughIndex = args.indexOf("--");
|
|
52
|
-
const ownArgs = passthroughIndex === -1 ? args : args.slice(0, passthroughIndex);
|
|
53
|
-
if (ownArgs[0] === "exec") {
|
|
54
|
-
return "exec";
|
|
55
|
-
}
|
|
56
|
-
return ownArgs.flatMap((arg, index) => {
|
|
57
|
-
const optionName = arg.match(LONG_OPTION_NAME)?.[0] ?? arg.match(SHORT_OPTION_NAME)?.[0];
|
|
58
|
-
if (optionName) {
|
|
59
|
-
return [optionName];
|
|
60
|
-
}
|
|
61
|
-
return index === 0 && COMMAND_NAME.test(arg) ? [arg] : [];
|
|
62
|
-
}).join(" ");
|
|
63
|
-
};
|
|
64
|
-
const buildPayload = async () => {
|
|
65
|
-
let payload = {};
|
|
66
|
-
const processArgv = [...process.argv];
|
|
67
|
-
if (os.type() === "Windows_NT") {
|
|
68
|
-
const argvIndex = processArgv.findIndex((arg) => arg === "--argv") + 1;
|
|
69
|
-
let argvFormatted = argvIndex !== 0 ? processArgv[argvIndex] : null;
|
|
70
|
-
if (argvFormatted) {
|
|
71
|
-
argvFormatted = "[" + argvFormatted.substring(1, argvFormatted.length - 1).split(",").map((arg) => {
|
|
72
|
-
return arg.startsWith('"') || arg.startsWith("'") ? arg : `"${arg}"`;
|
|
73
|
-
}).join(",") + "]";
|
|
74
|
-
processArgv[argvIndex] = argvFormatted;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
const rawArgv = yargsParser(processArgv.slice(2)).parseSync();
|
|
78
|
-
const argvFlag = typeof rawArgv.argv === "string" ? rawArgv.argv : void 0;
|
|
79
|
-
const typeFlag = typeof rawArgv.type === "string" ? rawArgv.type : void 0;
|
|
80
|
-
const durationFlag = typeof rawArgv.duration === "string" ? rawArgv.duration : void 0;
|
|
81
|
-
const rwVersionFlag = typeof rawArgv.rwVersion === "string" ? rawArgv.rwVersion : void 0;
|
|
82
|
-
const errorFlag = typeof rawArgv.error === "string" ? rawArgv.error : void 0;
|
|
83
|
-
const commandFlag = typeof rawArgv.command === "string" ? rawArgv.command : void 0;
|
|
84
|
-
const command = commandFlag ? String(JSON.parse(commandFlag)) : argvFlag ? sanitizeArgv(JSON.parse(argvFlag)) : "";
|
|
85
|
-
payload = {
|
|
86
|
-
type: typeFlag || "command",
|
|
87
|
-
command,
|
|
88
|
-
duration: durationFlag ? parseInt(durationFlag) : null,
|
|
89
|
-
uid: uniqueId(),
|
|
90
|
-
ci: ci.isCI,
|
|
91
|
-
cedarCi: !!process.env.CEDAR_CI,
|
|
92
|
-
NODE_ENV: process.env.NODE_ENV || null,
|
|
93
|
-
...await getInfo({ cedarVersion: rwVersionFlag, command })
|
|
94
|
-
};
|
|
95
|
-
if (errorFlag) {
|
|
96
|
-
payload.type = "error";
|
|
97
|
-
payload.error = redactErrorMessage(errorFlag);
|
|
98
|
-
}
|
|
99
|
-
const project = new RWProject();
|
|
100
|
-
const routes = project.getRouter().routes;
|
|
101
|
-
const prerenderedRoutes = routes.filter((route) => route.hasPrerender);
|
|
102
|
-
payload = {
|
|
103
|
-
...payload,
|
|
104
|
-
complexity: `${routes.length}.${prerenderedRoutes.length}.${project.services.length}.${project.cells.length}.${project.pages.length}`,
|
|
105
|
-
sides: project.sides.join(",")
|
|
106
|
-
};
|
|
107
|
-
return payload;
|
|
108
|
-
};
|
|
109
|
-
function uniqueId() {
|
|
110
|
-
const telemetryCachePath = path.join(
|
|
111
|
-
getPaths().generated.base,
|
|
112
|
-
"telemetry.txt"
|
|
113
|
-
);
|
|
114
|
-
const now = Date.now();
|
|
115
|
-
const expires = now - 24 * 60 * 60 * 1e3;
|
|
116
|
-
let uuid;
|
|
117
|
-
if (!fs.existsSync(telemetryCachePath) || fs.statSync(telemetryCachePath).mtimeMs < expires) {
|
|
118
|
-
uuid = uuidv4();
|
|
119
|
-
try {
|
|
120
|
-
if (!fs.existsSync(path.dirname(telemetryCachePath))) {
|
|
121
|
-
fs.mkdirSync(path.dirname(telemetryCachePath), { recursive: true });
|
|
122
|
-
}
|
|
123
|
-
fs.writeFileSync(telemetryCachePath, uuid);
|
|
124
|
-
} catch {
|
|
125
|
-
console.error("\nCould not create telemetry.txt file\n");
|
|
126
|
-
}
|
|
127
|
-
} else {
|
|
128
|
-
uuid = fs.readFileSync(telemetryCachePath).toString();
|
|
129
|
-
}
|
|
130
|
-
return uuid;
|
|
131
|
-
}
|
|
132
|
-
const sendTelemetry = async () => {
|
|
133
|
-
const telemetryUrl = process.env.CEDAR_REDIRECT_TELEMETRY || process.env.REDWOOD_REDIRECT_TELEMETRY || "https://telemetry.redwoodjs.com/api/v1/telemetry";
|
|
134
|
-
const verboseTelemetry = process.env.CEDAR_VERBOSE_TELEMETRY || process.env.REDWOOD_VERBOSE_TELEMETRY;
|
|
135
|
-
try {
|
|
136
|
-
const payload = await buildPayload();
|
|
137
|
-
if (verboseTelemetry) {
|
|
138
|
-
console.info("Cedar Telemetry Payload", payload);
|
|
139
|
-
}
|
|
140
|
-
const response = await fetch(telemetryUrl, {
|
|
141
|
-
method: "post",
|
|
142
|
-
body: JSON.stringify(payload),
|
|
143
|
-
headers: { "Content-Type": "application/json" }
|
|
144
|
-
});
|
|
145
|
-
if (verboseTelemetry) {
|
|
146
|
-
console.info("Cedar Telemetry Response:", response);
|
|
147
|
-
}
|
|
148
|
-
if (verboseTelemetry && response.status !== 200) {
|
|
149
|
-
console.error("Error from telemetry insert:", await response.text());
|
|
150
|
-
}
|
|
151
|
-
} catch (e) {
|
|
152
|
-
if (verboseTelemetry) {
|
|
153
|
-
console.error("Uncaught error in telemetry:", e);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
};
|
|
157
|
-
export {
|
|
158
|
-
sanitizeArgv,
|
|
159
|
-
sendTelemetry
|
|
160
|
-
};
|