bitfab 0.54.2 → 0.54.4

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.
@@ -1,3 +1,90 @@
1
+ // src/traceCompletion.ts
2
+ var TraceCompletion = class {
3
+ constructor() {
4
+ this.traces = /* @__PURE__ */ new Map();
5
+ this.closed = /* @__PURE__ */ new Map();
6
+ }
7
+ state(traceId) {
8
+ let state = this.traces.get(traceId);
9
+ if (state === void 0) {
10
+ state = { spanIds: /* @__PURE__ */ new Set(), active: /* @__PURE__ */ new Set() };
11
+ this.traces.set(traceId, state);
12
+ }
13
+ return state;
14
+ }
15
+ start(traceId, spanId) {
16
+ if (this.closed.has(traceId)) {
17
+ return;
18
+ }
19
+ const state = this.state(traceId);
20
+ state.spanIds.add(spanId);
21
+ state.active.add(spanId);
22
+ }
23
+ open(traceId) {
24
+ if (!this.closed.has(traceId)) {
25
+ this.state(traceId);
26
+ }
27
+ }
28
+ hold(traceId, key) {
29
+ this.state(traceId).active.add(key);
30
+ }
31
+ record(traceId, spanId) {
32
+ if (!this.closed.has(traceId)) {
33
+ this.state(traceId).spanIds.add(spanId);
34
+ }
35
+ }
36
+ end(traceId, spanId) {
37
+ const state = this.traces.get(traceId);
38
+ if (state !== void 0) {
39
+ state.active.delete(spanId);
40
+ this.drain(traceId, state);
41
+ }
42
+ }
43
+ abort(traceId, spanId) {
44
+ const state = this.traces.get(traceId);
45
+ if (state === void 0) {
46
+ return;
47
+ }
48
+ state.active.delete(spanId);
49
+ state.spanIds.delete(spanId);
50
+ if (state.active.size === 0 && state.spanIds.size === 0 && state.complete === void 0) {
51
+ this.traces.delete(traceId);
52
+ return;
53
+ }
54
+ this.drain(traceId, state);
55
+ }
56
+ close(traceId, complete, dropped = false) {
57
+ const count = this.closed.get(traceId);
58
+ if (count !== void 0) {
59
+ complete(count);
60
+ return;
61
+ }
62
+ const state = this.traces.get(traceId);
63
+ if (state === void 0) {
64
+ return;
65
+ }
66
+ state.complete = complete;
67
+ if (dropped) {
68
+ state.active.clear();
69
+ }
70
+ this.drain(traceId, state);
71
+ }
72
+ drain(traceId, state) {
73
+ if (state.active.size !== 0 || state.complete === void 0) {
74
+ return;
75
+ }
76
+ this.traces.delete(traceId);
77
+ this.closed.set(traceId, state.spanIds.size);
78
+ if (this.closed.size > 1024) {
79
+ const oldest = this.closed.keys().next().value;
80
+ if (oldest !== void 0) {
81
+ this.closed.delete(oldest);
82
+ }
83
+ }
84
+ state.complete(state.spanIds.size);
85
+ }
86
+ };
87
+
1
88
  // src/readEnv.ts
2
89
  function readEnv(name) {
3
90
  if (typeof process !== "undefined" && process.env) {
@@ -94,7 +181,7 @@ function encodeRequestBody(body) {
94
181
  }
95
182
 
96
183
  // src/version.generated.ts
97
- var __version__ = "0.54.2";
184
+ var __version__ = "0.54.4";
98
185
  var __packageName__ = "bitfab";
99
186
 
100
187
  // src/errors.ts
@@ -1540,6 +1627,7 @@ var HttpClient = class {
1540
1627
  // must not wait on another client's slow finalize: a false `close()` failure
1541
1628
  // caused by unrelated work is worse than no signal at all.
1542
1629
  this.deferredWork = /* @__PURE__ */ new Set();
1630
+ this.traceCompletion = new TraceCompletion();
1543
1631
  this.closed = false;
1544
1632
  this.apiKey = config.apiKey;
1545
1633
  this.serviceUrl = config.serviceUrl;
@@ -1973,6 +2061,10 @@ var HttpClient = class {
1973
2061
  );
1974
2062
  }
1975
2063
  sendExternalSpan(payload) {
2064
+ const ref = carrierRef(payload);
2065
+ if (ref?.spanId !== void 0) {
2066
+ this.traceCompletion.record(ref.traceId, ref.spanId);
2067
+ }
1976
2068
  const gate = this.externalSpanTransform;
1977
2069
  if (gate === void 0) {
1978
2070
  this.submitExternalSpan(payload);
@@ -2004,13 +2096,28 @@ var HttpClient = class {
2004
2096
  */
2005
2097
  sendExternalTrace(rawPayload) {
2006
2098
  const payload = mergeCallerMetadataIntoTracePayload(rawPayload);
2007
- const gate = this.externalTraceTransform;
2008
- if (gate === void 0) {
2009
- this.submitExternalTrace(payload);
2099
+ const traceId = sourceTraceIdOf(payload);
2100
+ if (payload.completed === true && traceId !== void 0) {
2101
+ this.traceCompletion.close(
2102
+ traceId,
2103
+ (expectedSpanCount) => this.sendCountedExternalTrace({ ...payload, expectedSpanCount }),
2104
+ payload.dropped === true
2105
+ );
2010
2106
  return;
2011
2107
  }
2108
+ if (traceId !== void 0) {
2109
+ this.traceCompletion.open(traceId);
2110
+ }
2111
+ this.sendCountedExternalTrace(payload);
2112
+ }
2113
+ sendCountedExternalTrace(payload) {
2012
2114
  try {
2013
- gate(payload, (ready) => this.submitExternalTrace(ready));
2115
+ const gate = this.externalTraceTransform;
2116
+ if (gate === void 0) {
2117
+ this.submitExternalTrace(payload);
2118
+ } else {
2119
+ gate(payload, (ready) => this.submitExternalTrace(ready));
2120
+ }
2014
2121
  } catch (error) {
2015
2122
  warnDroppedExternalTrace(error);
2016
2123
  }
@@ -2264,4 +2371,4 @@ export {
2264
2371
  parseRetryAfterMs,
2265
2372
  HttpClient
2266
2373
  };
2267
- //# sourceMappingURL=chunk-ELGX3GWC.js.map
2374
+ //# sourceMappingURL=chunk-KDLFM5FD.js.map