@utk09/ui-observability 0.9.4 → 0.9.5

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 (208) hide show
  1. package/CHANGELOG.md +45 -0
  2. package/README.md +122 -94
  3. package/dist/batch-BQFtb3i5.cjs +2 -0
  4. package/dist/batch-BQFtb3i5.cjs.map +1 -0
  5. package/dist/{batch-D-4xl7op.js → batch-BUJh-1jZ.js} +7 -5
  6. package/dist/batch-BUJh-1jZ.js.map +1 -0
  7. package/dist/bus/bus.d.ts +76 -2
  8. package/dist/bus/links.d.ts +9 -2
  9. package/dist/bus/validation.d.ts +7 -0
  10. package/dist/bus-BW6N-pY8.cjs +2 -0
  11. package/dist/bus-BW6N-pY8.cjs.map +1 -0
  12. package/dist/bus-DeOPHWaD.js +647 -0
  13. package/dist/bus-DeOPHWaD.js.map +1 -0
  14. package/dist/capture/storm.d.ts +2 -1
  15. package/dist/capture/types.d.ts +2 -0
  16. package/dist/{console-Boqa7new.cjs → console-Ce6heqZZ.cjs} +2 -2
  17. package/dist/console-Ce6heqZZ.cjs.map +1 -0
  18. package/dist/{console-BT7mcgU6.js → console-DbAZYJuy.js} +4 -4
  19. package/dist/console-DbAZYJuy.js.map +1 -0
  20. package/dist/constants-CN7GE1Lt.js +246 -0
  21. package/dist/constants-CN7GE1Lt.js.map +1 -0
  22. package/dist/constants-CW-MlHdY.cjs +2 -0
  23. package/dist/constants-CW-MlHdY.cjs.map +1 -0
  24. package/dist/constants.d.ts +29 -9
  25. package/dist/context-storage-BjzeBSxH.cjs +2 -0
  26. package/dist/context-storage-BjzeBSxH.cjs.map +1 -0
  27. package/dist/context-storage-DxZf4Gfh.js +52 -0
  28. package/dist/context-storage-DxZf4Gfh.js.map +1 -0
  29. package/dist/core/breadcrumbs.d.ts +7 -2
  30. package/dist/core/context.d.ts +2 -0
  31. package/dist/core/diagnostics.d.ts +11 -3
  32. package/dist/core/meter.d.ts +6 -1
  33. package/dist/core/pipeline.d.ts +29 -2
  34. package/dist/core/record.d.ts +2 -1
  35. package/dist/core/runtime.d.ts +91 -3
  36. package/dist/core/sampling.d.ts +3 -1
  37. package/dist/core/sender.d.ts +35 -1
  38. package/dist/deadline-5_PecIl5.js +26 -0
  39. package/dist/deadline-5_PecIl5.js.map +1 -0
  40. package/dist/deadline-XSnm95pg.cjs +2 -0
  41. package/dist/deadline-XSnm95pg.cjs.map +1 -0
  42. package/dist/emergency-queue-BYUyDHGD.js +69 -0
  43. package/dist/emergency-queue-BYUyDHGD.js.map +1 -0
  44. package/dist/emergency-queue-Cs1hrjsC.cjs +2 -0
  45. package/dist/emergency-queue-Cs1hrjsC.cjs.map +1 -0
  46. package/dist/{errors-BDI96zKc.js → errors-BhC7HTSt.js} +8 -9
  47. package/dist/errors-BhC7HTSt.js.map +1 -0
  48. package/dist/errors-CgY633cG.cjs +3 -0
  49. package/dist/errors-CgY633cG.cjs.map +1 -0
  50. package/dist/factory-C5TujXlU.js +280 -0
  51. package/dist/factory-C5TujXlU.js.map +1 -0
  52. package/dist/factory-CNj4DH3n.cjs +2 -0
  53. package/dist/factory-CNj4DH3n.cjs.map +1 -0
  54. package/dist/fixed-window-CtaFHanJ.js +12 -0
  55. package/dist/fixed-window-CtaFHanJ.js.map +1 -0
  56. package/dist/fixed-window-CvESpFYV.cjs +2 -0
  57. package/dist/fixed-window-CvESpFYV.cjs.map +1 -0
  58. package/dist/identity-8vKkyAWm.js +103 -0
  59. package/dist/identity-8vKkyAWm.js.map +1 -0
  60. package/dist/identity-Da9TkuKd.cjs +2 -0
  61. package/dist/identity-Da9TkuKd.cjs.map +1 -0
  62. package/dist/index.cjs +1 -1
  63. package/dist/index.cjs.map +1 -1
  64. package/dist/index.d.ts +45 -7
  65. package/dist/index.js +908 -529
  66. package/dist/index.js.map +1 -1
  67. package/dist/indexeddb-storage-BTfIFRHF.cjs +2 -0
  68. package/dist/indexeddb-storage-BTfIFRHF.cjs.map +1 -0
  69. package/dist/{indexeddb-storage-HgkQUTyS.js → indexeddb-storage-CCA1LvtO.js} +33 -18
  70. package/dist/indexeddb-storage-CCA1LvtO.js.map +1 -0
  71. package/dist/{interactions-Ck01cEij.js → interactions-BUDLxbv1.js} +16 -12
  72. package/dist/interactions-BUDLxbv1.js.map +1 -0
  73. package/dist/interactions-NHbLKrqe.cjs +2 -0
  74. package/dist/interactions-NHbLKrqe.cjs.map +1 -0
  75. package/dist/{journey-DRK0A8u_.js → journey-BUJ4WJxP.js} +12 -12
  76. package/dist/{journey-DRK0A8u_.js.map → journey-BUJ4WJxP.js.map} +1 -1
  77. package/dist/{journey-Co9OlzrW.cjs → journey-B_tTnvac.cjs} +2 -2
  78. package/dist/{journey-Co9OlzrW.cjs.map → journey-B_tTnvac.cjs.map} +1 -1
  79. package/dist/keys-BHhw_0Wh.cjs +2 -0
  80. package/dist/keys-BHhw_0Wh.cjs.map +1 -0
  81. package/dist/keys-D5r_GH9q.js +17 -0
  82. package/dist/keys-D5r_GH9q.js.map +1 -0
  83. package/dist/{library-request-ChCar5F8.js → library-request-BKOcaKtQ.js} +2 -2
  84. package/dist/{library-request-ChCar5F8.js.map → library-request-BKOcaKtQ.js.map} +1 -1
  85. package/dist/{library-request-gMFOCNcn.cjs → library-request-BaZOi4eg.cjs} +2 -2
  86. package/dist/{library-request-gMFOCNcn.cjs.map → library-request-BaZOi4eg.cjs.map} +1 -1
  87. package/dist/{long-frames-Bky6nZDF.cjs → long-frames-CF3qiSAX.cjs} +2 -2
  88. package/dist/long-frames-CF3qiSAX.cjs.map +1 -0
  89. package/dist/{long-frames-DDumopji.js → long-frames-DI5ypfAF.js} +5 -5
  90. package/dist/long-frames-DI5ypfAF.js.map +1 -0
  91. package/dist/models/batch.d.ts +4 -0
  92. package/dist/models/bus.d.ts +32 -9
  93. package/dist/models/config.d.ts +347 -9
  94. package/dist/models/drop.d.ts +1 -1
  95. package/dist/models/health.d.ts +27 -0
  96. package/dist/models/storage.d.ts +13 -0
  97. package/dist/network-CF8UO5PK.cjs +2 -0
  98. package/dist/network-CF8UO5PK.cjs.map +1 -0
  99. package/dist/{network-BsBZ6_-Q.js → network-DdzXDI3J.js} +18 -16
  100. package/dist/network-DdzXDI3J.js.map +1 -0
  101. package/dist/openfin-BwEN0uj4.js.map +1 -1
  102. package/dist/openfin-DtH2f7zX.cjs.map +1 -1
  103. package/dist/{platform-4yYjMD-B.js → platform-Bw1btWCT.js} +16 -10
  104. package/dist/platform-Bw1btWCT.js.map +1 -0
  105. package/dist/platform-DBSKTvFn.cjs +2 -0
  106. package/dist/platform-DBSKTvFn.cjs.map +1 -0
  107. package/dist/{remote-rules-1KqfVBIu.js → remote-rules-DIcC5vVC.js} +3 -3
  108. package/dist/{remote-rules-1KqfVBIu.js.map → remote-rules-DIcC5vVC.js.map} +1 -1
  109. package/dist/{remote-rules-BHgEGlYi.cjs → remote-rules-DpFmOef2.cjs} +2 -2
  110. package/dist/{remote-rules-BHgEGlYi.cjs.map → remote-rules-DpFmOef2.cjs.map} +1 -1
  111. package/dist/sampling-BVjspJWu.cjs +2 -0
  112. package/dist/sampling-BVjspJWu.cjs.map +1 -0
  113. package/dist/{sampling-nYIWP_vP.js → sampling-Cm3WhI0a.js} +26 -36
  114. package/dist/sampling-Cm3WhI0a.js.map +1 -0
  115. package/dist/{sanitize-BiUKCUG6.js → sanitize-C-ahCFsG.js} +2 -2
  116. package/dist/{sanitize-BiUKCUG6.js.map → sanitize-C-ahCFsG.js.map} +1 -1
  117. package/dist/{sanitize-BxuPSXOk.cjs → sanitize-DHucKGdi.cjs} +2 -2
  118. package/dist/{sanitize-BxuPSXOk.cjs.map → sanitize-DHucKGdi.cjs.map} +1 -1
  119. package/dist/sender-D3fRCR8d.cjs +3 -0
  120. package/dist/sender-D3fRCR8d.cjs.map +1 -0
  121. package/dist/sender-DUUmSglb.js +1163 -0
  122. package/dist/sender-DUUmSglb.js.map +1 -0
  123. package/dist/storage/context-storage.d.ts +89 -0
  124. package/dist/storage/emergency-queue.d.ts +9 -1
  125. package/dist/storage/factory.d.ts +7 -1
  126. package/dist/storage/idb-driver.d.ts +13 -0
  127. package/dist/storage/indexeddb-storage.d.ts +13 -0
  128. package/dist/storage/local-storage.d.ts +21 -1
  129. package/dist/storage/memory-storage.d.ts +13 -0
  130. package/dist/storage-BA9RqRO-.cjs +2 -0
  131. package/dist/storage-BA9RqRO-.cjs.map +1 -0
  132. package/dist/storage-w7Tslvlq.js +17 -0
  133. package/dist/storage-w7Tslvlq.js.map +1 -0
  134. package/dist/storm-CNZjsw4T.cjs +2 -0
  135. package/dist/storm-CNZjsw4T.cjs.map +1 -0
  136. package/dist/storm-Daq2jGHU.js +10 -0
  137. package/dist/storm-Daq2jGHU.js.map +1 -0
  138. package/dist/transport/retry-engine.d.ts +7 -2
  139. package/dist/transport/status-transport.d.ts +17 -0
  140. package/dist/types-BR9t_w28.js.map +1 -1
  141. package/dist/types-C7WNpnG1.cjs.map +1 -1
  142. package/dist/utils/fixed-window.d.ts +9 -0
  143. package/dist/utils/identity.d.ts +9 -2
  144. package/dist/utils/lock.d.ts +8 -0
  145. package/dist/utils/openfin.d.ts +1 -1
  146. package/dist/utils/platform.d.ts +3 -1
  147. package/dist/web-socket-BacvKOvD.cjs +2 -0
  148. package/dist/web-socket-BacvKOvD.cjs.map +1 -0
  149. package/dist/web-socket-CpsD8WAa.js +105 -0
  150. package/dist/web-socket-CpsD8WAa.js.map +1 -0
  151. package/package.json +1 -1
  152. package/dist/batch-C4oF6XZT.cjs +0 -2
  153. package/dist/batch-C4oF6XZT.cjs.map +0 -1
  154. package/dist/batch-D-4xl7op.js.map +0 -1
  155. package/dist/bus-Cyvq-Ngx.js +0 -478
  156. package/dist/bus-Cyvq-Ngx.js.map +0 -1
  157. package/dist/bus-rOhVuRee.cjs +0 -2
  158. package/dist/bus-rOhVuRee.cjs.map +0 -1
  159. package/dist/console-BT7mcgU6.js.map +0 -1
  160. package/dist/console-Boqa7new.cjs.map +0 -1
  161. package/dist/constants-BgRS6MB0.cjs +0 -2
  162. package/dist/constants-BgRS6MB0.cjs.map +0 -1
  163. package/dist/constants-L_d5YvvQ.js +0 -160
  164. package/dist/constants-L_d5YvvQ.js.map +0 -1
  165. package/dist/errors-BDI96zKc.js.map +0 -1
  166. package/dist/errors-_xEsCVXn.cjs +0 -3
  167. package/dist/errors-_xEsCVXn.cjs.map +0 -1
  168. package/dist/fixed-window-BR0bu32Y.cjs +0 -2
  169. package/dist/fixed-window-BR0bu32Y.cjs.map +0 -1
  170. package/dist/fixed-window-Re7jIRJH.js +0 -9
  171. package/dist/fixed-window-Re7jIRJH.js.map +0 -1
  172. package/dist/identity-Bwku7zzN.cjs +0 -2
  173. package/dist/identity-Bwku7zzN.cjs.map +0 -1
  174. package/dist/identity-Dtv917nz.js +0 -98
  175. package/dist/identity-Dtv917nz.js.map +0 -1
  176. package/dist/indexeddb-storage-Cvavrf-y.cjs +0 -2
  177. package/dist/indexeddb-storage-Cvavrf-y.cjs.map +0 -1
  178. package/dist/indexeddb-storage-HgkQUTyS.js.map +0 -1
  179. package/dist/interactions-Bhe1ft8P.cjs +0 -2
  180. package/dist/interactions-Bhe1ft8P.cjs.map +0 -1
  181. package/dist/interactions-Ck01cEij.js.map +0 -1
  182. package/dist/log-record-cwFiLyPY.js +0 -17
  183. package/dist/log-record-cwFiLyPY.js.map +0 -1
  184. package/dist/log-record-jX9eWUP8.cjs +0 -2
  185. package/dist/log-record-jX9eWUP8.cjs.map +0 -1
  186. package/dist/long-frames-Bky6nZDF.cjs.map +0 -1
  187. package/dist/long-frames-DDumopji.js.map +0 -1
  188. package/dist/network-BsBZ6_-Q.js.map +0 -1
  189. package/dist/network-_4csBGBM.cjs +0 -2
  190. package/dist/network-_4csBGBM.cjs.map +0 -1
  191. package/dist/platform-4yYjMD-B.js.map +0 -1
  192. package/dist/platform-o7r1ocOu.cjs +0 -2
  193. package/dist/platform-o7r1ocOu.cjs.map +0 -1
  194. package/dist/sampling-C4my-ma0.cjs +0 -2
  195. package/dist/sampling-C4my-ma0.cjs.map +0 -1
  196. package/dist/sampling-nYIWP_vP.js.map +0 -1
  197. package/dist/sender-6vrT9fFL.cjs +0 -3
  198. package/dist/sender-6vrT9fFL.cjs.map +0 -1
  199. package/dist/sender-DFQMF39P.js +0 -1351
  200. package/dist/sender-DFQMF39P.js.map +0 -1
  201. package/dist/storm-6YYbXzT_.cjs +0 -2
  202. package/dist/storm-6YYbXzT_.cjs.map +0 -1
  203. package/dist/storm-CfO0Sam2.js +0 -10
  204. package/dist/storm-CfO0Sam2.js.map +0 -1
  205. package/dist/web-socket-Ar4cZxmd.js +0 -102
  206. package/dist/web-socket-Ar4cZxmd.js.map +0 -1
  207. package/dist/web-socket-BwJdwSHl.cjs +0 -2
  208. package/dist/web-socket-BwJdwSHl.cjs.map +0 -1
