@nestjs/observe 0.3.5 → 0.3.6

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.
Files changed (30) hide show
  1. package/README.md +66 -4
  2. package/dist/agent/observe-agent.shared-buffer.js +7 -3
  3. package/dist/agent/telemetry-wire-contract.js +30 -12
  4. package/dist/custom-metrics/counter.d.ts +7 -3
  5. package/dist/custom-metrics/counter.js +34 -7
  6. package/dist/custom-metrics/finite-value.util.d.ts +20 -0
  7. package/dist/custom-metrics/finite-value.util.js +29 -0
  8. package/dist/custom-metrics/gauge.d.ts +2 -0
  9. package/dist/custom-metrics/gauge.js +31 -7
  10. package/dist/custom-metrics/summary.d.ts +5 -0
  11. package/dist/custom-metrics/summary.js +10 -3
  12. package/dist/encoders/custom-metrics.encoder.js +63 -1
  13. package/dist/encoders/trace.encoder.js +34 -0
  14. package/dist/interfaces/custom-metric.interface.d.ts +5 -0
  15. package/dist/interfaces/observe-options.interface.d.ts +23 -14
  16. package/dist/observe.module.js +25 -6
  17. package/dist/protocols/http-observe-agent.service.d.ts +13 -0
  18. package/dist/protocols/http-observe-agent.service.js +34 -8
  19. package/dist/protocols/rpc-observe-agent.service.js +0 -2
  20. package/dist/protocols/schedule-observe-agent.service.js +7 -3
  21. package/dist/services/node-runtime-metrics.service.d.ts +12 -0
  22. package/dist/services/node-runtime-metrics.service.js +95 -41
  23. package/dist/services/operation-trace.registry.js +0 -6
  24. package/dist/services/stdout-forwarder.service.js +2 -2
  25. package/dist/services/tracer.service.js +0 -3
  26. package/dist/utils/fit-service-version.util.js +2 -8
  27. package/dist/utils/fit-to-length.util.d.ts +10 -0
  28. package/dist/utils/fit-to-length.util.js +44 -0
  29. package/package.json +5 -3
  30. package/dist/.tsbuildinfo +0 -1
