@nestjs/observe 0.3.21 → 0.3.23
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 +100 -14
- package/dist/agent/degraded-ingest.protocol.d.ts +14 -0
- package/dist/agent/degraded-ingest.protocol.js +19 -0
- package/dist/agent/detached-observe-worker.assembly.d.ts +2 -0
- package/dist/agent/detached-observe-worker.assembly.js +5 -1
- package/dist/agent/detached-observe-worker.d.ts +10 -0
- package/dist/agent/detached-observe-worker.js +27 -2
- package/dist/agent/observe-agent.shared-buffer.d.ts +63 -1
- package/dist/agent/observe-agent.shared-buffer.js +117 -2
- package/dist/agent/observe-agent.worker.d.ts +27 -20
- package/dist/agent/observe-agent.worker.js +109 -62
- package/dist/agent/observe-reporting.util.d.ts +35 -0
- package/dist/agent/observe-reporting.util.js +69 -0
- package/dist/interfaces/observe-options.interface.d.ts +100 -8
- package/dist/observe.constants.d.ts +17 -0
- package/dist/observe.constants.js +17 -0
- package/dist/observe.module.js +53 -4
- package/dist/otlp/otlp-encoders.d.ts +26 -0
- package/dist/otlp/otlp-encoders.js +296 -0
- package/dist/otlp/otlp-export-grant.d.ts +34 -0
- package/dist/otlp/otlp-export-grant.js +22 -0
- package/dist/otlp/otlp-options.interface.d.ts +59 -0
- package/dist/otlp/otlp-options.interface.js +1 -0
- package/dist/otlp/otlp-settings.util.d.ts +38 -0
- package/dist/otlp/otlp-settings.util.js +154 -0
- package/dist/otlp/otlp-span.mapper.d.ts +73 -0
- package/dist/otlp/otlp-span.mapper.js +345 -0
- package/dist/otlp/otlp-trace-exporter.service.d.ts +130 -0
- package/dist/otlp/otlp-trace-exporter.service.js +497 -0
- package/dist/outgoing/http.integration.d.ts +36 -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/outgoing/sql-statement.util.js +2 -2
- package/dist/protocols/http-observe-agent.service.d.ts +1 -8
- package/dist/protocols/http-observe-agent.service.js +44 -14
- package/dist/protocols/job-trace-runner.d.ts +11 -0
- package/dist/protocols/job-trace-runner.js +42 -8
- package/dist/protocols/rpc-observe-agent.service.d.ts +5 -0
- package/dist/protocols/rpc-observe-agent.service.js +43 -3
- package/dist/services/operation-trace.registry.d.ts +46 -3
- package/dist/services/operation-trace.registry.js +69 -13
- 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 +39 -2
- package/dist/utils/opentelemetry.util.d.ts +57 -0
- package/dist/utils/opentelemetry.util.js +100 -0
- package/dist/utils/trace-context.util.d.ts +91 -0
- package/dist/utils/trace-context.util.js +219 -0
- package/dist/utils/trace-handoff.util.d.ts +45 -0
- package/dist/utils/trace-handoff.util.js +90 -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/package.json +26 -13
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
import { randomSpanId, w3cSpanIdOf, w3cTraceIdOf, } from "../utils/trace-context.util.js";
|
|
2
|
+
/** OTLP's `Span.SpanKind`. */
|
|
3
|
+
export const SpanKind = {
|
|
4
|
+
INTERNAL: 1,
|
|
5
|
+
SERVER: 2,
|
|
6
|
+
CLIENT: 3,
|
|
7
|
+
CONSUMER: 5,
|
|
8
|
+
};
|
|
9
|
+
/** OTLP's `Status.StatusCode`. Only errors are ever set. */
|
|
10
|
+
export const STATUS_CODE_ERROR = 2;
|
|
11
|
+
/**
|
|
12
|
+
* `Span.flags`: the W3C trace flags in the low byte - every exported span
|
|
13
|
+
* was recorded, so `sampled` - and whether the parent is remote, which
|
|
14
|
+
* OTLP 1.2 added so a backend can tell a process boundary from a local
|
|
15
|
+
* call without guessing.
|
|
16
|
+
*/
|
|
17
|
+
const SAMPLED_FLAG = 0x01;
|
|
18
|
+
const HAS_IS_REMOTE_FLAG = 0x100;
|
|
19
|
+
const IS_REMOTE_FLAG = 0x200;
|
|
20
|
+
const LOCAL_PARENT_FLAGS = SAMPLED_FLAG | HAS_IS_REMOTE_FLAG;
|
|
21
|
+
const REMOTE_PARENT_FLAGS = LOCAL_PARENT_FLAGS | IS_REMOTE_FLAG;
|
|
22
|
+
/**
|
|
23
|
+
* Most attributes one span carries; the rest are counted as dropped. The
|
|
24
|
+
* OpenTelemetry SDKs' default limit.
|
|
25
|
+
*/
|
|
26
|
+
const MAX_ATTRIBUTES_PER_SPAN = 128;
|
|
27
|
+
/**
|
|
28
|
+
* The span classes the outgoing integrations open - see
|
|
29
|
+
* `OutgoingSpanRecorder.open` - which are calls out of the process: CLIENT
|
|
30
|
+
* spans, named for what they did (`GET api.stripe.com`, `SELECT orders`)
|
|
31
|
+
* rather than for a class and method.
|
|
32
|
+
*/
|
|
33
|
+
const CLIENT_SPAN_CLASSES = new Set([
|
|
34
|
+
"http",
|
|
35
|
+
"pg",
|
|
36
|
+
"mysql2",
|
|
37
|
+
"mongodb",
|
|
38
|
+
"mssql",
|
|
39
|
+
]);
|
|
40
|
+
/**
|
|
41
|
+
* The queue names a scheduled handler reports under - see
|
|
42
|
+
* `ScheduleObserveAgentService` - for a run that nothing sent: INTERNAL,
|
|
43
|
+
* where a queue job is a CONSUMER.
|
|
44
|
+
*/
|
|
45
|
+
const SCHEDULER_QUEUES = new Set(["cron", "interval", "timeout", "schedule"]);
|
|
46
|
+
/**
|
|
47
|
+
* Tags the agent's own integrations write under pre-1.0 semantic convention
|
|
48
|
+
* names, beside the names the stable conventions settled on, so a backend
|
|
49
|
+
* reading either finds them.
|
|
50
|
+
*/
|
|
51
|
+
const STABLE_CONVENTION_NAMES = {
|
|
52
|
+
"http.method": "http.request.method",
|
|
53
|
+
"http.url": "url.full",
|
|
54
|
+
"db.statement": "db.query.text",
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* A request, a GraphQL operation, an RPC message or a gateway message as
|
|
58
|
+
* OTLP spans: one SERVER span for the operation, and under it a span for
|
|
59
|
+
* every node of its tree.
|
|
60
|
+
*
|
|
61
|
+
* The SERVER span is what the agent's own snapshot has no node for - the
|
|
62
|
+
* operation itself, with its route, status and user - and gets an id of its
|
|
63
|
+
* own, minted here: several requests can share a trace id (a caller that
|
|
64
|
+
* fans out), and anything derived from it would give them all one span. It
|
|
65
|
+
* goes under the caller's span when the request arrived with a
|
|
66
|
+
* `traceparent`, which is what joins it to the trace upstream.
|
|
67
|
+
*
|
|
68
|
+
* Without `withTree`, the SERVER span alone: what NestJS Observe keeps of an
|
|
69
|
+
* operation whose spans it holds back - see `OtlpExportGrant`.
|
|
70
|
+
*/
|
|
71
|
+
export function requestSnapshotSpans(snapshot, context, withTree = true) {
|
|
72
|
+
const traceId = context?.traceId ?? w3cTraceIdOf(snapshot.traceId);
|
|
73
|
+
const startNs = epochNanos(snapshot.calledAt);
|
|
74
|
+
const statusCode = snapshot.attributes?.statusCode;
|
|
75
|
+
const method = snapshot.attributes?.method;
|
|
76
|
+
const tags = {
|
|
77
|
+
...snapshot.tags,
|
|
78
|
+
...requestConventionAttributes(snapshot),
|
|
79
|
+
"enduser.id": snapshot.userId,
|
|
80
|
+
"error.type": snapshot.error?.cls ??
|
|
81
|
+
(statusCode !== undefined && statusCode >= 500
|
|
82
|
+
? String(statusCode)
|
|
83
|
+
: undefined),
|
|
84
|
+
};
|
|
85
|
+
for (const [name, value] of Object.entries(snapshot.request?.headers ?? {})) {
|
|
86
|
+
tags[`http.request.header.${name.toLowerCase()}`] = value;
|
|
87
|
+
}
|
|
88
|
+
// A 4xx is the caller's mistake, not the server's: the HTTP conventions
|
|
89
|
+
// leave such a server span's status unset. Transports with no status of
|
|
90
|
+
// their own fail on the error alone.
|
|
91
|
+
const failed = statusCode !== undefined ? statusCode >= 500 : snapshot.error !== undefined;
|
|
92
|
+
const server = {
|
|
93
|
+
traceId,
|
|
94
|
+
spanId: randomSpanId(),
|
|
95
|
+
...(context
|
|
96
|
+
? { parentSpanId: context.parentSpanId, traceState: context.traceState }
|
|
97
|
+
: {}),
|
|
98
|
+
name: [method, snapshot.operationId].filter(Boolean).join(" ") ||
|
|
99
|
+
snapshot.protocol,
|
|
100
|
+
kind: SpanKind.SERVER,
|
|
101
|
+
startTimeUnixNano: startNs,
|
|
102
|
+
endTimeUnixNano: startNs + millisToNanos(snapshot.duration ?? 0),
|
|
103
|
+
...toAttributes(tags),
|
|
104
|
+
events: [],
|
|
105
|
+
...(failed ? { status: errorStatus(snapshot.error) } : {}),
|
|
106
|
+
flags: context ? REMOTE_PARENT_FLAGS : LOCAL_PARENT_FLAGS,
|
|
107
|
+
};
|
|
108
|
+
const spans = [server];
|
|
109
|
+
for (const node of withTree ? snapshot.traces ?? [] : []) {
|
|
110
|
+
appendNodeSpans(spans, node, traceId, server.spanId, startNs);
|
|
111
|
+
}
|
|
112
|
+
return spans;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* A queue job or a scheduled run as OTLP spans: a CONSUMER span for the run
|
|
116
|
+
* - INTERNAL for a scheduled one, which nothing sent - and its tree under
|
|
117
|
+
* it. A job reports under the trace id of the operation that enqueued it,
|
|
118
|
+
* so it lands in that trace; nothing records which span enqueued it, so it
|
|
119
|
+
* has no parent there. Without `withTree`, the run's span alone.
|
|
120
|
+
*/
|
|
121
|
+
export function jobSnapshotSpans(snapshot, withTree = true) {
|
|
122
|
+
const traceId = w3cTraceIdOf(snapshot.traceId);
|
|
123
|
+
const startNs = epochNanos(snapshot.calledAt);
|
|
124
|
+
const scheduled = SCHEDULER_QUEUES.has(snapshot.queueName);
|
|
125
|
+
const tags = {
|
|
126
|
+
...snapshot.tags,
|
|
127
|
+
"observe.job.name": snapshot.name,
|
|
128
|
+
"observe.job.queue": snapshot.queueName,
|
|
129
|
+
"observe.job.attempts_made": snapshot.attemptsMade,
|
|
130
|
+
"observe.job.max_attempts": snapshot.maxAttempts,
|
|
131
|
+
"observe.job.wait_ms": snapshot.waitDuration,
|
|
132
|
+
"error.type": snapshot.error?.cls,
|
|
133
|
+
};
|
|
134
|
+
if (!scheduled) {
|
|
135
|
+
tags["messaging.operation.type"] = "process";
|
|
136
|
+
tags["messaging.destination.name"] = snapshot.queueName;
|
|
137
|
+
tags["messaging.message.id"] = snapshot.id;
|
|
138
|
+
}
|
|
139
|
+
const failed = snapshot.status === "failed" ||
|
|
140
|
+
(snapshot.status === undefined && snapshot.error !== undefined);
|
|
141
|
+
const run = {
|
|
142
|
+
traceId,
|
|
143
|
+
spanId: randomSpanId(),
|
|
144
|
+
name: `${snapshot.queueName} ${snapshot.name}`,
|
|
145
|
+
kind: scheduled ? SpanKind.INTERNAL : SpanKind.CONSUMER,
|
|
146
|
+
startTimeUnixNano: startNs,
|
|
147
|
+
endTimeUnixNano: startNs + millisToNanos(snapshot.duration ?? 0),
|
|
148
|
+
...toAttributes(tags),
|
|
149
|
+
events: [],
|
|
150
|
+
...(failed ? { status: errorStatus(snapshot.error) } : {}),
|
|
151
|
+
flags: LOCAL_PARENT_FLAGS,
|
|
152
|
+
};
|
|
153
|
+
const spans = [run];
|
|
154
|
+
for (const node of withTree ? snapshot.traces ?? [] : []) {
|
|
155
|
+
appendNodeSpans(spans, node, traceId, run.spanId, startNs);
|
|
156
|
+
}
|
|
157
|
+
return spans;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* One node of an operation's tree and everything below it, depth first.
|
|
161
|
+
*
|
|
162
|
+
* A node's span id is the low half of its Observe span id - the same id an
|
|
163
|
+
* outbound call put in its `traceparent` - so the server span a downstream
|
|
164
|
+
* service exports nests under the client span exported here.
|
|
165
|
+
*
|
|
166
|
+
* Placed on the clock by `startOffset`, the node's start relative to the
|
|
167
|
+
* operation's, which is what lets overlapping calls overlap. A node from an
|
|
168
|
+
* agent older than that field starts with the operation. A node still open
|
|
169
|
+
* is not exported: the registry ships a tree only once its spans closed, or
|
|
170
|
+
* without the ones that never did.
|
|
171
|
+
*/
|
|
172
|
+
function appendNodeSpans(spans, node, traceId, parentSpanId, operationStartNs) {
|
|
173
|
+
if ("type" in node && node.type === "start") {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
const complete = node;
|
|
177
|
+
const client = complete.origin === "auto" && CLIENT_SPAN_CLASSES.has(complete.className);
|
|
178
|
+
const startNs = operationStartNs + millisToNanos(complete.startOffset ?? 0);
|
|
179
|
+
const endNs = startNs + millisToNanos(complete.duration ?? 0);
|
|
180
|
+
const tags = {
|
|
181
|
+
...(client
|
|
182
|
+
? {}
|
|
183
|
+
: {
|
|
184
|
+
"code.function.name": `${complete.className}.${complete.methodKey}`,
|
|
185
|
+
}),
|
|
186
|
+
...complete.tags,
|
|
187
|
+
};
|
|
188
|
+
for (const [legacy, stable] of Object.entries(STABLE_CONVENTION_NAMES)) {
|
|
189
|
+
if (tags[legacy] !== undefined && tags[stable] === undefined) {
|
|
190
|
+
tags[stable] = tags[legacy];
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
if (client && complete.className === "http") {
|
|
194
|
+
Object.assign(tags, serverAddress(tags["url.full"]));
|
|
195
|
+
}
|
|
196
|
+
const error = typeof complete.error === "object" ? complete.error : undefined;
|
|
197
|
+
const span = {
|
|
198
|
+
traceId,
|
|
199
|
+
spanId: complete.spanId ? w3cSpanIdOf(complete.spanId) : randomSpanId(),
|
|
200
|
+
parentSpanId,
|
|
201
|
+
name: complete.name ??
|
|
202
|
+
(client
|
|
203
|
+
? complete.methodKey
|
|
204
|
+
: `${complete.className}.${complete.methodKey}`),
|
|
205
|
+
kind: client ? SpanKind.CLIENT : SpanKind.INTERNAL,
|
|
206
|
+
startTimeUnixNano: startNs,
|
|
207
|
+
endTimeUnixNano: endNs,
|
|
208
|
+
...toAttributes({ ...tags, "error.type": error?.cls }),
|
|
209
|
+
events: error ? [exceptionEvent(error, endNs)] : [],
|
|
210
|
+
...(complete.error ? { status: errorStatus(error) } : {}),
|
|
211
|
+
flags: LOCAL_PARENT_FLAGS,
|
|
212
|
+
};
|
|
213
|
+
spans.push(span);
|
|
214
|
+
for (const child of complete.children ?? []) {
|
|
215
|
+
appendNodeSpans(spans, child, traceId, span.spanId, operationStartNs);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* The HTTP semantic conventions for a request that came in over HTTP. A
|
|
220
|
+
* GraphQL operation carries its document where a request carries its URL,
|
|
221
|
+
* which is told apart by not starting with a slash.
|
|
222
|
+
*/
|
|
223
|
+
function requestConventionAttributes(snapshot) {
|
|
224
|
+
const { protocol, operationId, attributes } = snapshot;
|
|
225
|
+
const http = protocol === "http" || protocol === "https";
|
|
226
|
+
const originalUrl = attributes?.originalUrl;
|
|
227
|
+
const document = originalUrl !== undefined && !originalUrl.startsWith("/")
|
|
228
|
+
? originalUrl
|
|
229
|
+
: undefined;
|
|
230
|
+
const [path, query] = originalUrl !== undefined && document === undefined
|
|
231
|
+
? splitOnce(originalUrl, "?")
|
|
232
|
+
: [undefined, undefined];
|
|
233
|
+
if (http) {
|
|
234
|
+
return {
|
|
235
|
+
"http.request.method": attributes?.method,
|
|
236
|
+
// The name the request would have had: `setOperationId` renames it
|
|
237
|
+
// for the dashboard, not the route it matched.
|
|
238
|
+
"http.route": attributes?.defaultOperationId ?? operationId,
|
|
239
|
+
"http.response.status_code": attributes?.statusCode,
|
|
240
|
+
"url.scheme": protocol,
|
|
241
|
+
"url.path": path,
|
|
242
|
+
"url.query": query,
|
|
243
|
+
"graphql.document": document,
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
if (protocol === "graphql" || protocol === "ws") {
|
|
247
|
+
return {
|
|
248
|
+
"observe.operation_id": operationId,
|
|
249
|
+
"observe.status_code": attributes?.statusCode,
|
|
250
|
+
"graphql.document": document,
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
// A microservice transport (TCP, NATS, Kafka, gRPC, ...).
|
|
254
|
+
return {
|
|
255
|
+
"rpc.system": protocol.toLowerCase(),
|
|
256
|
+
"rpc.method": operationId,
|
|
257
|
+
"observe.status_code": attributes?.statusCode,
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
/** `server.address` and `server.port` of a client call's URL. */
|
|
261
|
+
function serverAddress(url) {
|
|
262
|
+
if (typeof url !== "string") {
|
|
263
|
+
return {};
|
|
264
|
+
}
|
|
265
|
+
try {
|
|
266
|
+
const { hostname, port, protocol } = new URL(url);
|
|
267
|
+
return {
|
|
268
|
+
"server.address": hostname,
|
|
269
|
+
"server.port": port ? Number(port) : protocol === "https:" ? 443 : 80,
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
catch {
|
|
273
|
+
return {};
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* An error as the exception event the semantic conventions define - the
|
|
278
|
+
* same class, message and stack the dashboard shows, redacted as they were
|
|
279
|
+
* for it. Placed at the span's end: the agent learns of an error when the
|
|
280
|
+
* call that raised it returns.
|
|
281
|
+
*/
|
|
282
|
+
function exceptionEvent(error, timeUnixNano) {
|
|
283
|
+
return {
|
|
284
|
+
timeUnixNano,
|
|
285
|
+
name: "exception",
|
|
286
|
+
attributes: toAttributes({
|
|
287
|
+
"exception.type": error.cls,
|
|
288
|
+
"exception.message": error.message,
|
|
289
|
+
"exception.stacktrace": error.stack,
|
|
290
|
+
}).attributes,
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
function errorStatus(error) {
|
|
294
|
+
return error?.message
|
|
295
|
+
? { code: STATUS_CODE_ERROR, message: error.message }
|
|
296
|
+
: { code: STATUS_CODE_ERROR };
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Attributes from a flat record, in its order, without the keys that hold
|
|
300
|
+
* nothing - and at most `MAX_ATTRIBUTES_PER_SPAN` of them, the rest counted.
|
|
301
|
+
*/
|
|
302
|
+
export function toAttributes(record) {
|
|
303
|
+
const attributes = [];
|
|
304
|
+
let droppedAttributesCount = 0;
|
|
305
|
+
for (const [key, value] of Object.entries(record)) {
|
|
306
|
+
if (value === undefined ||
|
|
307
|
+
value === null ||
|
|
308
|
+
(typeof value === "number" && !Number.isFinite(value))) {
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
if (typeof value !== "string" &&
|
|
312
|
+
typeof value !== "number" &&
|
|
313
|
+
typeof value !== "boolean") {
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
316
|
+
if (attributes.length >= MAX_ATTRIBUTES_PER_SPAN) {
|
|
317
|
+
droppedAttributesCount += 1;
|
|
318
|
+
continue;
|
|
319
|
+
}
|
|
320
|
+
attributes.push({ key, value });
|
|
321
|
+
}
|
|
322
|
+
return { attributes, droppedAttributesCount };
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Unix nanoseconds for an ISO timestamp. Whole milliseconds, as `calledAt`
|
|
326
|
+
* is written, so the product stays exact in a BigInt where a double would
|
|
327
|
+
* already have lost the nanoseconds.
|
|
328
|
+
*/
|
|
329
|
+
function epochNanos(iso) {
|
|
330
|
+
const ms = iso === undefined ? Number.NaN : Date.parse(iso);
|
|
331
|
+
return BigInt(Number.isFinite(ms) ? ms : Date.now()) * 1000000n;
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Nanoseconds for a duration or offset in milliseconds - small enough, unlike
|
|
335
|
+
* an epoch time, for a double to carry to the nanosecond.
|
|
336
|
+
*/
|
|
337
|
+
function millisToNanos(ms) {
|
|
338
|
+
return Number.isFinite(ms) && ms > 0 ? BigInt(Math.round(ms * 1e6)) : 0n;
|
|
339
|
+
}
|
|
340
|
+
function splitOnce(value, separator) {
|
|
341
|
+
const index = value.indexOf(separator);
|
|
342
|
+
return index === -1
|
|
343
|
+
? [value, undefined]
|
|
344
|
+
: [value.slice(0, index), value.slice(index + 1)];
|
|
345
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { OnApplicationShutdown, OnModuleInit } from "@nestjs/common";
|
|
2
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
3
|
+
import type { JobSnapshot } from "../interfaces/job-snapshot.interface.js";
|
|
4
|
+
import type { ObserveModuleOptionsWithDefaults } from "../interfaces/observe-options.interface.js";
|
|
5
|
+
import type { RequestSnapshot } from "../interfaces/request-snapshot.interface.js";
|
|
6
|
+
import { OperationTraceRegistry } from "../services/operation-trace.registry.js";
|
|
7
|
+
/**
|
|
8
|
+
* Exports what the agent records as OpenTelemetry traces, over OTLP/HTTP, to
|
|
9
|
+
* whatever receiver `otlp` names - a collector, Jaeger, Tempo, Honeycomb, a
|
|
10
|
+
* vendor's agent - beside what goes to Observe, never instead of it: an agent
|
|
11
|
+
* that sends Observe nothing (see `sendsToObserve`) exports nothing either.
|
|
12
|
+
* Part of OpenTelemetry interop, so it also takes `openTelemetry: true`: the
|
|
13
|
+
* `traceparent` the spans nest under is read only with that on.
|
|
14
|
+
*
|
|
15
|
+
* Fed finished operations by the shared buffer, which decides what to hand
|
|
16
|
+
* over: nothing until the collector takes the app's telemetry and its reply
|
|
17
|
+
* grants export for the account's plan, and then - see `OtlpExportGrant` -
|
|
18
|
+
* either everything, before anything the Observe side decides about it, or
|
|
19
|
+
* only what NestJS Observe keeps. What it is fed has already been through
|
|
20
|
+
* `tracesSampleRate`, `skipSpans` and `spanCollapse`, and through redaction.
|
|
21
|
+
*
|
|
22
|
+
* Runs on the application's thread, unlike the Observe worker: an export is
|
|
23
|
+
* a small encode and an asynchronous request, compression runs on libuv's
|
|
24
|
+
* pool, and none of it sits on a request's path. Every send runs outside the
|
|
25
|
+
* agent's async context, so the agent never traces - or puts trace headers
|
|
26
|
+
* on - its own exports.
|
|
27
|
+
*/
|
|
28
|
+
export declare class OtlpTraceExporter implements OnModuleInit, OnApplicationShutdown {
|
|
29
|
+
private readonly options;
|
|
30
|
+
private readonly operationTraceRegistry;
|
|
31
|
+
private readonly asyncLocalStorage;
|
|
32
|
+
private readonly logger;
|
|
33
|
+
private readonly settings;
|
|
34
|
+
private readonly resource;
|
|
35
|
+
/** Its version is read only once export is known to be on. */
|
|
36
|
+
private readonly scope;
|
|
37
|
+
private readonly agent;
|
|
38
|
+
private readonly queue;
|
|
39
|
+
private sending;
|
|
40
|
+
private flushTimer;
|
|
41
|
+
private shutdownPromise;
|
|
42
|
+
/**
|
|
43
|
+
* Set once shutdown has let go of the connection. An export still open
|
|
44
|
+
* then fails as its socket goes, which is the shutdown, not the receiver,
|
|
45
|
+
* and nothing will retry it.
|
|
46
|
+
*/
|
|
47
|
+
private closed;
|
|
48
|
+
/** No export starts before this (epoch ms): a retry is waiting. */
|
|
49
|
+
private retryNotBefore;
|
|
50
|
+
private retryDelayMs;
|
|
51
|
+
/** Whether the last export failed - the final flush does not retry it. */
|
|
52
|
+
private lastSendFailed;
|
|
53
|
+
/**
|
|
54
|
+
* What the last failure was, while exports are failing: said once when it
|
|
55
|
+
* starts or changes, and again when exports recover. Null while healthy.
|
|
56
|
+
*/
|
|
57
|
+
private failure;
|
|
58
|
+
/** Spans dropped since the last report of it. */
|
|
59
|
+
private droppedSpans;
|
|
60
|
+
private reportedDrop;
|
|
61
|
+
private reportedMappingFailure;
|
|
62
|
+
/**
|
|
63
|
+
* Why `otlp` is set and export still cannot run, said once at start-up;
|
|
64
|
+
* null when it can, or when `otlp` is not set at all.
|
|
65
|
+
*/
|
|
66
|
+
private readonly offReason;
|
|
67
|
+
constructor(options: ObserveModuleOptionsWithDefaults, operationTraceRegistry: OperationTraceRegistry, asyncLocalStorage: AsyncLocalStorage<Map<any, any>>);
|
|
68
|
+
/**
|
|
69
|
+
* Whether `otlp` is set up and export can run once the collector grants
|
|
70
|
+
* it - `openTelemetry` on, reporting to NestJS Observe, a usable endpoint.
|
|
71
|
+
*/
|
|
72
|
+
get enabled(): boolean;
|
|
73
|
+
/** Where export goes, once it is enabled. */
|
|
74
|
+
get url(): string | undefined;
|
|
75
|
+
onModuleInit(): void;
|
|
76
|
+
/**
|
|
77
|
+
* Queues a finished request's spans - its own span, and with `withTree` the
|
|
78
|
+
* spans under it. Never throws into the caller.
|
|
79
|
+
*
|
|
80
|
+
* Left out when the request arrived in a trace its caller sampled out
|
|
81
|
+
* (`traceparent` flags 00), as an OpenTelemetry SDK sampling by its parent
|
|
82
|
+
* would leave it out: the caller exports none of that trace, so these
|
|
83
|
+
* spans would only hang in the backend under a parent it never gets.
|
|
84
|
+
*/
|
|
85
|
+
exportRequestSnapshot(snapshot: RequestSnapshot, withTree?: boolean): void;
|
|
86
|
+
/**
|
|
87
|
+
* Queues a finished job's spans - its own span, and with `withTree` the
|
|
88
|
+
* spans under it. Never throws into the caller.
|
|
89
|
+
*/
|
|
90
|
+
exportJobSnapshot(snapshot: JobSnapshot, withTree?: boolean): void;
|
|
91
|
+
/**
|
|
92
|
+
* Starts an export of what is queued, unless one is already under way, a
|
|
93
|
+
* retry is waiting out its delay, or there is nothing to send.
|
|
94
|
+
*/
|
|
95
|
+
flush(): void;
|
|
96
|
+
onApplicationShutdown(): Promise<void>;
|
|
97
|
+
private enqueue;
|
|
98
|
+
/**
|
|
99
|
+
* One export. Resolves whatever happens: a failure is reported and either
|
|
100
|
+
* put back for a retry or dropped, never thrown.
|
|
101
|
+
*/
|
|
102
|
+
private send;
|
|
103
|
+
/**
|
|
104
|
+
* Puts a batch back at the front of the queue, if it still fits, and holds
|
|
105
|
+
* exports back for the receiver's `Retry-After` or the next backoff step.
|
|
106
|
+
*/
|
|
107
|
+
private retryLater;
|
|
108
|
+
/** Clears the failure state after an accepted export, and says so. */
|
|
109
|
+
private recovered;
|
|
110
|
+
/**
|
|
111
|
+
* Says that exports are failing, once when it starts and again only when
|
|
112
|
+
* the reason changes: a receiver that is down for an hour is one line, not
|
|
113
|
+
* one per flush.
|
|
114
|
+
*/
|
|
115
|
+
private reportFailure;
|
|
116
|
+
private reportMappingFailure;
|
|
117
|
+
private post;
|
|
118
|
+
/**
|
|
119
|
+
* Exports what is still queued, for at most `shutdownFlushTimeout`, then
|
|
120
|
+
* lets go of the connection.
|
|
121
|
+
*
|
|
122
|
+
* Operations finish their traces a tick after their response, and one
|
|
123
|
+
* whose spans were still open waits a further second in the registry, so
|
|
124
|
+
* the drain waits for both before deciding it is done - as the Observe
|
|
125
|
+
* worker's final flush does. A receiver that fails the final export is not
|
|
126
|
+
* retried: shutdown is not the time to wait out a backoff.
|
|
127
|
+
*/
|
|
128
|
+
private shutDown;
|
|
129
|
+
private drain;
|
|
130
|
+
}
|