wrangler 2.0.18 → 2.0.19
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/bin/wrangler.js +36 -5
- package/package.json +1 -2
- package/src/__tests__/configuration.test.ts +11 -7
- package/src/__tests__/helpers/run-in-tmp.ts +2 -2
- package/src/__tests__/metrics.test.ts +415 -0
- package/src/__tests__/test-old-node-version.js +31 -0
- package/src/config/config.ts +8 -0
- package/src/config/validation.ts +9 -0
- package/src/config-cache.ts +2 -1
- package/src/dev/local.tsx +5 -3
- package/src/dev.tsx +6 -0
- package/src/index.tsx +73 -0
- package/src/metrics/index.ts +4 -0
- package/src/metrics/metrics-config.ts +222 -0
- package/src/metrics/metrics-dispatcher.ts +93 -0
- package/src/metrics/send-event.ts +80 -0
- package/src/pages/build.tsx +2 -0
- package/src/pages/deployments.tsx +2 -0
- package/src/pages/dev.tsx +4 -0
- package/src/pages/projects.tsx +3 -0
- package/src/pages/publish.tsx +3 -0
- package/src/pages/upload.tsx +2 -2
- package/src/pubsub/pubsub-commands.tsx +43 -0
- package/src/worker-namespace.ts +16 -0
- package/wrangler-dist/cli.js +1282 -986
package/src/dev.tsx
CHANGED
|
@@ -10,6 +10,7 @@ import { getVarsForDev } from "./dev/dev-vars";
|
|
|
10
10
|
|
|
11
11
|
import { getEntry } from "./entry";
|
|
12
12
|
import { logger } from "./logger";
|
|
13
|
+
import * as metrics from "./metrics";
|
|
13
14
|
import { getAssetPaths, getSiteAssetPaths } from "./sites";
|
|
14
15
|
import { getAccountFromCache } from "./user";
|
|
15
16
|
import { getZoneIdFromHost, getZoneForRoute, getHostFromRoute } from "./zones";
|
|
@@ -264,6 +265,11 @@ export async function startDev(args: ArgumentsCamelCase<DevArgs>) {
|
|
|
264
265
|
((args.script &&
|
|
265
266
|
findWranglerToml(path.dirname(args.script))) as ConfigPath);
|
|
266
267
|
let config = readConfig(configPath, args);
|
|
268
|
+
metrics.sendMetricsEvent(
|
|
269
|
+
"run dev",
|
|
270
|
+
{ local: args.local },
|
|
271
|
+
{ sendMetrics: config.send_metrics, offline: args.local }
|
|
272
|
+
);
|
|
267
273
|
|
|
268
274
|
if (config.configPath) {
|
|
269
275
|
watcher = watch(config.configPath, {
|
package/src/index.tsx
CHANGED
|
@@ -33,6 +33,7 @@ import {
|
|
|
33
33
|
deleteKVKeyValue,
|
|
34
34
|
} from "./kv";
|
|
35
35
|
import { logger } from "./logger";
|
|
36
|
+
import * as metrics from "./metrics";
|
|
36
37
|
import { pages } from "./pages";
|
|
37
38
|
import {
|
|
38
39
|
formatMessage,
|
|
@@ -500,6 +501,9 @@ function createCLIParser(argv: string[]) {
|
|
|
500
501
|
(args.config as ConfigPath) ||
|
|
501
502
|
(args.script && findWranglerToml(path.dirname(args.script)));
|
|
502
503
|
const config = readConfig(configPath, args);
|
|
504
|
+
metrics.sendMetricsEvent("deploy worker script", {
|
|
505
|
+
sendMetrics: config.send_metrics,
|
|
506
|
+
});
|
|
503
507
|
const entry = await getEntry(args, config, "publish");
|
|
504
508
|
|
|
505
509
|
if (args.public) {
|
|
@@ -640,6 +644,9 @@ function createCLIParser(argv: string[]) {
|
|
|
640
644
|
await printWranglerBanner();
|
|
641
645
|
}
|
|
642
646
|
const config = readConfig(args.config as ConfigPath, args);
|
|
647
|
+
metrics.sendMetricsEvent("begin log stream", {
|
|
648
|
+
sendMetrics: config.send_metrics,
|
|
649
|
+
});
|
|
643
650
|
|
|
644
651
|
const scriptName = getLegacyScriptName(args, config);
|
|
645
652
|
|
|
@@ -685,6 +692,9 @@ function createCLIParser(argv: string[]) {
|
|
|
685
692
|
onExit(async () => {
|
|
686
693
|
tail.terminate();
|
|
687
694
|
await deleteTail();
|
|
695
|
+
metrics.sendMetricsEvent("end log stream", {
|
|
696
|
+
sendMetrics: config.send_metrics,
|
|
697
|
+
});
|
|
688
698
|
});
|
|
689
699
|
|
|
690
700
|
const printLog: (data: RawData) => void =
|
|
@@ -701,6 +711,9 @@ function createCLIParser(argv: string[]) {
|
|
|
701
711
|
await setTimeout(100);
|
|
702
712
|
break;
|
|
703
713
|
case tail.CLOSED:
|
|
714
|
+
metrics.sendMetricsEvent("end log stream", {
|
|
715
|
+
sendMetrics: config.send_metrics,
|
|
716
|
+
});
|
|
704
717
|
throw new Error(
|
|
705
718
|
`Connection to ${scriptDisplayName} closed unexpectedly.`
|
|
706
719
|
);
|
|
@@ -714,6 +727,9 @@ function createCLIParser(argv: string[]) {
|
|
|
714
727
|
tail.on("close", async () => {
|
|
715
728
|
tail.terminate();
|
|
716
729
|
await deleteTail();
|
|
730
|
+
metrics.sendMetricsEvent("end log stream", {
|
|
731
|
+
sendMetrics: config.send_metrics,
|
|
732
|
+
});
|
|
717
733
|
});
|
|
718
734
|
}
|
|
719
735
|
);
|
|
@@ -944,6 +960,9 @@ function createCLIParser(argv: string[]) {
|
|
|
944
960
|
|
|
945
961
|
try {
|
|
946
962
|
await submitSecret();
|
|
963
|
+
metrics.sendMetricsEvent("create encrypted variable", {
|
|
964
|
+
sendMetrics: config.send_metrics,
|
|
965
|
+
});
|
|
947
966
|
} catch (e) {
|
|
948
967
|
if (isMissingWorkerError(e)) {
|
|
949
968
|
// create a draft worker and try again
|
|
@@ -1016,6 +1035,9 @@ function createCLIParser(argv: string[]) {
|
|
|
1016
1035
|
: `/accounts/${accountId}/workers/services/${scriptName}/environments/${args.env}/secrets`;
|
|
1017
1036
|
|
|
1018
1037
|
await fetchResult(`${url}/${args.key}`, { method: "DELETE" });
|
|
1038
|
+
metrics.sendMetricsEvent("delete encrypted variable", {
|
|
1039
|
+
sendMetrics: config.send_metrics,
|
|
1040
|
+
});
|
|
1019
1041
|
logger.log(`✨ Success! Deleted secret ${args.key}`);
|
|
1020
1042
|
}
|
|
1021
1043
|
}
|
|
@@ -1056,6 +1078,9 @@ function createCLIParser(argv: string[]) {
|
|
|
1056
1078
|
: `/accounts/${accountId}/workers/services/${scriptName}/environments/${args.env}/secrets`;
|
|
1057
1079
|
|
|
1058
1080
|
logger.log(JSON.stringify(await fetchResult(url), null, " "));
|
|
1081
|
+
metrics.sendMetricsEvent("list encrypted variables", {
|
|
1082
|
+
sendMetrics: config.send_metrics,
|
|
1083
|
+
});
|
|
1059
1084
|
}
|
|
1060
1085
|
);
|
|
1061
1086
|
}
|
|
@@ -1117,6 +1142,9 @@ function createCLIParser(argv: string[]) {
|
|
|
1117
1142
|
|
|
1118
1143
|
logger.log(`🌀 Creating namespace with title "${title}"`);
|
|
1119
1144
|
const namespaceId = await createKVNamespace(accountId, title);
|
|
1145
|
+
metrics.sendMetricsEvent("create kv namespace", {
|
|
1146
|
+
sendMetrics: config.send_metrics,
|
|
1147
|
+
});
|
|
1120
1148
|
|
|
1121
1149
|
logger.log("✨ Success!");
|
|
1122
1150
|
const envString = args.env ? ` under [env.${args.env}]` : "";
|
|
@@ -1145,6 +1173,9 @@ function createCLIParser(argv: string[]) {
|
|
|
1145
1173
|
logger.log(
|
|
1146
1174
|
JSON.stringify(await listKVNamespaces(accountId), null, " ")
|
|
1147
1175
|
);
|
|
1176
|
+
metrics.sendMetricsEvent("list kv namespaces", {
|
|
1177
|
+
sendMetrics: config.send_metrics,
|
|
1178
|
+
});
|
|
1148
1179
|
}
|
|
1149
1180
|
)
|
|
1150
1181
|
.command(
|
|
@@ -1190,6 +1221,9 @@ function createCLIParser(argv: string[]) {
|
|
|
1190
1221
|
const accountId = await requireAuth(config);
|
|
1191
1222
|
|
|
1192
1223
|
await deleteKVNamespace(accountId, id);
|
|
1224
|
+
metrics.sendMetricsEvent("delete kv namespace", {
|
|
1225
|
+
sendMetrics: config.send_metrics,
|
|
1226
|
+
});
|
|
1193
1227
|
|
|
1194
1228
|
// TODO: recommend they remove it from wrangler.toml
|
|
1195
1229
|
|
|
@@ -1313,6 +1347,9 @@ function createCLIParser(argv: string[]) {
|
|
|
1313
1347
|
expiration_ttl: ttl,
|
|
1314
1348
|
metadata: metadata as KeyValue["metadata"],
|
|
1315
1349
|
});
|
|
1350
|
+
metrics.sendMetricsEvent("write kv key-value", {
|
|
1351
|
+
sendMetrics: config.send_metrics,
|
|
1352
|
+
});
|
|
1316
1353
|
}
|
|
1317
1354
|
)
|
|
1318
1355
|
.command(
|
|
@@ -1362,6 +1399,9 @@ function createCLIParser(argv: string[]) {
|
|
|
1362
1399
|
prefix
|
|
1363
1400
|
);
|
|
1364
1401
|
logger.log(JSON.stringify(results, undefined, 2));
|
|
1402
|
+
metrics.sendMetricsEvent("list kv keys", {
|
|
1403
|
+
sendMetrics: config.send_metrics,
|
|
1404
|
+
});
|
|
1365
1405
|
}
|
|
1366
1406
|
)
|
|
1367
1407
|
.command(
|
|
@@ -1422,6 +1462,9 @@ function createCLIParser(argv: string[]) {
|
|
|
1422
1462
|
} else {
|
|
1423
1463
|
process.stdout.write(bufferKVValue);
|
|
1424
1464
|
}
|
|
1465
|
+
metrics.sendMetricsEvent("read kv value", {
|
|
1466
|
+
sendMetrics: config.send_metrics,
|
|
1467
|
+
});
|
|
1425
1468
|
}
|
|
1426
1469
|
)
|
|
1427
1470
|
.command(
|
|
@@ -1468,6 +1511,9 @@ function createCLIParser(argv: string[]) {
|
|
|
1468
1511
|
const accountId = await requireAuth(config);
|
|
1469
1512
|
|
|
1470
1513
|
await deleteKVKeyValue(accountId, namespaceId, key);
|
|
1514
|
+
metrics.sendMetricsEvent("delete kv key-value", {
|
|
1515
|
+
sendMetrics: config.send_metrics,
|
|
1516
|
+
});
|
|
1471
1517
|
}
|
|
1472
1518
|
);
|
|
1473
1519
|
}
|
|
@@ -1572,6 +1618,9 @@ function createCLIParser(argv: string[]) {
|
|
|
1572
1618
|
|
|
1573
1619
|
const accountId = await requireAuth(config);
|
|
1574
1620
|
await putKVBulkKeyValue(accountId, namespaceId, content);
|
|
1621
|
+
metrics.sendMetricsEvent("write kv key-values (bulk)", {
|
|
1622
|
+
sendMetrics: config.send_metrics,
|
|
1623
|
+
});
|
|
1575
1624
|
|
|
1576
1625
|
logger.log("Success!");
|
|
1577
1626
|
}
|
|
@@ -1662,6 +1711,9 @@ function createCLIParser(argv: string[]) {
|
|
|
1662
1711
|
const accountId = await requireAuth(config);
|
|
1663
1712
|
|
|
1664
1713
|
await deleteKVBulkKeyValue(accountId, namespaceId, content);
|
|
1714
|
+
metrics.sendMetricsEvent("delete kv key-values (bulk)", {
|
|
1715
|
+
sendMetrics: config.send_metrics,
|
|
1716
|
+
});
|
|
1665
1717
|
|
|
1666
1718
|
logger.log("Success!");
|
|
1667
1719
|
}
|
|
@@ -1701,6 +1753,9 @@ function createCLIParser(argv: string[]) {
|
|
|
1701
1753
|
logger.log(`Creating bucket ${args.name}.`);
|
|
1702
1754
|
await createR2Bucket(accountId, args.name);
|
|
1703
1755
|
logger.log(`Created bucket ${args.name}.`);
|
|
1756
|
+
metrics.sendMetricsEvent("create r2 bucket", {
|
|
1757
|
+
sendMetrics: config.send_metrics,
|
|
1758
|
+
});
|
|
1704
1759
|
}
|
|
1705
1760
|
);
|
|
1706
1761
|
|
|
@@ -1710,6 +1765,9 @@ function createCLIParser(argv: string[]) {
|
|
|
1710
1765
|
const accountId = await requireAuth(config);
|
|
1711
1766
|
|
|
1712
1767
|
logger.log(JSON.stringify(await listR2Buckets(accountId), null, 2));
|
|
1768
|
+
metrics.sendMetricsEvent("list r2 buckets", {
|
|
1769
|
+
sendMetrics: config.send_metrics,
|
|
1770
|
+
});
|
|
1713
1771
|
});
|
|
1714
1772
|
|
|
1715
1773
|
r2BucketYargs.command(
|
|
@@ -1732,6 +1790,9 @@ function createCLIParser(argv: string[]) {
|
|
|
1732
1790
|
logger.log(`Deleting bucket ${args.name}.`);
|
|
1733
1791
|
await deleteR2Bucket(accountId, args.name);
|
|
1734
1792
|
logger.log(`Deleted bucket ${args.name}.`);
|
|
1793
|
+
metrics.sendMetricsEvent("delete r2 bucket", {
|
|
1794
|
+
sendMetrics: config.send_metrics,
|
|
1795
|
+
});
|
|
1735
1796
|
}
|
|
1736
1797
|
);
|
|
1737
1798
|
return r2BucketYargs;
|
|
@@ -1800,6 +1861,10 @@ function createCLIParser(argv: string[]) {
|
|
|
1800
1861
|
return;
|
|
1801
1862
|
}
|
|
1802
1863
|
await login();
|
|
1864
|
+
const config = readConfig(args.config as ConfigPath, args);
|
|
1865
|
+
metrics.sendMetricsEvent("login user", {
|
|
1866
|
+
sendMetrics: config.send_metrics,
|
|
1867
|
+
});
|
|
1803
1868
|
|
|
1804
1869
|
// TODO: would be nice if it optionally saved login
|
|
1805
1870
|
// credentials inside node_modules/.cache or something
|
|
@@ -1816,6 +1881,10 @@ function createCLIParser(argv: string[]) {
|
|
|
1816
1881
|
async () => {
|
|
1817
1882
|
await printWranglerBanner();
|
|
1818
1883
|
await logout();
|
|
1884
|
+
const config = readConfig(undefined, {});
|
|
1885
|
+
metrics.sendMetricsEvent("logout user", {
|
|
1886
|
+
sendMetrics: config.send_metrics,
|
|
1887
|
+
});
|
|
1819
1888
|
}
|
|
1820
1889
|
);
|
|
1821
1890
|
|
|
@@ -1827,6 +1896,10 @@ function createCLIParser(argv: string[]) {
|
|
|
1827
1896
|
async () => {
|
|
1828
1897
|
await printWranglerBanner();
|
|
1829
1898
|
await whoami();
|
|
1899
|
+
const config = readConfig(undefined, {});
|
|
1900
|
+
metrics.sendMetricsEvent("view accounts", {
|
|
1901
|
+
sendMetrics: config.send_metrics,
|
|
1902
|
+
});
|
|
1830
1903
|
}
|
|
1831
1904
|
);
|
|
1832
1905
|
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { fetchResult } from "../cfetch";
|
|
6
|
+
import { getConfigCache, saveToConfigCache } from "../config-cache";
|
|
7
|
+
import { confirm } from "../dialogs";
|
|
8
|
+
import isInteractive from "../is-interactive";
|
|
9
|
+
import { logger } from "../logger";
|
|
10
|
+
import { getAPIToken } from "../user";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The date that the metrics being gathered was last updated in a way that would require
|
|
14
|
+
* the user to give their permission again.
|
|
15
|
+
*
|
|
16
|
+
* When reading from a config file, we check the recorded date in the config file against
|
|
17
|
+
* this one here. We ignore the permissions set in the the file if the recorded date is older.
|
|
18
|
+
* This allows us to prompt the user to re-opt-in when we make substantive changes to our metrics
|
|
19
|
+
* gathering.
|
|
20
|
+
*/
|
|
21
|
+
export const CURRENT_METRICS_DATE = new Date(2022, 6, 4);
|
|
22
|
+
export const USER_ID_CACHE_PATH = "user-id.json";
|
|
23
|
+
|
|
24
|
+
export interface MetricsConfigOptions {
|
|
25
|
+
/**
|
|
26
|
+
* Defines whether to send metrics to Cloudflare:
|
|
27
|
+
* If defined, then use this value for whether the dispatch is enabled.
|
|
28
|
+
* Otherwise, infer the enabled value from the user configuration.
|
|
29
|
+
*/
|
|
30
|
+
sendMetrics?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* When true, do not make any CF API requests.
|
|
33
|
+
*/
|
|
34
|
+
offline?: boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The information needed to set up the MetricsDispatcher.
|
|
39
|
+
*/
|
|
40
|
+
export interface MetricsConfig {
|
|
41
|
+
/** True if usage tracking is enabled. */
|
|
42
|
+
enabled: boolean;
|
|
43
|
+
/** A UID that identifies this user and machine pair for Wrangler. */
|
|
44
|
+
deviceId: string;
|
|
45
|
+
/** The currently logged in user - undefined if not logged in. */
|
|
46
|
+
userId: string | undefined;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Get hold of the permissions and device-id for metrics dispatch.
|
|
51
|
+
*
|
|
52
|
+
* The device-id is just a unique identifier sent along with events to help
|
|
53
|
+
* to collate metrics. Once generated, this id is cached in the metrics config file.
|
|
54
|
+
*
|
|
55
|
+
* The permissions define whether we can send metrics or not. They can come from a variety of places:
|
|
56
|
+
* - the `send_metrics` setting in `wrangler.toml`
|
|
57
|
+
* - a cached response from the current user
|
|
58
|
+
* - prompting the user to opt-in to metrics
|
|
59
|
+
*
|
|
60
|
+
* If the user was prompted to opt-in, then their response is cached in the metrics config file.
|
|
61
|
+
*
|
|
62
|
+
* If the current process is not interactive then we will cannot prompt the user and just assume
|
|
63
|
+
* we cannot send metrics if there is no cached or project-level preference available.
|
|
64
|
+
*/
|
|
65
|
+
export async function getMetricsConfig({
|
|
66
|
+
sendMetrics,
|
|
67
|
+
offline = false,
|
|
68
|
+
}: MetricsConfigOptions): Promise<MetricsConfig> {
|
|
69
|
+
const config = await readMetricsConfig();
|
|
70
|
+
const deviceId = await getDeviceId(config);
|
|
71
|
+
const userId = await getUserId(offline);
|
|
72
|
+
|
|
73
|
+
// If the project is explicitly set the `send_metrics` options in `wrangler.toml`
|
|
74
|
+
// then use that and ignore any user preference.
|
|
75
|
+
if (sendMetrics !== undefined) {
|
|
76
|
+
return { enabled: sendMetrics, deviceId, userId };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Get the user preference from the metrics config.
|
|
80
|
+
const permission = config.permission;
|
|
81
|
+
if (permission !== undefined) {
|
|
82
|
+
if (new Date(permission.date) >= CURRENT_METRICS_DATE) {
|
|
83
|
+
return { enabled: permission.enabled, deviceId, userId };
|
|
84
|
+
} else if (permission.enabled) {
|
|
85
|
+
logger.log(
|
|
86
|
+
"Usage metrics tracking has changed since you last granted permission."
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// We couldn't get the metrics permission from the project-level nor the user-level config.
|
|
92
|
+
// If we are not interactive then just bail out.
|
|
93
|
+
if (!isInteractive()) {
|
|
94
|
+
return { enabled: false, deviceId, userId };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Otherwise, let's ask the user and store the result in the metrics config.
|
|
98
|
+
const enabled = await confirm(
|
|
99
|
+
"Would you like to help improve Wrangler by sending usage metrics to Cloudflare?"
|
|
100
|
+
);
|
|
101
|
+
logger.log(
|
|
102
|
+
`Your choice has been saved in the following file: ${path.relative(
|
|
103
|
+
process.cwd(),
|
|
104
|
+
getMetricsConfigPath()
|
|
105
|
+
)}.\n\n` +
|
|
106
|
+
" You can override the user level setting for a project in `wrangler.toml`:\n\n" +
|
|
107
|
+
" - to disable sending metrics for a project: `send_metrics = false`\n" +
|
|
108
|
+
" - to enable sending metrics for a project: `send_metrics = true`"
|
|
109
|
+
);
|
|
110
|
+
await writeMetricsConfig({
|
|
111
|
+
permission: {
|
|
112
|
+
enabled,
|
|
113
|
+
date: CURRENT_METRICS_DATE,
|
|
114
|
+
},
|
|
115
|
+
deviceId,
|
|
116
|
+
});
|
|
117
|
+
return { enabled, deviceId, userId };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Stringify and write the given info to the metrics config file.
|
|
122
|
+
*/
|
|
123
|
+
export async function writeMetricsConfig(config: MetricsConfigFile) {
|
|
124
|
+
await mkdir(path.dirname(getMetricsConfigPath()), { recursive: true });
|
|
125
|
+
await writeFile(
|
|
126
|
+
getMetricsConfigPath(),
|
|
127
|
+
JSON.stringify(
|
|
128
|
+
config,
|
|
129
|
+
(_key, value) => (value instanceof Date ? value.toISOString() : value),
|
|
130
|
+
"\t"
|
|
131
|
+
)
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Read and parse the metrics config file.
|
|
137
|
+
*/
|
|
138
|
+
export async function readMetricsConfig(): Promise<MetricsConfigFile> {
|
|
139
|
+
try {
|
|
140
|
+
return JSON.parse(
|
|
141
|
+
await readFile(getMetricsConfigPath(), "utf8"),
|
|
142
|
+
(key, value) => (key === "date" ? new Date(value) : value)
|
|
143
|
+
);
|
|
144
|
+
} catch {
|
|
145
|
+
return {};
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Get the path to the metrics config file.
|
|
151
|
+
*/
|
|
152
|
+
function getMetricsConfigPath(): string {
|
|
153
|
+
return path.resolve(os.homedir(), ".wrangler/config/metrics.json");
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* The format of the metrics config file.
|
|
158
|
+
*/
|
|
159
|
+
export interface MetricsConfigFile {
|
|
160
|
+
permission?: {
|
|
161
|
+
/** True if Wrangler should send metrics to Cloudflare. */
|
|
162
|
+
enabled: boolean;
|
|
163
|
+
/** The date that this permission was set. */
|
|
164
|
+
date: Date;
|
|
165
|
+
};
|
|
166
|
+
/** A unique UUID that identifies this device for metrics purposes. */
|
|
167
|
+
deviceId?: string;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Returns an ID that uniquely identifies Wrangler on this device to help collate events.
|
|
172
|
+
*
|
|
173
|
+
* Once created this ID is stored in the metrics config file.
|
|
174
|
+
*/
|
|
175
|
+
async function getDeviceId(config: MetricsConfigFile) {
|
|
176
|
+
// Get or create the deviceId.
|
|
177
|
+
const deviceId = config.deviceId ?? randomUUID();
|
|
178
|
+
if (config.deviceId === undefined) {
|
|
179
|
+
// We had to create a new deviceID so store it now.
|
|
180
|
+
await writeMetricsConfig({ ...config, deviceId });
|
|
181
|
+
}
|
|
182
|
+
return deviceId;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Returns the ID of the current user, which will be sent with each event.
|
|
187
|
+
*
|
|
188
|
+
* The ID is retrieved from the CF API `/user` endpoint if the user is authenticated and then
|
|
189
|
+
* stored in the `node_modules/.cache`.
|
|
190
|
+
*
|
|
191
|
+
* If it is not possible to retrieve the ID (perhaps the user is not logged in) then we just use
|
|
192
|
+
* `undefined`.
|
|
193
|
+
*/
|
|
194
|
+
async function getUserId(offline: boolean) {
|
|
195
|
+
// Get the userId from the cache.
|
|
196
|
+
// If it has not been found in the cache and we are not offline then make an API call to get it.
|
|
197
|
+
// If we can't work in out then just use `anonymous`.
|
|
198
|
+
let userId = getConfigCache<{ userId: string }>(USER_ID_CACHE_PATH).userId;
|
|
199
|
+
if (userId === undefined && !offline) {
|
|
200
|
+
userId = await fetchUserId();
|
|
201
|
+
if (userId !== undefined) {
|
|
202
|
+
saveToConfigCache(USER_ID_CACHE_PATH, { userId });
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return userId;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Ask the Cloudflare API for the User ID of the current user.
|
|
210
|
+
*
|
|
211
|
+
* We will only do this if we are not "offline", e.g. not running `wrangler dev --local`.
|
|
212
|
+
* Quietly return undefined if anything goes wrong.
|
|
213
|
+
*/
|
|
214
|
+
async function fetchUserId(): Promise<string | undefined> {
|
|
215
|
+
try {
|
|
216
|
+
return getAPIToken()
|
|
217
|
+
? (await fetchResult<{ id: string }>("/user")).id
|
|
218
|
+
: undefined;
|
|
219
|
+
} catch (e) {
|
|
220
|
+
return undefined;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { fetch } from "undici";
|
|
2
|
+
import { version as wranglerVersion } from "../../package.json";
|
|
3
|
+
import { logger } from "../logger";
|
|
4
|
+
import { getMetricsConfig } from "./metrics-config";
|
|
5
|
+
import type { MetricsConfigOptions } from "./metrics-config";
|
|
6
|
+
|
|
7
|
+
// The SPARROW_SOURCE_KEY is provided at esbuild time as a `define` for production and beta
|
|
8
|
+
// releases. Otherwise it is left undefined, which automatically disables metrics requests.
|
|
9
|
+
declare const SPARROW_SOURCE_KEY: string;
|
|
10
|
+
const SPARROW_URL = "https://sparrow.cloudflare.com";
|
|
11
|
+
|
|
12
|
+
export async function getMetricsDispatcher(options: MetricsConfigOptions) {
|
|
13
|
+
return {
|
|
14
|
+
/**
|
|
15
|
+
* Dispatch a event to the analytics target.
|
|
16
|
+
*
|
|
17
|
+
* The event should follow these conventions
|
|
18
|
+
* - name is of the form `[action] [object]` (lower case)
|
|
19
|
+
* - additional properties are camelCased
|
|
20
|
+
*/
|
|
21
|
+
async sendEvent(name: string, properties: Properties = {}): Promise<void> {
|
|
22
|
+
await dispatch({ type: "event", name, properties });
|
|
23
|
+
},
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Dispatch a user profile information to the analytics target.
|
|
27
|
+
*
|
|
28
|
+
* This call can be used to inform the analytics target of relevant properties associated
|
|
29
|
+
* with the current user.
|
|
30
|
+
*/
|
|
31
|
+
async identify(properties: Properties): Promise<void> {
|
|
32
|
+
await dispatch({ type: "identify", name: "identify", properties });
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
async function dispatch(event: {
|
|
37
|
+
type: "identify" | "event";
|
|
38
|
+
name: string;
|
|
39
|
+
properties: Properties;
|
|
40
|
+
}): Promise<void> {
|
|
41
|
+
if (!SPARROW_SOURCE_KEY) {
|
|
42
|
+
logger.debug(
|
|
43
|
+
"Metrics dispatcher: Source Key not provided. Be sure to initialize before sending events.",
|
|
44
|
+
event
|
|
45
|
+
);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Lazily get the config for this dispatcher only when an event is being dispatched.
|
|
50
|
+
const metricsConfig = await getMetricsConfig(options);
|
|
51
|
+
if (!metricsConfig.enabled) {
|
|
52
|
+
logger.debug(
|
|
53
|
+
`Metrics dispatcher: Dispatching disabled - would have sent ${JSON.stringify(
|
|
54
|
+
event
|
|
55
|
+
)}.`
|
|
56
|
+
);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
try {
|
|
61
|
+
logger.debug(`Metrics dispatcher: Posting data ${JSON.stringify(event)}`);
|
|
62
|
+
const body = JSON.stringify({
|
|
63
|
+
deviceId: metricsConfig.deviceId,
|
|
64
|
+
userId: metricsConfig.userId,
|
|
65
|
+
event: event.name,
|
|
66
|
+
properties: {
|
|
67
|
+
category: "Workers",
|
|
68
|
+
wranglerVersion,
|
|
69
|
+
...event.properties,
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
await fetch(`${SPARROW_URL}/api/v1/${event.type}`, {
|
|
74
|
+
method: "POST",
|
|
75
|
+
headers: {
|
|
76
|
+
Accept: "*/*",
|
|
77
|
+
"Content-Type": "application/json",
|
|
78
|
+
"Sparrow-Source-Key": SPARROW_SOURCE_KEY,
|
|
79
|
+
},
|
|
80
|
+
mode: "cors",
|
|
81
|
+
keepalive: true,
|
|
82
|
+
body,
|
|
83
|
+
});
|
|
84
|
+
} catch (e) {
|
|
85
|
+
logger.debug(
|
|
86
|
+
"Metrics dispatcher: Failed to send request:",
|
|
87
|
+
(e as Error).message
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export type Properties = Record<string, unknown>;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { logger } from "../logger";
|
|
2
|
+
import { getMetricsDispatcher } from "./metrics-dispatcher";
|
|
3
|
+
import type { MetricsConfigOptions } from "./metrics-config";
|
|
4
|
+
import type { Properties } from "./metrics-dispatcher";
|
|
5
|
+
|
|
6
|
+
/** These are event names used by this wrangler client. */
|
|
7
|
+
export type EventNames =
|
|
8
|
+
| "view accounts"
|
|
9
|
+
| "deploy worker script"
|
|
10
|
+
| "begin log stream"
|
|
11
|
+
| "end log stream"
|
|
12
|
+
| "create encrypted variable"
|
|
13
|
+
| "delete encrypted variable"
|
|
14
|
+
| "list encrypted variables"
|
|
15
|
+
| "create kv namespace"
|
|
16
|
+
| "list kv namespaces"
|
|
17
|
+
| "delete kv namespace"
|
|
18
|
+
| "write kv key-value"
|
|
19
|
+
| "list kv keys"
|
|
20
|
+
| "read kv value"
|
|
21
|
+
| "delete kv key-value"
|
|
22
|
+
| "write kv key-values (bulk)"
|
|
23
|
+
| "delete kv key-values (bulk)"
|
|
24
|
+
| "create r2 bucket"
|
|
25
|
+
| "list r2 buckets"
|
|
26
|
+
| "delete r2 bucket"
|
|
27
|
+
| "login user"
|
|
28
|
+
| "logout user"
|
|
29
|
+
| "create pubsub namespace"
|
|
30
|
+
| "list pubsub namespaces"
|
|
31
|
+
| "delete pubsub namespace"
|
|
32
|
+
| "view pubsub namespace"
|
|
33
|
+
| "create pubsub broker"
|
|
34
|
+
| "update pubsub broker"
|
|
35
|
+
| "list pubsub brokers"
|
|
36
|
+
| "delete pubsub broker"
|
|
37
|
+
| "view pubsub broker"
|
|
38
|
+
| "issue pubsub broker credentials"
|
|
39
|
+
| "revoke pubsub broker credentials"
|
|
40
|
+
| "unrevoke pubsub broker credentials"
|
|
41
|
+
| "list pubsub broker revoked credentials"
|
|
42
|
+
| "list pubsub broker public-keys"
|
|
43
|
+
| "list worker namespaces"
|
|
44
|
+
| "view worker namespace"
|
|
45
|
+
| "create worker namespace"
|
|
46
|
+
| "delete worker namespace"
|
|
47
|
+
| "rename worker namespace"
|
|
48
|
+
| "create pages project"
|
|
49
|
+
| "list pages projects"
|
|
50
|
+
| "deploy pages project"
|
|
51
|
+
| "list pages projects deployments"
|
|
52
|
+
| "build pages functions"
|
|
53
|
+
| "run dev"
|
|
54
|
+
| "run pages dev";
|
|
55
|
+
|
|
56
|
+
export function sendMetricsEvent(event: EventNames): void;
|
|
57
|
+
export function sendMetricsEvent(
|
|
58
|
+
event: EventNames,
|
|
59
|
+
options: MetricsConfigOptions
|
|
60
|
+
): void;
|
|
61
|
+
export function sendMetricsEvent(
|
|
62
|
+
event: EventNames,
|
|
63
|
+
properties: Properties,
|
|
64
|
+
options: MetricsConfigOptions
|
|
65
|
+
): void;
|
|
66
|
+
/**
|
|
67
|
+
* Send a metrics event to Cloudflare, if usage tracking is enabled.
|
|
68
|
+
*/
|
|
69
|
+
export function sendMetricsEvent(
|
|
70
|
+
event: EventNames,
|
|
71
|
+
...args: [] | [MetricsConfigOptions] | [Properties, MetricsConfigOptions]
|
|
72
|
+
): void {
|
|
73
|
+
const options = args.pop() ?? {};
|
|
74
|
+
const properties = (args.pop() ?? {}) as Properties;
|
|
75
|
+
getMetricsDispatcher(options)
|
|
76
|
+
.then((metricsDispatcher) => metricsDispatcher.sendEvent(event, properties))
|
|
77
|
+
.catch((err) => {
|
|
78
|
+
logger.debug("Error sending metrics event", err);
|
|
79
|
+
});
|
|
80
|
+
}
|
package/src/pages/build.tsx
CHANGED
|
@@ -2,6 +2,7 @@ import { writeFileSync } from "node:fs";
|
|
|
2
2
|
import { tmpdir } from "node:os";
|
|
3
3
|
import { dirname, join } from "node:path";
|
|
4
4
|
import { logger } from "../logger";
|
|
5
|
+
import * as metrics from "../metrics";
|
|
5
6
|
import { toUrlPath } from "../paths";
|
|
6
7
|
import { isInPagesCI } from "./constants";
|
|
7
8
|
import { buildPlugin } from "./functions/buildPlugin";
|
|
@@ -120,6 +121,7 @@ export const Handler = async ({
|
|
|
120
121
|
buildOutputDirectory,
|
|
121
122
|
nodeCompat,
|
|
122
123
|
});
|
|
124
|
+
metrics.sendMetricsEvent("build pages functions");
|
|
123
125
|
};
|
|
124
126
|
|
|
125
127
|
export async function buildFunctions({
|
|
@@ -6,6 +6,7 @@ import { format as timeagoFormat } from "timeago.js";
|
|
|
6
6
|
import { fetchResult } from "../cfetch";
|
|
7
7
|
import { getConfigCache, saveToConfigCache } from "../config-cache";
|
|
8
8
|
import { FatalError } from "../errors";
|
|
9
|
+
import * as metrics from "../metrics";
|
|
9
10
|
import { requireAuth } from "../user";
|
|
10
11
|
import { PAGES_CONFIG_CACHE_FILENAME } from "./constants";
|
|
11
12
|
import { listProjects } from "./projects";
|
|
@@ -98,4 +99,5 @@ export async function ListHandler({
|
|
|
98
99
|
});
|
|
99
100
|
|
|
100
101
|
render(<Table data={data}></Table>, { patchConsole: false });
|
|
102
|
+
metrics.sendMetricsEvent("list pages projects deployments");
|
|
101
103
|
}
|