@nestjs/observe 0.3.20 → 0.3.22
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/LICENSE +105 -22
- package/NOTICE +31 -0
- package/README.md +138 -21
- package/dist/agent/degraded-ingest.protocol.d.ts +20 -0
- package/dist/agent/degraded-ingest.protocol.js +25 -0
- package/dist/agent/detached-observe-worker.assembly.js +2 -1
- package/dist/agent/detached-observe-worker.d.ts +2 -0
- package/dist/agent/detached-observe-worker.js +7 -0
- package/dist/agent/observe-agent.shared-buffer.d.ts +36 -0
- package/dist/agent/observe-agent.shared-buffer.js +56 -1
- package/dist/agent/observe-agent.worker.d.ts +12 -0
- package/dist/agent/observe-agent.worker.js +63 -8
- package/dist/instrument/create-instance-decorator.instrument.js +99 -86
- package/dist/interfaces/observe-options.interface.d.ts +42 -3
- package/dist/interfaces/trace-events.interfaces.d.ts +11 -5
- package/dist/observe.constants.d.ts +15 -5
- package/dist/observe.constants.js +15 -5
- package/dist/observe.module.js +3 -0
- package/dist/outgoing/http.integration.d.ts +34 -3
- package/dist/outgoing/http.integration.js +84 -17
- package/dist/outgoing/outgoing-observe-agent.service.d.ts +14 -1
- package/dist/outgoing/outgoing-observe-agent.service.js +24 -9
- package/dist/outgoing/outgoing-span.recorder.d.ts +11 -1
- package/dist/outgoing/outgoing-span.recorder.js +8 -2
- package/dist/protocols/deferred-trace-completion.d.ts +13 -2
- package/dist/protocols/deferred-trace-completion.js +27 -4
- package/dist/protocols/graphql-observe-agent.service.js +1 -1
- package/dist/protocols/http-observe-agent.service.js +45 -30
- package/dist/protocols/job-trace-runner.js +2 -2
- package/dist/protocols/rpc-observe-agent.service.js +19 -10
- package/dist/protocols/ws-observe-agent.service.js +1 -1
- package/dist/services/collapse-repeated-spans.util.js +41 -37
- package/dist/services/operation-trace.registry.d.ts +32 -11
- package/dist/services/operation-trace.registry.js +102 -33
- package/dist/services/tracer.service.d.ts +6 -0
- package/dist/services/tracer.service.js +11 -2
- package/dist/utils/default-trace-id-generator.util.d.ts +18 -0
- package/dist/utils/default-trace-id-generator.util.js +33 -0
- package/dist/utils/opentelemetry.util.d.ts +31 -0
- package/dist/utils/opentelemetry.util.js +52 -0
- package/dist/utils/trace-context.util.d.ts +91 -0
- package/dist/utils/trace-context.util.js +219 -0
- package/dist/utils/trace-id-generator-origin.util.d.ts +11 -0
- package/dist/utils/trace-id-generator-origin.util.js +26 -0
- package/dist/utils/uuid-v7.util.js +53 -15
- package/package.json +18 -4
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A span's identity as `@opentelemetry/api` hands it out - the slice of
|
|
3
|
+
* `SpanContext` the agent reads.
|
|
4
|
+
*/
|
|
5
|
+
export interface OpenTelemetrySpanContext {
|
|
6
|
+
traceId: string;
|
|
7
|
+
spanId: string;
|
|
8
|
+
traceFlags: number;
|
|
9
|
+
traceState?: {
|
|
10
|
+
serialize(): string;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The span an OpenTelemetry SDK in this process has open in the current
|
|
15
|
+
* context - for an incoming request, the server span its HTTP instrumentation
|
|
16
|
+
* opened - or `undefined` when none runs.
|
|
17
|
+
*
|
|
18
|
+
* Read so that a service running both reports to Observe under the trace id
|
|
19
|
+
* the SDK gave the request, including when the request started the trace.
|
|
20
|
+
* Never throws: an SDK this cannot read is treated as absent.
|
|
21
|
+
*/
|
|
22
|
+
export declare function activeOpenTelemetrySpanContext(): OpenTelemetrySpanContext | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* Whether an OpenTelemetry SDK in this process propagates W3C Trace Context,
|
|
25
|
+
* and so writes `traceparent` on the outbound calls it instruments.
|
|
26
|
+
*
|
|
27
|
+
* The agent then leaves the header to it, whichever of the two sees a call
|
|
28
|
+
* first: two `traceparent` headers on one request are not a valid one, and
|
|
29
|
+
* the SDK's names the span the rest of its trace nests under.
|
|
30
|
+
*/
|
|
31
|
+
export declare function openTelemetryWritesTraceparent(): boolean;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { loadOptionalPeer } from "./optional-peer.util.js";
|
|
2
|
+
/** `undefined` until first asked for, `null` when the package is absent. */
|
|
3
|
+
let api;
|
|
4
|
+
/**
|
|
5
|
+
* `@opentelemetry/api`, as the application installed it, or `null`.
|
|
6
|
+
*
|
|
7
|
+
* Any copy will do: every copy of the package keeps its state on one global,
|
|
8
|
+
* so the one resolved from here sees the SDK the application registered
|
|
9
|
+
* through its own. With the package installed and no SDK registered, it
|
|
10
|
+
* answers as a no-op - no active span, no propagated fields - which is the
|
|
11
|
+
* agent's own behaviour anyway.
|
|
12
|
+
*/
|
|
13
|
+
function openTelemetryApi() {
|
|
14
|
+
if (api === undefined) {
|
|
15
|
+
const loaded = loadOptionalPeer("@opentelemetry/api");
|
|
16
|
+
api = (loaded.installed && loaded.module) || null;
|
|
17
|
+
}
|
|
18
|
+
return api;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The span an OpenTelemetry SDK in this process has open in the current
|
|
22
|
+
* context - for an incoming request, the server span its HTTP instrumentation
|
|
23
|
+
* opened - or `undefined` when none runs.
|
|
24
|
+
*
|
|
25
|
+
* Read so that a service running both reports to Observe under the trace id
|
|
26
|
+
* the SDK gave the request, including when the request started the trace.
|
|
27
|
+
* Never throws: an SDK this cannot read is treated as absent.
|
|
28
|
+
*/
|
|
29
|
+
export function activeOpenTelemetrySpanContext() {
|
|
30
|
+
try {
|
|
31
|
+
return openTelemetryApi()?.trace.getActiveSpan()?.spanContext();
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Whether an OpenTelemetry SDK in this process propagates W3C Trace Context,
|
|
39
|
+
* and so writes `traceparent` on the outbound calls it instruments.
|
|
40
|
+
*
|
|
41
|
+
* The agent then leaves the header to it, whichever of the two sees a call
|
|
42
|
+
* first: two `traceparent` headers on one request are not a valid one, and
|
|
43
|
+
* the SDK's names the span the rest of its trace nests under.
|
|
44
|
+
*/
|
|
45
|
+
export function openTelemetryWritesTraceparent() {
|
|
46
|
+
try {
|
|
47
|
+
return (openTelemetryApi()?.propagation.fields().includes("traceparent") ?? false);
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* W3C Trace Context (https://www.w3.org/TR/trace-context/): the `traceparent`
|
|
3
|
+
* and `tracestate` headers every OpenTelemetry SDK, most proxies and meshes,
|
|
4
|
+
* and the major clouds' load balancers read and write.
|
|
5
|
+
*
|
|
6
|
+
* Observe's own propagation is `x-request-id`, which only another service
|
|
7
|
+
* running this agent understands. Speaking `traceparent` as well is what
|
|
8
|
+
* lets a call through a Go, Python or Java service - or an OpenTelemetry
|
|
9
|
+
* backend reading what `otlp` exports - stay one trace.
|
|
10
|
+
*/
|
|
11
|
+
export interface TraceContext {
|
|
12
|
+
/** 32 lowercase hex digits, never all zeros. */
|
|
13
|
+
traceId: string;
|
|
14
|
+
/**
|
|
15
|
+
* The caller's span, 16 lowercase hex digits: the parent of the span this
|
|
16
|
+
* process opens for the operation.
|
|
17
|
+
*/
|
|
18
|
+
parentSpanId: string;
|
|
19
|
+
/** The caller's `sampled` flag: whether it may be recording the trace. */
|
|
20
|
+
sampled: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* `tracestate`, as received with it. Vendors keep their own entries there,
|
|
23
|
+
* and the spec asks every hop to pass the list on.
|
|
24
|
+
*/
|
|
25
|
+
traceState?: string;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The trace context a request arrived with, or `undefined` when it carried
|
|
29
|
+
* no `traceparent` the spec would accept.
|
|
30
|
+
*
|
|
31
|
+
* Headers are client-supplied, so anything malformed is ignored rather than
|
|
32
|
+
* repaired - an array (a repeated header), the wrong case, a zero id. The
|
|
33
|
+
* spec's answer to an invalid `traceparent` is to start a new trace, and its
|
|
34
|
+
* `tracestate` goes with it.
|
|
35
|
+
*/
|
|
36
|
+
export declare function readTraceContext(headers: Record<string, unknown> | undefined): TraceContext | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* The trace id a `traceparent` header carries, written as a UUID - the
|
|
39
|
+
* shape of every id this agent mints - or `undefined` when it carries none
|
|
40
|
+
* the spec would accept.
|
|
41
|
+
*
|
|
42
|
+
* Written that way rather than as 32 bare digits so the id reads the same
|
|
43
|
+
* in the dashboard, in logs and in `currentTraceId()` whichever side minted
|
|
44
|
+
* it, and so `w3cTraceIdOf` turns it back into exactly the trace id the
|
|
45
|
+
* caller sent.
|
|
46
|
+
*/
|
|
47
|
+
export declare function traceIdFromTraceparent(headers: Record<string, unknown> | undefined): string | undefined;
|
|
48
|
+
/**
|
|
49
|
+
* The W3C trace context an operation runs in: the span an OpenTelemetry SDK
|
|
50
|
+
* in this process opened for it, when one runs, else the `traceparent` it
|
|
51
|
+
* arrived with.
|
|
52
|
+
*
|
|
53
|
+
* The SDK's span first, because the SDK has already decided the trace: it
|
|
54
|
+
* continued the caller's, through whichever propagator it was given, or
|
|
55
|
+
* started one when there was nothing to continue. Taking that decision is
|
|
56
|
+
* what makes a service running both report one trace id to both, the
|
|
57
|
+
* service at the root of a trace included.
|
|
58
|
+
*/
|
|
59
|
+
export declare function inboundTraceContext(headers: Record<string, unknown> | undefined): TraceContext | undefined;
|
|
60
|
+
/**
|
|
61
|
+
* `inboundTraceContext`'s trace id, written as a UUID - as
|
|
62
|
+
* `traceIdFromTraceparent` writes it.
|
|
63
|
+
*/
|
|
64
|
+
export declare function inboundTraceId(headers: Record<string, unknown> | undefined): string | undefined;
|
|
65
|
+
/**
|
|
66
|
+
* The W3C trace id an Observe trace id goes out under.
|
|
67
|
+
*
|
|
68
|
+
* Every id the agent mints is a UUID, which is a 128-bit W3C trace id with
|
|
69
|
+
* dashes in it, so those - and any 32-digit hex id - map onto one by
|
|
70
|
+
* dropping the dashes. An adopted `x-request-id` can be anything at all
|
|
71
|
+
* (`req_8f2c`), and becomes the first 128 bits of its SHA-256 instead: the
|
|
72
|
+
* same id always gives the same trace id, so every hop that derives it -
|
|
73
|
+
* this process's outbound calls, another service running this agent, the
|
|
74
|
+
* spans `otlp` exports - lands in one trace.
|
|
75
|
+
*/
|
|
76
|
+
export declare function w3cTraceIdOf(traceId: string): string;
|
|
77
|
+
/**
|
|
78
|
+
* The W3C span id an Observe span id goes out under: the low 64 bits of its
|
|
79
|
+
* UUID.
|
|
80
|
+
*
|
|
81
|
+
* The low half rather than the high one because in a UUIDv7 the high half
|
|
82
|
+
* is mostly the mint time, which every span opened in the same millisecond
|
|
83
|
+
* shares; the low half is 62 random bits under the variant, which also keeps
|
|
84
|
+
* it from ever being all zeros. Anything that is not a UUID is hashed, as
|
|
85
|
+
* `w3cTraceIdOf` does.
|
|
86
|
+
*/
|
|
87
|
+
export declare function w3cSpanIdOf(spanId: string): string;
|
|
88
|
+
/** A fresh W3C span id, for a span that has no Observe id to derive one from. */
|
|
89
|
+
export declare function randomSpanId(): string;
|
|
90
|
+
/** A version-00 `traceparent`. Only the `sampled` flag is ever set. */
|
|
91
|
+
export declare function formatTraceparent(traceId: string, spanId: string, sampled: boolean): string;
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { createHash, randomBytes } from "crypto";
|
|
2
|
+
import { activeOpenTelemetrySpanContext, } from "./opentelemetry.util.js";
|
|
3
|
+
/**
|
|
4
|
+
* `version-traceid-parentid-flags`, as the spec defines it: lowercase hex
|
|
5
|
+
* only, a trace id and a parent id that are not all zeros, and any version
|
|
6
|
+
* but the reserved `ff`. Version 00 ends after the flags; a later version may
|
|
7
|
+
* carry more after another dash, which is read past rather than refused, so
|
|
8
|
+
* a caller on a newer revision of the spec is still followed.
|
|
9
|
+
*/
|
|
10
|
+
const TRACEPARENT_PATTERN = /^[ \t]*((?!ff)[0-9a-f]{2})-((?![0]{32})[0-9a-f]{32})-((?![0]{16})[0-9a-f]{16})-([0-9a-f]{2})(-[^ \t]*)?[ \t]*$/;
|
|
11
|
+
/**
|
|
12
|
+
* The longest `tracestate` passed on. The spec asks every vendor to carry at
|
|
13
|
+
* least 512 characters of it and lets a longer one lose entries from its
|
|
14
|
+
* end; a header is not the place for an unbounded value either way.
|
|
15
|
+
*/
|
|
16
|
+
const MAX_TRACE_STATE_LENGTH = 512;
|
|
17
|
+
/** Printable ASCII: no control characters, nothing a header cannot carry. */
|
|
18
|
+
const TRACE_STATE_PATTERN = /^[\x20-\x7e]+$/;
|
|
19
|
+
/** What a span context's ids must be: lowercase hex, never all zeros. */
|
|
20
|
+
const SPAN_CONTEXT_TRACE_ID_PATTERN = /^(?![0]{32})[0-9a-f]{32}$/;
|
|
21
|
+
const SPAN_CONTEXT_SPAN_ID_PATTERN = /^(?![0]{16})[0-9a-f]{16}$/;
|
|
22
|
+
const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
23
|
+
const HEX_TRACE_ID_PATTERN = /^[0-9a-f]{32}$/i;
|
|
24
|
+
const ZERO_TRACE_ID = "0".repeat(32);
|
|
25
|
+
const ZERO_SPAN_ID = "0".repeat(16);
|
|
26
|
+
/**
|
|
27
|
+
* The trace context a request arrived with, or `undefined` when it carried
|
|
28
|
+
* no `traceparent` the spec would accept.
|
|
29
|
+
*
|
|
30
|
+
* Headers are client-supplied, so anything malformed is ignored rather than
|
|
31
|
+
* repaired - an array (a repeated header), the wrong case, a zero id. The
|
|
32
|
+
* spec's answer to an invalid `traceparent` is to start a new trace, and its
|
|
33
|
+
* `tracestate` goes with it.
|
|
34
|
+
*/
|
|
35
|
+
export function readTraceContext(headers) {
|
|
36
|
+
const traceparent = headers?.["traceparent"];
|
|
37
|
+
if (typeof traceparent !== "string") {
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
const match = TRACEPARENT_PATTERN.exec(traceparent);
|
|
41
|
+
if (!match) {
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
const [, version, traceId, parentSpanId, flags, rest] = match;
|
|
45
|
+
// Version 00 has exactly four fields, so anything after them means the
|
|
46
|
+
// header is not what it claims to be - two headers folded into one, say.
|
|
47
|
+
if (version === "00" && rest !== undefined) {
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
const context = {
|
|
51
|
+
traceId,
|
|
52
|
+
parentSpanId,
|
|
53
|
+
sampled: (parseInt(flags, 16) & 0x01) === 0x01,
|
|
54
|
+
};
|
|
55
|
+
const traceState = fitTraceState(headers?.["tracestate"]);
|
|
56
|
+
if (traceState) {
|
|
57
|
+
context.traceState = traceState;
|
|
58
|
+
}
|
|
59
|
+
return context;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The received `tracestate`, held to what is safe to send on: printable,
|
|
63
|
+
* and at most `MAX_TRACE_STATE_LENGTH` characters - a longer one loses
|
|
64
|
+
* whole entries from its end, which the spec allows, never half of one.
|
|
65
|
+
* Node joins a repeated header with ", ", which is the list the spec means.
|
|
66
|
+
*/
|
|
67
|
+
function fitTraceState(value) {
|
|
68
|
+
if (typeof value !== "string") {
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
const trimmed = value.trim();
|
|
72
|
+
if (!trimmed || !TRACE_STATE_PATTERN.test(trimmed)) {
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
if (trimmed.length <= MAX_TRACE_STATE_LENGTH) {
|
|
76
|
+
return trimmed;
|
|
77
|
+
}
|
|
78
|
+
let fitted = "";
|
|
79
|
+
for (const entry of trimmed.split(",")) {
|
|
80
|
+
const member = entry.trim();
|
|
81
|
+
if (!member) {
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
const next = fitted ? `${fitted},${member}` : member;
|
|
85
|
+
if (next.length > MAX_TRACE_STATE_LENGTH) {
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
fitted = next;
|
|
89
|
+
}
|
|
90
|
+
return fitted || undefined;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* The trace id a `traceparent` header carries, written as a UUID - the
|
|
94
|
+
* shape of every id this agent mints - or `undefined` when it carries none
|
|
95
|
+
* the spec would accept.
|
|
96
|
+
*
|
|
97
|
+
* Written that way rather than as 32 bare digits so the id reads the same
|
|
98
|
+
* in the dashboard, in logs and in `currentTraceId()` whichever side minted
|
|
99
|
+
* it, and so `w3cTraceIdOf` turns it back into exactly the trace id the
|
|
100
|
+
* caller sent.
|
|
101
|
+
*/
|
|
102
|
+
export function traceIdFromTraceparent(headers) {
|
|
103
|
+
const traceId = readTraceContext(headers)?.traceId;
|
|
104
|
+
return traceId === undefined ? undefined : toUuid(traceId);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* The W3C trace context an operation runs in: the span an OpenTelemetry SDK
|
|
108
|
+
* in this process opened for it, when one runs, else the `traceparent` it
|
|
109
|
+
* arrived with.
|
|
110
|
+
*
|
|
111
|
+
* The SDK's span first, because the SDK has already decided the trace: it
|
|
112
|
+
* continued the caller's, through whichever propagator it was given, or
|
|
113
|
+
* started one when there was nothing to continue. Taking that decision is
|
|
114
|
+
* what makes a service running both report one trace id to both, the
|
|
115
|
+
* service at the root of a trace included.
|
|
116
|
+
*/
|
|
117
|
+
export function inboundTraceContext(headers) {
|
|
118
|
+
return (traceContextOfSpan(activeOpenTelemetrySpanContext()) ??
|
|
119
|
+
readTraceContext(headers));
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* `inboundTraceContext`'s trace id, written as a UUID - as
|
|
123
|
+
* `traceIdFromTraceparent` writes it.
|
|
124
|
+
*/
|
|
125
|
+
export function inboundTraceId(headers) {
|
|
126
|
+
const traceId = inboundTraceContext(headers)?.traceId;
|
|
127
|
+
return traceId === undefined ? undefined : toUuid(traceId);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* An OpenTelemetry span context as the trace context an operation runs in,
|
|
131
|
+
* the span becoming the operation's parent - or `undefined` for one the spec
|
|
132
|
+
* would not accept, such as the all-zero context of a span never started.
|
|
133
|
+
*/
|
|
134
|
+
function traceContextOfSpan(spanContext) {
|
|
135
|
+
if (!spanContext ||
|
|
136
|
+
typeof spanContext.traceId !== "string" ||
|
|
137
|
+
typeof spanContext.spanId !== "string" ||
|
|
138
|
+
!SPAN_CONTEXT_TRACE_ID_PATTERN.test(spanContext.traceId) ||
|
|
139
|
+
!SPAN_CONTEXT_SPAN_ID_PATTERN.test(spanContext.spanId)) {
|
|
140
|
+
return undefined;
|
|
141
|
+
}
|
|
142
|
+
const context = {
|
|
143
|
+
traceId: spanContext.traceId,
|
|
144
|
+
parentSpanId: spanContext.spanId,
|
|
145
|
+
sampled: (spanContext.traceFlags & 0x01) === 0x01,
|
|
146
|
+
};
|
|
147
|
+
let traceState;
|
|
148
|
+
try {
|
|
149
|
+
traceState = fitTraceState(spanContext.traceState?.serialize());
|
|
150
|
+
}
|
|
151
|
+
catch {
|
|
152
|
+
// A trace state that cannot be written out is no trace state.
|
|
153
|
+
}
|
|
154
|
+
if (traceState) {
|
|
155
|
+
context.traceState = traceState;
|
|
156
|
+
}
|
|
157
|
+
return context;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* The W3C trace id an Observe trace id goes out under.
|
|
161
|
+
*
|
|
162
|
+
* Every id the agent mints is a UUID, which is a 128-bit W3C trace id with
|
|
163
|
+
* dashes in it, so those - and any 32-digit hex id - map onto one by
|
|
164
|
+
* dropping the dashes. An adopted `x-request-id` can be anything at all
|
|
165
|
+
* (`req_8f2c`), and becomes the first 128 bits of its SHA-256 instead: the
|
|
166
|
+
* same id always gives the same trace id, so every hop that derives it -
|
|
167
|
+
* this process's outbound calls, another service running this agent, the
|
|
168
|
+
* spans `otlp` exports - lands in one trace.
|
|
169
|
+
*/
|
|
170
|
+
export function w3cTraceIdOf(traceId) {
|
|
171
|
+
let hex;
|
|
172
|
+
if (UUID_PATTERN.test(traceId)) {
|
|
173
|
+
hex = traceId.replace(/-/g, "").toLowerCase();
|
|
174
|
+
}
|
|
175
|
+
else if (HEX_TRACE_ID_PATTERN.test(traceId)) {
|
|
176
|
+
hex = traceId.toLowerCase();
|
|
177
|
+
}
|
|
178
|
+
// All zeros is the one value the spec forbids - the nil UUID, sent as an
|
|
179
|
+
// `x-request-id` - so it is hashed like any other id that does not fit.
|
|
180
|
+
if (hex === undefined || hex === ZERO_TRACE_ID) {
|
|
181
|
+
hex = createHash("sha256").update(traceId).digest("hex").slice(0, 32);
|
|
182
|
+
}
|
|
183
|
+
return hex;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* The W3C span id an Observe span id goes out under: the low 64 bits of its
|
|
187
|
+
* UUID.
|
|
188
|
+
*
|
|
189
|
+
* The low half rather than the high one because in a UUIDv7 the high half
|
|
190
|
+
* is mostly the mint time, which every span opened in the same millisecond
|
|
191
|
+
* shares; the low half is 62 random bits under the variant, which also keeps
|
|
192
|
+
* it from ever being all zeros. Anything that is not a UUID is hashed, as
|
|
193
|
+
* `w3cTraceIdOf` does.
|
|
194
|
+
*/
|
|
195
|
+
export function w3cSpanIdOf(spanId) {
|
|
196
|
+
if (UUID_PATTERN.test(spanId)) {
|
|
197
|
+
const low = spanId.replace(/-/g, "").slice(16).toLowerCase();
|
|
198
|
+
if (low !== ZERO_SPAN_ID) {
|
|
199
|
+
return low;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return createHash("sha256").update(spanId).digest("hex").slice(0, 16);
|
|
203
|
+
}
|
|
204
|
+
/** A fresh W3C span id, for a span that has no Observe id to derive one from. */
|
|
205
|
+
export function randomSpanId() {
|
|
206
|
+
for (;;) {
|
|
207
|
+
const id = randomBytes(8).toString("hex");
|
|
208
|
+
if (id !== ZERO_SPAN_ID) {
|
|
209
|
+
return id;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
/** A version-00 `traceparent`. Only the `sampled` flag is ever set. */
|
|
214
|
+
export function formatTraceparent(traceId, spanId, sampled) {
|
|
215
|
+
return `00-${traceId}-${spanId}-${sampled ? "01" : "00"}`;
|
|
216
|
+
}
|
|
217
|
+
function toUuid(hex) {
|
|
218
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
219
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** Records whether `options` holds the default generator - see above. */
|
|
2
|
+
export declare function recordTraceIdGeneratorOrigin(options: {
|
|
3
|
+
traceIdGenerator?: (req: unknown) => string;
|
|
4
|
+
}): void;
|
|
5
|
+
/**
|
|
6
|
+
* Whether the options hold the default generator, which `openTelemetry`
|
|
7
|
+
* swaps for `openTelemetryTraceIdGenerator` while interop runs. Options
|
|
8
|
+
* `createObserveModule` never saw read as a generator of the application's
|
|
9
|
+
* own, which is left alone.
|
|
10
|
+
*/
|
|
11
|
+
export declare function usesDefaultTraceIdGenerator(options: object): boolean;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { defaultTraceIdGenerator } from "./default-trace-id-generator.util.js";
|
|
2
|
+
/**
|
|
3
|
+
* Whether the module's `traceIdGenerator` is `defaultTraceIdGenerator`,
|
|
4
|
+
* recorded on its options by `createObserveModule` while they are still a
|
|
5
|
+
* plain object.
|
|
6
|
+
*
|
|
7
|
+
* Read back rather than found by comparing the function: Nest hands the
|
|
8
|
+
* options out instrumented like any provider, and a function read off them is
|
|
9
|
+
* a wrapper, never equal to the one stored. A boolean comes through as it is.
|
|
10
|
+
*/
|
|
11
|
+
const USES_DEFAULT_TRACE_ID_GENERATOR = Symbol("usesDefaultTraceIdGenerator");
|
|
12
|
+
/** Records whether `options` holds the default generator - see above. */
|
|
13
|
+
export function recordTraceIdGeneratorOrigin(options) {
|
|
14
|
+
options[USES_DEFAULT_TRACE_ID_GENERATOR] =
|
|
15
|
+
options.traceIdGenerator === defaultTraceIdGenerator;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Whether the options hold the default generator, which `openTelemetry`
|
|
19
|
+
* swaps for `openTelemetryTraceIdGenerator` while interop runs. Options
|
|
20
|
+
* `createObserveModule` never saw read as a generator of the application's
|
|
21
|
+
* own, which is left alone.
|
|
22
|
+
*/
|
|
23
|
+
export function usesDefaultTraceIdGenerator(options) {
|
|
24
|
+
return (options[USES_DEFAULT_TRACE_ID_GENERATOR] ===
|
|
25
|
+
true);
|
|
26
|
+
}
|
|
@@ -1,4 +1,33 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { randomFillSync } from "crypto";
|
|
2
|
+
/** Random bytes in each id: 12 bits go into `rand_a`, 62 into `rand_b`. */
|
|
3
|
+
const RANDOM_BYTES_PER_ID = 10;
|
|
4
|
+
/**
|
|
5
|
+
* Random bytes for the ids below, refilled 1,024 ids' worth at a time.
|
|
6
|
+
*
|
|
7
|
+
* The agent mints an id for every span, so this runs once per provider call
|
|
8
|
+
* of every traced request. A `randomBytes(10)` per id - a native call and a
|
|
9
|
+
* buffer each time - was the agent's largest single cost on the request path:
|
|
10
|
+
* a fifth of the main thread's busy time on an endpoint with nine spans. The
|
|
11
|
+
* bytes still come from the CSPRNG; it is asked once per 1,024 ids, the way
|
|
12
|
+
* `crypto.randomUUID()` caches its own entropy.
|
|
13
|
+
*/
|
|
14
|
+
const pool = Buffer.allocUnsafe(RANDOM_BYTES_PER_ID * 1024);
|
|
15
|
+
let poolOffset = pool.length;
|
|
16
|
+
const HEX_DIGITS = "0123456789abcdef";
|
|
17
|
+
/** Each byte value's two hex digits, as character codes. */
|
|
18
|
+
const HIGH_DIGIT = Uint8Array.from({ length: 256 }, (_, byte) => HEX_DIGITS.charCodeAt(byte >>> 4));
|
|
19
|
+
const LOW_DIGIT = Uint8Array.from({ length: 256 }, (_, byte) => HEX_DIGITS.charCodeAt(byte & 0x0f));
|
|
20
|
+
/**
|
|
21
|
+
* The id's 36 characters, written in place and read out as one string; the
|
|
22
|
+
* hyphens never move. Joined from pieces instead, an id was a concatenation
|
|
23
|
+
* V8 had to flatten before it could hash it, and the registry hashes every
|
|
24
|
+
* span id - as a map key - the moment it is minted.
|
|
25
|
+
*/
|
|
26
|
+
const text = Buffer.alloc(36, "-");
|
|
27
|
+
function writeByte(at, byte) {
|
|
28
|
+
text[at] = HIGH_DIGIT[byte];
|
|
29
|
+
text[at + 1] = LOW_DIGIT[byte];
|
|
30
|
+
}
|
|
2
31
|
/**
|
|
3
32
|
* A UUID whose first 48 bits are the minting time, milliseconds since the
|
|
4
33
|
* epoch (RFC 9562 version 7).
|
|
@@ -15,20 +44,29 @@ import { randomBytes } from "crypto";
|
|
|
15
44
|
* decode the same way.
|
|
16
45
|
*/
|
|
17
46
|
export function uuidv7(now = Date.now()) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
47
|
+
if (poolOffset + RANDOM_BYTES_PER_ID > pool.length) {
|
|
48
|
+
randomFillSync(pool);
|
|
49
|
+
poolOffset = 0;
|
|
50
|
+
}
|
|
51
|
+
const r = poolOffset;
|
|
52
|
+
poolOffset += RANDOM_BYTES_PER_ID;
|
|
53
|
+
// 48-bit big-endian timestamp, split 32/16 so no shift runs past 2^32.
|
|
54
|
+
const high = Math.floor(now / 0x10000);
|
|
55
|
+
const low = now % 0x10000;
|
|
56
|
+
writeByte(0, (high >>> 24) & 0xff);
|
|
57
|
+
writeByte(2, (high >>> 16) & 0xff);
|
|
58
|
+
writeByte(4, (high >>> 8) & 0xff);
|
|
59
|
+
writeByte(6, high & 0xff);
|
|
60
|
+
writeByte(9, (low >>> 8) & 0xff);
|
|
61
|
+
writeByte(11, low & 0xff);
|
|
26
62
|
// Version 7 in the top nibble, 12 random bits below it.
|
|
27
|
-
|
|
28
|
-
|
|
63
|
+
writeByte(14, 0x70 | (pool[r] & 0x0f));
|
|
64
|
+
writeByte(16, pool[r + 1]);
|
|
29
65
|
// Variant `10` in the top two bits, 62 random bits below.
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
66
|
+
writeByte(19, 0x80 | (pool[r + 2] & 0x3f));
|
|
67
|
+
writeByte(21, pool[r + 3]);
|
|
68
|
+
for (let i = 0; i < 6; i++) {
|
|
69
|
+
writeByte(24 + i * 2, pool[r + 4 + i]);
|
|
70
|
+
}
|
|
71
|
+
return text.toString("latin1");
|
|
34
72
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nestjs/observe",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.22",
|
|
4
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",
|
|
@@ -42,7 +42,8 @@
|
|
|
42
42
|
},
|
|
43
43
|
"files": [
|
|
44
44
|
"dist",
|
|
45
|
-
"!dist/**/*.tsbuildinfo"
|
|
45
|
+
"!dist/**/*.tsbuildinfo",
|
|
46
|
+
"NOTICE"
|
|
46
47
|
],
|
|
47
48
|
"publishConfig": {
|
|
48
49
|
"access": "public"
|
|
@@ -61,6 +62,7 @@
|
|
|
61
62
|
"test:watch": "vitest",
|
|
62
63
|
"test:cov": "vitest run --coverage",
|
|
63
64
|
"test:int": "vitest run --config vitest.int.config.ts",
|
|
65
|
+
"bench": "npm run build && tsc -p bench/tsconfig.json && node bench/.build/run.js",
|
|
64
66
|
"prepack": "npm run build",
|
|
65
67
|
"prepublish:next": "npm run build",
|
|
66
68
|
"publish:next": "npm publish --access public --tag next",
|
|
@@ -70,7 +72,7 @@
|
|
|
70
72
|
"release": "release-it",
|
|
71
73
|
"prepare": "husky"
|
|
72
74
|
},
|
|
73
|
-
"license": "
|
|
75
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
74
76
|
"dependencies": {
|
|
75
77
|
"@jridgewell/trace-mapping": "0.3.31"
|
|
76
78
|
},
|
|
@@ -83,6 +85,7 @@
|
|
|
83
85
|
"@nestjs/microservices": "^11.0.0 || ^12.0.0",
|
|
84
86
|
"@nestjs/schedule": "^4.0.0 || ^5.0.0 || ^6.0.0 || ^12.0.0",
|
|
85
87
|
"@nestjs/websockets": "^11.0.0 || ^12.0.0",
|
|
88
|
+
"@opentelemetry/api": "^1.0.0",
|
|
86
89
|
"@taskforcesh/bullmq-pro": "^7.0.0",
|
|
87
90
|
"bull": "^3.3.0 || ^4.0.0",
|
|
88
91
|
"bullmq": "^5.0.0 || ^6.0.0",
|
|
@@ -108,6 +111,9 @@
|
|
|
108
111
|
"@nestjs/websockets": {
|
|
109
112
|
"optional": true
|
|
110
113
|
},
|
|
114
|
+
"@opentelemetry/api": {
|
|
115
|
+
"optional": true
|
|
116
|
+
},
|
|
111
117
|
"@taskforcesh/bullmq-pro": {
|
|
112
118
|
"optional": true
|
|
113
119
|
},
|
|
@@ -123,7 +129,7 @@
|
|
|
123
129
|
"@as-integrations/express5": "1.1.2",
|
|
124
130
|
"@commitlint/cli": "21.2.4",
|
|
125
131
|
"@commitlint/config-angular": "21.2.4",
|
|
126
|
-
"@grpc/grpc-js": "1.14.
|
|
132
|
+
"@grpc/grpc-js": "1.14.7",
|
|
127
133
|
"@grpc/proto-loader": "0.8.1",
|
|
128
134
|
"@mikro-orm/core": "7.2.0",
|
|
129
135
|
"@mikro-orm/mysql": "7.2.0",
|
|
@@ -143,6 +149,13 @@
|
|
|
143
149
|
"@nestjs/schedule": "12.0.1",
|
|
144
150
|
"@nestjs/testing": "12.0.1",
|
|
145
151
|
"@nestjs/websockets": "12.0.3",
|
|
152
|
+
"@opentelemetry/api": "1.9.1",
|
|
153
|
+
"@opentelemetry/auto-instrumentations-node": "0.81.0",
|
|
154
|
+
"@opentelemetry/context-async-hooks": "2.12.0",
|
|
155
|
+
"@opentelemetry/core": "2.12.0",
|
|
156
|
+
"@opentelemetry/sdk-node": "0.223.0",
|
|
157
|
+
"@opentelemetry/sdk-trace-base": "2.12.0",
|
|
158
|
+
"@sentry/nestjs": "11.10.0",
|
|
146
159
|
"@swc/core": "1.16.1",
|
|
147
160
|
"@types/mssql": "12.3.0",
|
|
148
161
|
"@types/node": "22.20.5",
|
|
@@ -152,6 +165,7 @@
|
|
|
152
165
|
"@vitest/coverage-v8": "4.1.11",
|
|
153
166
|
"bull": "4.16.5",
|
|
154
167
|
"bullmq": "6.2.0",
|
|
168
|
+
"dd-trace": "6.23.0",
|
|
155
169
|
"drizzle-orm": "0.45.4",
|
|
156
170
|
"express": "5.2.1",
|
|
157
171
|
"graphql": "16.14.2",
|