@@ -0,0 +1,1163 @@
1
+ import { $ as e, B as t, Ct as n, Dt as r, F as i, Gt as a, H as o, I as s, K as c, Kt as l, Mt as u, Nt as d, Ot as f, P as p, Q as m, Tt as h, U as ee, Vt as te, Z as ne, _n as g, cn as _, et as v, f as y, nt as re, on as b, r as x, tn as S, tt as ie, vn as C, zt as ae } from "./constants-CN7GE1Lt.js";
2
+ import { l as oe, t as se } from "./sampling-Cm3WhI0a.js";
3
+ import { n as w, r as T, t as ce } from "./deadline-5_PecIl5.js";
4
+ import { o as le, t as E } from "./sanitize-C-ahCFsG.js";
5
+ import { i as ue } from "./identity-8vKkyAWm.js";
6
+ import { i as de, r as D } from "./library-request-BKOcaKtQ.js";
7
+ import { t as O } from "./openfin-BwEN0uj4.js";
8
+ import { r as k } from "./platform-Bw1btWCT.js";
9
+ import { a as fe, t as pe } from "./batch-BUJh-1jZ.js";
10
+ import { n as me, t as he } from "./remote-rules-DIcC5vVC.js";
11
+ import { storageLimits as A } from "./storage-w7Tslvlq.js";
12
+ import { clearEmergencyContext as j, drainEmergencyQueue as ge, n as _e, saveToEmergencyQueue as ve, t as ye } from "./emergency-queue-BYUyDHGD.js";
13
+ import { createStorage as be, storageName as xe } from "./factory-C5TujXlU.js";
14
+ import { ContextStorage as Se } from "./context-storage-DxZf4Gfh.js";
15
+ //#region src/transport/errors.ts
16
+ var M = class extends Error {
17
+ kind;
18
+ status;
19
+ retryAfterMs;
20
+ maxBytes;
21
+ constructor(e, t, n, r, i, a) {
22
+ super(t, a === void 0 ? void 0 : { cause: a }), this.kind = e, this.status = n, this.retryAfterMs = r, this.maxBytes = i, this.name = "TransportError";
23
+ }
24
+ };
25
+ function Ce(e) {
26
+ if (!e) return;
27
+ let t = Number(e);
28
+ if (Number.isFinite(t)) return Math.max(0, t * h);
29
+ let n = Date.parse(e);
30
+ return Number.isNaN(n) ? void 0 : Math.max(0, n - Date.now());
31
+ }
32
+ //#endregion
33
+ //#region src/transport/serializers/ecs.ts
34
+ var N = {
35
+ "error.type": "type",
36
+ "error.message": "message",
37
+ "error.stack_trace": "stack_trace"
38
+ }, we = /* @__PURE__ */ new Set([x, ...Object.keys(N)]);
39
+ function Te(e) {
40
+ return r.test(e) ? Number(BigInt(e) / f) : Date.now();
41
+ }
42
+ function Ee(e) {
43
+ return typeof e == "string" || typeof e == "number" || typeof e == "boolean" ? e : typeof e == "bigint" ? e.toString() : null;
44
+ }
45
+ function De(e, t, n) {
46
+ let r = e[t];
47
+ r === void 0 ? e[t] = n : Array.isArray(r) ? r.push(n) : e[t] = [r, n];
48
+ }
49
+ function P(e, t, n, r) {
50
+ let i = Ee(n);
51
+ if (i !== null) De(e, t, i);
52
+ else if (typeof n == "object" && n && !r.has(n)) {
53
+ if (r.add(n), Array.isArray(n)) for (let i of n) P(e, t, i, r);
54
+ else for (let [i, a] of Object.entries(n)) P(e, `${t}.${i}`, a, r);
55
+ r.delete(n);
56
+ }
57
+ }
58
+ function F(e) {
59
+ let t = Object.create(null), n = /* @__PURE__ */ new WeakSet();
60
+ for (let [r, i] of Object.entries(e)) P(t, r, i, n);
61
+ return t;
62
+ }
63
+ function Oe(e) {
64
+ let t = [];
65
+ for (let [n, r] of Object.entries(N)) {
66
+ let i = e[n];
67
+ typeof i == "string" && t.push([r, i]);
68
+ }
69
+ return t.length > 0 ? Object.fromEntries(t) : void 0;
70
+ }
71
+ function ke(e) {
72
+ return Object.fromEntries(Object.entries(e).filter(([e, t]) => !(we.has(e) && typeof t == "string")));
73
+ }
74
+ var Ae = {
75
+ name: "ecs",
76
+ serialize(e) {
77
+ return {
78
+ body: `${e.map((e) => {
79
+ let t = Te(e.timeUnixNano), { attributes: n } = e, r = n[x];
80
+ return JSON.stringify({
81
+ "@timestamp": new Date(t).toISOString(),
82
+ message: e.body,
83
+ ecs: { version: ee },
84
+ log: {
85
+ level: o[e.severityText] ?? e.severityText.toLowerCase(),
86
+ logger: typeof r == "string" ? r : void 0
87
+ },
88
+ event: { kind: n["log.type"] === "metric" ? "metric" : "event" },
89
+ error: Oe(n),
90
+ trace: { id: e.traceId },
91
+ span: { id: e.spanId },
92
+ service: {
93
+ name: e.resource[a],
94
+ version: e.resource[l],
95
+ environment: e.resource[te]
96
+ },
97
+ user_agent: { original: e.resource[ae] },
98
+ url: { full: n["url.full"] ?? n["page.url"] },
99
+ labels: {
100
+ ...F(e.resource),
101
+ ...F(ke(n))
102
+ }
103
+ });
104
+ }).join("\n")}\n`,
105
+ contentType: i
106
+ };
107
+ }
108
+ };
109
+ //#endregion
110
+ //#region src/transport/serializers/otlp.ts
111
+ function I(e) {
112
+ return e == null ? null : typeof e == "string" ? { stringValue: e } : typeof e == "boolean" ? { boolValue: e } : typeof e == "number" ? Number.isSafeInteger(e) ? { intValue: String(e) } : { doubleValue: e } : typeof e == "bigint" ? { intValue: e.toString() } : Array.isArray(e) ? { arrayValue: { values: e.map(I).filter((e) => e !== null) } } : typeof e == "object" ? { kvlistValue: { values: L(e) } } : null;
113
+ }
114
+ function L(e) {
115
+ let t = [];
116
+ for (let [n, r] of Object.entries(e)) {
117
+ let e = I(r);
118
+ e && t.push({
119
+ key: n,
120
+ value: e
121
+ });
122
+ }
123
+ return t;
124
+ }
125
+ var R = /* @__PURE__ */ new WeakMap();
126
+ function je(e) {
127
+ let t = R.get(e);
128
+ if (t !== void 0) return t;
129
+ let n = JSON.stringify(e);
130
+ return R.set(e, n), n;
131
+ }
132
+ function z(e) {
133
+ let t = /* @__PURE__ */ new Map();
134
+ for (let n of e) {
135
+ let e = je(n.resource), r = t.get(e);
136
+ r ? r.records.push(n) : t.set(e, {
137
+ resource: n.resource,
138
+ records: [n]
139
+ });
140
+ }
141
+ return [...t.values()];
142
+ }
143
+ var Me = {
144
+ name: S,
145
+ serialize(e) {
146
+ let t = z(e).map((e) => ({
147
+ resource: { attributes: L(e.resource) },
148
+ scopeLogs: [{
149
+ scope: {
150
+ name: g,
151
+ version: C
152
+ },
153
+ logRecords: e.records.map((e) => ({
154
+ timeUnixNano: e.timeUnixNano,
155
+ observedTimeUnixNano: e.observedTimeUnixNano ?? e.timeUnixNano,
156
+ severityNumber: e.severityNumber,
157
+ severityText: e.severityText,
158
+ body: { stringValue: e.body },
159
+ traceId: e.traceId,
160
+ spanId: e.spanId,
161
+ flags: e.traceFlags,
162
+ attributes: L(e.attributes)
163
+ }))
164
+ }]
165
+ }));
166
+ return {
167
+ body: JSON.stringify({ resourceLogs: t }),
168
+ contentType: p
169
+ };
170
+ }
171
+ }, Ne = 1;
172
+ function B(e, t) {
173
+ let n = e.attributes[t];
174
+ return typeof n == "number" && Number.isFinite(n) ? n : void 0;
175
+ }
176
+ function V(e) {
177
+ return Array.isArray(e) && e.every((e) => typeof e == "number" && Number.isFinite(e));
178
+ }
179
+ function H(e) {
180
+ let t = e.attributes[n.attributeKeys], r = {};
181
+ if (!Array.isArray(t)) return r;
182
+ for (let n of t) {
183
+ let t = typeof n == "string" && !n.startsWith("metric.") ? e.attributes[n] : void 0;
184
+ (typeof t == "string" || typeof t == "number" || typeof t == "boolean") && (r[n] = t);
185
+ }
186
+ return r;
187
+ }
188
+ function Pe(e) {
189
+ let t = B(e, n.count);
190
+ if (t === void 0) {
191
+ let t = B(e, "metric.value");
192
+ return t === void 0 ? null : {
193
+ count: "1",
194
+ sum: t < 0 ? void 0 : t,
195
+ min: t,
196
+ max: t,
197
+ bucketCounts: ["1"],
198
+ explicitBounds: []
199
+ };
200
+ }
201
+ let r = e.attributes[n.bounds], i = e.attributes[n.buckets];
202
+ if (!V(r) || !V(i) || i.length !== r.length + 1) return null;
203
+ let a = B(e, n.min);
204
+ return {
205
+ count: String(t),
206
+ sum: a !== void 0 && a < 0 ? void 0 : B(e, n.sum),
207
+ min: a,
208
+ max: B(e, n.max),
209
+ bucketCounts: i.map(String),
210
+ explicitBounds: r
211
+ };
212
+ }
213
+ function Fe(e) {
214
+ let t = B(e, "metric.value");
215
+ return t === void 0 ? null : { asDouble: t };
216
+ }
217
+ function Ie(e, t) {
218
+ let r = e.attributes[n.start], i = t === "histogram" ? Pe(e) : Fe(e);
219
+ return i === null ? null : {
220
+ attributes: L(H(e)),
221
+ startTimeUnixNano: typeof r == "string" ? r : e.timeUnixNano,
222
+ timeUnixNano: e.timeUnixNano,
223
+ ...i
224
+ };
225
+ }
226
+ function Le({ name: e, unit: t, field: n, points: r }) {
227
+ if (n === "gauge") return {
228
+ name: e,
229
+ unit: t,
230
+ gauge: { dataPoints: r }
231
+ };
232
+ let i = {
233
+ aggregationTemporality: Ne,
234
+ dataPoints: r
235
+ };
236
+ return n === "sum" && (i.isMonotonic = r.every((e) => e.asDouble >= 0)), {
237
+ name: e,
238
+ unit: t,
239
+ [n]: i
240
+ };
241
+ }
242
+ var Re = {
243
+ name: S,
244
+ serialize(e) {
245
+ let t = z(e).map((e) => {
246
+ let t = /* @__PURE__ */ new Map();
247
+ for (let n of e.records) {
248
+ let e = n.attributes["metric.type"], r = e === "counter" ? "sum" : e === "histogram" ? "histogram" : "gauge", i = Ie(n, r);
249
+ if (i === null) continue;
250
+ let a = n.attributes["metric.name"], o = n.attributes["metric.unit"], s = {
251
+ name: typeof a == "string" ? a : n.body,
252
+ unit: typeof o == "string" ? o : "",
253
+ field: r,
254
+ points: []
255
+ }, c = JSON.stringify([
256
+ s.field,
257
+ s.name,
258
+ s.unit
259
+ ]), l = t.get(c) ?? s;
260
+ l.points.push(i), t.set(c, l);
261
+ }
262
+ return {
263
+ resource: { attributes: L(e.resource) },
264
+ scopeMetrics: [{
265
+ scope: {
266
+ name: g,
267
+ version: C
268
+ },
269
+ metrics: [...t.values()].map(Le)
270
+ }]
271
+ };
272
+ });
273
+ return {
274
+ body: JSON.stringify({ resourceMetrics: t }),
275
+ contentType: p
276
+ };
277
+ }
278
+ }, U = {
279
+ internal: 1,
280
+ client: 3
281
+ }, ze = 2, Be = 0, Ve = /* @__PURE__ */ new Set([y, ...Object.values(b)]);
282
+ function W(e, t) {
283
+ let n = e.attributes[t];
284
+ return typeof n == "string" ? n : void 0;
285
+ }
286
+ function He(e) {
287
+ let t = Object.fromEntries(Object.entries(e.attributes).filter(([e]) => !Ve.has(e))), n = W(e, b.parentId);
288
+ return {
289
+ traceId: e.traceId,
290
+ spanId: e.spanId,
291
+ ...n ? { parentSpanId: n } : {},
292
+ name: e.body,
293
+ kind: W(e, b.kind) === "client" ? U.client : U.internal,
294
+ startTimeUnixNano: W(e, b.start) ?? e.timeUnixNano,
295
+ endTimeUnixNano: W(e, b.end) ?? e.timeUnixNano,
296
+ attributes: L(t),
297
+ status: { code: W(e, b.status) === "error" ? ze : Be }
298
+ };
299
+ }
300
+ var Ue = {
301
+ name: S,
302
+ serialize(e) {
303
+ let t = z(e).map((e) => ({
304
+ resource: { attributes: L(e.resource) },
305
+ scopeSpans: [{
306
+ scope: {
307
+ name: g,
308
+ version: C
309
+ },
310
+ spans: e.records.map(He)
311
+ }]
312
+ }));
313
+ return {
314
+ body: JSON.stringify({ resourceSpans: t }),
315
+ contentType: p
316
+ };
317
+ }
318
+ };
319
+ //#endregion
320
+ //#region src/transport/serializers/select.ts
321
+ function We(e) {
322
+ return e === "otlp" ? Me : e === "ecs" ? Ae : e;
323
+ }
324
+ function G(e, t) {
325
+ let n = (t) => e.records.length > 0 && e.records.every((e) => pe(e, "log.type") === t);
326
+ return t.metricsEndpoint !== "" && n("metric") ? "metrics" : n("span") ? "traces" : "logs";
327
+ }
328
+ function K(e, t) {
329
+ return e === "metrics" ? t.metricsEndpoint : e === "traces" ? t.tracesEndpoint : t.endpoint;
330
+ }
331
+ var Ge = {
332
+ metrics: Re,
333
+ traces: Ue
334
+ };
335
+ function q(e, t, n, r) {
336
+ try {
337
+ return (Ge[G(t, e)] ?? We(e.serializer)).serialize(t.records);
338
+ } catch (e) {
339
+ n.report("transport.serialize_failed", r, { batchId: t.id }, e);
340
+ return;
341
+ }
342
+ }
343
+ //#endregion
344
+ //#region src/transport/status-transport.ts
345
+ var J = class {
346
+ config;
347
+ diagnostics;
348
+ onSampling;
349
+ onLoss;
350
+ cancellation = new AbortController();
351
+ stop() {
352
+ this.cancellation.abort();
353
+ }
354
+ assertContext(e) {
355
+ if (this.cancellation.signal.aborted || this.config.securityContext.isolation && this.config.securityContext.id && e.routingContextId !== this.config.securityContext.id) throw this.diagnostics.count("security.context_mismatch"), new M("permanent", "The delivery context is inactive or does not match the batch.");
356
+ }
357
+ async untilCancelled(e) {
358
+ if (this.cancellation.signal.aborted) throw new M("permanent", "The delivery context retired.");
359
+ let t, n = new Promise((e, n) => {
360
+ t = () => {
361
+ n(new M("permanent", "The delivery context retired."));
362
+ }, this.cancellation.signal.addEventListener("abort", t, { once: !0 });
363
+ });
364
+ try {
365
+ return await Promise.race([e, n]);
366
+ } finally {
367
+ this.cancellation.signal.removeEventListener("abort", t);
368
+ }
369
+ }
370
+ throttledUntil = {
371
+ logs: 0,
372
+ metrics: 0,
373
+ traces: 0
374
+ };
375
+ acceptedAt = null;
376
+ constructor(e, t, n = () => void 0, r = () => void 0) {
377
+ this.config = e, this.diagnostics = t, this.onSampling = n, this.onLoss = r;
378
+ }
379
+ throttledForMs(e) {
380
+ return Math.max(0, this.throttledUntil[G(e, this.config)] - Date.now());
381
+ }
382
+ lastSendAt() {
383
+ return this.acceptedAt;
384
+ }
385
+ serialize(e) {
386
+ let t = q(this.config, e, this.diagnostics, "The serializer threw an error. The library dropped the batch.");
387
+ return t || this.onLoss("unserializable", e.records, [e.id]), t;
388
+ }
389
+ accept(e, t) {
390
+ this.throttledUntil[e] = 0, this.acceptedAt = Date.now(), this.config.sampling.remote && t != null && this.onSampling(t);
391
+ }
392
+ reject(e, t, n, r) {
393
+ let i = String(e);
394
+ if (e === 401 || e === 403) {
395
+ let t = r.advice === void 0 ? "" : ` ${r.advice}`;
396
+ return this.diagnostics.report("transport.rejected_credentials", `The server returned ${i}.${t}`, {
397
+ status: e,
398
+ endpoint: K(n, this.config)
399
+ }), new M("permanent", `The server rejected the batch with ${i}.`, e);
400
+ }
401
+ if (e === 413) return new M("too_large", "The payload is too large.", e, void 0, r.maxBytes);
402
+ if (e === 429 || e === 503) {
403
+ let t = e === 429, i = r.retryAfterMs === void 0 ? void 0 : Math.min(r.retryAfterMs, this.config.delivery.retryMaxDelayMs);
404
+ return this.throttledUntil[n] = Date.now() + (i ?? this.config.delivery.retryBaseDelayMs), t && this.diagnostics.report("transport.throttled", "The server throttles the requests.", { retryAfterMs: i }), new M("throttled", t ? "The server throttles the requests." : "The service is not available.", e, i);
405
+ }
406
+ return e === 408 || e >= 500 ? new M("transient", `The server returned the error ${i}.`, e) : (this.diagnostics.report("transport.dropped_permanent", `The server rejected the batch with ${i}. The library dropped it.`, {
407
+ batchId: t,
408
+ status: e
409
+ }), new M("permanent", `The server rejected the batch with ${i}.`, e));
410
+ }
411
+ };
412
+ //#endregion
413
+ //#region src/transport/custom-transport.ts
414
+ function Y(e, t, n) {
415
+ return {
416
+ kind: n,
417
+ body: t.body,
418
+ contentType: t.contentType,
419
+ batchId: e.id,
420
+ attempt: e.attempts,
421
+ records: e.records,
422
+ routingContextId: e.routingContextId
423
+ };
424
+ }
425
+ function Ke(e) {
426
+ if (typeof e != "object" || !e) return !1;
427
+ let t = Reflect.get(e, "status");
428
+ return typeof t == "number" && Number.isInteger(t) && t >= 100 && t <= 599;
429
+ }
430
+ function X(e) {
431
+ return typeof e == "number" && Number.isFinite(e) ? Math.max(0, e) : void 0;
432
+ }
433
+ var qe = class extends J {
434
+ async send(e) {
435
+ this.assertContext(e);
436
+ let { transport: t } = this.config;
437
+ if (!t || e.records.length === 0) return;
438
+ let n = this.serialize(e);
439
+ if (!n) return;
440
+ let r = G(e, this.config), i = Y(e, n, r);
441
+ i.signal = this.cancellation.signal;
442
+ let a;
443
+ try {
444
+ a = await w(this.untilCancelled(de(() => D(() => le(() => t(i))))), this.config.delivery.requestTimeoutMs);
445
+ } catch (e) {
446
+ let t = e instanceof ce ? e : void 0;
447
+ throw new M(t ? "timeout" : "transient", t?.message ?? "The custom transport failed.", void 0, void 0, void 0, e);
448
+ }
449
+ if (this.assertContext(e), !Ke(a)) throw this.diagnostics.report("transport.http_error", "The custom transport returned no valid status. The library sends the batch again later.", { batchId: e.id }), new M("transient", "The custom transport returned no valid status.");
450
+ let { status: o } = a;
451
+ if (o >= 200 && o < 300) this.accept(r, a.sampling);
452
+ else throw this.reject(o, e.id, r, {
453
+ retryAfterMs: X(a.retryAfterMs),
454
+ maxBytes: X(a.maxBytes)
455
+ });
456
+ }
457
+ }, Je = class {
458
+ deps;
459
+ installed = !1;
460
+ keepaliveBytes = 0;
461
+ onVisibility = () => {
462
+ document.visibilityState === "hidden" && this.flush("hidden");
463
+ };
464
+ onPageHide = () => {
465
+ this.flush("pagehide");
466
+ };
467
+ onFreeze = () => {
468
+ this.flush("freeze");
469
+ };
470
+ onOpenFinClose = () => {
471
+ this.flush("openfin-close");
472
+ };
473
+ constructor(e) {
474
+ this.deps = e;
475
+ }
476
+ install() {
477
+ if (this.installed || typeof addEventListener > "u") return;
478
+ this.installed = !0, typeof document < "u" && (document.addEventListener("visibilitychange", this.onVisibility), document.addEventListener("freeze", this.onFreeze)), addEventListener("pagehide", this.onPageHide);
479
+ let e = O()?.me;
480
+ this.deps.diagnostics.guard("openfin.unavailable", "The library could not subscribe to close-requested.", () => {
481
+ e?.on?.("close-requested", this.onOpenFinClose);
482
+ });
483
+ }
484
+ uninstall() {
485
+ if (!this.installed) return;
486
+ this.installed = !1, typeof document < "u" && (document.removeEventListener("visibilitychange", this.onVisibility), document.removeEventListener("freeze", this.onFreeze)), removeEventListener("pagehide", this.onPageHide);
487
+ let e = O()?.me;
488
+ this.deps.diagnostics.guard("openfin.unavailable", "The library could not unsubscribe from close-requested.", () => {
489
+ e?.removeListener?.("close-requested", this.onOpenFinClose);
490
+ });
491
+ }
492
+ flush(e) {
493
+ let { config: t, diagnostics: n } = this.deps;
494
+ if (n.guard("pipeline.crashed", "The metric points before the exit flush failed.", () => {
495
+ this.deps.beforeExit?.();
496
+ }), e === "hidden") {
497
+ this.flushAlive();
498
+ return;
499
+ }
500
+ if (t.transport) {
501
+ this.flushCustom(e, t.transportOnExit);
502
+ return;
503
+ }
504
+ let r = this.endpointUrl();
505
+ if (r !== null) {
506
+ this.keepaliveBytes = 0;
507
+ for (let i of this.deps.drainForExit()) {
508
+ let a = G(i, t), o = a === "logs" ? r.href : K(a, t);
509
+ o ? this.flushBatch(i, e, new URL(o)) : n.count("record.dropped_no_destination");
510
+ }
511
+ }
512
+ }
513
+ flushBatch(e, t, n) {
514
+ let { config: r } = this.deps;
515
+ if (e.records.length === 0) return;
516
+ let i = this.serialize(e, t);
517
+ if (!i) return;
518
+ if (E(i.body) > 6e4) {
519
+ this.park(e);
520
+ return;
521
+ }
522
+ n.searchParams.set(u, e.id), n.searchParams.set(d, t);
523
+ let a = n.toString(), o = this.deps.exitHeaders();
524
+ if (o) {
525
+ this.keepaliveFetch(a, e, i.body, t, () => {
526
+ let t = new Headers(o);
527
+ return t.set(v, i.contentType), t.set(m, e.id), t;
528
+ });
529
+ return;
530
+ }
531
+ if (r.headers) {
532
+ this.park(e);
533
+ return;
534
+ }
535
+ let c = this.beacon(a, i.body);
536
+ c !== "sent" && (c === "refused" ? this.park(e) : this.keepaliveFetch(a, e, i.body, t, () => new Headers({ [v]: s })));
537
+ }
538
+ flushAlive() {
539
+ let { diagnostics: e } = this.deps, t = "The flush of the hidden document failed.";
540
+ e.guard("pipeline.crashed", t, () => {
541
+ Promise.resolve(this.deps.flushAlive()).catch((n) => {
542
+ e.report("pipeline.crashed", t, void 0, n);
543
+ });
544
+ });
545
+ }
546
+ flushCustom(e, t) {
547
+ let { config: n, diagnostics: r } = this.deps;
548
+ for (let i of this.deps.drainForExit()) {
549
+ let a = this.serialize(i, e);
550
+ if (!a) continue;
551
+ let o = Y(i, a, G(i, n));
552
+ (t === void 0 || r.guard("transport.http_error", "The transportOnExit hook threw an error.", () => D(() => t(o))) !== !0) && this.park(i);
553
+ }
554
+ }
555
+ park(e) {
556
+ let { config: t, diagnostics: n, onLoss: r } = this.deps;
557
+ t.storage === "memory" || t.storage === "none" ? r?.("not_stored", e.records, [e.id]) : ve(e, n, r);
558
+ }
559
+ serialize(e, t) {
560
+ let n = q(this.deps.config, e, this.deps.diagnostics, `The ${t} exit flush could not serialize its batch.`);
561
+ return n || this.deps.onLoss?.("unserializable", e.records, [e.id]), n;
562
+ }
563
+ endpointUrl() {
564
+ let { endpoint: e } = this.deps.config;
565
+ return e ? this.deps.diagnostics.guard("config.invalid", `The exit flush cannot use the endpoint ${e}.`, () => new URL(e)) ?? null : null;
566
+ }
567
+ beacon(e, t) {
568
+ return this.deps.diagnostics.guard("transport.http_error", "The sendBeacon call failed.", () => {
569
+ let n = globalThis.navigator;
570
+ return n?.sendBeacon ? n.sendBeacon(e, new Blob([t], { type: "text/plain;charset=UTF-8" })) ? "sent" : "refused" : "unavailable";
571
+ }) ?? "unavailable";
572
+ }
573
+ keepaliveFetch(e, t, n, r, i) {
574
+ let { config: a, diagnostics: o } = this.deps, s = () => {
575
+ this.park(t);
576
+ }, c = E(n);
577
+ this.keepaliveBytes + c > 6e4 ? s() : (this.keepaliveBytes += c, o.guard("transport.http_error", "The keepalive fetch failed.", () => (fetch(e, {
578
+ method: "POST",
579
+ body: n,
580
+ headers: i(),
581
+ credentials: a.credentials,
582
+ keepalive: !0
583
+ }).catch((e) => {
584
+ o.report("transport.http_error", "The exit flush failed.", { reason: r }, e), s();
585
+ }), !0)) !== !0 && s());
586
+ }
587
+ };
588
+ //#endregion
589
+ //#region src/transport/compression.ts
590
+ async function Ye(e) {
591
+ let t = globalThis.CompressionStream;
592
+ if (t === void 0) return null;
593
+ let n = new Blob([e]).stream().pipeThrough(new t(c)), r = await new Response(n).arrayBuffer();
594
+ return new Uint8Array(r);
595
+ }
596
+ //#endregion
597
+ //#region src/transport/http-transport.ts
598
+ var Xe = "In a cross-origin iframe, the session cookie is a third-party cookie. The browser often blocks it. Forward the records to the platform document. As an alternative, issue the cookie with the Partitioned attribute.", Ze = class extends J {
599
+ cachedHeaders;
600
+ async send(e, t) {
601
+ this.assertContext(e);
602
+ let { config: n } = this, r = G(e, n), i = K(r, n);
603
+ i ? e.records.length !== 0 && await this.post(e, r, i, t) : this.diagnostics.count("record.dropped_no_destination");
604
+ }
605
+ lastHeaders() {
606
+ let { headers: e } = this.config;
607
+ if (!e) return;
608
+ if (typeof e != "function") return e;
609
+ let t = this.cachedHeaders;
610
+ return t?.source === e && t.endpoint === this.config.endpoint && t.metricsEndpoint === this.config.metricsEndpoint && t.tracesEndpoint === this.config.tracesEndpoint ? t.headers : void 0;
611
+ }
612
+ async post(e, t, n, r) {
613
+ let i = this.serialize(e);
614
+ if (!i) return;
615
+ let a = await this.attempt(e, n, i, r);
616
+ if (this.assertContext(e), this.rejectsCredentials(a.status) && typeof this.config.headers == "function" && (this.cachedHeaders = void 0, a = await this.attempt(e, n, i, r), this.assertContext(e)), a.ok) {
617
+ this.accept(t, a.headers.get(re));
618
+ return;
619
+ }
620
+ let o = a.status, s = Ce(a.headers.get(ie)), c = o === 413 ? await this.readMaxBytes(a) : void 0;
621
+ throw this.reject(o, e.id, t, {
622
+ retryAfterMs: s,
623
+ maxBytes: c,
624
+ advice: Xe
625
+ });
626
+ }
627
+ rejectsCredentials(e) {
628
+ return e === 401 || e === 403;
629
+ }
630
+ async attempt(t, n, r, i) {
631
+ let a = Date.now() + this.config.delivery.requestTimeoutMs, o = new Headers(await this.before(this.resolveHeaders(), a, n));
632
+ o.set(v, r.contentType), o.set(m, t.id), o.set(ne, String(t.attempts));
633
+ let s = await this.before(this.encodeBody(r.body), a, n);
634
+ this.assertContext(t), typeof s != "string" && o.set(e, c);
635
+ let l = this.keepalive(s);
636
+ return l && i?.(), this.dispatch(n, o, s, a, l);
637
+ }
638
+ async before(e, t, n) {
639
+ try {
640
+ return await w(this.untilCancelled(e), Math.max(0, t - Date.now()));
641
+ } catch {
642
+ throw this.cancellation.signal.aborted ? new M("permanent", "The delivery context retired.") : this.timeout(n);
643
+ }
644
+ }
645
+ timeout(e) {
646
+ let t = `The request took longer than ${String(this.config.delivery.requestTimeoutMs)} ms.`;
647
+ return this.diagnostics.report("transport.timeout", t, { endpoint: e }), new M("timeout", t);
648
+ }
649
+ async encodeBody(e) {
650
+ let { config: t } = this;
651
+ return t.compression !== "gzip" || E(e) < 1024 ? e : await this.diagnostics.guardAsync("transport.http_error", "The library could not compress the payload.", () => Ye(e)) ?? e;
652
+ }
653
+ async dispatch(e, t, n, r, i) {
654
+ let { config: a } = this, o = new AbortController(), s = () => {
655
+ o.abort();
656
+ };
657
+ this.cancellation.signal.addEventListener("abort", s, { once: !0 }), this.cancellation.signal.aborted && o.abort();
658
+ let c = setTimeout(() => {
659
+ o.abort();
660
+ }, Math.max(0, r - Date.now()));
661
+ T(c);
662
+ try {
663
+ return await D(() => fetch(e, {
664
+ method: "POST",
665
+ headers: t,
666
+ body: n,
667
+ credentials: a.credentials,
668
+ signal: o.signal,
669
+ cache: "no-store",
670
+ keepalive: i
671
+ }));
672
+ } catch {
673
+ throw this.cancellation.signal.aborted ? new M("permanent", "The delivery context retired.") : o.signal.aborted ? this.timeout(e) : new M(k() ? "transient" : "offline", "A network error occurred.");
674
+ } finally {
675
+ this.cancellation.signal.removeEventListener("abort", s), clearTimeout(c);
676
+ }
677
+ }
678
+ keepalive(e) {
679
+ let t = globalThis.document?.visibilityState === "hidden", n = typeof e == "string" ? E(e) : e.byteLength;
680
+ return t && n <= 6e4;
681
+ }
682
+ async readMaxBytes(e) {
683
+ return this.diagnostics.guardAsync("transport.http_error", "The library could not read the body of the 413 response.", async () => {
684
+ let t = JSON.parse(await e.text());
685
+ return typeof t.maxBytes == "number" ? t.maxBytes : void 0;
686
+ });
687
+ }
688
+ async resolveHeaders() {
689
+ let { headers: e } = this.config;
690
+ if (!e) return {};
691
+ if (typeof e != "function") return e;
692
+ let { endpoint: t, metricsEndpoint: n, tracesEndpoint: r } = this.config, i = await this.diagnostics.guardAsync("transport.http_error", "The headers() function threw an error.", () => Promise.resolve(e()));
693
+ return i && (this.cachedHeaders = {
694
+ source: e,
695
+ endpoint: t,
696
+ metricsEndpoint: n,
697
+ tracesEndpoint: r,
698
+ headers: i
699
+ }), i ?? {};
700
+ }
701
+ }, Qe = class {
702
+ storage;
703
+ transport;
704
+ diagnostics;
705
+ config;
706
+ batchesPerDrain;
707
+ onLoss;
708
+ timer = null;
709
+ attempt = 0;
710
+ draining = !1;
711
+ stopped = !1;
712
+ maxAttempts;
713
+ onOnline = () => {
714
+ this.attempt = 0, this.schedule(0);
715
+ };
716
+ constructor(e, t, n, r, i, a = () => void 0) {
717
+ this.storage = e, this.transport = t, this.diagnostics = n, this.config = r, this.batchesPerDrain = i, this.onLoss = a, this.maxAttempts = A(r.retention).maxAttempts;
718
+ }
719
+ start() {
720
+ this.stopped = !1, typeof addEventListener < "u" && addEventListener("online", this.onOnline), this.schedule(this.config.delivery.retryIdleDelayMs);
721
+ }
722
+ nudge() {
723
+ this.draining || this.schedule(this.config.delivery.retryBaseDelayMs);
724
+ }
725
+ async storeFailed(e, t) {
726
+ let n = this.toStorable(e, t);
727
+ for (let e of n) await this.bounded(this.storage.save(e));
728
+ n.length > 0 && this.nudge();
729
+ }
730
+ stop() {
731
+ this.stopped = !0, typeof removeEventListener < "u" && removeEventListener("online", this.onOnline), this.clearTimer();
732
+ }
733
+ async flush() {
734
+ this.stopped = !1;
735
+ try {
736
+ await this.drain();
737
+ } finally {
738
+ this.stop();
739
+ }
740
+ }
741
+ schedule(e) {
742
+ if (this.stopped) return;
743
+ this.clearTimer();
744
+ let t = setTimeout(() => {
745
+ this.drain();
746
+ }, e);
747
+ this.timer = t, T(t);
748
+ }
749
+ bounded(e) {
750
+ return w(e, _);
751
+ }
752
+ clearTimer() {
753
+ this.timer !== null && (clearTimeout(this.timer), this.timer = null);
754
+ }
755
+ backoffMs() {
756
+ let e = Math.min(this.config.delivery.retryBaseDelayMs * 2 ** this.attempt, this.config.delivery.retryMaxDelayMs);
757
+ return Math.random() * e;
758
+ }
759
+ async drain() {
760
+ if (!this.draining) {
761
+ this.draining = !0;
762
+ try {
763
+ let e = ye(t, this.config.persistence.namespace);
764
+ await _e(e, this.diagnostics, () => this.drainLocked()) !== !0 && this.schedule(this.config.delivery.retryIdleDelayMs);
765
+ } finally {
766
+ this.draining = !1;
767
+ }
768
+ }
769
+ }
770
+ async drainLocked() {
771
+ try {
772
+ if (!k()) return this.schedule(this.config.delivery.retryIdleDelayMs), !0;
773
+ let e = await this.bounded(this.storage.take(this.batchesPerDrain ?? this.config.delivery.batchesPerDrain));
774
+ if (e.length === 0) return this.schedule(this.config.delivery.retryIdleDelayMs), !0;
775
+ let t = /* @__PURE__ */ new Set(), n = 0;
776
+ for (let r of e) {
777
+ if (this.stopped) return !0;
778
+ let e = G(r, this.config);
779
+ if (t.has(e)) continue;
780
+ let i = await this.deliver(r);
781
+ if (i === "stop") return !0;
782
+ i !== "next" && (t.add(e), n = Math.max(n, i));
783
+ }
784
+ return t.size > 0 ? this.schedule(n) : this.schedule(e.length === (this.batchesPerDrain ?? this.config.delivery.batchesPerDrain) ? 0 : this.config.delivery.retryIdleDelayMs), !0;
785
+ } catch (e) {
786
+ return this.diagnostics.report("storage.degraded", "The drain loop threw an error.", void 0, e), this.attempt++, this.schedule(this.backoffMs()), !0;
787
+ }
788
+ }
789
+ async deliver(e) {
790
+ if (e.attempts >= this.maxAttempts) return this.diagnostics.report("storage.dead_lettered", `The library dropped a batch after ${e.attempts.toString()} attempts.`, {
791
+ batchId: e.id,
792
+ records: e.records.length
793
+ }), this.onLoss("max_attempts", e.records, [e.id]), await this.bounded(this.storage.remove(e.id)), "next";
794
+ try {
795
+ return await this.transport.send(e), await this.bounded(this.storage.remove(e.id)), this.attempt = 0, "next";
796
+ } catch (t) {
797
+ if (this.stopped) return "stop";
798
+ let n = t instanceof M ? t : void 0;
799
+ return n?.kind === "permanent" || n?.kind === "too_large" ? (await this.replaceStored(e, t), "next") : n?.kind === "offline" ? (this.schedule(this.config.delivery.retryIdleDelayMs), "stop") : n?.kind === "throttled" ? (this.attempt++, n.retryAfterMs ?? this.backoffMs()) : (await this.bounded(this.storage.bumpAttempts(e.id, e.attempts + 1)), this.attempt++, n?.retryAfterMs ?? this.backoffMs());
800
+ }
801
+ }
802
+ async replaceStored(e, t) {
803
+ let n = this.toStorable(e, t);
804
+ await this.bounded(this.storage.remove(e.id));
805
+ for (let e of n) await this.bounded(this.storage.save(e));
806
+ }
807
+ toStorable(e, t) {
808
+ let n = t instanceof M ? t : void 0;
809
+ if (n?.kind === "permanent") return this.onLoss("rejected", e.records, [e.id]), [];
810
+ if (n?.kind !== "too_large") return [e];
811
+ let r = fe(e, this.diagnostics);
812
+ return r ? (this.diagnostics.report("transport.batch_split", "The server refused the batch because it is too large.", {
813
+ from: e.records.length,
814
+ to: r[0].records.length,
815
+ serverMaxBytes: n.maxBytes
816
+ }), r) : (this.diagnostics.report("transport.dropped_permanent", "One record is larger than the server limit. The library dropped it.", {
817
+ batchId: e.id,
818
+ bodies: e.records.map((e) => e.body)
819
+ }), this.onLoss("rejected", e.records, [e.id]), []);
820
+ }
821
+ }, Z = [
822
+ "metrics",
823
+ "logs",
824
+ "traces"
825
+ ];
826
+ function Q(e) {
827
+ let t = e.attributes[y];
828
+ return t === "metric" ? "metrics" : t === "span" ? "traces" : "logs";
829
+ }
830
+ var $ = class {
831
+ options;
832
+ onFlush;
833
+ pending = [];
834
+ timer = null;
835
+ constructor(e, t) {
836
+ this.options = e, this.onFlush = t;
837
+ }
838
+ add(e) {
839
+ this.pending.push(e), this.pending.length >= this.options.batchSize ? this.onFlush(this) : this.arm();
840
+ }
841
+ take() {
842
+ this.clearTimer();
843
+ let e = this.pending;
844
+ return this.pending = [], e;
845
+ }
846
+ get size() {
847
+ return this.pending.length;
848
+ }
849
+ stop() {
850
+ this.clearTimer();
851
+ }
852
+ arm() {
853
+ if (this.timer !== null) return;
854
+ let e = setTimeout(() => {
855
+ this.timer = null, this.onFlush(this);
856
+ }, this.options.flushIntervalMs);
857
+ this.timer = e, T(e);
858
+ }
859
+ clearTimer() {
860
+ this.timer !== null && (clearTimeout(this.timer), this.timer = null);
861
+ }
862
+ }, $e = class {
863
+ transport;
864
+ storage;
865
+ retry;
866
+ diagnostics;
867
+ routingContextId;
868
+ policy;
869
+ onLoss;
870
+ streams;
871
+ unconfirmed = /* @__PURE__ */ new Map();
872
+ queue = [];
873
+ open = /* @__PURE__ */ new Set();
874
+ ceiling = new se();
875
+ inFlight = 0;
876
+ pumpScheduled = !1;
877
+ stopped = !1;
878
+ held = /* @__PURE__ */ new Map();
879
+ heldBytes = 0;
880
+ persisting = !1;
881
+ release(e) {
882
+ for (let t of e) this.heldBytes -= this.held.get(t) ?? 0, this.held.delete(t);
883
+ }
884
+ evict() {
885
+ let e = this.queue.shift();
886
+ if (e) return this.open.delete(e), this.unconfirmed.delete(e.batch.id), this.release(e.batch.records), e.resolve(), this.onLoss("buffer_full", e.batch.records, [e.batch.id]), !0;
887
+ for (let e of Object.values(this.streams)) {
888
+ let t = e.take();
889
+ if (t.length) return this.release(t), this.onLoss("buffer_full", t), !0;
890
+ }
891
+ return !1;
892
+ }
893
+ admit(e) {
894
+ if (!this.policy) return !0;
895
+ let { maxPendingRecords: t, maxPendingBytes: n, overflow: r } = this.policy.buffering, i;
896
+ try {
897
+ i = E(JSON.stringify(e));
898
+ } catch {
899
+ return this.onLoss("unserializable", [e]), !1;
900
+ }
901
+ let a = () => !!(t && this.held.size >= t || n && this.heldBytes + i > n), o = !n || i <= n;
902
+ for (; o && a() && r === "drop-oldest" && this.evict(););
903
+ if (a()) {
904
+ if (r === "persist" && !this.persisting) {
905
+ this.persisting = !0;
906
+ let t = this.toBatch([e]);
907
+ this.diagnostics.guardAsync("storage.degraded", "Could not persist an overflow record.", async () => {
908
+ try {
909
+ await this.storage.save(t), this.retry.nudge();
910
+ } finally {
911
+ this.persisting = !1;
912
+ }
913
+ });
914
+ } else this.onLoss("buffer_full", [e]);
915
+ return !1;
916
+ }
917
+ return this.held.set(e, i), this.heldBytes += i, !0;
918
+ }
919
+ constructor(e, t, n, r, i, a, o, s = () => void 0) {
920
+ this.transport = e, this.storage = t, this.retry = n, this.diagnostics = r, this.routingContextId = a, this.policy = o, this.onLoss = s;
921
+ let c = (e) => {
922
+ this.claim(e);
923
+ };
924
+ this.streams = {
925
+ logs: new $(i.logs, c),
926
+ metrics: new $(i.metrics, c),
927
+ traces: new $(i.traces, c)
928
+ };
929
+ }
930
+ push(e) {
931
+ if (this.stopped) return;
932
+ e.observedTimeUnixNano = oe();
933
+ let t = { ...e };
934
+ this.admit(t) && this.route(t);
935
+ }
936
+ drainForExit() {
937
+ let e = [...this.unconfirmed.values()].flatMap((e) => e.records);
938
+ return this.unconfirmed.clear(), this.held.clear(), this.heldBytes = 0, Z.map((t) => this.toBatch([...e.filter((e) => Q(e) === t), ...this.streams[t].take()])).filter((e) => e !== null);
939
+ }
940
+ async flushPending() {
941
+ if (!this.stopped) for (let e of Z) this.claim(this.streams[e]);
942
+ await Promise.all([...this.open].map((e) => e.done));
943
+ }
944
+ counts() {
945
+ let e = (e) => {
946
+ let t = 0;
947
+ for (let n of e) t += n.batch.records.length;
948
+ return t;
949
+ }, t = e(this.queue);
950
+ return {
951
+ pending: Z.reduce((e, t) => e + this.streams[t].size, 0) + t,
952
+ inFlight: e(this.open) - t
953
+ };
954
+ }
955
+ destroy() {
956
+ this.stopped = !0, this.held.clear(), this.heldBytes = 0;
957
+ for (let e of Z) this.streams[e].stop();
958
+ this.queue.length = 0;
959
+ for (let e of this.open) e.resolve();
960
+ this.open.clear();
961
+ }
962
+ toBatch(e) {
963
+ return e.length === 0 ? null : {
964
+ id: ue(this.diagnostics),
965
+ records: e,
966
+ createdAt: Date.now(),
967
+ attempts: 0,
968
+ routingContextId: this.routingContextId?.(),
969
+ persistenceNamespace: this.policy?.persistence.namespace
970
+ };
971
+ }
972
+ route(e) {
973
+ try {
974
+ let t = this.streams[Q(e)];
975
+ if (!this.ceiling.allow(e, this.policy?.capturePolicy.errorCeiling, this.policy?.capturePolicy.windowMs)) {
976
+ this.release([e]), this.diagnostics.count("record.dropped_by_ceiling");
977
+ return;
978
+ }
979
+ t.add(e);
980
+ } catch (e) {
981
+ this.diagnostics.report("pipeline.crashed", "The pipeline dropped a record before it reached a stream.", void 0, e);
982
+ }
983
+ }
984
+ claim(e) {
985
+ this.enqueue(this.toBatch(e.take()));
986
+ }
987
+ enqueue(e) {
988
+ if (!e) return;
989
+ let t, n = {
990
+ batch: e,
991
+ done: new Promise((e) => {
992
+ t = e;
993
+ }),
994
+ resolve: t
995
+ };
996
+ this.unconfirmed.set(e.id, e), this.open.add(n), this.queue.push(n), !this.pumpScheduled && (this.pumpScheduled = !0, queueMicrotask(() => {
997
+ this.pumpScheduled = !1, this.pump();
998
+ }));
999
+ }
1000
+ pump() {
1001
+ if (!this.stopped) for (; this.inFlight < (this.policy?.delivery.maxConcurrentRequests ?? 2);) {
1002
+ let e = this.queue.shift();
1003
+ if (!e) return;
1004
+ this.inFlight++, this.run(e);
1005
+ }
1006
+ }
1007
+ async run(e) {
1008
+ let { batch: t } = e;
1009
+ try {
1010
+ await this.dispatch(t);
1011
+ } catch (e) {
1012
+ this.diagnostics.report("pipeline.crashed", "A dispatch rejected its promise. This must not occur.", { batchId: t.id }, e);
1013
+ } finally {
1014
+ this.release(t.records), this.inFlight--, this.open.delete(e), e.resolve(), this.pump();
1015
+ }
1016
+ }
1017
+ async dispatch(e) {
1018
+ try {
1019
+ let t = this.transport.throttledForMs(e);
1020
+ if (t > 0) {
1021
+ await this.store(e), this.diagnostics.report("transport.throttled", "The server throttles requests. The pipeline stored the batch and did not send it.", {
1022
+ batchId: e.id,
1023
+ records: e.records.length,
1024
+ throttledMs: t
1025
+ });
1026
+ return;
1027
+ }
1028
+ try {
1029
+ await this.transport.send(e, () => {
1030
+ this.unconfirmed.delete(e.id);
1031
+ });
1032
+ } catch (t) {
1033
+ if (this.stopped) return;
1034
+ await this.storeFailed({
1035
+ ...e,
1036
+ attempts: e.attempts + 1
1037
+ }, t), this.diagnostics.report("transport.http_error", "The send failed. The pipeline stored the batch for a retry.", {
1038
+ batchId: e.id,
1039
+ records: e.records.length
1040
+ }, t);
1041
+ }
1042
+ } finally {
1043
+ this.unconfirmed.delete(e.id);
1044
+ }
1045
+ }
1046
+ async storeFailed(e, t) {
1047
+ await this.diagnostics.guardAsync("storage.degraded", "The library could not store an undelivered batch.", async () => {
1048
+ await this.retry.storeFailed(e, t);
1049
+ });
1050
+ }
1051
+ async store(e) {
1052
+ await this.diagnostics.guardAsync("storage.degraded", "The library could not store an undelivered batch.", async () => {
1053
+ await w(this.storage.save(e), _), this.retry.nudge();
1054
+ });
1055
+ }
1056
+ }, et = class e {
1057
+ storage;
1058
+ retry;
1059
+ pipeline;
1060
+ exitFlush;
1061
+ diagnostics;
1062
+ transport;
1063
+ retired = !1;
1064
+ emergencyImport = Promise.resolve();
1065
+ constructor(e, t, n, r, i, a) {
1066
+ this.storage = e, this.retry = t, this.pipeline = n, this.exitFlush = r, this.diagnostics = i, this.transport = a;
1067
+ }
1068
+ static async create(t) {
1069
+ let { config: n, diagnostics: r } = t, i = t.storage ?? await be(n.storage, xe(n.persistence.namespace), A(n.retention), r, t.onGap, n.persistence.namespace);
1070
+ t.onStorage?.(i);
1071
+ let a = new Se(i, n.securityContext, n.persistence.namespace), o = (e) => {
1072
+ let i = me(e, r);
1073
+ i && he(t.remote, n, i);
1074
+ }, { onLoss: s } = t, c = n.transport ? void 0 : new Ze(n, r, o, s), l = c ?? new qe(n, r, o, s), u = new Qe(a, l, r, n, void 0, s), { batching: d } = n, f = new $e(l, a, u, r, {
1075
+ logs: {
1076
+ flushIntervalMs: d.logFlushIntervalMs,
1077
+ batchSize: d.logBatchSize
1078
+ },
1079
+ metrics: {
1080
+ flushIntervalMs: d.metricFlushIntervalMs,
1081
+ batchSize: d.metricBatchSize
1082
+ },
1083
+ traces: {
1084
+ flushIntervalMs: d.logFlushIntervalMs,
1085
+ batchSize: d.logBatchSize
1086
+ }
1087
+ }, () => n.securityContext.id || void 0, n, s), p = new Je({
1088
+ config: n,
1089
+ diagnostics: r,
1090
+ drainForExit: () => f.drainForExit(),
1091
+ flushAlive: () => f.flushPending(),
1092
+ exitHeaders: () => c?.lastHeaders(),
1093
+ onLoss: s,
1094
+ beforeExit: t.beforeExit
1095
+ }), m = !1;
1096
+ try {
1097
+ p.install(), u.start(), m = !0;
1098
+ } finally {
1099
+ m || (p.uninstall(), u.stop());
1100
+ }
1101
+ let h = new e(a, u, f, p, r, l);
1102
+ return h.emergencyImport = h.importEmergency(), h;
1103
+ }
1104
+ push(e) {
1105
+ this.pipeline.push(e);
1106
+ }
1107
+ async flush() {
1108
+ await this.pipeline.flushPending();
1109
+ }
1110
+ async clearPersisted(e, t) {
1111
+ e === "namespace" ? await this.storage.clearNamespace() : await this.storage.clear(), j(t, this.diagnostics, this.storage.namespace, e === "namespace");
1112
+ }
1113
+ async queueDepth() {
1114
+ return await this.diagnostics.guardAsync("storage.degraded", "The library could not count the batches in storage.", () => this.storage.count()) ?? 0;
1115
+ }
1116
+ stats() {
1117
+ return {
1118
+ ...this.pipeline.counts(),
1119
+ lastSendAt: this.transport.lastSendAt()
1120
+ };
1121
+ }
1122
+ async destroy() {
1123
+ await this.diagnostics.guardAsync("pipeline.crashed", "The flush before shutdown failed.", () => this.flush()), this.exitFlush.flush("shutdown"), this.exitFlush.uninstall(), this.retry.stop(), this.pipeline.destroy(), await this.storage.close();
1124
+ }
1125
+ async retire(e, t, n) {
1126
+ if (this.retired = !0, this.exitFlush.uninstall(), this.retry.stop(), e === "flush") try {
1127
+ await w(Promise.all([this.flush(), this.retry.flush()]), t);
1128
+ } catch (e) {
1129
+ this.diagnostics.report("transport.timeout", "The flush of the old security context did not complete before its time limit.", void 0, e);
1130
+ }
1131
+ this.retry.stop(), this.transport.stop();
1132
+ let r = this.pipeline.drainForExit(), i = r.flatMap((e) => e.records);
1133
+ this.pipeline.destroy(), await this.emergencyImport;
1134
+ let a = await this.storage.take(2 ** 53 - 1);
1135
+ if (e === "retain") for (let e of r) await this.storage.save({
1136
+ ...e,
1137
+ routingContextId: n,
1138
+ persistenceNamespace: this.storage.namespace
1139
+ });
1140
+ let o = e === "discard" || e === "flush";
1141
+ return await this.storage.retire(o), o && j(n, this.diagnostics, this.storage.namespace), {
1142
+ buffered: i,
1143
+ stored: a
1144
+ };
1145
+ }
1146
+ async adopt(e, t) {
1147
+ for (let n of e) await this.storage.save({
1148
+ ...n,
1149
+ attempts: 0,
1150
+ routingContextId: t,
1151
+ persistenceNamespace: this.storage.namespace
1152
+ });
1153
+ e.length > 0 && this.retry.nudge();
1154
+ }
1155
+ async importEmergency() {
1156
+ let e = await this.diagnostics.guardAsync("storage.degraded", "The emergency import failed.", () => ge(this.storage, this.diagnostics, (e) => this.storage.accepts(e), this.storage.namespace));
1157
+ !this.retired && e !== void 0 && e > 0 && this.retry.nudge();
1158
+ }
1159
+ };
1160
+ //#endregion
1161
+ export { et as Sender };
1162
+
1163
+ //# sourceMappingURL=sender-DUUmSglb.js.map