@nestjs/observe 0.3.4 → 0.3.5
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 +32 -1
- package/dist/.tsbuildinfo +1 -1
- package/dist/instrument/create-instance-decorator.instrument.js +20 -4
- package/dist/interfaces/observe-options.interface.d.ts +5 -0
- package/dist/observe.module.js +11 -1
- package/dist/utils/fit-service-version.util.d.ts +16 -0
- package/dist/utils/fit-service-version.util.js +32 -0
- package/dist/utils/infer-service-version.util.js +9 -11
- package/package.json +1 -1
|
@@ -86,7 +86,7 @@ export function createInstanceDecorator(als, operationTraceRegistry, options) {
|
|
|
86
86
|
const instrumentFunction = (fn) => {
|
|
87
87
|
// Classes are functions too, but calling one is a construction, not the
|
|
88
88
|
// kind of call this decorator traces.
|
|
89
|
-
if (
|
|
89
|
+
if (isConstructor(fn) || options.skipInstrumentation(fn)) {
|
|
90
90
|
return fn;
|
|
91
91
|
}
|
|
92
92
|
const methodName = fn.name || "anonymous";
|
|
@@ -161,7 +161,10 @@ export function createInstanceDecorator(als, operationTraceRegistry, options) {
|
|
|
161
161
|
const attributeValue = Reflect.get(target, prop);
|
|
162
162
|
const isFunction = typeof attributeValue === "function" && prop !== "constructor";
|
|
163
163
|
const shouldProxy = isFunction &&
|
|
164
|
-
|
|
164
|
+
// A constructor held in a field - pg-pool's `this.Promise` - is
|
|
165
|
+
// invoked with `new` and read for statics (`this.Promise.reject`);
|
|
166
|
+
// the traced wrapper supports neither.
|
|
167
|
+
!isConstructor(attributeValue) &&
|
|
165
168
|
// A callable *object* - a Mongoose model, an Axios instance, an
|
|
166
169
|
// EventEmitter-backed client - is a field, not a method. The traced
|
|
167
170
|
// wrapper is a fresh bound function, so wrapping one silently drops
|
|
@@ -291,8 +294,21 @@ function relabelProxyFrame(err, className, methodName) {
|
|
|
291
294
|
function escapeRegExp(value) {
|
|
292
295
|
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
293
296
|
}
|
|
294
|
-
|
|
295
|
-
|
|
297
|
+
/**
|
|
298
|
+
* Whether a function is meant to be invoked with `new`: a `class`, or a
|
|
299
|
+
* constructor that predates the syntax - a built-in like `Promise`, or an
|
|
300
|
+
* ES5-style (or down-compiled) class - told apart by a `prototype` carrying
|
|
301
|
+
* members of its own. Methods have no `prototype` at all, and a plain
|
|
302
|
+
* `function` gets one holding only `constructor`.
|
|
303
|
+
*/
|
|
304
|
+
function isConstructor(value) {
|
|
305
|
+
if (/^\s*class\s+/.test(Function.prototype.toString.call(value))) {
|
|
306
|
+
return true;
|
|
307
|
+
}
|
|
308
|
+
const prototype = value.prototype;
|
|
309
|
+
return (typeof prototype === "object" &&
|
|
310
|
+
prototype !== null &&
|
|
311
|
+
Reflect.ownKeys(prototype).some((key) => key !== "constructor"));
|
|
296
312
|
}
|
|
297
313
|
/**
|
|
298
314
|
* The prototypes every ordinary function - plain, async, generator, async
|
|
@@ -178,6 +178,11 @@ export interface ObserveOptions {
|
|
|
178
178
|
* (`VERCEL_GIT_COMMIT_SHA`, `RENDER_GIT_COMMIT`, `GITHUB_SHA`, ...), else the
|
|
179
179
|
* commit checked out where the process runs, else a container platform's
|
|
180
180
|
* revision name. `false` reports no release at all.
|
|
181
|
+
*
|
|
182
|
+
* At most 50 characters, the longest the collector accepts. A longer one is
|
|
183
|
+
* cut to fit, with a warning when the application starts: a commit keeps its
|
|
184
|
+
* first 50, anything else its first 41 and a hash of the whole, so two
|
|
185
|
+
* releases that differ only past the cut stay apart.
|
|
181
186
|
*/
|
|
182
187
|
serviceVersion?: string | false;
|
|
183
188
|
/**
|
package/dist/observe.module.js
CHANGED
|
@@ -36,6 +36,7 @@ import { TraceSamplerService } from "./services/trace-sampler.service.js";
|
|
|
36
36
|
import { TracerService } from "./services/tracer.service.js";
|
|
37
37
|
import { assertModuleOptions } from "./utils/assert-module-options.util.js";
|
|
38
38
|
import { defaultTraceIdGenerator } from "./utils/default-trace-id-generator.util.js";
|
|
39
|
+
import { fitServiceVersion, MAX_SERVICE_VERSION_LENGTH, } from "./utils/fit-service-version.util.js";
|
|
39
40
|
import { inferServiceVersion } from "./utils/infer-service-version.util.js";
|
|
40
41
|
import { LogRedactor } from "./utils/log-redactor.js";
|
|
41
42
|
/**
|
|
@@ -49,9 +50,18 @@ const MISSING_ASYNC_OPTIONS_PROVIDER = 'ObserveModule.forRootAsync() requires on
|
|
|
49
50
|
* and fix verification work without anyone threading a version through the
|
|
50
51
|
* build. Resolved once, with the rest of the options: the answer cannot change
|
|
51
52
|
* while the process runs. `serviceVersion: false` opts out.
|
|
53
|
+
*
|
|
54
|
+
* A release the application names is cut to what the collector accepts, as
|
|
55
|
+
* an inferred one is - sent whole, it would cost every batch it rides in.
|
|
52
56
|
*/
|
|
53
57
|
function withServiceVersion(options) {
|
|
54
|
-
|
|
58
|
+
const named = options.serviceVersion;
|
|
59
|
+
if (typeof named === "string" && named.length > MAX_SERVICE_VERSION_LENGTH) {
|
|
60
|
+
const sent = fitServiceVersion(named);
|
|
61
|
+
new Logger("ObserveModule").warn(`serviceVersion "${named}" is sent as "${sent}": the collector takes at most ${MAX_SERVICE_VERSION_LENGTH} characters.`);
|
|
62
|
+
return { ...options, serviceVersion: sent };
|
|
63
|
+
}
|
|
64
|
+
if (named || named === false) {
|
|
55
65
|
return options;
|
|
56
66
|
}
|
|
57
67
|
const inferred = inferServiceVersion();
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The longest version the collector accepts. A longer one is not truncated
|
|
3
|
+
* there - the whole batch it rides in is refused - so a version is cut to fit
|
|
4
|
+
* before it is ever sent.
|
|
5
|
+
*/
|
|
6
|
+
export declare const MAX_SERVICE_VERSION_LENGTH = 50;
|
|
7
|
+
/**
|
|
8
|
+
* `version`, at most as long as the collector accepts.
|
|
9
|
+
*
|
|
10
|
+
* A commit keeps its head, which is what identifies it - and what git and the
|
|
11
|
+
* dashboard shorten it to. Anything else keeps its head too, but ends in a
|
|
12
|
+
* hash of the whole after a `~`: the part each deploy changes can sit past the
|
|
13
|
+
* cut - the tag at the end of an image reference, say - and cut there, every
|
|
14
|
+
* release would arrive as the same one.
|
|
15
|
+
*/
|
|
16
|
+
export declare function fitServiceVersion(version: string): string;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { createHash } from "crypto";
|
|
2
|
+
/**
|
|
3
|
+
* The longest version the collector accepts. A longer one is not truncated
|
|
4
|
+
* there - the whole batch it rides in is refused - so a version is cut to fit
|
|
5
|
+
* before it is ever sent.
|
|
6
|
+
*/
|
|
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
|
+
const HEX = /^[0-9a-f]+$/i;
|
|
11
|
+
/**
|
|
12
|
+
* `version`, at most as long as the collector accepts.
|
|
13
|
+
*
|
|
14
|
+
* A commit keeps its head, which is what identifies it - and what git and the
|
|
15
|
+
* dashboard shorten it to. Anything else keeps its head too, but ends in a
|
|
16
|
+
* hash of the whole after a `~`: the part each deploy changes can sit past the
|
|
17
|
+
* cut - the tag at the end of an image reference, say - and cut there, every
|
|
18
|
+
* release would arrive as the same one.
|
|
19
|
+
*/
|
|
20
|
+
export function fitServiceVersion(version) {
|
|
21
|
+
if (version.length <= MAX_SERVICE_VERSION_LENGTH) {
|
|
22
|
+
return version;
|
|
23
|
+
}
|
|
24
|
+
if (HEX.test(version)) {
|
|
25
|
+
return version.slice(0, MAX_SERVICE_VERSION_LENGTH);
|
|
26
|
+
}
|
|
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}`;
|
|
32
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { readFileSync, statSync } from "fs";
|
|
2
2
|
import { dirname, join, resolve } from "path";
|
|
3
|
+
import { fitServiceVersion, MAX_SERVICE_VERSION_LENGTH, } from "./fit-service-version.util.js";
|
|
3
4
|
/**
|
|
4
5
|
* Environment variables that name the running release, in the order they are
|
|
5
6
|
* trusted.
|
|
@@ -47,14 +48,6 @@ const REVISION_ENV_VARS = [
|
|
|
47
48
|
"K_REVISION", // Cloud Run
|
|
48
49
|
"CONTAINER_APP_REVISION", // Azure Container Apps
|
|
49
50
|
];
|
|
50
|
-
/**
|
|
51
|
-
* The longest version the collector accepts. A longer one is not truncated
|
|
52
|
-
* there - the whole batch it rides in is refused - so an inferred version is
|
|
53
|
-
* cut to fit before it is ever sent: from the end for a commit, whose head is
|
|
54
|
-
* what identifies it, and from the start for a revision, whose head is the
|
|
55
|
-
* service's name that every revision shares.
|
|
56
|
-
*/
|
|
57
|
-
const MAX_VERSION_LENGTH = 50;
|
|
58
51
|
/** A full commit id: SHA-1, or SHA-256 in a repository created with it. */
|
|
59
52
|
const COMMIT_ID = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/;
|
|
60
53
|
/**
|
|
@@ -69,7 +62,7 @@ export function inferServiceVersion(env = process.env, searchFrom = defaultSearc
|
|
|
69
62
|
for (const name of VERSION_ENV_VARS) {
|
|
70
63
|
const value = env[name]?.trim();
|
|
71
64
|
if (value) {
|
|
72
|
-
return { version: value
|
|
65
|
+
return { version: fitServiceVersion(value), source: name };
|
|
73
66
|
}
|
|
74
67
|
}
|
|
75
68
|
for (const directory of searchFrom) {
|
|
@@ -81,7 +74,12 @@ export function inferServiceVersion(env = process.env, searchFrom = defaultSearc
|
|
|
81
74
|
for (const name of REVISION_ENV_VARS) {
|
|
82
75
|
const value = env[name]?.trim();
|
|
83
76
|
if (value) {
|
|
84
|
-
|
|
77
|
+
// Cut from the start: a revision's head is the service's name, which
|
|
78
|
+
// every revision shares, and its end is the part each deploy changes.
|
|
79
|
+
return {
|
|
80
|
+
version: value.slice(-MAX_SERVICE_VERSION_LENGTH),
|
|
81
|
+
source: name,
|
|
82
|
+
};
|
|
85
83
|
}
|
|
86
84
|
}
|
|
87
85
|
return undefined;
|
|
@@ -110,7 +108,7 @@ function readCheckoutHead(start) {
|
|
|
110
108
|
if (gitDir) {
|
|
111
109
|
const commit = readHeadCommit(gitDir);
|
|
112
110
|
return commit
|
|
113
|
-
? { version: commit
|
|
111
|
+
? { version: fitServiceVersion(commit), source: gitDir }
|
|
114
112
|
: undefined;
|
|
115
113
|
}
|
|
116
114
|
const parent = dirname(directory);
|
package/package.json
CHANGED