@dxos/tracing 0.10.0 → 0.11.1
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/dist/lib/index.mjs +509 -0
- package/dist/lib/index.mjs.map +1 -0
- package/dist/types/src/api.d.ts +0 -44
- package/dist/types/src/api.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +0 -2
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/trace-processor.d.ts +0 -40
- package/dist/types/src/trace-processor.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +10 -13
- package/src/api.ts +3 -186
- package/src/index.ts +0 -2
- package/src/trace-processor.ts +1 -274
- package/src/tracing.test.ts +41 -326
- package/dist/lib/browser/index.mjs +0 -1065
- package/dist/lib/browser/index.mjs.map +0 -7
- package/dist/lib/browser/meta.json +0 -1
- package/dist/lib/node-esm/index.mjs +0 -1067
- package/dist/lib/node-esm/index.mjs.map +0 -7
- package/dist/lib/node-esm/meta.json +0 -1
- package/dist/types/src/metrics/base.d.ts +0 -7
- package/dist/types/src/metrics/base.d.ts.map +0 -1
- package/dist/types/src/metrics/custom-counter.d.ts +0 -8
- package/dist/types/src/metrics/custom-counter.d.ts.map +0 -1
- package/dist/types/src/metrics/index.d.ts +0 -7
- package/dist/types/src/metrics/index.d.ts.map +0 -1
- package/dist/types/src/metrics/map-counter.d.ts +0 -12
- package/dist/types/src/metrics/map-counter.d.ts.map +0 -1
- package/dist/types/src/metrics/time-series-counter.d.ts +0 -15
- package/dist/types/src/metrics/time-series-counter.d.ts.map +0 -1
- package/dist/types/src/metrics/time-usage-counter.d.ts +0 -15
- package/dist/types/src/metrics/time-usage-counter.d.ts.map +0 -1
- package/dist/types/src/metrics/unary-counter.d.ts +0 -12
- package/dist/types/src/metrics/unary-counter.d.ts.map +0 -1
- package/dist/types/src/symbols.d.ts +0 -10
- package/dist/types/src/symbols.d.ts.map +0 -1
- package/dist/types/src/weak-ref.d.ts +0 -2
- package/dist/types/src/weak-ref.d.ts.map +0 -1
- package/src/metrics/base.ts +0 -26
- package/src/metrics/custom-counter.ts +0 -22
- package/src/metrics/index.ts +0 -10
- package/src/metrics/map-counter.ts +0 -35
- package/src/metrics/time-series-counter.ts +0 -52
- package/src/metrics/time-usage-counter.ts +0 -62
- package/src/metrics/unary-counter.ts +0 -31
- package/src/symbols.ts +0 -24
- package/src/weak-ref.ts +0 -18
|
@@ -1,1067 +0,0 @@
|
|
|
1
|
-
import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
|
|
2
|
-
|
|
3
|
-
// src/api.ts
|
|
4
|
-
import { Context as Context2, LifecycleState, Resource, TRACE_SPAN_ATTRIBUTE } from "@dxos/context";
|
|
5
|
-
|
|
6
|
-
// src/symbols.ts
|
|
7
|
-
var symbolTracingContext = /* @__PURE__ */ Symbol("dxos.tracing.context");
|
|
8
|
-
var getTracingContext = (target) => {
|
|
9
|
-
return target[symbolTracingContext] ??= {
|
|
10
|
-
infoProperties: {},
|
|
11
|
-
metricsProperties: {}
|
|
12
|
-
};
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
// src/trace-processor.ts
|
|
16
|
-
import { LogLevel, log } from "@dxos/log";
|
|
17
|
-
import { getPrototypeSpecificInstanceId } from "@dxos/util";
|
|
18
|
-
|
|
19
|
-
// src/buffering-backend.ts
|
|
20
|
-
var BUFFERED_PREFIX = "buffered-";
|
|
21
|
-
var BufferedSpan = class {
|
|
22
|
-
options;
|
|
23
|
-
spanContext;
|
|
24
|
-
startTime;
|
|
25
|
-
delegate;
|
|
26
|
-
#ended = false;
|
|
27
|
-
#endTime;
|
|
28
|
-
#error;
|
|
29
|
-
#hasError = false;
|
|
30
|
-
constructor(options, id) {
|
|
31
|
-
this.options = options;
|
|
32
|
-
this.spanContext = {
|
|
33
|
-
traceparent: `${BUFFERED_PREFIX}${id}`
|
|
34
|
-
};
|
|
35
|
-
this.startTime = Date.now();
|
|
36
|
-
}
|
|
37
|
-
end(endTime) {
|
|
38
|
-
if (this.delegate) {
|
|
39
|
-
this.delegate.end(endTime);
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
this.#endTime = endTime ?? Date.now();
|
|
43
|
-
this.#ended = true;
|
|
44
|
-
}
|
|
45
|
-
setError(err) {
|
|
46
|
-
if (this.delegate) {
|
|
47
|
-
this.delegate.setError?.(err);
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
this.#error = err;
|
|
51
|
-
this.#hasError = true;
|
|
52
|
-
}
|
|
53
|
-
replay(real) {
|
|
54
|
-
if (this.#hasError) {
|
|
55
|
-
real.setError?.(this.#error);
|
|
56
|
-
}
|
|
57
|
-
if (this.#ended) {
|
|
58
|
-
real.end(this.#endTime);
|
|
59
|
-
} else {
|
|
60
|
-
this.delegate = real;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
var BufferingTracingBackend = class {
|
|
65
|
-
#pending = [];
|
|
66
|
-
#counter = 0;
|
|
67
|
-
startSpan(options) {
|
|
68
|
-
const span2 = new BufferedSpan(options, ++this.#counter);
|
|
69
|
-
this.#pending.push(span2);
|
|
70
|
-
return span2;
|
|
71
|
-
}
|
|
72
|
-
/** Discard all buffered spans without replaying them. */
|
|
73
|
-
clear() {
|
|
74
|
-
this.#pending.length = 0;
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Replay all buffered spans into {@link backend}.
|
|
78
|
-
*
|
|
79
|
-
* @returns Map from synthetic buffered traceparent to real {@link TraceContextData},
|
|
80
|
-
* used by the post-drain translating wrapper to resolve stale buffered IDs
|
|
81
|
-
* still present on in-flight {@link Context} objects.
|
|
82
|
-
*/
|
|
83
|
-
drain(backend) {
|
|
84
|
-
const idMap = /* @__PURE__ */ new Map();
|
|
85
|
-
for (const buffered of this.#pending) {
|
|
86
|
-
let parentContext = buffered.options.parentContext;
|
|
87
|
-
if (parentContext && parentContext.traceparent.startsWith(BUFFERED_PREFIX)) {
|
|
88
|
-
parentContext = idMap.get(parentContext.traceparent) ?? parentContext;
|
|
89
|
-
}
|
|
90
|
-
const real = backend.startSpan({
|
|
91
|
-
...buffered.options,
|
|
92
|
-
parentContext,
|
|
93
|
-
startTime: buffered.startTime
|
|
94
|
-
});
|
|
95
|
-
if (real.spanContext) {
|
|
96
|
-
idMap.set(buffered.spanContext.traceparent, real.spanContext);
|
|
97
|
-
}
|
|
98
|
-
buffered.replay(real);
|
|
99
|
-
}
|
|
100
|
-
this.#pending.length = 0;
|
|
101
|
-
return idMap;
|
|
102
|
-
}
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
// src/diagnostic.ts
|
|
106
|
-
import { asyncTimeout } from "@dxos/async";
|
|
107
|
-
import { invariant } from "@dxos/invariant";
|
|
108
|
-
|
|
109
|
-
// src/util.ts
|
|
110
|
-
var createId = () => Math.random().toString(36).slice(2);
|
|
111
|
-
|
|
112
|
-
// src/diagnostic.ts
|
|
113
|
-
var __dxlog_file = "/__w/dxos/dxos/packages/common/tracing/src/diagnostic.ts";
|
|
114
|
-
var DIAGNOSTICS_TIMEOUT = 1e4;
|
|
115
|
-
var TraceDiagnosticImpl = class {
|
|
116
|
-
id;
|
|
117
|
-
fetch;
|
|
118
|
-
name;
|
|
119
|
-
_onUnregister;
|
|
120
|
-
constructor(id, fetch, name, _onUnregister) {
|
|
121
|
-
this.id = id;
|
|
122
|
-
this.fetch = fetch;
|
|
123
|
-
this.name = name;
|
|
124
|
-
this._onUnregister = _onUnregister;
|
|
125
|
-
}
|
|
126
|
-
unregister() {
|
|
127
|
-
this._onUnregister();
|
|
128
|
-
}
|
|
129
|
-
};
|
|
130
|
-
var DiagnosticsManager = class {
|
|
131
|
-
instanceId = createId();
|
|
132
|
-
registry = /* @__PURE__ */ new Map();
|
|
133
|
-
_instanceTag = null;
|
|
134
|
-
get instanceTag() {
|
|
135
|
-
return this._instanceTag;
|
|
136
|
-
}
|
|
137
|
-
setInstanceTag(tag) {
|
|
138
|
-
this._instanceTag = tag;
|
|
139
|
-
}
|
|
140
|
-
registerDiagnostic(params) {
|
|
141
|
-
const impl = new TraceDiagnosticImpl(params.id, params.fetch, params.name ?? params.id, () => {
|
|
142
|
-
if (this.registry.get(params.id) === impl) {
|
|
143
|
-
this.registry.delete(params.id);
|
|
144
|
-
}
|
|
145
|
-
});
|
|
146
|
-
this.registry.set(params.id, impl);
|
|
147
|
-
return impl;
|
|
148
|
-
}
|
|
149
|
-
list() {
|
|
150
|
-
return Array.from(this.registry.values()).map((diagnostic2) => ({
|
|
151
|
-
id: diagnostic2.id,
|
|
152
|
-
instanceId: this.instanceId,
|
|
153
|
-
instanceTag: this._instanceTag,
|
|
154
|
-
name: diagnostic2.name
|
|
155
|
-
}));
|
|
156
|
-
}
|
|
157
|
-
async fetch(request) {
|
|
158
|
-
if (request.instanceId != null) {
|
|
159
|
-
invariant(request.instanceId === this.instanceId, "Invalid instance id", { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 52, S: this, A: ["request.instanceId === this.instanceId", "'Invalid instance id'"] });
|
|
160
|
-
}
|
|
161
|
-
const { id } = request;
|
|
162
|
-
const diagnostic2 = this.registry.get(id);
|
|
163
|
-
invariant(diagnostic2, "Diagnostic not found", { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 56, S: this, A: ["diagnostic", "'Diagnostic not found'"] });
|
|
164
|
-
try {
|
|
165
|
-
const data = await asyncTimeout(diagnostic2.fetch(), DIAGNOSTICS_TIMEOUT);
|
|
166
|
-
return {
|
|
167
|
-
id,
|
|
168
|
-
instanceId: this.instanceId,
|
|
169
|
-
data
|
|
170
|
-
};
|
|
171
|
-
} catch (err) {
|
|
172
|
-
return {
|
|
173
|
-
id,
|
|
174
|
-
instanceId: this.instanceId,
|
|
175
|
-
data: null,
|
|
176
|
-
error: err.stack
|
|
177
|
-
};
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
};
|
|
181
|
-
|
|
182
|
-
// src/diagnostics-channel.ts
|
|
183
|
-
import { Trigger, sleep } from "@dxos/async";
|
|
184
|
-
import { Context } from "@dxos/context";
|
|
185
|
-
import { invariant as invariant2 } from "@dxos/invariant";
|
|
186
|
-
var __dxlog_file2 = "/__w/dxos/dxos/packages/common/tracing/src/diagnostics-channel.ts";
|
|
187
|
-
var DEFAULT_CHANNEL_NAME = "dxos-diagnostics";
|
|
188
|
-
var DISCOVER_TIME = 500;
|
|
189
|
-
var DiagnosticsChannel = class _DiagnosticsChannel {
|
|
190
|
-
_channelName;
|
|
191
|
-
static get supported() {
|
|
192
|
-
return globalThis.BroadcastChannel != null;
|
|
193
|
-
}
|
|
194
|
-
_ctx = new Context(void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 16 });
|
|
195
|
-
// Separate channels becauase the client and server may be in the same process.
|
|
196
|
-
_serveChannel = void 0;
|
|
197
|
-
_clientChannel = void 0;
|
|
198
|
-
constructor(_channelName = DEFAULT_CHANNEL_NAME) {
|
|
199
|
-
this._channelName = _channelName;
|
|
200
|
-
if (_DiagnosticsChannel.supported) {
|
|
201
|
-
this._serveChannel = new BroadcastChannel(_channelName);
|
|
202
|
-
this._clientChannel = new BroadcastChannel(_channelName);
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
destroy() {
|
|
206
|
-
void this._ctx.dispose();
|
|
207
|
-
this._serveChannel?.close();
|
|
208
|
-
this._clientChannel?.close();
|
|
209
|
-
}
|
|
210
|
-
/**
|
|
211
|
-
* In node.js, the channel will keep the process alive.
|
|
212
|
-
* This method allows the process to exit.
|
|
213
|
-
* Noop in the browser.
|
|
214
|
-
*/
|
|
215
|
-
unref() {
|
|
216
|
-
if (this._serveChannel && typeof this._serveChannel.unref === "function") {
|
|
217
|
-
this._serveChannel.unref();
|
|
218
|
-
this._clientChannel.unref();
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
serve(manager) {
|
|
222
|
-
invariant2(this._serveChannel, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 43, S: this, A: ["this._serveChannel", ""] });
|
|
223
|
-
const listener = async (event) => {
|
|
224
|
-
switch (event.data.type) {
|
|
225
|
-
case "DIAGNOSTICS_DISCOVER": {
|
|
226
|
-
const diagnostics = manager.list();
|
|
227
|
-
this._serveChannel.postMessage({
|
|
228
|
-
type: "DIAGNOSTICS_ANNOUNCE",
|
|
229
|
-
diagnostics
|
|
230
|
-
});
|
|
231
|
-
break;
|
|
232
|
-
}
|
|
233
|
-
case "DIAGNOSTICS_FETCH": {
|
|
234
|
-
const { requestId, request } = event.data;
|
|
235
|
-
if (request.instanceId != null && request.instanceId !== manager.instanceId) {
|
|
236
|
-
break;
|
|
237
|
-
} else if (request.instanceTag != null && request.instanceTag !== manager.instanceTag) {
|
|
238
|
-
break;
|
|
239
|
-
}
|
|
240
|
-
const data = await manager.fetch(request);
|
|
241
|
-
this._serveChannel.postMessage({
|
|
242
|
-
type: "DIAGNOSTICS_RESPONSE",
|
|
243
|
-
requestId,
|
|
244
|
-
data
|
|
245
|
-
});
|
|
246
|
-
break;
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
};
|
|
250
|
-
this._serveChannel.addEventListener("message", listener);
|
|
251
|
-
this._ctx.onDispose(() => this._serveChannel.removeEventListener("message", listener));
|
|
252
|
-
}
|
|
253
|
-
async discover() {
|
|
254
|
-
invariant2(this._clientChannel, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 77, S: this, A: ["this._clientChannel", ""] });
|
|
255
|
-
const diagnostics = [];
|
|
256
|
-
const collector = (event) => {
|
|
257
|
-
const data = event.data;
|
|
258
|
-
switch (data.type) {
|
|
259
|
-
case "DIAGNOSTICS_ANNOUNCE":
|
|
260
|
-
diagnostics.push(...data.diagnostics);
|
|
261
|
-
break;
|
|
262
|
-
}
|
|
263
|
-
};
|
|
264
|
-
try {
|
|
265
|
-
this._clientChannel.addEventListener("message", collector);
|
|
266
|
-
this._clientChannel.postMessage({
|
|
267
|
-
type: "DIAGNOSTICS_DISCOVER"
|
|
268
|
-
});
|
|
269
|
-
await sleep(DISCOVER_TIME);
|
|
270
|
-
const result = [];
|
|
271
|
-
for (const diagnostic2 of diagnostics) {
|
|
272
|
-
if (!result.some((d) => d.id === diagnostic2.id && d.instanceId === diagnostic2.instanceId)) {
|
|
273
|
-
result.push(diagnostic2);
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
return diagnostics;
|
|
277
|
-
} finally {
|
|
278
|
-
this._clientChannel.removeEventListener("message", collector);
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
async fetch(request) {
|
|
282
|
-
invariant2(this._clientChannel, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 106, S: this, A: ["this._clientChannel", ""] });
|
|
283
|
-
const requestId = createId();
|
|
284
|
-
const trigger = new Trigger();
|
|
285
|
-
const listener = (event) => {
|
|
286
|
-
const data = event.data;
|
|
287
|
-
if (data.type === "DIAGNOSTICS_RESPONSE" && data.requestId === requestId) {
|
|
288
|
-
trigger.wake(data.data);
|
|
289
|
-
}
|
|
290
|
-
};
|
|
291
|
-
try {
|
|
292
|
-
this._clientChannel.addEventListener("message", listener);
|
|
293
|
-
this._clientChannel.postMessage({
|
|
294
|
-
type: "DIAGNOSTICS_FETCH",
|
|
295
|
-
requestId,
|
|
296
|
-
request
|
|
297
|
-
});
|
|
298
|
-
const result = await trigger.wait({
|
|
299
|
-
timeout: DIAGNOSTICS_TIMEOUT
|
|
300
|
-
});
|
|
301
|
-
return result;
|
|
302
|
-
} finally {
|
|
303
|
-
this._clientChannel.removeEventListener("message", listener);
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
};
|
|
307
|
-
|
|
308
|
-
// src/remote/metrics.ts
|
|
309
|
-
var RemoteMetrics = class {
|
|
310
|
-
_metrics = /* @__PURE__ */ new Set();
|
|
311
|
-
registerProcessor(processor) {
|
|
312
|
-
this._metrics.add(processor);
|
|
313
|
-
}
|
|
314
|
-
increment(name, value, data) {
|
|
315
|
-
return Array.from(this._metrics.values()).map((processor) => processor.increment(name, value, data));
|
|
316
|
-
}
|
|
317
|
-
distribution(name, value, data) {
|
|
318
|
-
return Array.from(this._metrics.values()).map((processor) => processor.distribution(name, value, data));
|
|
319
|
-
}
|
|
320
|
-
set(name, value, data) {
|
|
321
|
-
return Array.from(this._metrics.values()).map((processor) => processor.set(name, value, data));
|
|
322
|
-
}
|
|
323
|
-
gauge(name, value, data) {
|
|
324
|
-
return Array.from(this._metrics.values()).map((processor) => processor.gauge(name, value, data));
|
|
325
|
-
}
|
|
326
|
-
};
|
|
327
|
-
|
|
328
|
-
// src/weak-ref.ts
|
|
329
|
-
var WeakRefMock = class {
|
|
330
|
-
// eslint-disable-next-line @typescript-eslint/no-useless-constructor
|
|
331
|
-
constructor(target) {
|
|
332
|
-
}
|
|
333
|
-
deref() {
|
|
334
|
-
return void 0;
|
|
335
|
-
}
|
|
336
|
-
};
|
|
337
|
-
var WeakRef = globalThis.WeakRef ?? WeakRefMock;
|
|
338
|
-
|
|
339
|
-
// src/trace-processor.ts
|
|
340
|
-
var __dxlog_file3 = "/__w/dxos/dxos/packages/common/tracing/src/trace-processor.ts";
|
|
341
|
-
var ResourceEntry = class {
|
|
342
|
-
data;
|
|
343
|
-
instance;
|
|
344
|
-
annotation;
|
|
345
|
-
/**
|
|
346
|
-
* Sometimes bundlers mangle class names: WebFile -> WebFile2.
|
|
347
|
-
*
|
|
348
|
-
* We use a heuristic to remove the suffix.
|
|
349
|
-
*/
|
|
350
|
-
sanitizedClassName;
|
|
351
|
-
constructor(data, instance, annotation) {
|
|
352
|
-
this.data = data;
|
|
353
|
-
this.instance = instance;
|
|
354
|
-
this.annotation = annotation;
|
|
355
|
-
this.sanitizedClassName = sanitizeClassName(data.className);
|
|
356
|
-
}
|
|
357
|
-
getMetric(name) {
|
|
358
|
-
return this.data.metrics?.find((metric) => metric.name === name);
|
|
359
|
-
}
|
|
360
|
-
};
|
|
361
|
-
var MAX_RESOURCE_RECORDS = 2e3;
|
|
362
|
-
var MAX_LOG_RECORDS = 1e3;
|
|
363
|
-
var MAX_INFO_OBJECT_DEPTH = 8;
|
|
364
|
-
var TraceProcessor = class {
|
|
365
|
-
diagnostics = new DiagnosticsManager();
|
|
366
|
-
diagnosticsChannel = new DiagnosticsChannel();
|
|
367
|
-
remoteMetrics = new RemoteMetrics();
|
|
368
|
-
#bufferingBackend = new BufferingTracingBackend();
|
|
369
|
-
#activeBackend = this.#bufferingBackend;
|
|
370
|
-
/**
|
|
371
|
-
* Tracing backend. Initially a buffering backend that records spans;
|
|
372
|
-
* once the observability package sets a real backend, the buffer is drained
|
|
373
|
-
* and a thin translating wrapper is installed that resolves stale buffered
|
|
374
|
-
* parent IDs still held by in-flight {@link Context} objects.
|
|
375
|
-
*
|
|
376
|
-
* The wrapper only allocates when a `buffered-*` parent is actually encountered;
|
|
377
|
-
* the common path is a single `startsWith` check and direct passthrough.
|
|
378
|
-
*/
|
|
379
|
-
get tracingBackend() {
|
|
380
|
-
return this.#activeBackend;
|
|
381
|
-
}
|
|
382
|
-
set tracingBackend(backend) {
|
|
383
|
-
if (!backend || backend === this.#bufferingBackend) {
|
|
384
|
-
this.#bufferingBackend.clear();
|
|
385
|
-
this.#activeBackend = this.#bufferingBackend;
|
|
386
|
-
return;
|
|
387
|
-
}
|
|
388
|
-
const idMap = this.#bufferingBackend.drain(backend);
|
|
389
|
-
this.#activeBackend = {
|
|
390
|
-
startSpan: (options) => {
|
|
391
|
-
const parent = options.parentContext;
|
|
392
|
-
if (parent?.traceparent.startsWith(BUFFERED_PREFIX)) {
|
|
393
|
-
const translated = idMap.get(parent.traceparent);
|
|
394
|
-
if (translated) {
|
|
395
|
-
return backend.startSpan({
|
|
396
|
-
...options,
|
|
397
|
-
parentContext: translated
|
|
398
|
-
});
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
return backend.startSpan(options);
|
|
402
|
-
}
|
|
403
|
-
};
|
|
404
|
-
}
|
|
405
|
-
resources = /* @__PURE__ */ new Map();
|
|
406
|
-
resourceInstanceIndex = /* @__PURE__ */ new WeakMap();
|
|
407
|
-
resourceIdList = [];
|
|
408
|
-
logs = [];
|
|
409
|
-
_instanceTag = null;
|
|
410
|
-
constructor() {
|
|
411
|
-
log.addProcessor(this._logProcessor.bind(this), void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 80, S: this });
|
|
412
|
-
if (DiagnosticsChannel.supported) {
|
|
413
|
-
this.diagnosticsChannel.serve(this.diagnostics);
|
|
414
|
-
}
|
|
415
|
-
this.diagnosticsChannel.unref();
|
|
416
|
-
}
|
|
417
|
-
setInstanceTag(tag) {
|
|
418
|
-
this._instanceTag = tag;
|
|
419
|
-
this.diagnostics.setInstanceTag(tag);
|
|
420
|
-
}
|
|
421
|
-
/** @internal */
|
|
422
|
-
createTraceResource(params) {
|
|
423
|
-
const id = this.resources.size;
|
|
424
|
-
const tracingContext = getTracingContext(Object.getPrototypeOf(params.instance));
|
|
425
|
-
for (const key of Object.keys(tracingContext.metricsProperties)) {
|
|
426
|
-
params.instance[key]._assign(params.instance, key);
|
|
427
|
-
}
|
|
428
|
-
const entry = new ResourceEntry({
|
|
429
|
-
id,
|
|
430
|
-
className: params.constructor.name,
|
|
431
|
-
instanceId: getPrototypeSpecificInstanceId(params.instance),
|
|
432
|
-
info: this.getResourceInfo(params.instance),
|
|
433
|
-
links: [],
|
|
434
|
-
metrics: this.getResourceMetrics(params.instance)
|
|
435
|
-
}, new WeakRef(params.instance), params.annotation);
|
|
436
|
-
this.resources.set(id, entry);
|
|
437
|
-
this.resourceInstanceIndex.set(params.instance, entry);
|
|
438
|
-
this.resourceIdList.push(id);
|
|
439
|
-
if (this.resourceIdList.length > MAX_RESOURCE_RECORDS) {
|
|
440
|
-
this._clearResources();
|
|
441
|
-
}
|
|
442
|
-
}
|
|
443
|
-
// TODO(burdon): Not implemented.
|
|
444
|
-
addLink(parent, child, opts) {
|
|
445
|
-
}
|
|
446
|
-
getDiagnostics() {
|
|
447
|
-
this.refresh();
|
|
448
|
-
return {
|
|
449
|
-
resources: Object.fromEntries(Array.from(this.resources.entries()).map(([id, entry]) => [
|
|
450
|
-
`${entry.sanitizedClassName}#${entry.data.instanceId}`,
|
|
451
|
-
entry.data
|
|
452
|
-
])),
|
|
453
|
-
logs: this.logs.filter((log2) => log2.level >= LogLevel.INFO)
|
|
454
|
-
};
|
|
455
|
-
}
|
|
456
|
-
getResourceInfo(instance) {
|
|
457
|
-
const res = {};
|
|
458
|
-
const tracingContext = getTracingContext(Object.getPrototypeOf(instance));
|
|
459
|
-
for (const [key, { options }] of Object.entries(tracingContext.infoProperties)) {
|
|
460
|
-
try {
|
|
461
|
-
const value = typeof instance[key] === "function" ? instance[key]() : instance[key];
|
|
462
|
-
if (options.enum) {
|
|
463
|
-
res[key] = options.enum[value];
|
|
464
|
-
} else {
|
|
465
|
-
res[key] = sanitizeValue(value, options.depth === void 0 ? 1 : options.depth ?? MAX_INFO_OBJECT_DEPTH, this);
|
|
466
|
-
}
|
|
467
|
-
} catch (err) {
|
|
468
|
-
res[key] = err.message;
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
return res;
|
|
472
|
-
}
|
|
473
|
-
getResourceMetrics(instance) {
|
|
474
|
-
const res = [];
|
|
475
|
-
const tracingContext = getTracingContext(Object.getPrototypeOf(instance));
|
|
476
|
-
for (const [key, _opts] of Object.entries(tracingContext.metricsProperties)) {
|
|
477
|
-
res.push(instance[key].getData());
|
|
478
|
-
}
|
|
479
|
-
return res;
|
|
480
|
-
}
|
|
481
|
-
getResourceId(instance) {
|
|
482
|
-
const entry = this.resourceInstanceIndex.get(instance);
|
|
483
|
-
return entry ? entry.data.id : null;
|
|
484
|
-
}
|
|
485
|
-
findResourcesByClassName(className) {
|
|
486
|
-
return [
|
|
487
|
-
...this.resources.values()
|
|
488
|
-
].filter((res) => res.data.className === className || res.sanitizedClassName === className);
|
|
489
|
-
}
|
|
490
|
-
findResourcesByAnnotation(annotation) {
|
|
491
|
-
return [
|
|
492
|
-
...this.resources.values()
|
|
493
|
-
].filter((res) => res.annotation === annotation);
|
|
494
|
-
}
|
|
495
|
-
refresh() {
|
|
496
|
-
for (const resource2 of this.resources.values()) {
|
|
497
|
-
const instance = resource2.instance.deref();
|
|
498
|
-
if (!instance) {
|
|
499
|
-
continue;
|
|
500
|
-
}
|
|
501
|
-
const tracingContext = getTracingContext(Object.getPrototypeOf(instance));
|
|
502
|
-
const time = performance.now();
|
|
503
|
-
instance.tick?.(time);
|
|
504
|
-
for (const key of Object.keys(tracingContext.metricsProperties)) {
|
|
505
|
-
instance[key]._tick?.(time);
|
|
506
|
-
}
|
|
507
|
-
resource2.data.info = this.getResourceInfo(instance);
|
|
508
|
-
resource2.data.metrics = this.getResourceMetrics(instance);
|
|
509
|
-
}
|
|
510
|
-
}
|
|
511
|
-
_clearResources() {
|
|
512
|
-
while (this.resourceIdList.length > MAX_RESOURCE_RECORDS) {
|
|
513
|
-
const id = this.resourceIdList.shift();
|
|
514
|
-
this.resources.delete(id);
|
|
515
|
-
}
|
|
516
|
-
}
|
|
517
|
-
_pushLog(log2) {
|
|
518
|
-
this.logs.push(log2);
|
|
519
|
-
if (this.logs.length > MAX_LOG_RECORDS) {
|
|
520
|
-
this.logs.shift();
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
_logProcessor = (config, entry) => {
|
|
524
|
-
switch (entry.level) {
|
|
525
|
-
case LogLevel.ERROR:
|
|
526
|
-
case LogLevel.WARN:
|
|
527
|
-
case LogLevel.TRACE: {
|
|
528
|
-
const scope = entry.meta?.S;
|
|
529
|
-
const resource2 = this.resourceInstanceIndex.get(scope);
|
|
530
|
-
if (!resource2) {
|
|
531
|
-
return;
|
|
532
|
-
}
|
|
533
|
-
const context = {
|
|
534
|
-
...entry.computedContext
|
|
535
|
-
};
|
|
536
|
-
if (entry.computedError !== void 0) {
|
|
537
|
-
context.error = entry.computedError;
|
|
538
|
-
}
|
|
539
|
-
const { filename, line } = entry.computedMeta;
|
|
540
|
-
const entryToPush = {
|
|
541
|
-
level: entry.level,
|
|
542
|
-
message: entry.message ?? entry.computedError ?? "",
|
|
543
|
-
context,
|
|
544
|
-
timestamp: new Date(entry.timestamp),
|
|
545
|
-
meta: {
|
|
546
|
-
file: filename ?? "",
|
|
547
|
-
line: line ?? 0,
|
|
548
|
-
resourceId: resource2.data.id
|
|
549
|
-
}
|
|
550
|
-
};
|
|
551
|
-
this._pushLog(entryToPush);
|
|
552
|
-
break;
|
|
553
|
-
}
|
|
554
|
-
default:
|
|
555
|
-
}
|
|
556
|
-
};
|
|
557
|
-
};
|
|
558
|
-
var TRACE_PROCESSOR = globalThis.TRACE_PROCESSOR ??= new TraceProcessor();
|
|
559
|
-
var sanitizeValue = (value, depth, traceProcessor) => {
|
|
560
|
-
switch (typeof value) {
|
|
561
|
-
case "string":
|
|
562
|
-
case "number":
|
|
563
|
-
case "boolean":
|
|
564
|
-
case "undefined":
|
|
565
|
-
return value;
|
|
566
|
-
case "object":
|
|
567
|
-
case "function":
|
|
568
|
-
if (value === null) {
|
|
569
|
-
return value;
|
|
570
|
-
}
|
|
571
|
-
{
|
|
572
|
-
const resourceEntry = traceProcessor.resourceInstanceIndex.get(value);
|
|
573
|
-
if (resourceEntry) {
|
|
574
|
-
return `${resourceEntry.sanitizedClassName}#${resourceEntry.data.instanceId}`;
|
|
575
|
-
}
|
|
576
|
-
}
|
|
577
|
-
if (typeof value.toJSON === "function") {
|
|
578
|
-
return sanitizeValue(value.toJSON(), depth, traceProcessor);
|
|
579
|
-
}
|
|
580
|
-
if (depth > 0) {
|
|
581
|
-
if (isSetLike(value)) {
|
|
582
|
-
return Object.fromEntries(Array.from(value.entries()).map((value2) => sanitizeValue(value2, depth - 1, traceProcessor)));
|
|
583
|
-
} else if (isMapLike(value)) {
|
|
584
|
-
return Object.fromEntries(Array.from(value.entries()).map(([key, value2]) => [
|
|
585
|
-
key,
|
|
586
|
-
sanitizeValue(value2, depth - 1, traceProcessor)
|
|
587
|
-
]));
|
|
588
|
-
} else if (Array.isArray(value)) {
|
|
589
|
-
return value.map((item) => sanitizeValue(item, depth - 1, traceProcessor));
|
|
590
|
-
} else if (typeof value === "object") {
|
|
591
|
-
const res = {};
|
|
592
|
-
for (const key of Object.keys(value)) {
|
|
593
|
-
res[key] = sanitizeValue(value[key], depth - 1, traceProcessor);
|
|
594
|
-
}
|
|
595
|
-
return res;
|
|
596
|
-
}
|
|
597
|
-
}
|
|
598
|
-
if (typeof value.truncate === "function") {
|
|
599
|
-
return value.truncate();
|
|
600
|
-
}
|
|
601
|
-
return value.toString();
|
|
602
|
-
}
|
|
603
|
-
};
|
|
604
|
-
var sanitizeClassName = (className) => {
|
|
605
|
-
let name = className.replace(/^_+/, "");
|
|
606
|
-
const SANITIZE_REGEX = /[^_](\d+)$/;
|
|
607
|
-
const m = name.match(SANITIZE_REGEX);
|
|
608
|
-
if (m) {
|
|
609
|
-
name = name.slice(0, -m[1].length);
|
|
610
|
-
}
|
|
611
|
-
return name;
|
|
612
|
-
};
|
|
613
|
-
var isSetLike = (value) => value instanceof Set || typeof value === "object" && value !== null && Object.getPrototypeOf(value).constructor.name === "ComplexSet";
|
|
614
|
-
var isMapLike = (value) => value instanceof Map || typeof value === "object" && value !== null && Object.getPrototypeOf(value).constructor.name === "ComplexMap";
|
|
615
|
-
|
|
616
|
-
// src/api.ts
|
|
617
|
-
var __dxlog_file4 = "/__w/dxos/dxos/packages/common/tracing/src/api.ts";
|
|
618
|
-
var LIFECYCLE_SPAN = /* @__PURE__ */ Symbol("dxos.tracing.lifecycle-span");
|
|
619
|
-
var TRACE_ALL_KEY = "dxos.debug.traceAll";
|
|
620
|
-
var collectSpanAttributes = (instance, spanAttributes) => {
|
|
621
|
-
const proto = Object.getPrototypeOf(instance);
|
|
622
|
-
if (!proto) {
|
|
623
|
-
return;
|
|
624
|
-
}
|
|
625
|
-
const tracingContext = getTracingContext(proto);
|
|
626
|
-
for (const [key, { options }] of Object.entries(tracingContext.infoProperties)) {
|
|
627
|
-
if (!options.spanAttribute) {
|
|
628
|
-
continue;
|
|
629
|
-
}
|
|
630
|
-
try {
|
|
631
|
-
const value = typeof instance[key] === "function" ? instance[key]() : instance[key];
|
|
632
|
-
if (value != null) {
|
|
633
|
-
const resolved = options.enum ? options.enum[value] : String(value);
|
|
634
|
-
spanAttributes[`ctx.${key}`] = resolved;
|
|
635
|
-
}
|
|
636
|
-
} catch {
|
|
637
|
-
}
|
|
638
|
-
}
|
|
639
|
-
};
|
|
640
|
-
var resource = (options) => (constructor) => {
|
|
641
|
-
if (options?.lifecycle && !(constructor.prototype instanceof Resource)) {
|
|
642
|
-
throw new Error(`@trace.resource({ lifecycle: true }) requires ${constructor.name} to extend Resource`);
|
|
643
|
-
}
|
|
644
|
-
const klass = /* @__PURE__ */ (() => class extends constructor {
|
|
645
|
-
constructor(...rest) {
|
|
646
|
-
super(...rest);
|
|
647
|
-
TRACE_PROCESSOR.createTraceResource({
|
|
648
|
-
constructor,
|
|
649
|
-
annotation: options?.annotation,
|
|
650
|
-
instance: this
|
|
651
|
-
});
|
|
652
|
-
}
|
|
653
|
-
})();
|
|
654
|
-
if (options?.lifecycle) {
|
|
655
|
-
const sanitizedName = sanitizeClassName(constructor.name);
|
|
656
|
-
const proto = klass.prototype;
|
|
657
|
-
const originalOpen = proto.open;
|
|
658
|
-
const originalClose = proto.close;
|
|
659
|
-
proto.open = async function(ctx) {
|
|
660
|
-
const self = this;
|
|
661
|
-
if (self._lifecycleState !== LifecycleState.CLOSED) {
|
|
662
|
-
return originalOpen.call(this, ctx);
|
|
663
|
-
}
|
|
664
|
-
const parentSpanContext = ctx?.getAttribute(TRACE_SPAN_ATTRIBUTE);
|
|
665
|
-
const resourceEntry = TRACE_PROCESSOR.resourceInstanceIndex.get(this);
|
|
666
|
-
const spanAttributes = {};
|
|
667
|
-
if (resourceEntry) {
|
|
668
|
-
spanAttributes.entryPoint = resourceEntry.sanitizedClassName;
|
|
669
|
-
}
|
|
670
|
-
const remoteSpan = TRACE_PROCESSOR.tracingBackend?.startSpan({
|
|
671
|
-
name: `${sanitizedName}.lifecycle`,
|
|
672
|
-
op: "lifecycle",
|
|
673
|
-
attributes: spanAttributes,
|
|
674
|
-
parentContext: parentSpanContext
|
|
675
|
-
});
|
|
676
|
-
self[LIFECYCLE_SPAN] = remoteSpan;
|
|
677
|
-
let openCtx = ctx;
|
|
678
|
-
if (remoteSpan?.spanContext != null) {
|
|
679
|
-
const traceAttrs = {
|
|
680
|
-
[TRACE_SPAN_ATTRIBUTE]: remoteSpan.spanContext
|
|
681
|
-
};
|
|
682
|
-
openCtx = ctx ? ctx.derive({
|
|
683
|
-
attributes: traceAttrs
|
|
684
|
-
}) : new Context2({
|
|
685
|
-
attributes: traceAttrs
|
|
686
|
-
}, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 79 });
|
|
687
|
-
}
|
|
688
|
-
try {
|
|
689
|
-
return await originalOpen.call(this, openCtx);
|
|
690
|
-
} catch (err) {
|
|
691
|
-
remoteSpan?.setError?.(err);
|
|
692
|
-
remoteSpan?.end();
|
|
693
|
-
self[LIFECYCLE_SPAN] = void 0;
|
|
694
|
-
throw err;
|
|
695
|
-
}
|
|
696
|
-
};
|
|
697
|
-
proto.close = async function(ctx) {
|
|
698
|
-
const self = this;
|
|
699
|
-
const remoteSpan = self[LIFECYCLE_SPAN];
|
|
700
|
-
try {
|
|
701
|
-
return await originalClose.call(this, ctx);
|
|
702
|
-
} catch (err) {
|
|
703
|
-
remoteSpan?.setError?.(err);
|
|
704
|
-
throw err;
|
|
705
|
-
} finally {
|
|
706
|
-
if (remoteSpan) {
|
|
707
|
-
remoteSpan.end();
|
|
708
|
-
self[LIFECYCLE_SPAN] = void 0;
|
|
709
|
-
}
|
|
710
|
-
}
|
|
711
|
-
};
|
|
712
|
-
}
|
|
713
|
-
Object.defineProperty(klass, "name", {
|
|
714
|
-
value: constructor.name
|
|
715
|
-
});
|
|
716
|
-
return klass;
|
|
717
|
-
};
|
|
718
|
-
var info = (opts = {}) => (target, propertyKey, descriptor) => {
|
|
719
|
-
getTracingContext(target).infoProperties[propertyKey] = {
|
|
720
|
-
options: opts
|
|
721
|
-
};
|
|
722
|
-
};
|
|
723
|
-
var mark = (name) => {
|
|
724
|
-
performance.mark(name);
|
|
725
|
-
};
|
|
726
|
-
var span = ({ showInBrowserTimeline = false, showInRemoteTracing = true, op, attributes } = {}) => (target, propertyKey, descriptor) => {
|
|
727
|
-
const method = descriptor.value;
|
|
728
|
-
descriptor.value = async function(...args) {
|
|
729
|
-
const parentCtx = args[0] instanceof Context2 ? args[0] : null;
|
|
730
|
-
const startTs = performance.now();
|
|
731
|
-
const parentSpanContext = parentCtx?.getAttribute(TRACE_SPAN_ATTRIBUTE);
|
|
732
|
-
const resourceEntry = TRACE_PROCESSOR.resourceInstanceIndex.get(this);
|
|
733
|
-
const className = resourceEntry?.sanitizedClassName ?? sanitizeClassName(target.constructor?.name ?? "unknown");
|
|
734
|
-
const spanName = `${className}.${propertyKey}`;
|
|
735
|
-
const spanAttributes = {};
|
|
736
|
-
if (resourceEntry) {
|
|
737
|
-
spanAttributes.entryPoint = resourceEntry.sanitizedClassName;
|
|
738
|
-
}
|
|
739
|
-
collectSpanAttributes(this, spanAttributes);
|
|
740
|
-
if (attributes) {
|
|
741
|
-
for (const [key, value] of Object.entries(attributes)) {
|
|
742
|
-
spanAttributes[key.startsWith("ctx.") ? key : `ctx.${key}`] = value;
|
|
743
|
-
}
|
|
744
|
-
}
|
|
745
|
-
const remoteSpan = showInRemoteTracing ? TRACE_PROCESSOR.tracingBackend?.startSpan({
|
|
746
|
-
name: spanName,
|
|
747
|
-
op: op ?? "function",
|
|
748
|
-
attributes: spanAttributes,
|
|
749
|
-
parentContext: parentSpanContext
|
|
750
|
-
}) : void 0;
|
|
751
|
-
let callArgs = args;
|
|
752
|
-
if (parentCtx) {
|
|
753
|
-
const childCtx = remoteSpan?.spanContext != null ? parentCtx.derive({
|
|
754
|
-
attributes: {
|
|
755
|
-
[TRACE_SPAN_ATTRIBUTE]: remoteSpan.spanContext
|
|
756
|
-
}
|
|
757
|
-
}) : parentCtx.derive();
|
|
758
|
-
callArgs = [
|
|
759
|
-
childCtx,
|
|
760
|
-
...args.slice(1)
|
|
761
|
-
];
|
|
762
|
-
}
|
|
763
|
-
try {
|
|
764
|
-
return await method.apply(this, callArgs);
|
|
765
|
-
} catch (err) {
|
|
766
|
-
remoteSpan?.setError?.(err);
|
|
767
|
-
throw err;
|
|
768
|
-
} finally {
|
|
769
|
-
remoteSpan?.end();
|
|
770
|
-
if (showInBrowserTimeline && typeof globalThis?.performance?.measure === "function") {
|
|
771
|
-
performance.measure(spanName, {
|
|
772
|
-
start: startTs,
|
|
773
|
-
end: performance.now()
|
|
774
|
-
});
|
|
775
|
-
}
|
|
776
|
-
}
|
|
777
|
-
};
|
|
778
|
-
};
|
|
779
|
-
var manualSpans = /* @__PURE__ */ new Map();
|
|
780
|
-
var manualSpanTimestamps = /* @__PURE__ */ new Map();
|
|
781
|
-
var spanStart = (params) => {
|
|
782
|
-
if (manualSpans.has(params.id) || manualSpanTimestamps.has(params.id)) {
|
|
783
|
-
return params.parentCtx;
|
|
784
|
-
}
|
|
785
|
-
const resourceEntry = TRACE_PROCESSOR.resourceInstanceIndex.get(params.instance);
|
|
786
|
-
const className = resourceEntry?.sanitizedClassName ?? "unknown";
|
|
787
|
-
const spanName = `${className}.${params.methodName}`;
|
|
788
|
-
if (params.showInBrowserTimeline) {
|
|
789
|
-
manualSpanTimestamps.set(params.id, {
|
|
790
|
-
name: spanName,
|
|
791
|
-
startTs: performance.now()
|
|
792
|
-
});
|
|
793
|
-
}
|
|
794
|
-
if (params.showInRemoteTracing === false || !TRACE_PROCESSOR.tracingBackend) {
|
|
795
|
-
return params.parentCtx;
|
|
796
|
-
}
|
|
797
|
-
const parentSpanContext = params.parentCtx?.getAttribute(TRACE_SPAN_ATTRIBUTE);
|
|
798
|
-
const spanAttributes = {};
|
|
799
|
-
if (resourceEntry) {
|
|
800
|
-
spanAttributes.entryPoint = resourceEntry.sanitizedClassName;
|
|
801
|
-
}
|
|
802
|
-
collectSpanAttributes(params.instance, spanAttributes);
|
|
803
|
-
if (params.attributes) {
|
|
804
|
-
for (const [key, value] of Object.entries(params.attributes)) {
|
|
805
|
-
spanAttributes[key.startsWith("ctx.") ? key : `ctx.${key}`] = value;
|
|
806
|
-
}
|
|
807
|
-
}
|
|
808
|
-
const remoteSpan = TRACE_PROCESSOR.tracingBackend.startSpan({
|
|
809
|
-
name: spanName,
|
|
810
|
-
op: params.op ?? "function",
|
|
811
|
-
attributes: spanAttributes,
|
|
812
|
-
parentContext: parentSpanContext
|
|
813
|
-
});
|
|
814
|
-
manualSpans.set(params.id, remoteSpan);
|
|
815
|
-
if (params.parentCtx && remoteSpan.spanContext != null) {
|
|
816
|
-
return params.parentCtx.derive({
|
|
817
|
-
attributes: {
|
|
818
|
-
[TRACE_SPAN_ATTRIBUTE]: remoteSpan.spanContext
|
|
819
|
-
}
|
|
820
|
-
});
|
|
821
|
-
}
|
|
822
|
-
return params.parentCtx;
|
|
823
|
-
};
|
|
824
|
-
var spanEnd = (id) => {
|
|
825
|
-
const remoteSpan = manualSpans.get(id);
|
|
826
|
-
if (remoteSpan) {
|
|
827
|
-
remoteSpan.end();
|
|
828
|
-
manualSpans.delete(id);
|
|
829
|
-
}
|
|
830
|
-
const timestamps = manualSpanTimestamps.get(id);
|
|
831
|
-
if (timestamps && typeof globalThis?.performance?.measure === "function") {
|
|
832
|
-
performance.measure(timestamps.name, {
|
|
833
|
-
start: timestamps.startTs,
|
|
834
|
-
end: performance.now()
|
|
835
|
-
});
|
|
836
|
-
manualSpanTimestamps.delete(id);
|
|
837
|
-
}
|
|
838
|
-
};
|
|
839
|
-
var metricsCounter = () => (target, propertyKey, descriptor) => {
|
|
840
|
-
getTracingContext(target).metricsProperties[propertyKey] = {};
|
|
841
|
-
};
|
|
842
|
-
var addLink = (parent, child, opts = {}) => {
|
|
843
|
-
TRACE_PROCESSOR.addLink(parent, child, opts);
|
|
844
|
-
};
|
|
845
|
-
var diagnostic = (params) => {
|
|
846
|
-
return TRACE_PROCESSOR.diagnostics.registerDiagnostic(params);
|
|
847
|
-
};
|
|
848
|
-
var trace = {
|
|
849
|
-
addLink,
|
|
850
|
-
diagnostic,
|
|
851
|
-
info,
|
|
852
|
-
mark,
|
|
853
|
-
metricsCounter,
|
|
854
|
-
resource,
|
|
855
|
-
span,
|
|
856
|
-
spanStart,
|
|
857
|
-
spanEnd,
|
|
858
|
-
metrics: TRACE_PROCESSOR.remoteMetrics
|
|
859
|
-
};
|
|
860
|
-
|
|
861
|
-
// src/metrics/base.ts
|
|
862
|
-
var BaseCounter = class {
|
|
863
|
-
/**
|
|
864
|
-
* @internal
|
|
865
|
-
*/
|
|
866
|
-
_instance;
|
|
867
|
-
name;
|
|
868
|
-
/**
|
|
869
|
-
* @internal
|
|
870
|
-
*/
|
|
871
|
-
_assign(instance, name) {
|
|
872
|
-
this._instance = instance;
|
|
873
|
-
this.name = name;
|
|
874
|
-
}
|
|
875
|
-
_tick(time) {
|
|
876
|
-
}
|
|
877
|
-
};
|
|
878
|
-
|
|
879
|
-
// src/metrics/unary-counter.ts
|
|
880
|
-
var UnaryCounter = class extends BaseCounter {
|
|
881
|
-
value = 0;
|
|
882
|
-
units;
|
|
883
|
-
constructor({ units } = {}) {
|
|
884
|
-
super();
|
|
885
|
-
this.units = units;
|
|
886
|
-
}
|
|
887
|
-
inc(by = 1) {
|
|
888
|
-
this.value += by;
|
|
889
|
-
}
|
|
890
|
-
getData() {
|
|
891
|
-
return {
|
|
892
|
-
name: this.name,
|
|
893
|
-
counter: {
|
|
894
|
-
value: this.value,
|
|
895
|
-
units: this.units
|
|
896
|
-
}
|
|
897
|
-
};
|
|
898
|
-
}
|
|
899
|
-
};
|
|
900
|
-
|
|
901
|
-
// src/metrics/time-series-counter.ts
|
|
902
|
-
var MAX_BUCKETS = 60;
|
|
903
|
-
var TimeSeriesCounter = class extends BaseCounter {
|
|
904
|
-
_currentValue = 0;
|
|
905
|
-
_totalValue = 0;
|
|
906
|
-
_buckets = [];
|
|
907
|
-
units;
|
|
908
|
-
constructor({ units } = {}) {
|
|
909
|
-
super();
|
|
910
|
-
this.units = units;
|
|
911
|
-
}
|
|
912
|
-
inc(by = 1) {
|
|
913
|
-
this._currentValue += by;
|
|
914
|
-
this._totalValue += by;
|
|
915
|
-
}
|
|
916
|
-
_tick(time) {
|
|
917
|
-
this._buckets.push(this._currentValue);
|
|
918
|
-
if (this._buckets.length > MAX_BUCKETS) {
|
|
919
|
-
this._buckets.shift();
|
|
920
|
-
}
|
|
921
|
-
this._currentValue = 0;
|
|
922
|
-
}
|
|
923
|
-
getData() {
|
|
924
|
-
return {
|
|
925
|
-
name: this.name,
|
|
926
|
-
timeSeries: {
|
|
927
|
-
tracks: [
|
|
928
|
-
{
|
|
929
|
-
name: this.name,
|
|
930
|
-
units: this.units,
|
|
931
|
-
points: this._buckets.map((value, index) => ({
|
|
932
|
-
value
|
|
933
|
-
})),
|
|
934
|
-
total: this._totalValue
|
|
935
|
-
}
|
|
936
|
-
]
|
|
937
|
-
}
|
|
938
|
-
};
|
|
939
|
-
}
|
|
940
|
-
};
|
|
941
|
-
|
|
942
|
-
// src/metrics/time-usage-counter.ts
|
|
943
|
-
var MAX_BUCKETS2 = 60;
|
|
944
|
-
var TimeUsageCounter = class extends BaseCounter {
|
|
945
|
-
_currentValue = 0;
|
|
946
|
-
_totalValue = 0;
|
|
947
|
-
_buckets = [];
|
|
948
|
-
_lastTickTime = performance.now();
|
|
949
|
-
record(time) {
|
|
950
|
-
this._currentValue += time;
|
|
951
|
-
this._totalValue += time;
|
|
952
|
-
}
|
|
953
|
-
beginRecording() {
|
|
954
|
-
const start = performance.now();
|
|
955
|
-
return {
|
|
956
|
-
end: () => {
|
|
957
|
-
const end = performance.now();
|
|
958
|
-
this.record(end - start);
|
|
959
|
-
}
|
|
960
|
-
};
|
|
961
|
-
}
|
|
962
|
-
_tick(time) {
|
|
963
|
-
const delta = time - this._lastTickTime;
|
|
964
|
-
this._lastTickTime = time;
|
|
965
|
-
const percentage = this._currentValue / delta * 100;
|
|
966
|
-
this._buckets.push(percentage);
|
|
967
|
-
if (this._buckets.length > MAX_BUCKETS2) {
|
|
968
|
-
this._buckets.shift();
|
|
969
|
-
}
|
|
970
|
-
this._currentValue = 0;
|
|
971
|
-
}
|
|
972
|
-
getData() {
|
|
973
|
-
return {
|
|
974
|
-
name: this.name,
|
|
975
|
-
timeSeries: {
|
|
976
|
-
tracks: [
|
|
977
|
-
{
|
|
978
|
-
name: this.name,
|
|
979
|
-
units: "%",
|
|
980
|
-
points: this._buckets.map((value, index) => ({
|
|
981
|
-
value
|
|
982
|
-
})),
|
|
983
|
-
total: this._totalValue
|
|
984
|
-
}
|
|
985
|
-
]
|
|
986
|
-
}
|
|
987
|
-
};
|
|
988
|
-
}
|
|
989
|
-
};
|
|
990
|
-
|
|
991
|
-
// src/metrics/map-counter.ts
|
|
992
|
-
var MapCounter = class extends BaseCounter {
|
|
993
|
-
values = /* @__PURE__ */ new Map();
|
|
994
|
-
units;
|
|
995
|
-
constructor({ units } = {}) {
|
|
996
|
-
super();
|
|
997
|
-
this.units = units;
|
|
998
|
-
}
|
|
999
|
-
inc(key, by = 1) {
|
|
1000
|
-
const prev = this.values.get(key) ?? 0;
|
|
1001
|
-
this.values.set(key, prev + by);
|
|
1002
|
-
}
|
|
1003
|
-
getData() {
|
|
1004
|
-
return {
|
|
1005
|
-
name: this.name,
|
|
1006
|
-
multiCounter: {
|
|
1007
|
-
records: Array.from(this.values.entries()).map(([key, value]) => ({
|
|
1008
|
-
key,
|
|
1009
|
-
value
|
|
1010
|
-
})),
|
|
1011
|
-
units: this.units
|
|
1012
|
-
}
|
|
1013
|
-
};
|
|
1014
|
-
}
|
|
1015
|
-
};
|
|
1016
|
-
|
|
1017
|
-
// src/metrics/custom-counter.ts
|
|
1018
|
-
var CustomCounter = class extends BaseCounter {
|
|
1019
|
-
_getData;
|
|
1020
|
-
constructor(_getData) {
|
|
1021
|
-
super(), this._getData = _getData;
|
|
1022
|
-
}
|
|
1023
|
-
getData() {
|
|
1024
|
-
return {
|
|
1025
|
-
name: this.name,
|
|
1026
|
-
custom: {
|
|
1027
|
-
payload: this._getData()
|
|
1028
|
-
}
|
|
1029
|
-
};
|
|
1030
|
-
}
|
|
1031
|
-
};
|
|
1032
|
-
|
|
1033
|
-
// src/index.ts
|
|
1034
|
-
trace.diagnostic({
|
|
1035
|
-
id: "process-info",
|
|
1036
|
-
name: "Process Info",
|
|
1037
|
-
fetch: async () => {
|
|
1038
|
-
return {
|
|
1039
|
-
platform: globalThis.process?.platform,
|
|
1040
|
-
arch: globalThis.process?.arch,
|
|
1041
|
-
versions: globalThis.process?.versions,
|
|
1042
|
-
href: globalThis.location?.href
|
|
1043
|
-
};
|
|
1044
|
-
}
|
|
1045
|
-
});
|
|
1046
|
-
export {
|
|
1047
|
-
BaseCounter,
|
|
1048
|
-
CustomCounter,
|
|
1049
|
-
DIAGNOSTICS_TIMEOUT,
|
|
1050
|
-
DiagnosticsChannel,
|
|
1051
|
-
DiagnosticsManager,
|
|
1052
|
-
MapCounter,
|
|
1053
|
-
RemoteMetrics,
|
|
1054
|
-
ResourceEntry,
|
|
1055
|
-
TRACE_ALL_KEY,
|
|
1056
|
-
TRACE_PROCESSOR,
|
|
1057
|
-
TimeSeriesCounter,
|
|
1058
|
-
TimeUsageCounter,
|
|
1059
|
-
TraceDiagnosticImpl,
|
|
1060
|
-
TraceProcessor,
|
|
1061
|
-
UnaryCounter,
|
|
1062
|
-
getTracingContext,
|
|
1063
|
-
sanitizeClassName,
|
|
1064
|
-
symbolTracingContext,
|
|
1065
|
-
trace
|
|
1066
|
-
};
|
|
1067
|
-
//# sourceMappingURL=index.mjs.map
|