@dxos/tracing 0.8.4-main.dedc0f3 → 0.8.4-main.e00bdcdb52

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/dist/lib/browser/index.mjs +445 -665
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/lib/node-esm/index.mjs +445 -665
  5. package/dist/lib/node-esm/index.mjs.map +4 -4
  6. package/dist/lib/node-esm/meta.json +1 -1
  7. package/dist/types/src/api.d.ts +36 -17
  8. package/dist/types/src/api.d.ts.map +1 -1
  9. package/dist/types/src/buffering-backend.d.ts +24 -0
  10. package/dist/types/src/buffering-backend.d.ts.map +1 -0
  11. package/dist/types/src/diagnostic.d.ts +2 -2
  12. package/dist/types/src/diagnostic.d.ts.map +1 -1
  13. package/dist/types/src/diagnostics-channel.d.ts.map +1 -1
  14. package/dist/types/src/index.d.ts +1 -2
  15. package/dist/types/src/index.d.ts.map +1 -1
  16. package/dist/types/src/metrics/base.d.ts.map +1 -1
  17. package/dist/types/src/metrics/custom-counter.d.ts.map +1 -1
  18. package/dist/types/src/metrics/map-counter.d.ts.map +1 -1
  19. package/dist/types/src/metrics/time-series-counter.d.ts.map +1 -1
  20. package/dist/types/src/metrics/time-usage-counter.d.ts.map +1 -1
  21. package/dist/types/src/metrics/unary-counter.d.ts.map +1 -1
  22. package/dist/types/src/remote/index.d.ts +0 -1
  23. package/dist/types/src/remote/index.d.ts.map +1 -1
  24. package/dist/types/src/remote/metrics.d.ts.map +1 -1
  25. package/dist/types/src/symbols.d.ts +0 -1
  26. package/dist/types/src/symbols.d.ts.map +1 -1
  27. package/dist/types/src/trace-processor.d.ts +16 -52
  28. package/dist/types/src/trace-processor.d.ts.map +1 -1
  29. package/dist/types/src/tracing-types.d.ts +67 -0
  30. package/dist/types/src/tracing-types.d.ts.map +1 -0
  31. package/dist/types/tsconfig.tsbuildinfo +1 -1
  32. package/package.json +13 -12
  33. package/src/api.ts +237 -35
  34. package/src/buffering-backend.ts +112 -0
  35. package/src/diagnostic.ts +2 -2
  36. package/src/index.ts +1 -2
  37. package/src/remote/index.ts +0 -1
  38. package/src/symbols.ts +0 -2
  39. package/src/trace-processor.ts +58 -258
  40. package/src/tracing-types.ts +77 -0
  41. package/src/tracing.test.ts +513 -4
  42. package/dist/types/src/remote/tracing.d.ts +0 -23
  43. package/dist/types/src/remote/tracing.d.ts.map +0 -1
  44. package/dist/types/src/trace-sender.d.ts +0 -9
  45. package/dist/types/src/trace-sender.d.ts.map +0 -1
  46. package/src/remote/tracing.ts +0 -53
  47. package/src/trace-sender.ts +0 -88
@@ -1,22 +1,105 @@
1
1
  // src/api.ts
2
- import { Context as Context2 } from "@dxos/context";
2
+ import { Context as Context2, LifecycleState, Resource, TRACE_SPAN_ATTRIBUTE } from "@dxos/context";
3
3
 
4
4
  // src/symbols.ts
5
- var symbolTracingContext = Symbol("dxos.tracing.context");
5
+ var symbolTracingContext = /* @__PURE__ */ Symbol("dxos.tracing.context");
6
6
  var getTracingContext = (target) => {
7
- var _target, _symbolTracingContext;
8
- return (_target = target)[_symbolTracingContext = symbolTracingContext] ?? (_target[_symbolTracingContext] = {
7
+ return target[symbolTracingContext] ??= {
9
8
  infoProperties: {},
10
9
  metricsProperties: {}
11
- });
10
+ };
12
11
  };
13
- var TRACE_SPAN_ATTRIBUTE = "dxos.trace-span";
14
12
 
15
13
  // src/trace-processor.ts
16
- import { unrefTimeout } from "@dxos/async";
17
- import { LogLevel, getContextFromEntry, log } from "@dxos/log";
14
+ import { LogLevel, log } from "@dxos/log";
18
15
  import { getPrototypeSpecificInstanceId } from "@dxos/util";
19
16
 