@@ -40,7 +40,6 @@ let TracerService = class TracerService {
40
40
  * The callback can be synchronous or asynchronous.
41
41
  */
42
42
  async createSpan(name, callback) {
43
- // await new Promise(resolve => setImmediate(resolve));
44
43
  const store = this.als.getStore();
45
44
  if (!store) {
46
45
  throw new Error('AsyncLocalStorage is not initialized. Ensure that you are using the "createSpan" method within an async context.');
@@ -58,7 +57,6 @@ let TracerService = class TracerService {
58
57
  * @returns {Promise<TraceSpanDelegate>} A promise that resolves to the active span delegate.
59
58
  */
60
59
  async activeSpan() {
61
- // await new Promise((resolve) => setImmediate(resolve));
62
60
  const store = this.als.getStore();
63
61
  if (!store) {
64
62
  throw new Error('AsyncLocalStorage is not initialized. Ensure that you are using the "activeSpan" method within an async context.');
@@ -93,7 +91,6 @@ let TracerService = class TracerService {
93
91
  * This method should be called within an async context where the AsyncLocalStorage is active.
94
92
  */
95
93
  async captureError(error, tags) {
96
- // await new Promise((resolve) => setImmediate(resolve));
97
94
  const store = this.als.getStore();
98
95
  if (!store) {
99
96
  throw new Error('AsyncLocalStorage is not initialized. Ensure that you are using the "captureError" method within an async context.');
@@ -1,12 +1,10 @@
1
- import { createHash } from "crypto";
1
+ import { fitToLength } from "./fit-to-length.util.js";
2
2
  /**
3
3
  * The longest version the collector accepts. A longer one is not truncated
4
4
  * there - the whole batch it rides in is refused - so a version is cut to fit
5
5
  * before it is ever sent.
6
6
  */
7
7
  export const MAX_SERVICE_VERSION_LENGTH = 50;
8
- /** How much of a cut version is a hash of the whole. */
9
- const HASH_LENGTH = 8;
10
8
  const HEX = /^[0-9a-f]+$/i;
11
9
  /**
12
10
  * `version`, at most as long as the collector accepts.
@@ -24,9 +22,5 @@ export function fitServiceVersion(version) {
24
22
  if (HEX.test(version)) {
25
23
  return version.slice(0, MAX_SERVICE_VERSION_LENGTH);
26
24
  }
27
- const hash = createHash("sha256")
28
- .update(version)
29
- .digest("hex")
30
- .slice(0, HASH_LENGTH);
31
- return `${version.slice(0, MAX_SERVICE_VERSION_LENGTH - HASH_LENGTH - 1)}~${hash}`;
25
+ return fitToLength(version, MAX_SERVICE_VERSION_LENGTH);
32
26
  }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * `value`, at most `maxLength` characters long, for a field the collector
3
+ * bounds. The collector does not cut a longer value down: it refuses the whole
4
+ * batch the value rides in, logs and spans included.
5
+ *
6
+ * Keeps the head, which is what a reader recognises, and ends in a hash of the
7
+ * whole after a `~`, so two values that differ only past the cut stay apart.
8
+ * Never splits a surrogate pair, which would send half a character.
9
+ */
10
+ export declare function fitToLength(value: string, maxLength: number): string;
@@ -0,0 +1,44 @@
1
+ import { createHash } from "crypto";
2
+ /** How much of a cut value is a hash of the whole. */
3
+ const HASH_LENGTH = 8;
4
+ /**
5
+ * Values already cut, so one that keeps arriving - a metric's name, encoded on
6
+ * every increment - is hashed once. Cleared rather than evicted when full: it
7
+ * only ever holds values longer than a limit, which are rare.
8
+ */
9
+ const fitted = new Map();
10
+ const MAX_REMEMBERED = 1_000;
11
+ /**
12
+ * `value`, at most `maxLength` characters long, for a field the collector
13
+ * bounds. The collector does not cut a longer value down: it refuses the whole
14
+ * batch the value rides in, logs and spans included.
15
+ *
16
+ * Keeps the head, which is what a reader recognises, and ends in a hash of the
17
+ * whole after a `~`, so two values that differ only past the cut stay apart.
18
+ * Never splits a surrogate pair, which would send half a character.
19
+ */
20
+ export function fitToLength(value, maxLength) {
21
+ if (value.length <= maxLength) {
22
+ return value;
23
+ }
24
+ const key = `${maxLength}:${value}`;
25
+ const remembered = fitted.get(key);
26
+ if (remembered !== undefined) {
27
+ return remembered;
28
+ }
29
+ let head = maxLength - HASH_LENGTH - 1;
30
+ const last = value.charCodeAt(head - 1);
31
+ if (last >= 0xd800 && last <= 0xdbff) {
32
+ head -= 1;
33
+ }
34
+ const hash = createHash("sha256")
35
+ .update(value)
36
+ .digest("hex")
37
+ .slice(0, HASH_LENGTH);
38
+ const cut = `${value.slice(0, head)}~${hash}`;
39
+ if (fitted.size >= MAX_REMEMBERED) {
40
+ fitted.clear();
41
+ }
42
+ fitted.set(key, cut);
43
+ return cut;
44
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nestjs/observe",
3
- "version": "0.3.5",
4
- "description": "Observability and APM agent for NestJS - traces, runtime metrics, custom metrics and CPU profiles",
3
+ "version": "0.3.6",
4
+ "description": "Observability and APM agent for NestJS - traces, runtime metrics and custom metrics",
5
5
  "author": "Kamil Mysliwiec",
6
6
  "homepage": "https://observe.nestjs.com",
7
7
  "repository": {
@@ -38,7 +38,8 @@
38
38
  "./package.json": "./package.json"
39
39
  },
40
40
  "files": [
41
- "dist"
41
+ "dist",
42
+ "!dist/**/*.tsbuildinfo"
42
43
  ],
43
44
  "publishConfig": {
44
45
  "access": "public"
@@ -52,6 +53,7 @@
52
53
  "format": "prettier --write \"src/**/*.ts\"",
53
54
  "lint": "oxlint --type-aware src",
54
55
  "lint:fix": "oxlint --type-aware --fix src",
56
+ "typecheck": "tsc -p tsconfig.json --noEmit --incremental false",
55
57
  "test": "vitest run",
56
58
  "test:watch": "vitest",
57
59
  "test:cov": "vitest run --coverage",