@prairielearn/node-metrics 1.0.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @prairielearn/node-metrics
2
2
 
3
+ ## 2.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - 4199824: Add required `namespace` option
8
+
9
+ ## 1.0.3
10
+
11
+ ### Patch Changes
12
+
13
+ - 82f9c2f: Upgrade all JavaScript dependencies
14
+
3
15
  ## 1.0.2
4
16
 
5
17
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -2,6 +2,7 @@ import { type CloudWatchClientConfig, type Dimension } from '@aws-sdk/client-clo
2
2
  interface NodeMetricsOptions {
3
3
  awsConfig: CloudWatchClientConfig;
4
4
  intervalSeconds: number;
5
+ namespace: string;
5
6
  dimensions: Dimension[];
6
7
  onError: (err: Error) => void;
7
8
  }
package/dist/index.js CHANGED
@@ -65,7 +65,7 @@ async function emit(options) {
65
65
  // eslint-disable-next-line @prairielearn/aws-client-shared-config
66
66
  const cloudwatch = new CloudWatch(options.awsConfig);
67
67
  await cloudwatch.putMetricData({
68
- Namespace: 'PrairieLearn',
68
+ Namespace: options.namespace,
69
69
  MetricData: metrics.map((m) => ({
70
70
  ...m,
71
71
  StorageResolution: 1,
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAElD,OAAO,EACL,UAAU,GAGX,MAAM,4BAA4B,CAAC;AACpC,OAAO,SAAS,MAAM,WAAW,CAAC;AAElC,MAAM,iBAAiB,GAAG,SAAS,EAAE,CAAC;AAEtC,IAAI,eAAe,GAA2B,IAAI,CAAC;AACnD,IAAI,QAAQ,GAAG,OAAO,CAAC,QAAQ,EAAE,CAAC;AAClC,IAAI,IAAI,GAAG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AAI,KAAK,YAAY;YAAE,OAAO;QACtC,MAAM,GAAG,CAAC;IACZ,CAAC;AACH,CAAC;AAED,MAAM,UAAU,KAAK,CAAC,OAA2B;IAC/C,eAAe,GAAG,IAAI,eAAe,EAAE,CAAC;IAExC,sFAAsF;IACtF,QAAQ,GAAG,OAAO,CAAC,QAAQ,EAAE,CAAC;IAC9B,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;IAE/B,CAAC,GAAG,EAAE,CAAC,QAAQ,CAAC,EAAE,MAAM,EAAE,eAAe,CAAC,MAAM,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC,EAAE,CAAC;AACrE,CAAC;AAED,MAAM,UAAU,IAAI;IAClB,eAAe,EAAE,KAAK,EAAE,CAAC;AAC3B,CAAC","sourcesContent":["import { setTimeout } from 'node:timers/promises';\n\nimport {\n CloudWatch,\n type CloudWatchClientConfig,\n type Dimension,\n} from '@aws-sdk/client-cloudwatch';\nimport loopbench from 'loopbench';\n\nconst loopbenchInstance = loopbench();\n\nlet abortController: AbortController | null = null;\nlet cpuUsage = process.cpuUsage();\nlet time = process.hrtime.bigint();\n\ninterface NodeMetricsOptions {\n awsConfig: CloudWatchClientConfig;\n intervalSeconds: number;\n dimensions: Dimension[];\n onError: (err: Error) => void;\n}\n\nasync function emit(options: NodeMetricsOptions) {\n try {\n const memoryStats = process.memoryUsage();\n\n const elapsedCpuUsage = process.cpuUsage(cpuUsage);\n // This delta is in nanoseconds.\n const elapsedTime = process.hrtime.bigint() - time;\n // This conversion should be safe, as `Number.MAX_SAFE_INTEGER` microseconds\n // corresponds to about 258 years.\n const elapsedMicroseconds = Number(elapsedTime / BigInt(1000));\n\n const userCpuPercent = (100 * elapsedCpuUsage.user) / elapsedMicroseconds;\n const systemCpuPercent = (100 * elapsedCpuUsage.system) / elapsedMicroseconds;\n const totalCpuPercent = userCpuPercent + systemCpuPercent;\n\n cpuUsage = process.cpuUsage();\n time = process.hrtime.bigint();\n\n const metrics = [\n {\n MetricName: 'NodeEventLoopDelay',\n Unit: 'Milliseconds',\n Value: loopbenchInstance.delay,\n },\n {\n MetricName: 'NodeMemoryRss',\n Unit: 'Bytes',\n Value: memoryStats.rss,\n },\n {\n MetricName: 'NodeMemoryHeapTotal',\n Unit: 'Bytes',\n Value: memoryStats.heapTotal,\n },\n {\n MetricName: 'NodeMemoryHeapUsed',\n Unit: 'Bytes',\n Value: memoryStats.heapUsed,\n },\n {\n MetricName: 'NodeMemoryExternal',\n Unit: 'Bytes',\n Value: memoryStats.external,\n },\n {\n MetricName: 'NodeCpuUser',\n Unit: 'Percent',\n Value: userCpuPercent,\n },\n {\n MetricName: 'NodeCpuSystem',\n Unit: 'Percent',\n Value: systemCpuPercent,\n },\n {\n MetricName: 'NodeCpuTotal',\n Unit: 'Percent',\n Value: totalCpuPercent,\n },\n ] as const;\n\n // We must use a config passed in from outside this package.\n // eslint-disable-next-line @prairielearn/aws-client-shared-config\n const cloudwatch = new CloudWatch(options.awsConfig);\n await cloudwatch.putMetricData({\n Namespace: 'PrairieLearn',\n MetricData: metrics.map((m) => ({\n ...m,\n StorageResolution: 1,\n Timestamp: new Date(),\n Dimensions: options.dimensions,\n })),\n });\n } catch (err: any) {\n options.onError(err);\n }\n}\n\nasync function emitLoop({\n signal,\n ...options\n}: NodeMetricsOptions & {\n signal: AbortSignal;\n}) {\n try {\n while (!signal.aborted) {\n await setTimeout(options.intervalSeconds * 1000, null, { signal, ref: false });\n await emit(options);\n }\n } catch (err: any) {\n if (err.name === 'AbortError') return;\n throw err;\n }\n}\n\nexport function start(options: NodeMetricsOptions) {\n abortController = new AbortController();\n\n // Initialize these so that we can compute a valid delta on the first run of `emit()`.\n cpuUsage = process.cpuUsage();\n time = process.hrtime.bigint();\n\n (() => emitLoop({ signal: abortController.signal, ...options }))();\n}\n\nexport function stop() {\n abortController?.abort();\n}\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,O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AI,CAAC;QACH,OAAO,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;YACvB,MAAM,UAAU,CAAC,OAAO,CAAC,eAAe,GAAG,IAAI,EAAE,IAAI,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC,CAAC;YAC/E,MAAM,IAAI,CAAC,OAAO,CAAC,CAAC;QACtB,CAAC;IACH,CAAC;IAAC,OAAO,GAAQ,EAAE,CAAC;QAClB,IAAI,GAAG,CAAC,IAAI,KAAK,YAAY;YAAE,OAAO;QACtC,MAAM,GAAG,CAAC;IACZ,CAAC;AACH,CAAC;AAED,MAAM,UAAU,KAAK,CAAC,OAA2B;IAC/C,eAAe,GAAG,IAAI,eAAe,EAAE,CAAC;IAExC,sFAAsF;IACtF,QAAQ,GAAG,OAAO,CAAC,QAAQ,EAAE,CAAC;IAC9B,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;IAE/B,CAAC,GAAG,EAAE,CAAC,QAAQ,CAAC,EAAE,MAAM,EAAE,eAAe,CAAC,MAAM,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC,EAAE,CAAC;AACrE,CAAC;AAED,MAAM,UAAU,IAAI;IAClB,eAAe,EAAE,KAAK,EAAE,CAAC;AAC3B,CAAC","sourcesContent":["import { setTimeout } from 'node:timers/promises';\n\nimport {\n CloudWatch,\n type CloudWatchClientConfig,\n type Dimension,\n} from '@aws-sdk/client-cloudwatch';\nimport loopbench from 'loopbench';\n\nconst loopbenchInstance = loopbench();\n\nlet abortController: AbortController | null = null;\nlet cpuUsage = process.cpuUsage();\nlet time = process.hrtime.bigint();\n\ninterface NodeMetricsOptions {\n awsConfig: CloudWatchClientConfig;\n intervalSeconds: number;\n namespace: string;\n dimensions: Dimension[];\n onError: (err: Error) => void;\n}\n\nasync function emit(options: NodeMetricsOptions) {\n try {\n const memoryStats = process.memoryUsage();\n\n const elapsedCpuUsage = process.cpuUsage(cpuUsage);\n // This delta is in nanoseconds.\n const elapsedTime = process.hrtime.bigint() - time;\n // This conversion should be safe, as `Number.MAX_SAFE_INTEGER` microseconds\n // corresponds to about 258 years.\n const elapsedMicroseconds = Number(elapsedTime / BigInt(1000));\n\n const userCpuPercent = (100 * elapsedCpuUsage.user) / elapsedMicroseconds;\n const systemCpuPercent = (100 * elapsedCpuUsage.system) / elapsedMicroseconds;\n const totalCpuPercent = userCpuPercent + systemCpuPercent;\n\n cpuUsage = process.cpuUsage();\n time = process.hrtime.bigint();\n\n const metrics = [\n {\n MetricName: 'NodeEventLoopDelay',\n Unit: 'Milliseconds',\n Value: loopbenchInstance.delay,\n },\n {\n MetricName: 'NodeMemoryRss',\n Unit: 'Bytes',\n Value: memoryStats.rss,\n },\n {\n MetricName: 'NodeMemoryHeapTotal',\n Unit: 'Bytes',\n Value: memoryStats.heapTotal,\n },\n {\n MetricName: 'NodeMemoryHeapUsed',\n Unit: 'Bytes',\n Value: memoryStats.heapUsed,\n },\n {\n MetricName: 'NodeMemoryExternal',\n Unit: 'Bytes',\n Value: memoryStats.external,\n },\n {\n MetricName: 'NodeCpuUser',\n Unit: 'Percent',\n Value: userCpuPercent,\n },\n {\n MetricName: 'NodeCpuSystem',\n Unit: 'Percent',\n Value: systemCpuPercent,\n },\n {\n MetricName: 'NodeCpuTotal',\n Unit: 'Percent',\n Value: totalCpuPercent,\n },\n ] as const;\n\n // We must use a config passed in from outside this package.\n // eslint-disable-next-line @prairielearn/aws-client-shared-config\n const cloudwatch = new CloudWatch(options.awsConfig);\n await cloudwatch.putMetricData({\n Namespace: options.namespace,\n MetricData: metrics.map((m) => ({\n ...m,\n StorageResolution: 1,\n Timestamp: new Date(),\n Dimensions: options.dimensions,\n })),\n });\n } catch (err: any) {\n options.onError(err);\n }\n}\n\nasync function emitLoop({\n signal,\n ...options\n}: NodeMetricsOptions & {\n signal: AbortSignal;\n}) {\n try {\n while (!signal.aborted) {\n await setTimeout(options.intervalSeconds * 1000, null, { signal, ref: false });\n await emit(options);\n }\n } catch (err: any) {\n if (err.name === 'AbortError') return;\n throw err;\n }\n}\n\nexport function start(options: NodeMetricsOptions) {\n abortController = new AbortController();\n\n // Initialize these so that we can compute a valid delta on the first run of `emit()`.\n cpuUsage = process.cpuUsage();\n time = process.hrtime.bigint();\n\n (() => emitLoop({ signal: abortController.signal, ...options }))();\n}\n\nexport function stop() {\n abortController?.abort();\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prairielearn/node-metrics",
3
- "version": "1.0.2",
3
+ "version": "2.0.0",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -13,13 +13,13 @@
13
13
  "dev": "tsc --watch --preserveWatchOutput"
14
14
  },
15
15
  "dependencies": {
16
- "@aws-sdk/client-cloudwatch": "^3.777.0",
16
+ "@aws-sdk/client-cloudwatch": "^3.787.0",
17
17
  "loopbench": "^2.0.0"
18
18
  },
19
19
  "devDependencies": {
20
20
  "@prairielearn/tsconfig": "^0.0.0",
21
21
  "@types/loopbench": "^1.2.4",
22
22
  "@types/node": "^20.17.30",
23
- "typescript": "^5.8.2"
23
+ "typescript": "^5.8.3"
24
24
  }
25
25
  }
package/src/index.ts CHANGED
@@ -16,6 +16,7 @@ let time = process.hrtime.bigint();
16
16
  interface NodeMetricsOptions {
17
17
  awsConfig: CloudWatchClientConfig;
18
18
  intervalSeconds: number;
19
+ namespace: string;
19
20
  dimensions: Dimension[];
20
21
  onError: (err: Error) => void;
21
22
  }
@@ -85,7 +86,7 @@ async function emit(options: NodeMetricsOptions) {
85
86
  // eslint-disable-next-line @prairielearn/aws-client-shared-config
86
87
  const cloudwatch = new CloudWatch(options.awsConfig);
87
88
  await cloudwatch.putMetricData({
88
- Namespace: 'PrairieLearn',
89
+ Namespace: options.namespace,
89
90
  MetricData: metrics.map((m) => ({
90
91
  ...m,
91
92
  StorageResolution: 1,