17
+ // src/buffering-backend.ts
18
+ var BUFFERED_PREFIX = "buffered-";
19
+ var BufferedSpan = class {
20
+ options;
21
+ spanContext;
22
+ startTime;
23
+ delegate;
24
+ #ended = false;
25
+ #endTime;
26
+ #error;
27
+ #hasError = false;
28
+ constructor(options, id) {
29
+ this.options = options;
30
+ this.spanContext = {
31
+ traceparent: `${BUFFERED_PREFIX}${id}`
32
+ };
33
+ this.startTime = Date.now();
34
+ }
35
+ end(endTime) {
36
+ if (this.delegate) {
37
+ this.delegate.end(endTime);
38
+ return;
39
+ }
40
+ this.#endTime = endTime ?? Date.now();
41
+ this.#ended = true;
42
+ }
43
+ setError(err) {
44
+ if (this.delegate) {
45
+ this.delegate.setError?.(err);
46
+ return;
47
+ }
48
+ this.#error = err;
49
+ this.#hasError = true;
50
+ }
51
+ replay(real) {
52
+ if (this.#hasError) {
53
+ real.setError?.(this.#error);
54
+ }
55
+ if (this.#ended) {
56
+ real.end(this.#endTime);
57
+ } else {
58
+ this.delegate = real;
59
+ }
60
+ }
61
+ };
62
+ var BufferingTracingBackend = class {
63
+ #pending = [];
64
+ #counter = 0;
65
+ startSpan(options) {
66
+ const span2 = new BufferedSpan(options, ++this.#counter);
67
+ this.#pending.push(span2);
68
+ return span2;
69
+ }
70
+ /** Discard all buffered spans without replaying them. */
71
+ clear() {
72
+ this.#pending.length = 0;
73
+ }
74
+ /**
75
+ * Replay all buffered spans into {@link backend}.
76
+ *
77
+ * @returns Map from synthetic buffered traceparent to real {@link TraceContextData},
78
+ * used by the post-drain translating wrapper to resolve stale buffered IDs
79
+ * still present on in-flight {@link Context} objects.
80
+ */
81
+ drain(backend) {
82
+ const idMap = /* @__PURE__ */ new Map();
83
+ for (const buffered of this.#pending) {
84
+ let parentContext = buffered.options.parentContext;
85
+ if (parentContext && parentContext.traceparent.startsWith(BUFFERED_PREFIX)) {
86
+ parentContext = idMap.get(parentContext.traceparent) ?? parentContext;
87
+ }
88
+ const real = backend.startSpan({
89
+ ...buffered.options,
90
+ parentContext,
91
+ startTime: buffered.startTime
92
+ });
93
+ if (real.spanContext) {
94
+ idMap.set(buffered.spanContext.traceparent, real.spanContext);
95
+ }
96
+ buffered.replay(real);
97
+ }
98
+ this.#pending.length = 0;
99
+ return idMap;
100
+ }
101
+ };
102
+
20
103
  // src/diagnostic.ts
21
104
  import { asyncTimeout } from "@dxos/async";
22
105
  import { invariant } from "@dxos/invariant";
@@ -25,37 +108,27 @@ import { invariant } from "@dxos/invariant";
25
108
  var createId = () => Math.random().toString(36).slice(2);
26
109
 
27
110
  // src/diagnostic.ts
28
- function _define_property(obj, key, value) {
29
- if (key in obj) {
30
- Object.defineProperty(obj, key, {
31
- value,
32
- enumerable: true,
33
- configurable: true,
34
- writable: true
35
- });
36
- } else {
37
- obj[key] = value;
38
- }
39
- return obj;
40
- }
41
111
  var __dxlog_file = "/__w/dxos/dxos/packages/common/tracing/src/diagnostic.ts";
42
112
  var DIAGNOSTICS_TIMEOUT = 1e4;
43
113
  var TraceDiagnosticImpl = class {
44
- unregister() {
45
- this._onUnregister();
46
- }
114
+ id;
115
+ fetch;
116
+ name;
117
+ _onUnregister;
47
118
  constructor(id, fetch, name, _onUnregister) {
48
- _define_property(this, "id", void 0);
49
- _define_property(this, "fetch", void 0);
50
- _define_property(this, "name", void 0);
51
- _define_property(this, "_onUnregister", void 0);
52
119
  this.id = id;
53
120
  this.fetch = fetch;
54
121
  this.name = name;
55
122
  this._onUnregister = _onUnregister;
56
123
  }
124
+ unregister() {
125
+ this._onUnregister();
126
+ }
57
127
  };
58
128
  var DiagnosticsManager = class {
129
+ instanceId = createId();
130
+ registry = /* @__PURE__ */ new Map();
131
+ _instanceTag = null;
59
132
  get instanceTag() {
60
133
  return this._instanceTag;
61
134
  }
@@ -81,27 +154,11 @@ var DiagnosticsManager = class {
81
154
  }
82
155
  async fetch(request) {
83
156
  if (request.instanceId != null) {
84
- invariant(request.instanceId === this.instanceId, "Invalid instance id", {
85
- F: __dxlog_file,
86
- L: 82,
87
- S: this,
88
- A: [
89
- "request.instanceId === this.instanceId",
90
- "'Invalid instance id'"
91
- ]
92
- });
157
+ 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'"] });
93
158
  }
94
159
  const { id } = request;
95
160
  const diagnostic2 = this.registry.get(id);
96
- invariant(diagnostic2, "Diagnostic not found", {
97
- F: __dxlog_file,
98
- L: 86,
99
- S: this,
100
- A: [
101
- "diagnostic",
102
- "'Diagnostic not found'"
103
- ]
104
- });
161
+ invariant(diagnostic2, "Diagnostic not found", { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 56, S: this, A: ["diagnostic", "'Diagnostic not found'"] });
105
162
  try {
106
163
  const data = await asyncTimeout(diagnostic2.fetch(), DIAGNOSTICS_TIMEOUT);
107
164
  return {
@@ -118,37 +175,31 @@ var DiagnosticsManager = class {
118
175
  };
119
176
  }
120
177
  }
121
- constructor() {
122
- _define_property(this, "instanceId", createId());
123
- _define_property(this, "registry", /* @__PURE__ */ new Map());
124
- _define_property(this, "_instanceTag", null);
125
- }
126
178
  };
127
179
 
128
180
  // src/diagnostics-channel.ts
129
181
  import { Trigger, sleep } from "@dxos/async";
130
182
  import { Context } from "@dxos/context";
131
183
  import { invariant as invariant2 } from "@dxos/invariant";
132
- function _define_property2(obj, key, value) {
133
- if (key in obj) {
134
- Object.defineProperty(obj, key, {
135
- value,
136
- enumerable: true,
137
- configurable: true,
138
- writable: true
139
- });
140
- } else {
141
- obj[key] = value;
142
- }
143
- return obj;
144
- }
145
184
  var __dxlog_file2 = "/__w/dxos/dxos/packages/common/tracing/src/diagnostics-channel.ts";
146
185
  var DEFAULT_CHANNEL_NAME = "dxos-diagnostics";
147
186
  var DISCOVER_TIME = 500;
148
187
  var DiagnosticsChannel = class _DiagnosticsChannel {
188
+ _channelName;
149
189
  static get supported() {
150
190
  return globalThis.BroadcastChannel != null;
151
191
  }
192
+ _ctx = new Context(void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 16 });
193
+ // Separate channels becauase the client and server may be in the same process.
194
+ _serveChannel = void 0;
195
+ _clientChannel = void 0;
196
+ constructor(_channelName = DEFAULT_CHANNEL_NAME) {
197
+ this._channelName = _channelName;
198
+ if (_DiagnosticsChannel.supported) {
199
+ this._serveChannel = new BroadcastChannel(_channelName);
200
+ this._clientChannel = new BroadcastChannel(_channelName);
201
+ }
202
+ }
152
203
  destroy() {
153
204
  void this._ctx.dispose();
154
205
  this._serveChannel?.close();
@@ -166,15 +217,7 @@ var DiagnosticsChannel = class _DiagnosticsChannel {
166
217
  }
167
218
  }
168
219
  serve(manager) {
169
- invariant2(this._serveChannel, void 0, {
170
- F: __dxlog_file2,
171
- L: 78,
172
- S: this,
173
- A: [
174
- "this._serveChannel",
175
- ""
176
- ]
177
- });
220
+ invariant2(this._serveChannel, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 43, S: this, A: ["this._serveChannel", ""] });
178
221
  const listener = async (event) => {
179
222
  switch (event.data.type) {
180
223
  case "DIAGNOSTICS_DISCOVER": {
@@ -206,15 +249,7 @@ var DiagnosticsChannel = class _DiagnosticsChannel {
206
249
  this._ctx.onDispose(() => this._serveChannel.removeEventListener("message", listener));
207
250
  }
208
251
  async discover() {
209
- invariant2(this._clientChannel, void 0, {
210
- F: __dxlog_file2,
211
- L: 114,
212
- S: this,
213
- A: [
214
- "this._clientChannel",
215
- ""
216
- ]
217
- });
252
+ invariant2(this._clientChannel, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 77, S: this, A: ["this._clientChannel", ""] });
218
253
  const diagnostics = [];
219
254
  const collector = (event) => {
220
255
  const data = event.data;
@@ -242,15 +277,7 @@ var DiagnosticsChannel = class _DiagnosticsChannel {
242
277
  }
243
278
  }
244
279
  async fetch(request) {
245
- invariant2(this._clientChannel, void 0, {
246
- F: __dxlog_file2,
247
- L: 147,
248
- S: this,
249
- A: [
250
- "this._clientChannel",
251
- ""
252
- ]
253
- });
280
+ invariant2(this._clientChannel, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 106, S: this, A: ["this._clientChannel", ""] });
254
281
  const requestId = createId();
255
282
  const trigger = new Trigger();
256
283
  const listener = (event) => {
@@ -274,40 +301,11 @@ var DiagnosticsChannel = class _DiagnosticsChannel {
274
301
  this._clientChannel.removeEventListener("message", listener);
275
302
  }
276
303
  }
277
- constructor(_channelName = DEFAULT_CHANNEL_NAME) {
278
- _define_property2(this, "_channelName", void 0);
279
- _define_property2(this, "_ctx", void 0);
280
- _define_property2(this, "_serveChannel", void 0);
281
- _define_property2(this, "_clientChannel", void 0);
282
- this._channelName = _channelName;
283
- this._ctx = new Context(void 0, {
284
- F: __dxlog_file2,
285
- L: 46
286
- });
287
- this._serveChannel = void 0;
288
- this._clientChannel = void 0;
289
- if (_DiagnosticsChannel.supported) {
290
- this._serveChannel = new BroadcastChannel(_channelName);
291
- this._clientChannel = new BroadcastChannel(_channelName);
292
- }
293
- }
294
304
  };
295
305
 
296
306
  // src/remote/metrics.ts
297
- function _define_property3(obj, key, value) {
298
- if (key in obj) {
299
- Object.defineProperty(obj, key, {
300
- value,
301
- enumerable: true,
302
- configurable: true,
303
- writable: true
304
- });
305
- } else {
306
- obj[key] = value;
307
- }
308
- return obj;
309
- }
310
307
  var RemoteMetrics = class {
308
+ _metrics = /* @__PURE__ */ new Set();
311
309
  registerProcessor(processor) {
312
310
  this._metrics.add(processor);
313
311
  }
@@ -323,214 +321,102 @@ var RemoteMetrics = class {
323
321
  gauge(name, value, data) {
324
322
  return Array.from(this._metrics.values()).map((processor) => processor.gauge(name, value, data));
325
323
  }
326
- constructor() {
327
- _define_property3(this, "_metrics", /* @__PURE__ */ new Set());
328
- }
329
- };
330
-
331
- // src/remote/tracing.ts
332
- function _define_property4(obj, key, value) {
333
- if (key in obj) {
334
- Object.defineProperty(obj, key, {
335
- value,
336
- enumerable: true,
337
- configurable: true,
338
- writable: true
339
- });
340
- } else {
341
- obj[key] = value;
342
- }
343
- return obj;
344
- }
345
- var RemoteTracing = class {
346
- registerProcessor(processor) {
347
- this._tracing = processor;
348
- }
349
- flushSpan(span2) {
350
- if (!this._tracing) {
351
- return;
352
- }
353
- if (!span2.endTs) {
354
- const remoteSpan = this._tracing.startSpan({
355
- name: span2.methodName,
356
- op: span2.op ?? "function",
357
- attributes: span2.attributes
358
- });
359
- this._spanMap.set(span2, remoteSpan);
360
- } else {
361
- const remoteSpan = this._spanMap.get(span2);
362
- if (remoteSpan) {
363
- remoteSpan.end();
364
- this._spanMap.delete(span2);
365
- }
366
- }
367
- }
368
- constructor() {
369
- _define_property4(this, "_tracing", void 0);
370
- _define_property4(this, "_spanMap", /* @__PURE__ */ new Map());
371
- }
372
- };
373
-
374
- // src/trace-sender.ts
375
- import { Stream } from "@dxos/codec-protobuf/stream";
376
- function _define_property5(obj, key, value) {
377
- if (key in obj) {
378
- Object.defineProperty(obj, key, {
379
- value,
380
- enumerable: true,
381
- configurable: true,
382
- writable: true
383
- });
384
- } else {
385
- obj[key] = value;
386
- }
387
- return obj;
388
- }
389
- var TraceSender = class {
390
- streamTrace(request) {
391
- return new Stream(({ ctx, next }) => {
392
- const flushEvents = (resources, spans2, logs) => {
393
- const event = {
394
- resourceAdded: [],
395
- resourceRemoved: [],
396
- spanAdded: [],
397
- logAdded: []
398
- };
399
- if (resources) {
400
- for (const id of resources) {
401
- const entry = this._traceProcessor.resources.get(id);
402
- if (entry) {
403
- event.resourceAdded.push({
404
- resource: entry.data
405
- });
406
- } else {
407
- event.resourceRemoved.push({
408
- id
409
- });
410
- }
411
- }
412
- } else {
413
- for (const entry of this._traceProcessor.resources.values()) {
414
- event.resourceAdded.push({
415
- resource: entry.data
416
- });
417
- }
418
- }
419
- if (spans2) {
420
- for (const id of spans2) {
421
- const span2 = this._traceProcessor.spans.get(id);
422
- if (span2) {
423
- event.spanAdded.push({
424
- span: span2
425
- });
426
- }
427
- }
428
- } else {
429
- for (const span2 of this._traceProcessor.spans.values()) {
430
- event.spanAdded.push({
431
- span: span2
432
- });
433
- }
434
- }
435
- if (logs) {
436
- for (const log2 of logs) {
437
- event.logAdded.push({
438
- log: log2
439
- });
440
- }
441
- } else {
442
- for (const log2 of this._traceProcessor.logs) {
443
- event.logAdded.push({
444
- log: log2
445
- });
446
- }
447
- }
448
- if (event.resourceAdded.length > 0 || event.resourceRemoved.length > 0 || event.spanAdded.length > 0) {
449
- next(event);
450
- }
451
- };
452
- const flush = () => {
453
- flushEvents(subscription.dirtyResources, subscription.dirtySpans, subscription.newLogs);
454
- subscription.dirtyResources.clear();
455
- subscription.dirtySpans.clear();
456
- subscription.newLogs.length = 0;
457
- };
458
- const subscription = {
459
- flush,
460
- dirtyResources: /* @__PURE__ */ new Set(),
461
- dirtySpans: /* @__PURE__ */ new Set(),
462
- newLogs: []
463
- };
464
- this._traceProcessor.subscriptions.add(subscription);
465
- ctx.onDispose(() => {
466
- this._traceProcessor.subscriptions.delete(subscription);
467
- });
468
- flushEvents(null, null, null);
469
- });
470
- }
471
- constructor(_traceProcessor) {
472
- _define_property5(this, "_traceProcessor", void 0);
473
- this._traceProcessor = _traceProcessor;
474
- }
475
324
  };
476
325
 
477
326
  // src/weak-ref.ts
478
327
  var WeakRefMock = class {
479
- deref() {
480
- return void 0;
481
- }
482
328
  // eslint-disable-next-line @typescript-eslint/no-useless-constructor
483
329
  constructor(target) {
484
330
  }
331
+ deref() {
332
+ return void 0;
333
+ }
485
334
  };
486
335
  var WeakRef = globalThis.WeakRef ?? WeakRefMock;
487
336
 
488
337
  // src/trace-processor.ts
489
- function _define_property6(obj, key, value) {
490
- if (key in obj) {
491
- Object.defineProperty(obj, key, {
492
- value,
493
- enumerable: true,
494
- configurable: true,
495
- writable: true
496
- });
497
- } else {
498
- obj[key] = value;
499
- }
500
- return obj;
501
- }
502
- var _globalThis;
503
338
  var __dxlog_file3 = "/__w/dxos/dxos/packages/common/tracing/src/trace-processor.ts";
504
339
  var ResourceEntry = class {
505
- getMetric(name) {
506
- return this.data.metrics?.find((metric) => metric.name === name);
507
- }
340
+ data;
341
+ instance;
342
+ annotation;
343
+ /**
344
+ * Sometimes bundlers mangle class names: WebFile -> WebFile2.
345
+ *
346
+ * We use a heuristic to remove the suffix.
347
+ */
348
+ sanitizedClassName;
508
349
  constructor(data, instance, annotation) {
509
- _define_property6(this, "data", void 0);
510
- _define_property6(this, "instance", void 0);
511
- _define_property6(this, "annotation", void 0);
512
- _define_property6(this, "sanitizedClassName", void 0);
513
350
  this.data = data;
514
351
  this.instance = instance;
515
352
  this.annotation = annotation;
516
353
  this.sanitizedClassName = sanitizeClassName(data.className);
517
354
  }
355
+ getMetric(name) {
356
+ return this.data.metrics?.find((metric) => metric.name === name);
357
+ }
518
358
  };
519
359
  var MAX_RESOURCE_RECORDS = 2e3;
520
- var MAX_SPAN_RECORDS = 1e3;
521
360
  var MAX_LOG_RECORDS = 1e3;
522
- var REFRESH_INTERVAL = 1e3;
523
361
  var MAX_INFO_OBJECT_DEPTH = 8;
524
- var IS_CLOUDFLARE_WORKERS = !!globalThis?.navigator?.userAgent?.includes("Cloudflare-Workers");
525
362
  var TraceProcessor = class {
363
+ diagnostics = new DiagnosticsManager();
364
+ diagnosticsChannel = new DiagnosticsChannel();
365
+ remoteMetrics = new RemoteMetrics();
366
+ #bufferingBackend = new BufferingTracingBackend();
367
+ #activeBackend = this.#bufferingBackend;
368
+ /**
369
+ * Tracing backend. Initially a buffering backend that records spans;
370
+ * once the observability package sets a real backend, the buffer is drained
371
+ * and a thin translating wrapper is installed that resolves stale buffered
372
+ * parent IDs still held by in-flight {@link Context} objects.
373
+ *
374
+ * The wrapper only allocates when a `buffered-*` parent is actually encountered;
375
+ * the common path is a single `startsWith` check and direct passthrough.
376
+ */
377
+ get tracingBackend() {
378
+ return this.#activeBackend;
379
+ }
380
+ set tracingBackend(backend) {
381
+ if (!backend || backend === this.#bufferingBackend) {
382
+ this.#bufferingBackend.clear();
383
+ this.#activeBackend = this.#bufferingBackend;
384
+ return;
385
+ }
386
+ const idMap = this.#bufferingBackend.drain(backend);
387
+ this.#activeBackend = {
388
+ startSpan: (options) => {
389
+ const parent = options.parentContext;
390
+ if (parent?.traceparent.startsWith(BUFFERED_PREFIX)) {
391
+ const translated = idMap.get(parent.traceparent);
392
+ if (translated) {
393
+ return backend.startSpan({
394
+ ...options,
395
+ parentContext: translated
396
+ });
397
+ }
398
+ }
399
+ return backend.startSpan(options);
400
+ }
401
+ };
402
+ }
403
+ resources = /* @__PURE__ */ new Map();
404
+ resourceInstanceIndex = /* @__PURE__ */ new WeakMap();
405
+ resourceIdList = [];
406
+ logs = [];
407
+ _instanceTag = null;
408
+ constructor() {
409
+ log.addProcessor(this._logProcessor.bind(this), void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 80, S: this });
410
+ if (DiagnosticsChannel.supported) {
411
+ this.diagnosticsChannel.serve(this.diagnostics);
412
+ }
413
+ this.diagnosticsChannel.unref();
414
+ }
526
415
  setInstanceTag(tag) {
527
416
  this._instanceTag = tag;
528
417
  this.diagnostics.setInstanceTag(tag);
529
418
  }
530
- /**
531
- * @internal
532
- */
533
- // TODO(burdon): Comment.
419
+ /** @internal */
534
420
  createTraceResource(params) {
535
421
  const id = this.resources.size;
536
422
  const tracingContext = getTracingContext(Object.getPrototypeOf(params.instance));
@@ -551,24 +437,10 @@ var TraceProcessor = class {
551
437
  if (this.resourceIdList.length > MAX_RESOURCE_RECORDS) {
552
438
  this._clearResources();
553
439
  }
554
- this._markResourceDirty(id);
555
- }
556
- createTraceSender() {
557
- return new TraceSender(this);
558
- }
559
- traceSpan(params) {
560
- const span2 = new TracingSpan(this, params);
561
- this._flushSpan(span2);
562
- return span2;
563
440
  }
564
441
  // TODO(burdon): Not implemented.
565
442
  addLink(parent, child, opts) {
566
443
  }
567
- //
568
- // Getters
569
- //
570
- // TODO(burdon): Define type.
571
- // TODO(burdon): Reconcile with system service.
572
444
  getDiagnostics() {
573
445
  this.refresh();
574
446
  return {
@@ -576,7 +448,6 @@ var TraceProcessor = class {
576
448
  `${entry.sanitizedClassName}#${entry.data.instanceId}`,
577
449
  entry.data
578
450
  ])),
579
- spans: Array.from(this.spans.values()),
580
451
  logs: this.logs.filter((log2) => log2.level >= LogLevel.INFO)
581
452
  };
582
453
  }
@@ -631,43 +502,8 @@ var TraceProcessor = class {
631
502
  for (const key of Object.keys(tracingContext.metricsProperties)) {
632
503
  instance[key]._tick?.(time);
633
504
  }
634
- let _changed = false;
635
- const oldInfo = resource2.data.info;
636
505
  resource2.data.info = this.getResourceInfo(instance);
637
- _changed || (_changed = !areEqualShallow(oldInfo, resource2.data.info));
638
- const oldMetrics = resource2.data.metrics;
639
506
  resource2.data.metrics = this.getResourceMetrics(instance);
640
- _changed || (_changed = !areEqualShallow(oldMetrics, resource2.data.metrics));
641
- this._markResourceDirty(resource2.data.id);
642
- }
643
- for (const subscription of this.subscriptions) {
644
- subscription.flush();
645
- }
646
- }
647
- //
648
- // Implementation
649
- //
650
- /**
651
- * @internal
652
- */
653
- _flushSpan(runtimeSpan) {
654
- const span2 = runtimeSpan.serialize();
655
- this.spans.set(span2.id, span2);
656
- this.spanIdList.push(span2.id);
657
- if (this.spanIdList.length > MAX_SPAN_RECORDS) {
658
- this._clearSpans();
659
- }
660
- this._markSpanDirty(span2.id);
661
- this.remoteTracing.flushSpan(runtimeSpan);
662
- }
663
- _markResourceDirty(id) {
664
- for (const subscription of this.subscriptions) {
665
- subscription.dirtyResources.add(id);
666
- }
667
- }
668
- _markSpanDirty(id) {
669
- for (const subscription of this.subscriptions) {
670
- subscription.dirtySpans.add(id);
671
507
  }
672
508
  }
673
509
  _clearResources() {
@@ -676,175 +512,48 @@ var TraceProcessor = class {
676
512
  this.resources.delete(id);
677
513
  }
678
514
  }
679
- _clearSpans() {
680
- while (this.spanIdList.length > MAX_SPAN_RECORDS) {
681
- const id = this.spanIdList.shift();
682
- this.spans.delete(id);
683
- }
684
- }
685
515
  _pushLog(log2) {
686
516
  this.logs.push(log2);
687
517
  if (this.logs.length > MAX_LOG_RECORDS) {
688
518
  this.logs.shift();
689
519
  }
690
- for (const subscription of this.subscriptions) {
691
- subscription.newLogs.push(log2);
692
- }
693
520
  }
694
- constructor() {
695
- _define_property6(this, "diagnostics", new DiagnosticsManager());
696
- _define_property6(this, "diagnosticsChannel", new DiagnosticsChannel());
697
- _define_property6(this, "remoteMetrics", new RemoteMetrics());
698
- _define_property6(this, "remoteTracing", new RemoteTracing());
699
- _define_property6(this, "subscriptions", /* @__PURE__ */ new Set());
700
- _define_property6(this, "resources", /* @__PURE__ */ new Map());
701
- _define_property6(this, "resourceInstanceIndex", /* @__PURE__ */ new WeakMap());
702
- _define_property6(this, "resourceIdList", []);
703
- _define_property6(this, "spans", /* @__PURE__ */ new Map());
704
- _define_property6(this, "spanIdList", []);
705
- _define_property6(this, "logs", []);
706
- _define_property6(this, "_instanceTag", null);
707
- _define_property6(this, "_logProcessor", (config, entry) => {
708
- switch (entry.level) {
709
- case LogLevel.ERROR:
710
- case LogLevel.WARN:
711
- case LogLevel.TRACE: {
712
- const scope = entry.meta?.S;
713
- const resource2 = this.resourceInstanceIndex.get(scope);
714
- if (!resource2) {
715
- return;
716
- }
717
- const context = getContextFromEntry(entry) ?? {};
718
- for (const key of Object.keys(context)) {
719
- context[key] = sanitizeValue(context[key], 0, this);
720
- }
721
- const entryToPush = {
722
- level: entry.level,
723
- message: entry.message ?? (entry.error ? entry.error.message ?? String(entry.error) : ""),
724
- context,
725
- timestamp: /* @__PURE__ */ new Date(),
726
- meta: {
727
- file: entry.meta?.F ?? "",
728
- line: entry.meta?.L ?? 0,
729
- resourceId: resource2.data.id
730
- }
731
- };
732
- this._pushLog(entryToPush);
733
- break;
521
+ _logProcessor = (config, entry) => {
522
+ switch (entry.level) {
523
+ case LogLevel.ERROR:
524
+ case LogLevel.WARN:
525
+ case LogLevel.TRACE: {
526
+ const scope = entry.meta?.S;
527
+ const resource2 = this.resourceInstanceIndex.get(scope);
528
+ if (!resource2) {
529
+ return;
734
530
  }
735
- default:
736
- }
737
- });
738
- log.addProcessor(this._logProcessor.bind(this), void 0, {
739
- F: __dxlog_file3,
740
- L: 103,
741
- S: this,
742
- C: (f, a) => f(...a)
743
- });
744
- if (!IS_CLOUDFLARE_WORKERS) {
745
- const refreshInterval = setInterval(this.refresh.bind(this), REFRESH_INTERVAL);
746
- unrefTimeout(refreshInterval);
747
- }
748
- if (DiagnosticsChannel.supported) {
749
- this.diagnosticsChannel.serve(this.diagnostics);
750
- }
751
- this.diagnosticsChannel.unref();
752
- }
753
- };
754
- var TracingSpan = class _TracingSpan {
755
- get name() {
756
- const resource2 = this._traceProcessor.resources.get(this.resourceId);
757
- return resource2 ? `${resource2.sanitizedClassName}#${resource2.data.instanceId}.${this.methodName}` : this.methodName;
758
- }
759
- get ctx() {
760
- return this._ctx;
761
- }
762
- markSuccess() {
763
- this.endTs = performance.now();
764
- this._traceProcessor._flushSpan(this);
765
- if (this._showInBrowserTimeline) {
766
- this._markInBrowserTimeline();
767
- }
768
- }
769
- markError(err) {
770
- this.endTs = performance.now();
771
- this.error = serializeError(err);
772
- this._traceProcessor._flushSpan(this);
773
- if (this._showInBrowserTimeline) {
774
- this._markInBrowserTimeline();
775
- }
776
- }
777
- serialize() {
778
- return {
779
- id: this.id,
780
- resourceId: this.resourceId ?? void 0,
781
- methodName: this.methodName,
782
- parentId: this.parentId ?? void 0,
783
- startTs: this.startTs.toFixed(3),
784
- endTs: this.endTs?.toFixed(3) ?? void 0,
785
- error: this.error ?? void 0
786
- };
787
- }
788
- _markInBrowserTimeline() {
789
- if (typeof globalThis?.performance?.measure === "function") {
790
- performance.measure(this.name, {
791
- start: this.startTs,
792
- end: this.endTs
793
- });
794
- }
795
- }
796
- constructor(_traceProcessor, params) {
797
- _define_property6(this, "_traceProcessor", void 0);
798
- _define_property6(this, "id", void 0);
799
- _define_property6(this, "parentId", void 0);
800
- _define_property6(this, "methodName", void 0);
801
- _define_property6(this, "resourceId", void 0);
802
- _define_property6(this, "op", void 0);
803
- _define_property6(this, "attributes", void 0);
804
- _define_property6(this, "startTs", void 0);
805
- _define_property6(this, "endTs", void 0);
806
- _define_property6(this, "error", void 0);
807
- _define_property6(this, "_showInBrowserTimeline", void 0);
808
- _define_property6(this, "_ctx", void 0);
809
- this._traceProcessor = _traceProcessor;
810
- this.parentId = null;
811
- this.resourceId = null;
812
- this.endTs = null;
813
- this.error = null;
814
- this._ctx = null;
815
- this.id = _TracingSpan.nextId++;
816
- this.methodName = params.methodName;
817
- this.resourceId = _traceProcessor.getResourceId(params.instance);
818
- this.startTs = performance.now();
819
- this._showInBrowserTimeline = params.showInBrowserTimeline;
820
- this.op = params.op;
821
- this.attributes = params.attributes ?? {};
822
- if (params.parentCtx) {
823
- this._ctx = params.parentCtx.derive({
824
- attributes: {
825
- [TRACE_SPAN_ATTRIBUTE]: this.id
531
+ const context = {
532
+ ...entry.computedContext
533
+ };
534
+ if (entry.computedError !== void 0) {
535
+ context.error = entry.computedError;
826
536
  }
827
- });
828
- const parentId = params.parentCtx.getAttribute(TRACE_SPAN_ATTRIBUTE);
829
- if (typeof parentId === "number") {
830
- this.parentId = parentId;
537
+ const { filename, line } = entry.computedMeta;
538
+ const entryToPush = {
539
+ level: entry.level,
540
+ message: entry.message ?? entry.computedError ?? "",
541
+ context,
542
+ timestamp: new Date(entry.timestamp),
543
+ meta: {
544
+ file: filename ?? "",
545
+ line: line ?? 0,
546
+ resourceId: resource2.data.id
547
+ }
548
+ };
549
+ this._pushLog(entryToPush);
550
+ break;
831
551
  }
552
+ default:
832
553
  }
833
- }
834
- };
835
- _define_property6(TracingSpan, "nextId", 0);
836
- var serializeError = (err) => {
837
- if (err instanceof Error) {
838
- return {
839
- name: err.name,
840
- message: err.message
841
- };
842
- }
843
- return {
844
- message: String(err)
845
554
  };
846
555
  };
847
- var TRACE_PROCESSOR = (_globalThis = globalThis).TRACE_PROCESSOR ?? (_globalThis.TRACE_PROCESSOR = new TraceProcessor());
556
+ var TRACE_PROCESSOR = globalThis.TRACE_PROCESSOR ??= new TraceProcessor();
848
557
  var sanitizeValue = (value, depth, traceProcessor) => {
849
558
  switch (typeof value) {
850
559
  case "string":
@@ -890,46 +599,115 @@ var sanitizeValue = (value, depth, traceProcessor) => {
890
599
  return value.toString();
891
600
  }
892
601
  };
893
- var areEqualShallow = (a, b) => {
894
- for (const key in a) {
895
- if (!(key in b) || a[key] !== b[key]) {
896
- return false;
897
- }
898
- }
899
- for (const key in b) {
900
- if (!(key in a) || a[key] !== b[key]) {
901
- return false;
902
- }
903
- }
904
- return true;
905
- };
906
602
  var sanitizeClassName = (className) => {
603
+ let name = className.replace(/^_+/, "");
907
604
  const SANITIZE_REGEX = /[^_](\d+)$/;
908
- const m = className.match(SANITIZE_REGEX);
909
- if (!m) {
910
- return className;
911
- } else {
912
- return className.slice(0, -m[1].length);
605
+ const m = name.match(SANITIZE_REGEX);
606
+ if (m) {
607
+ name = name.slice(0, -m[1].length);
913
608
  }
609
+ return name;
914
610
  };
915
611
  var isSetLike = (value) => value instanceof Set || typeof value === "object" && value !== null && Object.getPrototypeOf(value).constructor.name === "ComplexSet";
916
612
  var isMapLike = (value) => value instanceof Map || typeof value === "object" && value !== null && Object.getPrototypeOf(value).constructor.name === "ComplexMap";
917
613
 
918
614
  // src/api.ts
919
- var resource = (options) => (constructor) => {
920
- const klass = /* @__PURE__ */ (() => {
921
- class _class extends constructor {
922
- constructor(...rest) {
923
- super(...rest);
924
- TRACE_PROCESSOR.createTraceResource({
925
- constructor,
926
- annotation: options?.annotation,
927
- instance: this
928
- });
615
+ var __dxlog_file4 = "/__w/dxos/dxos/packages/common/tracing/src/api.ts";
616
+ var LIFECYCLE_SPAN = /* @__PURE__ */ Symbol("dxos.tracing.lifecycle-span");
617
+ var TRACE_ALL_KEY = "dxos.debug.traceAll";
618
+ var collectSpanAttributes = (instance, spanAttributes) => {
619
+ const proto = Object.getPrototypeOf(instance);
620
+ if (!proto) {
621
+ return;
622
+ }
623
+ const tracingContext = getTracingContext(proto);
624
+ for (const [key, { options }] of Object.entries(tracingContext.infoProperties)) {
625
+ if (!options.spanAttribute) {
626
+ continue;
627
+ }
628
+ try {
629
+ const value = typeof instance[key] === "function" ? instance[key]() : instance[key];
630
+ if (value != null) {
631
+ const resolved = options.enum ? options.enum[value] : String(value);
632
+ spanAttributes[`ctx.${key}`] = resolved;
929
633
  }
634
+ } catch {
635
+ }
636
+ }
637
+ };
638
+ var resource = (options) => (constructor) => {
639
+ if (options?.lifecycle && !(constructor.prototype instanceof Resource)) {
640
+ throw new Error(`@trace.resource({ lifecycle: true }) requires ${constructor.name} to extend Resource`);
641
+ }
642
+ const klass = /* @__PURE__ */ (() => class extends constructor {
643
+ constructor(...rest) {
644
+ super(...rest);
645
+ TRACE_PROCESSOR.createTraceResource({
646
+ constructor,
647
+ annotation: options?.annotation,
648
+ instance: this
649
+ });
930
650
  }
931
- return _class;
932
651
  })();
652
+ if (options?.lifecycle) {
653
+ const sanitizedName = sanitizeClassName(constructor.name);
654
+ const proto = klass.prototype;
655
+ const originalOpen = proto.open;
656
+ const originalClose = proto.close;
657
+ proto.open = async function(ctx) {
658
+ const self = this;
659
+ if (self._lifecycleState !== LifecycleState.CLOSED) {
660
+ return originalOpen.call(this, ctx);
661
+ }
662
+ const parentSpanContext = ctx?.getAttribute(TRACE_SPAN_ATTRIBUTE);
663
+ const resourceEntry = TRACE_PROCESSOR.resourceInstanceIndex.get(this);
664
+ const spanAttributes = {};
665
+ if (resourceEntry) {
666
+ spanAttributes.entryPoint = resourceEntry.sanitizedClassName;
667
+ }
668
+ const remoteSpan = TRACE_PROCESSOR.tracingBackend?.startSpan({
669
+ name: `${sanitizedName}.lifecycle`,
670
+ op: "lifecycle",
671
+ attributes: spanAttributes,
672
+ parentContext: parentSpanContext
673
+ });
674
+ self[LIFECYCLE_SPAN] = remoteSpan;
675
+ let openCtx = ctx;
676
+ if (remoteSpan?.spanContext != null) {
677
+ const traceAttrs = {
678
+ [TRACE_SPAN_ATTRIBUTE]: remoteSpan.spanContext
679
+ };
680
+ openCtx = ctx ? ctx.derive({
681
+ attributes: traceAttrs
682
+ }) : new Context2({
683
+ attributes: traceAttrs
684
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 79 });
685
+ }
686
+ try {
687
+ return await originalOpen.call(this, openCtx);
688
+ } catch (err) {
689
+ remoteSpan?.setError?.(err);
690
+ remoteSpan?.end();
691
+ self[LIFECYCLE_SPAN] = void 0;
692
+ throw err;
693
+ }
694
+ };
695
+ proto.close = async function(ctx) {
696
+ const self = this;
697
+ const remoteSpan = self[LIFECYCLE_SPAN];
698
+ try {
699
+ return await originalClose.call(this, ctx);
700
+ } catch (err) {
701
+ remoteSpan?.setError?.(err);
702
+ throw err;
703
+ } finally {
704
+ if (remoteSpan) {
705
+ remoteSpan.end();
706
+ self[LIFECYCLE_SPAN] = void 0;
707
+ }
708
+ }
709
+ };
710
+ }
933
711
  Object.defineProperty(klass, "name", {
934
712
  value: constructor.name
935
713
  });
@@ -943,45 +721,117 @@ var info = (opts = {}) => (target, propertyKey, descriptor) => {
943
721
  var mark = (name) => {
944
722
  performance.mark(name);
945
723
  };
946
- var span = ({ showInBrowserTimeline = false, op, attributes } = {}) => (target, propertyKey, descriptor) => {
724
+ var span = ({ showInBrowserTimeline = false, showInRemoteTracing = true, op, attributes } = {}) => (target, propertyKey, descriptor) => {
947
725
  const method = descriptor.value;
948
726
  descriptor.value = async function(...args) {
949
727
  const parentCtx = args[0] instanceof Context2 ? args[0] : null;
950
- const span2 = TRACE_PROCESSOR.traceSpan({
951
- parentCtx,
952
- methodName: propertyKey,
953
- instance: this,
954
- showInBrowserTimeline,
955
- op,
956
- attributes
957
- });
958
- const callArgs = span2.ctx ? [
959
- span2.ctx,
960
- ...args.slice(1)
961
- ] : args;
728
+ const startTs = performance.now();
729
+ const parentSpanContext = parentCtx?.getAttribute(TRACE_SPAN_ATTRIBUTE);
730
+ const resourceEntry = TRACE_PROCESSOR.resourceInstanceIndex.get(this);
731
+ const className = resourceEntry?.sanitizedClassName ?? sanitizeClassName(target.constructor?.name ?? "unknown");
732
+ const spanName = `${className}.${propertyKey}`;
733
+ const spanAttributes = {};
734
+ if (resourceEntry) {
735
+ spanAttributes.entryPoint = resourceEntry.sanitizedClassName;
736
+ }
737
+ collectSpanAttributes(this, spanAttributes);
738
+ if (attributes) {
739
+ for (const [key, value] of Object.entries(attributes)) {
740
+ spanAttributes[key.startsWith("ctx.") ? key : `ctx.${key}`] = value;
741
+ }
742
+ }
743
+ const remoteSpan = showInRemoteTracing ? TRACE_PROCESSOR.tracingBackend?.startSpan({
744
+ name: spanName,
745
+ op: op ?? "function",
746
+ attributes: spanAttributes,
747
+ parentContext: parentSpanContext
748
+ }) : void 0;
749
+ let callArgs = args;
750
+ if (parentCtx) {
751
+ const childCtx = remoteSpan?.spanContext != null ? parentCtx.derive({
752
+ attributes: {
753
+ [TRACE_SPAN_ATTRIBUTE]: remoteSpan.spanContext
754
+ }
755
+ }) : parentCtx.derive();
756
+ callArgs = [
757
+ childCtx,
758
+ ...args.slice(1)
759
+ ];
760
+ }
962
761
  try {
963
762
  return await method.apply(this, callArgs);
964
763
  } catch (err) {
965
- span2.markError(err);
764
+ remoteSpan?.setError?.(err);
966
765
  throw err;
967
766
  } finally {
968
- span2.markSuccess();
767
+ remoteSpan?.end();
768
+ if (showInBrowserTimeline && typeof globalThis?.performance?.measure === "function") {
769
+ performance.measure(spanName, {
770
+ start: startTs,
771
+ end: performance.now()
772
+ });
773
+ }
969
774
  }
970
775
  };
971
776
  };
972
- var spans = /* @__PURE__ */ new Map();
777
+ var manualSpans = /* @__PURE__ */ new Map();
778
+ var manualSpanTimestamps = /* @__PURE__ */ new Map();
973
779
  var spanStart = (params) => {
974
- if (spans.has(params.id)) {
975
- return;
780
+ if (manualSpans.has(params.id) || manualSpanTimestamps.has(params.id)) {
781
+ return params.parentCtx;
782
+ }
783
+ const resourceEntry = TRACE_PROCESSOR.resourceInstanceIndex.get(params.instance);
784
+ const className = resourceEntry?.sanitizedClassName ?? "unknown";
785
+ const spanName = `${className}.${params.methodName}`;
786
+ if (params.showInBrowserTimeline) {
787
+ manualSpanTimestamps.set(params.id, {
788
+ name: spanName,
789
+ startTs: performance.now()
790
+ });
791
+ }
792
+ if (params.showInRemoteTracing === false || !TRACE_PROCESSOR.tracingBackend) {
793
+ return params.parentCtx;
794
+ }
795
+ const parentSpanContext = params.parentCtx?.getAttribute(TRACE_SPAN_ATTRIBUTE);
796
+ const spanAttributes = {};
797
+ if (resourceEntry) {
798
+ spanAttributes.entryPoint = resourceEntry.sanitizedClassName;
799
+ }
800
+ collectSpanAttributes(params.instance, spanAttributes);
801
+ if (params.attributes) {
802
+ for (const [key, value] of Object.entries(params.attributes)) {
803
+ spanAttributes[key.startsWith("ctx.") ? key : `ctx.${key}`] = value;
804
+ }
805
+ }
806
+ const remoteSpan = TRACE_PROCESSOR.tracingBackend.startSpan({
807
+ name: spanName,
808
+ op: params.op ?? "function",
809
+ attributes: spanAttributes,
810
+ parentContext: parentSpanContext
811
+ });
812
+ manualSpans.set(params.id, remoteSpan);
813
+ if (params.parentCtx && remoteSpan.spanContext != null) {
814
+ return params.parentCtx.derive({
815
+ attributes: {
816
+ [TRACE_SPAN_ATTRIBUTE]: remoteSpan.spanContext
817
+ }
818
+ });
976
819
  }
977
- const span2 = TRACE_PROCESSOR.traceSpan(params);
978
- spans.set(params.id, span2);
820
+ return params.parentCtx;
979
821
  };
980
822
  var spanEnd = (id) => {
981
- const span2 = spans.get(id);
982
- if (span2) {
983
- span2.markSuccess();
984
- spans.delete(id);
823
+ const remoteSpan = manualSpans.get(id);
824
+ if (remoteSpan) {
825
+ remoteSpan.end();
826
+ manualSpans.delete(id);
827
+ }
828
+ const timestamps = manualSpanTimestamps.get(id);
829
+ if (timestamps && typeof globalThis?.performance?.measure === "function") {
830
+ performance.measure(timestamps.name, {
831
+ start: timestamps.startTs,
832
+ end: performance.now()
833
+ });
834
+ manualSpanTimestamps.delete(id);
985
835
  }
986
836
  };
987
837
  var metricsCounter = () => (target, propertyKey, descriptor) => {
@@ -1007,20 +857,12 @@ var trace = {
1007
857
  };
1008
858
 
1009
859
  // src/metrics/base.ts
1010
- function _define_property7(obj, key, value) {
1011
- if (key in obj) {
1012
- Object.defineProperty(obj, key, {
1013
- value,
1014
- enumerable: true,
1015
- configurable: true,
1016
- writable: true
1017
- });
1018
- } else {
1019
- obj[key] = value;
1020
- }
1021
- return obj;
1022
- }
1023
860
  var BaseCounter = class {
861
+ /**
862
+ * @internal
863
+ */
864
+ _instance;
865
+ name;
1024
866
  /**
1025
867
  * @internal
1026
868
  */
@@ -1030,27 +872,16 @@ var BaseCounter = class {
1030
872
  }
1031
873
  _tick(time) {
1032
874
  }
1033
- constructor() {
1034
- _define_property7(this, "_instance", void 0);
1035
- _define_property7(this, "name", void 0);
1036
- }
1037
875
  };
1038
876
 
1039
877
  // src/metrics/unary-counter.ts
1040
- function _define_property8(obj, key, value) {
1041
- if (key in obj) {
1042
- Object.defineProperty(obj, key, {
1043
- value,
1044
- enumerable: true,
1045
- configurable: true,
1046
- writable: true
1047
- });
1048
- } else {
1049
- obj[key] = value;
1050
- }
1051
- return obj;
1052
- }
1053
878
  var UnaryCounter = class extends BaseCounter {
879
+ value = 0;
880
+ units;
881
+ constructor({ units } = {}) {
882
+ super();
883
+ this.units = units;
884
+ }
1054
885
  inc(by = 1) {
1055
886
  this.value += by;
1056
887
  }
@@ -1063,28 +894,19 @@ var UnaryCounter = class extends BaseCounter {
1063
894
  }
1064
895
  };
1065
896
  }
1066
- constructor({ units } = {}) {
1067
- super(), _define_property8(this, "value", 0), _define_property8(this, "units", void 0);
1068
- this.units = units;
1069
- }
1070
897
  };
1071
898
 
1072
899
  // src/metrics/time-series-counter.ts
1073
- function _define_property9(obj, key, value) {
1074
- if (key in obj) {
1075
- Object.defineProperty(obj, key, {
1076
- value,
1077
- enumerable: true,
1078
- configurable: true,
1079
- writable: true
1080
- });
1081
- } else {
1082
- obj[key] = value;
1083
- }
1084
- return obj;
1085
- }
1086
900
  var MAX_BUCKETS = 60;
1087
901
  var TimeSeriesCounter = class extends BaseCounter {
902
+ _currentValue = 0;
903
+ _totalValue = 0;
904
+ _buckets = [];
905
+ units;
906
+ constructor({ units } = {}) {
907
+ super();
908
+ this.units = units;
909
+ }
1088
910
  inc(by = 1) {
1089
911
  this._currentValue += by;
1090
912
  this._totalValue += by;
@@ -1113,28 +935,15 @@ var TimeSeriesCounter = class extends BaseCounter {
1113
935
  }
1114
936
  };
1115
937
  }
1116
- constructor({ units } = {}) {
1117
- super(), _define_property9(this, "_currentValue", 0), _define_property9(this, "_totalValue", 0), _define_property9(this, "_buckets", []), _define_property9(this, "units", void 0);
1118
- this.units = units;
1119
- }
1120
938
  };
1121
939
 
1122
940
  // src/metrics/time-usage-counter.ts
1123
- function _define_property10(obj, key, value) {
1124
- if (key in obj) {
1125
- Object.defineProperty(obj, key, {
1126
- value,
1127
- enumerable: true,
1128
- configurable: true,
1129
- writable: true
1130
- });
1131
- } else {
1132
- obj[key] = value;
1133
- }
1134
- return obj;
1135
- }
1136
941
  var MAX_BUCKETS2 = 60;
1137
942
  var TimeUsageCounter = class extends BaseCounter {
943
+ _currentValue = 0;
944
+ _totalValue = 0;
945
+ _buckets = [];
946
+ _lastTickTime = performance.now();
1138
947
  record(time) {
1139
948
  this._currentValue += time;
1140
949
  this._totalValue += time;
@@ -1175,26 +984,16 @@ var TimeUsageCounter = class extends BaseCounter {
1175
984
  }
1176
985
  };
1177
986
  }
1178
- constructor(...args) {
1179
- super(...args), _define_property10(this, "_currentValue", 0), _define_property10(this, "_totalValue", 0), _define_property10(this, "_buckets", []), _define_property10(this, "_lastTickTime", performance.now());
1180
- }
1181
987
  };
1182
988
 
1183
989
  // src/metrics/map-counter.ts
1184
- function _define_property11(obj, key, value) {
1185
- if (key in obj) {
1186
- Object.defineProperty(obj, key, {
1187
- value,
1188
- enumerable: true,
1189
- configurable: true,
1190
- writable: true
1191
- });
1192
- } else {
1193
- obj[key] = value;
1194
- }
1195
- return obj;
1196
- }
1197
990
  var MapCounter = class extends BaseCounter {
991
+ values = /* @__PURE__ */ new Map();
992
+ units;
993
+ constructor({ units } = {}) {
994
+ super();
995
+ this.units = units;
996
+ }
1198
997
  inc(key, by = 1) {
1199
998
  const prev = this.values.get(key) ?? 0;
1200
999
  this.values.set(key, prev + by);
@@ -1211,27 +1010,14 @@ var MapCounter = class extends BaseCounter {
1211
1010
  }
1212
1011
  };
1213
1012
  }
1214
- constructor({ units } = {}) {
1215
- super(), _define_property11(this, "values", /* @__PURE__ */ new Map()), _define_property11(this, "units", void 0);
1216
- this.units = units;
1217
- }
1218
1013
  };
1219
1014
 
1220
1015
  // src/metrics/custom-counter.ts
1221
- function _define_property12(obj, key, value) {
1222
- if (key in obj) {
1223
- Object.defineProperty(obj, key, {
1224
- value,
1225
- enumerable: true,
1226
- configurable: true,
1227
- writable: true
1228
- });
1229
- } else {
1230
- obj[key] = value;
1231
- }
1232
- return obj;
1233
- }
1234
1016
  var CustomCounter = class extends BaseCounter {
1017
+ _getData;
1018
+ constructor(_getData) {
1019
+ super(), this._getData = _getData;
1020
+ }
1235
1021
  getData() {
1236
1022
  return {
1237
1023
  name: this.name,
@@ -1240,9 +1026,6 @@ var CustomCounter = class extends BaseCounter {
1240
1026
  }
1241
1027
  };
1242
1028
  }
1243
- constructor(_getData) {
1244
- super(), _define_property12(this, "_getData", void 0), this._getData = _getData;
1245
- }
1246
1029
  };
1247
1030
 
1248
1031
  // src/index.ts
@@ -1266,16 +1049,13 @@ export {
1266
1049
  DiagnosticsManager,
1267
1050
  MapCounter,
1268
1051
  RemoteMetrics,
1269
- RemoteTracing,
1270
1052
  ResourceEntry,
1053
+ TRACE_ALL_KEY,
1271
1054
  TRACE_PROCESSOR,
1272
- TRACE_SPAN_ATTRIBUTE,
1273
1055
  TimeSeriesCounter,
1274
1056
  TimeUsageCounter,
1275
1057
  TraceDiagnosticImpl,
1276
1058
  TraceProcessor,
1277
- TraceSender,
1278
- TracingSpan,
1279
1059
  UnaryCounter,
1280
1060
  getTracingContext,
1281
1061
  sanitizeClassName,