@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
@@ -1,1351 +0,0 @@
1
- import { $ as e, $t as t, B as n, Dt as r, Et as i, Gt as a, Kt as o, M as s, Mt as c, N as l, P as u, Q as d, Qt as f, Rt as p, St as m, U as h, V as ee, Vt as te, W as g, X as ne, Z as re, _n as _, bn as v, d as ie, dn as y, en as b, et as ae, gn as oe, hn as se, in as x, jt as ce, ln as S, mn as le, n as C, tt as ue, un as de, wt as fe, x as w, xn as T, zt as pe } from "./constants-L_d5YvvQ.js";
2
- import { c as E, l as D, s as me, t as he } from "./sampling-nYIWP_vP.js";
3
- import { r as ge } from "./log-record-cwFiLyPY.js";
4
- import { o as _e, t as O } from "./sanitize-BiUKCUG6.js";
5
- import { i as ve } from "./identity-Dtv917nz.js";
6
- import { i as ye, r as k } from "./library-request-ChCar5F8.js";
7
- import { t as A } from "./openfin-BwEN0uj4.js";
8
- import { r as j } from "./platform-4yYjMD-B.js";
9
- import { a as be, i as M, n as xe, r as Se, t as Ce } from "./batch-D-4xl7op.js";
10
- import { n as we, t as Te } from "./remote-rules-1KqfVBIu.js";
11
- //#region src/models/storage.ts
12
- function Ee(e) {
13
- let { maxBatches: t } = e;
14
- return e.mode === "until-delivered" ? {
15
- maxBatches: t,
16
- maxAgeMs: Infinity,
17
- maxAttempts: Infinity
18
- } : {
19
- maxBatches: t,
20
- maxAgeMs: e.maxAgeMs,
21
- maxAttempts: e.maxAttempts
22
- };
23
- }
24
- //#endregion
25
- //#region src/utils/lock.ts
26
- function De(e, t, n, { skipIfBusy: r = !0 } = {}) {
27
- let i = globalThis.navigator?.locks;
28
- return i?.request ? t.guardAsync("storage.degraded", `The library could not get the lock ${e}.`, () => i.request(e, { ifAvailable: r }, (e) => e ? n() : Promise.resolve(void 0))) : n();
29
- }
30
- //#endregion
31
- //#region src/storage/keys.ts
32
- function Oe(e, t) {
33
- return `${e}${String(t.createdAt).padStart(14, "0")}.${t.id}`;
34
- }
35
- function N(e, t) {
36
- let n = [];
37
- return t.guard("storage.unavailable", "The library could not list the localStorage keys.", () => {
38
- for (let t = 0; t < localStorage.length; t++) {
39
- let r = String(localStorage.key(t));
40
- r.startsWith(e) && n.push(r);
41
- }
42
- }), n.sort();
43
- }
44
- //#endregion
45
- //#region src/storage/emergency-queue.ts
46
- function ke(e, t) {
47
- let n = t.guard("storage.degraded", "An emergency entry is not valid JSON. The library dropped it.", () => JSON.parse(e));
48
- return n === void 0 ? null : M(n) ? n : (t.report("storage.degraded", "An emergency entry is not a batch. The library dropped it."), null);
49
- }
50
- async function Ae(e, t) {
51
- let n = 0;
52
- for (let r of N(h, t)) {
53
- let i = t.guard("storage.unavailable", "The library could not read the emergency queue.", () => {
54
- let e = localStorage.getItem(r);
55
- return localStorage.removeItem(r), e;
56
- });
57
- if (!i) continue;
58
- let a = ke(i, t);
59
- if (a !== null) try {
60
- await E(e.save(a), y), n++;
61
- } catch (e) {
62
- t.report("storage.degraded", "The library could not recover an emergency batch.", { key: r }, e);
63
- }
64
- }
65
- return n;
66
- }
67
- function je(e, t, n) {
68
- let r = `writing the emergency queue, ${String(e.records.length)} records`, i = [], a = t.guard("storage.quota_exceeded", r, () => {
69
- let n = N(h, t);
70
- for (let e of n.slice(0, Math.max(0, n.length - 20 + 1))) {
71
- let n = ke(String(localStorage.getItem(e)), t);
72
- n !== null && i.push(n), localStorage.removeItem(e);
73
- }
74
- return localStorage.setItem(Oe(h, e), JSON.stringify(e)), !0;
75
- });
76
- t.guard("pipeline.crashed", "The loss report of the emergency queue failed.", () => {
77
- if (i.length > 0) {
78
- let e = i.flatMap((e) => e.records);
79
- n?.("exit_queue_full", e, i.map((e) => e.id));
80
- }
81
- a !== !0 && n?.("quota", e.records, [e.id]);
82
- });
83
- }
84
- async function Me(e, t) {
85
- return await De("ui-observability.emergency", t, () => Ae(e, t), { skipIfBusy: !1 }) ?? 0;
86
- }
87
- //#endregion
88
- //#region src/storage/eviction.ts
89
- function P(e) {
90
- return Array.isArray(e.records) && e.records.some(Se);
91
- }
92
- var Ne = class {
93
- howMany;
94
- isAudit;
95
- plain = [];
96
- audit = [];
97
- constructor(e, t) {
98
- this.howMany = e, this.isAudit = t;
99
- }
100
- add(e) {
101
- return this.isAudit(e) ? this.audit.length < this.howMany && this.audit.push(e) : this.plain.push(e), this.plain.length < this.howMany;
102
- }
103
- picked() {
104
- return [...this.plain, ...this.audit].slice(0, this.howMany);
105
- }
106
- };
107
- function F(e, t, n) {
108
- let r = new Ne(t, n);
109
- for (let t of e) if (!r.add(t)) break;
110
- return r.picked();
111
- }
112
- //#endregion
113
- //#region src/storage/gap.ts
114
- function I(e, t, n, r) {
115
- return e.batches += n, e.records += r, e.reason = t, {
116
- batches: n,
117
- records: r,
118
- reason: t
119
- };
120
- }
121
- function L(e, t, n, r) {
122
- n.batches !== 0 && (e.report("storage.evicted", `The library dropped ${String(n.batches)} stored batches (${String(n.records)} records).`, { ...n }), t?.(n, r));
123
- }
124
- //#endregion
125
- //#region src/storage/local-storage.ts
126
- function Pe(e) {
127
- let t = w.length, n = Number(e.slice(t, t + 14));
128
- return Number.isFinite(n) ? n : null;
129
- }
130
- var Fe = class {
131
- limits;
132
- diagnostics;
133
- onGap;
134
- name = le;
135
- constructor(e, t, n) {
136
- this.limits = e, this.diagnostics = t, this.onGap = n;
137
- }
138
- async save(e) {
139
- if (this.write(e, "localStorage refused a batch, evicting the oldest")) await this.prune();
140
- else if (this.evictForQuota(), !this.write(e, "localStorage refused a batch even after evicting")) {
141
- let t = {
142
- batches: 1,
143
- records: e.records.length,
144
- reason: "quota"
145
- };
146
- L(this.diagnostics, this.onGap, t, [e]);
147
- }
148
- }
149
- take(e) {
150
- let t = [];
151
- for (let n of this.keys().slice(0, e)) {
152
- let e = this.read(n);
153
- e === null ? this.removeKey(n) : t.push(e);
154
- }
155
- return Promise.resolve(t);
156
- }
157
- remove(e) {
158
- let t = this.keyOf(e);
159
- return t !== null && this.removeKey(t), Promise.resolve();
160
- }
161
- bumpAttempts(e, t) {
162
- let n = this.keyOf(e);
163
- if (n === null) return Promise.resolve();
164
- let r = this.read(n);
165
- return r === null || (r.attempts = t, this.diagnostics.guard("storage.degraded", "The library could not update the attempt count.", () => {
166
- localStorage.setItem(n, JSON.stringify(r));
167
- })), Promise.resolve();
168
- }
169
- prune() {
170
- let e = Date.now() - this.limits.maxAgeMs, t = {
171
- batches: 0,
172
- records: 0,
173
- reason: "expired"
174
- }, n = [], r = [];
175
- for (let t of this.keys()) {
176
- let i = Pe(t);
177
- i === null || i < e ? n.push(t) : r.push(t);
178
- }
179
- this.drop(t, "expired", [...this.entries(n)]);
180
- let i = r.length - this.limits.maxBatches;
181
- return i > 0 && this.drop(t, "over_capacity", this.evictable(r, i)), Promise.resolve(t);
182
- }
183
- count() {
184
- return Promise.resolve(this.keys().length);
185
- }
186
- clear() {
187
- return this.evict(this.keys()), Promise.resolve();
188
- }
189
- close() {
190
- return Promise.resolve();
191
- }
192
- write(e, t) {
193
- return this.diagnostics.guard("storage.quota_exceeded", t, () => (localStorage.setItem(Oe(w, e), JSON.stringify(e)), !0)) === !0;
194
- }
195
- evictForQuota() {
196
- let e = this.keys(), t = {
197
- batches: 0,
198
- records: 0,
199
- reason: "quota"
200
- }, n = Math.ceil(e.length / 4);
201
- this.drop(t, "quota", this.evictable(e, n));
202
- }
203
- evictable(e, t) {
204
- return F(this.entries(e), t, (e) => e.batch !== null && P(e.batch));
205
- }
206
- *entries(e) {
207
- for (let t of e) yield {
208
- key: t,
209
- batch: this.read(t)
210
- };
211
- }
212
- drop(e, t, n) {
213
- if (n.length === 0) return;
214
- let r = [];
215
- for (let { key: e, batch: t } of n) this.removeKey(e), t && r.push(t);
216
- let i = I(e, t, n.length, xe(r));
217
- L(this.diagnostics, this.onGap, i, r);
218
- }
219
- read(e) {
220
- let t = this.diagnostics.guard("storage.unavailable", "The library could not read a stored batch.", () => localStorage.getItem(e));
221
- if (!t) return null;
222
- let n = this.diagnostics.guard("storage.degraded", "A stored batch is corrupt. The library removed it.", () => JSON.parse(t));
223
- return n === void 0 ? null : M(n) ? n : (this.diagnostics.report("storage.degraded", "A stored entry is not a batch. The library dropped it."), null);
224
- }
225
- keyOf(e) {
226
- return this.keys().find((t) => t.endsWith(`.${e}`)) ?? null;
227
- }
228
- evict(e) {
229
- for (let t of e) this.removeKey(t);
230
- }
231
- removeKey(e) {
232
- this.diagnostics.guard("storage.degraded", "The library could not remove a stored batch.", () => {
233
- localStorage.removeItem(e);
234
- });
235
- }
236
- keys() {
237
- return N(w, this.diagnostics);
238
- }
239
- }, R = class {
240
- limits;
241
- onGap;
242
- name = se;
243
- batches = [];
244
- constructor(e, t) {
245
- this.limits = e, this.onGap = t;
246
- }
247
- async save(e) {
248
- this.batches.push(e), await this.prune();
249
- }
250
- take(e) {
251
- return Promise.resolve(this.batches.slice(0, e));
252
- }
253
- remove(e) {
254
- return this.batches = this.batches.filter((t) => t.id !== e), Promise.resolve();
255
- }
256
- bumpAttempts(e, t) {
257
- let n = this.batches.find((t) => t.id === e);
258
- return n && (n.attempts = t), Promise.resolve();
259
- }
260
- prune() {
261
- let e = Date.now() - this.limits.maxAgeMs, t = {
262
- batches: 0,
263
- records: 0,
264
- reason: "expired"
265
- }, n = this.batches.filter((t) => t.createdAt < e);
266
- this.batches = this.batches.filter((t) => t.createdAt >= e), this.report(t, "expired", n);
267
- let r = this.batches.length - this.limits.maxBatches;
268
- if (r > 0) {
269
- let e = F(this.batches, r, P);
270
- this.batches = this.batches.filter((t) => !e.includes(t)), this.report(t, "over_capacity", e);
271
- }
272
- return Promise.resolve(t);
273
- }
274
- report(e, t, n) {
275
- if (n.length === 0) return;
276
- let r = I(e, t, n.length, xe(n));
277
- this.onGap?.(r, n);
278
- }
279
- count() {
280
- return Promise.resolve(this.batches.length);
281
- }
282
- clear() {
283
- return this.batches = [], Promise.resolve();
284
- }
285
- close() {
286
- return this.batches = [], Promise.resolve();
287
- }
288
- }, Ie = (e) => ({
289
- name: oe,
290
- save: (t) => (e?.({
291
- batches: 1,
292
- records: t.records.length,
293
- reason: "not_stored"
294
- }, [t]), Promise.resolve()),
295
- take: () => Promise.resolve([]),
296
- remove: () => Promise.resolve(),
297
- bumpAttempts: () => Promise.resolve(),
298
- prune: () => Promise.resolve({
299
- batches: 0,
300
- records: 0,
301
- reason: "expired"
302
- }),
303
- count: () => Promise.resolve(0),
304
- clear: () => Promise.resolve(),
305
- close: () => Promise.resolve()
306
- });
307
- function Le(e) {
308
- return e.guard("storage.unavailable", "The library could not read indexedDB.", () => globalThis.indexedDB != null) === !0;
309
- }
310
- function Re(e) {
311
- return e.guard("storage.unavailable", "The library could not write a test key to localStorage.", () => (localStorage.setItem(_, "1"), localStorage.removeItem(_), !0)) === !0;
312
- }
313
- async function ze(e, t, n, r, i) {
314
- if (e === "none") return Ie(i);
315
- if (e === "memory") return new R(n, i);
316
- if (e === "indexeddb" || e === "auto") {
317
- if (Le(r)) {
318
- let e = await r.guardAsync("storage.unavailable", "The library could not open IndexedDB.", async () => {
319
- let { IndexedDbStorage: e } = await import("./indexeddb-storage-HgkQUTyS.js"), a = new e(t, n, r, i);
320
- return await a.open(), a;
321
- });
322
- if (e) return e;
323
- }
324
- if (e === "indexeddb") return r.report("storage.degraded", "IndexedDB is not available. The library uses memory storage instead."), new R(n, i);
325
- }
326
- return Re(r) ? new Fe(n, r, i) : (r.report("storage.degraded", "No persistent storage is available. The library keeps undelivered records in memory only."), new R(n, i));
327
- }
328
- //#endregion
329
- //#region src/transport/errors.ts
330
- var z = class extends Error {
331
- kind;
332
- status;
333
- retryAfterMs;
334
- maxBytes;
335
- constructor(e, t, n, r, i, a) {
336
- super(t, a === void 0 ? void 0 : { cause: a }), this.kind = e, this.status = n, this.retryAfterMs = r, this.maxBytes = i, this.name = "TransportError";
337
- }
338
- };
339
- function Be(e) {
340
- if (!e) return;
341
- let t = Number(e);
342
- if (Number.isFinite(t)) return Math.max(0, t * fe);
343
- let n = Date.parse(e);
344
- return Number.isNaN(n) ? void 0 : Math.max(0, n - Date.now());
345
- }
346
- //#endregion
347
- //#region src/transport/serializers/ecs.ts
348
- var B = {
349
- "error.type": "type",
350
- "error.message": "message",
351
- "error.stack_trace": "stack_trace"
352
- }, Ve = /* @__PURE__ */ new Set([C, ...Object.keys(B)]);
353
- function He(e) {
354
- return i.test(e) ? Number(BigInt(e) / r) : Date.now();
355
- }
356
- function Ue(e) {
357
- return typeof e == "string" || typeof e == "number" || typeof e == "boolean" ? e : typeof e == "bigint" ? e.toString() : null;
358
- }
359
- function We(e, t, n) {
360
- let r = e[t];
361
- r === void 0 ? e[t] = n : Array.isArray(r) ? r.push(n) : e[t] = [r, n];
362
- }
363
- function V(e, t, n, r) {
364
- let i = Ue(n);
365
- if (i !== null) We(e, t, i);
366
- else if (typeof n == "object" && n && !r.has(n)) {
367
- if (r.add(n), Array.isArray(n)) for (let i of n) V(e, t, i, r);
368
- else for (let [i, a] of Object.entries(n)) V(e, `${t}.${i}`, a, r);
369
- r.delete(n);
370
- }
371
- }
372
- function H(e) {
373
- let t = Object.create(null), n = /* @__PURE__ */ new WeakSet();
374
- for (let [r, i] of Object.entries(e)) V(t, r, i, n);
375
- return t;
376
- }
377
- function Ge(e) {
378
- let t = [];
379
- for (let [n, r] of Object.entries(B)) {
380
- let i = e[n];
381
- typeof i == "string" && t.push([r, i]);
382
- }
383
- return t.length > 0 ? Object.fromEntries(t) : void 0;
384
- }
385
- function Ke(e) {
386
- return Object.fromEntries(Object.entries(e).filter(([e, t]) => !(Ve.has(e) && typeof t == "string")));
387
- }
388
- var qe = {
389
- name: "ecs",
390
- serialize(e) {
391
- return {
392
- body: `${e.map((e) => {
393
- let t = He(e.timeUnixNano), { attributes: r } = e, i = r[C];
394
- return JSON.stringify({
395
- "@timestamp": new Date(t).toISOString(),
396
- message: e.body,
397
- ecs: { version: ee },
398
- log: {
399
- level: n[e.severityText] ?? e.severityText.toLowerCase(),
400
- logger: typeof i == "string" ? i : void 0
401
- },
402
- event: { kind: r["log.type"] === "metric" ? "metric" : "event" },
403
- error: Ge(r),
404
- trace: { id: e.traceId },
405
- span: { id: e.spanId },
406
- service: {
407
- name: e.resource[a],
408
- version: e.resource[o],
409
- environment: e.resource[te]
410
- },
411
- user_agent: { original: e.resource[pe] },
412
- url: { full: r["url.full"] ?? r["page.url"] },
413
- labels: {
414
- ...H(e.resource),
415
- ...H(Ke(r))
416
- }
417
- });
418
- }).join("\n")}\n`,
419
- contentType: l
420
- };
421
- }
422
- };
423
- //#endregion
424
- //#region src/transport/serializers/otlp.ts
425
- function U(e) {
426
- 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(U).filter((e) => e !== null) } } : typeof e == "object" ? { kvlistValue: { values: W(e) } } : null;
427
- }
428
- function W(e) {
429
- let t = [];
430
- for (let [n, r] of Object.entries(e)) {
431
- let e = U(r);
432
- e && t.push({
433
- key: n,
434
- value: e
435
- });
436
- }
437
- return t;
438
- }
439
- var G = /* @__PURE__ */ new WeakMap();
440
- function Je(e) {
441
- let t = G.get(e);
442
- if (t !== void 0) return t;
443
- let n = JSON.stringify(e);
444
- return G.set(e, n), n;
445
- }
446
- function K(e) {
447
- let t = /* @__PURE__ */ new Map();
448
- for (let n of e) {
449
- let e = Je(n.resource), r = t.get(e);
450
- r ? r.records.push(n) : t.set(e, {
451
- resource: n.resource,
452
- records: [n]
453
- });
454
- }
455
- return [...t.values()];
456
- }
457
- var Ye = {
458
- name: x,
459
- serialize(e) {
460
- let t = K(e).map((e) => ({
461
- resource: { attributes: W(e.resource) },
462
- scopeLogs: [{
463
- scope: {
464
- name: v,
465
- version: T
466
- },
467
- logRecords: e.records.map((e) => ({
468
- timeUnixNano: e.timeUnixNano,
469
- observedTimeUnixNano: e.observedTimeUnixNano ?? e.timeUnixNano,
470
- severityNumber: e.severityNumber,
471
- severityText: e.severityText,
472
- body: { stringValue: e.body },
473
- traceId: e.traceId,
474
- spanId: e.spanId,
475
- flags: e.traceFlags,
476
- attributes: W(e.attributes)
477
- }))
478
- }]
479
- }));
480
- return {
481
- body: JSON.stringify({ resourceLogs: t }),
482
- contentType: s
483
- };
484
- }
485
- }, Xe = 1;
486
- function q(e, t) {
487
- let n = e.attributes[t];
488
- return typeof n == "number" && Number.isFinite(n) ? n : void 0;
489
- }
490
- function Ze(e) {
491
- return Array.isArray(e) && e.every((e) => typeof e == "number" && Number.isFinite(e));
492
- }
493
- function Qe(e) {
494
- let t = e.attributes[m.attributeKeys], n = {};
495
- if (!Array.isArray(t)) return n;
496
- for (let r of t) {
497
- let t = typeof r == "string" && !r.startsWith("metric.") ? e.attributes[r] : void 0;
498
- (typeof t == "string" || typeof t == "number" || typeof t == "boolean") && (n[r] = t);
499
- }
500
- return n;
501
- }
502
- function $e(e) {
503
- let t = q(e, m.count);
504
- if (t === void 0) {
505
- let t = q(e, "metric.value");
506
- return t === void 0 ? null : {
507
- count: "1",
508
- sum: t < 0 ? void 0 : t,
509
- min: t,
510
- max: t,
511
- bucketCounts: ["1"],
512
- explicitBounds: []
513
- };
514
- }
515
- let n = e.attributes[m.bounds], r = e.attributes[m.buckets];
516
- if (!Ze(n) || !Ze(r) || r.length !== n.length + 1) return null;
517
- let i = q(e, m.min);
518
- return {
519
- count: String(t),
520
- sum: i !== void 0 && i < 0 ? void 0 : q(e, m.sum),
521
- min: i,
522
- max: q(e, m.max),
523
- bucketCounts: r.map(String),
524
- explicitBounds: n
525
- };
526
- }
527
- function et(e) {
528
- let t = q(e, "metric.value");
529
- return t === void 0 ? null : { asDouble: t };
530
- }
531
- function tt(e, t) {
532
- let n = e.attributes[m.start], r = t === "histogram" ? $e(e) : et(e);
533
- return r === null ? null : {
534
- attributes: W(Qe(e)),
535
- startTimeUnixNano: typeof n == "string" ? n : e.timeUnixNano,
536
- timeUnixNano: e.timeUnixNano,
537
- ...r
538
- };
539
- }
540
- function nt({ name: e, unit: t, field: n, points: r }) {
541
- if (n === "gauge") return {
542
- name: e,
543
- unit: t,
544
- gauge: { dataPoints: r }
545
- };
546
- let i = {
547
- aggregationTemporality: Xe,
548
- dataPoints: r
549
- };
550
- return n === "sum" && (i.isMonotonic = r.every((e) => e.asDouble >= 0)), {
551
- name: e,
552
- unit: t,
553
- [n]: i
554
- };
555
- }
556
- var rt = {
557
- name: x,
558
- serialize(e) {
559
- let t = K(e).map((e) => {
560
- let t = /* @__PURE__ */ new Map();
561
- for (let n of e.records) {
562
- let e = n.attributes["metric.type"], r = e === "counter" ? "sum" : e === "histogram" ? "histogram" : "gauge", i = tt(n, r);
563
- if (i === null) continue;
564
- let a = n.attributes["metric.name"], o = n.attributes["metric.unit"], s = {
565
- name: typeof a == "string" ? a : n.body,
566
- unit: typeof o == "string" ? o : "",
567
- field: r,
568
- points: []
569
- }, c = JSON.stringify([
570
- s.field,
571
- s.name,
572
- s.unit
573
- ]), l = t.get(c) ?? s;
574
- l.points.push(i), t.set(c, l);
575
- }
576
- return {
577
- resource: { attributes: W(e.resource) },
578
- scopeMetrics: [{
579
- scope: {
580
- name: v,
581
- version: T
582
- },
583
- metrics: [...t.values()].map(nt)
584
- }]
585
- };
586
- });
587
- return {
588
- body: JSON.stringify({ resourceMetrics: t }),
589
- contentType: s
590
- };
591
- }
592
- }, it = {
593
- internal: 1,
594
- client: 3
595
- }, at = 2, ot = 0, st = /* @__PURE__ */ new Set([ie, ...Object.values(S)]);
596
- function J(e, t) {
597
- let n = e.attributes[t];
598
- return typeof n == "string" ? n : void 0;
599
- }
600
- function ct(e) {
601
- let t = Object.fromEntries(Object.entries(e.attributes).filter(([e]) => !st.has(e))), n = J(e, S.parentId);
602
- return {
603
- traceId: e.traceId,
604
- spanId: e.spanId,
605
- ...n ? { parentSpanId: n } : {},
606
- name: e.body,
607
- kind: J(e, S.kind) === "client" ? it.client : it.internal,
608
- startTimeUnixNano: J(e, S.start) ?? e.timeUnixNano,
609
- endTimeUnixNano: J(e, S.end) ?? e.timeUnixNano,
610
- attributes: W(t),
611
- status: { code: J(e, S.status) === "error" ? at : ot }
612
- };
613
- }
614
- var lt = {
615
- name: x,
616
- serialize(e) {
617
- let t = K(e).map((e) => ({
618
- resource: { attributes: W(e.resource) },
619
- scopeSpans: [{
620
- scope: {
621
- name: v,
622
- version: T
623
- },
624
- spans: e.records.map(ct)
625
- }]
626
- }));
627
- return {
628
- body: JSON.stringify({ resourceSpans: t }),
629
- contentType: s
630
- };
631
- }
632
- };
633
- //#endregion
634
- //#region src/transport/serializers/select.ts
635
- function ut(e) {
636
- return e === "otlp" ? Ye : e === "ecs" ? qe : e;
637
- }
638
- function Y(e, t) {
639
- let n = (t) => e.records.length > 0 && e.records.every((e) => Ce(e, "log.type") === t);
640
- return t.metricsEndpoint !== "" && n("metric") ? "metrics" : n("span") ? "traces" : "logs";
641
- }
642
- function X(e, t) {
643
- return e === "metrics" ? t.metricsEndpoint : e === "traces" ? t.tracesEndpoint : t.endpoint;
644
- }
645
- var dt = {
646
- metrics: rt,
647
- traces: lt
648
- };
649
- function Z(e, t, n, r) {
650
- try {
651
- return (dt[Y(t, e)] ?? ut(e.serializer)).serialize(t.records);
652
- } catch (e) {
653
- n.report("transport.serialize_failed", r, { batchId: t.id }, e);
654
- return;
655
- }
656
- }
657
- //#endregion
658
- //#region src/transport/status-transport.ts
659
- var ft = class {
660
- config;
661
- diagnostics;
662
- onSampling;
663
- onLoss;
664
- throttledUntil = {
665
- logs: 0,
666
- metrics: 0,
667
- traces: 0
668
- };
669
- acceptedAt = null;
670
- constructor(e, t, n = () => void 0, r = () => void 0) {
671
- this.config = e, this.diagnostics = t, this.onSampling = n, this.onLoss = r;
672
- }
673
- throttledForMs(e) {
674
- return Math.max(0, this.throttledUntil[Y(e, this.config)] - Date.now());
675
- }
676
- lastSendAt() {
677
- return this.acceptedAt;
678
- }
679
- serialize(e) {
680
- let t = Z(this.config, e, this.diagnostics, "The serializer threw an error. The library dropped the batch.");
681
- return t || this.onLoss("unserializable", e.records, [e.id]), t;
682
- }
683
- accept(e, t) {
684
- this.throttledUntil[e] = 0, this.acceptedAt = Date.now(), this.config.sampling.remote && t != null && this.onSampling(t);
685
- }
686
- reject(e, t, n, r) {
687
- let i = String(e);
688
- if (e === 401 || e === 403) {
689
- let t = r.advice === void 0 ? "" : ` ${r.advice}`;
690
- return this.diagnostics.report("transport.rejected_credentials", `The server returned ${i}.${t}`, {
691
- status: e,
692
- endpoint: X(n, this.config)
693
- }), new z("permanent", `The server rejected the batch with ${i}.`, e);
694
- }
695
- if (e === 413) return new z("too_large", "The payload is too large.", e, void 0, r.maxBytes);
696
- if (e === 429 || e === 503) {
697
- let t = e === 429, i = r.retryAfterMs === void 0 ? void 0 : Math.min(r.retryAfterMs, b);
698
- return this.throttledUntil[n] = Date.now() + (i ?? 2e3), t && this.diagnostics.report("transport.throttled", "The server throttles the requests.", { retryAfterMs: i }), new z("throttled", t ? "The server throttles the requests." : "The service is not available.", e, i);
699
- }
700
- return e === 408 || e >= 500 ? new z("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.`, {
701
- batchId: t,
702
- status: e
703
- }), new z("permanent", `The server rejected the batch with ${i}.`, e));
704
- }
705
- };
706
- //#endregion
707
- //#region src/transport/custom-transport.ts
708
- function pt(e, t, n) {
709
- return {
710
- kind: n,
711
- body: t.body,
712
- contentType: t.contentType,
713
- batchId: e.id,
714
- attempt: e.attempts,
715
- records: e.records
716
- };
717
- }
718
- function mt(e) {
719
- if (typeof e != "object" || !e) return !1;
720
- let t = Reflect.get(e, "status");
721
- return typeof t == "number" && Number.isInteger(t) && t >= 100 && t <= 599;
722
- }
723
- function ht(e) {
724
- return typeof e == "number" && Number.isFinite(e) ? Math.max(0, e) : void 0;
725
- }
726
- var gt = class extends ft {
727
- async send(e) {
728
- let { transport: t } = this.config;
729
- if (!t || e.records.length === 0) return;
730
- let n = this.serialize(e);
731
- if (!n) return;
732
- let r = Y(e, this.config), i = pt(e, n, r), a;
733
- try {
734
- a = await E(ye(() => k(() => _e(() => t(i)))), p);
735
- } catch (e) {
736
- let t = e instanceof me ? e : void 0;
737
- throw new z(t ? "timeout" : "transient", t?.message ?? "The custom transport failed.", void 0, void 0, void 0, e);
738
- }
739
- if (!mt(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 z("transient", "The custom transport returned no valid status.");
740
- let { status: o } = a;
741
- if (o >= 200 && o < 300) this.accept(r, a.sampling);
742
- else throw this.reject(o, e.id, r, {
743
- retryAfterMs: ht(a.retryAfterMs),
744
- maxBytes: ht(a.maxBytes)
745
- });
746
- }
747
- }, _t = class {
748
- deps;
749
- installed = !1;
750
- keepaliveBytes = 0;
751
- onVisibility = () => {
752
- document.visibilityState === "hidden" && this.flush("hidden");
753
- };
754
- onPageHide = () => {
755
- this.flush("pagehide");
756
- };
757
- onFreeze = () => {
758
- this.flush("freeze");
759
- };
760
- onOpenFinClose = () => {
761
- this.flush("openfin-close");
762
- };
763
- constructor(e) {
764
- this.deps = e;
765
- }
766
- install() {
767
- if (this.installed || typeof addEventListener > "u") return;
768
- this.installed = !0, typeof document < "u" && (document.addEventListener("visibilitychange", this.onVisibility), document.addEventListener("freeze", this.onFreeze)), addEventListener("pagehide", this.onPageHide);
769
- let e = A()?.me;
770
- this.deps.diagnostics.guard("openfin.unavailable", "The library could not subscribe to close-requested.", () => {
771
- e?.on?.("close-requested", this.onOpenFinClose);
772
- });
773
- }
774
- uninstall() {
775
- if (!this.installed) return;
776
- this.installed = !1, typeof document < "u" && (document.removeEventListener("visibilitychange", this.onVisibility), document.removeEventListener("freeze", this.onFreeze)), removeEventListener("pagehide", this.onPageHide);
777
- let e = A()?.me;
778
- this.deps.diagnostics.guard("openfin.unavailable", "The library could not unsubscribe from close-requested.", () => {
779
- e?.removeListener?.("close-requested", this.onOpenFinClose);
780
- });
781
- }
782
- flush(e) {
783
- let { config: t, diagnostics: n } = this.deps;
784
- if (n.guard("pipeline.crashed", "The metric points before the exit flush failed.", () => {
785
- this.deps.beforeExit?.();
786
- }), e === "hidden") {
787
- this.flushAlive();
788
- return;
789
- }
790
- if (t.transport) {
791
- this.flushCustom(e, t.transportOnExit);
792
- return;
793
- }
794
- let r = this.endpointUrl();
795
- if (r !== null) {
796
- this.keepaliveBytes = 0;
797
- for (let i of this.deps.drainForExit()) {
798
- let a = Y(i, t), o = a === "logs" ? r.href : X(a, t);
799
- o ? this.flushBatch(i, e, new URL(o)) : n.count("record.dropped_no_destination");
800
- }
801
- }
802
- }
803
- flushBatch(t, n, r) {
804
- let { config: i } = this.deps;
805
- if (t.records.length === 0) return;
806
- let a = this.serialize(t, n);
807
- if (!a) return;
808
- if (O(a.body) > 6e4) {
809
- this.park(t);
810
- return;
811
- }
812
- r.searchParams.set(ce, t.id), r.searchParams.set(c, n);
813
- let o = r.toString(), s = this.deps.exitHeaders();
814
- if (s) {
815
- this.keepaliveFetch(o, t, a.body, n, () => {
816
- let n = new Headers(s);
817
- return n.set(e, a.contentType), n.set(re, t.id), n;
818
- });
819
- return;
820
- }
821
- if (i.headers) {
822
- this.park(t);
823
- return;
824
- }
825
- let l = this.beacon(o, a.body);
826
- l !== "sent" && (l === "refused" ? this.park(t) : this.keepaliveFetch(o, t, a.body, n, () => new Headers({ [e]: u })));
827
- }
828
- flushAlive() {
829
- let { diagnostics: e } = this.deps, t = "The flush of the hidden document failed.";
830
- e.guard("pipeline.crashed", t, () => {
831
- Promise.resolve(this.deps.flushAlive()).catch((n) => {
832
- e.report("pipeline.crashed", t, void 0, n);
833
- });
834
- });
835
- }
836
- flushCustom(e, t) {
837
- let { config: n, diagnostics: r } = this.deps;
838
- for (let i of this.deps.drainForExit()) {
839
- let a = this.serialize(i, e);
840
- if (!a) continue;
841
- let o = pt(i, a, Y(i, n));
842
- (t === void 0 || r.guard("transport.http_error", "The transportOnExit hook threw an error.", () => k(() => t(o))) !== !0) && this.park(i);
843
- }
844
- }
845
- park(e) {
846
- let { config: t, diagnostics: n, onLoss: r } = this.deps;
847
- t.storage === "memory" || t.storage === "none" ? r?.("not_stored", e.records, [e.id]) : je(e, n, r);
848
- }
849
- serialize(e, t) {
850
- let n = Z(this.deps.config, e, this.deps.diagnostics, `The ${t} exit flush could not serialize its batch.`);
851
- return n || this.deps.onLoss?.("unserializable", e.records, [e.id]), n;
852
- }
853
- endpointUrl() {
854
- let { endpoint: e } = this.deps.config;
855
- return e ? this.deps.diagnostics.guard("config.invalid", `The exit flush cannot use the endpoint ${e}.`, () => new URL(e)) ?? null : null;
856
- }
857
- beacon(e, t) {
858
- return this.deps.diagnostics.guard("transport.http_error", "The sendBeacon call failed.", () => {
859
- let n = globalThis.navigator;
860
- return n?.sendBeacon ? n.sendBeacon(e, new Blob([t], { type: "text/plain;charset=UTF-8" })) ? "sent" : "refused" : "unavailable";
861
- }) ?? "unavailable";
862
- }
863
- keepaliveFetch(e, t, n, r, i) {
864
- let { config: a, diagnostics: o } = this.deps, s = () => {
865
- this.park(t);
866
- }, c = O(n);
867
- this.keepaliveBytes + c > 6e4 ? s() : (this.keepaliveBytes += c, o.guard("transport.http_error", "The keepalive fetch failed.", () => (fetch(e, {
868
- method: "POST",
869
- body: n,
870
- headers: i(),
871
- credentials: a.credentials,
872
- keepalive: !0
873
- }).catch((e) => {
874
- o.report("transport.http_error", "The exit flush failed.", { reason: r }, e), s();
875
- }), !0)) !== !0 && s());
876
- }
877
- };
878
- //#endregion
879
- //#region src/transport/compression.ts
880
- async function vt(e) {
881
- let t = globalThis.CompressionStream;
882
- if (t === void 0) return null;
883
- let n = new Blob([e]).stream().pipeThrough(new t(g)), r = await new Response(n).arrayBuffer();
884
- return new Uint8Array(r);
885
- }
886
- //#endregion
887
- //#region src/transport/http-transport.ts
888
- var yt = "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.", bt = class extends ft {
889
- cachedHeaders;
890
- async send(e, t) {
891
- let { config: n } = this, r = Y(e, n), i = X(r, n);
892
- i ? e.records.length !== 0 && await this.post(e, r, i, t) : this.diagnostics.count("record.dropped_no_destination");
893
- }
894
- lastHeaders() {
895
- let { headers: e } = this.config;
896
- if (!e) return;
897
- if (typeof e != "function") return e;
898
- let t = this.cachedHeaders;
899
- return t?.source === e && t.endpoint === this.config.endpoint && t.metricsEndpoint === this.config.metricsEndpoint && t.tracesEndpoint === this.config.tracesEndpoint ? t.headers : void 0;
900
- }
901
- async post(e, t, n, r) {
902
- let i = this.serialize(e);
903
- if (!i) return;
904
- let a = await this.attempt(e, n, i, r);
905
- if (this.rejectsCredentials(a.status) && typeof this.config.headers == "function" && (this.cachedHeaders = void 0, a = await this.attempt(e, n, i, r)), a.ok) {
906
- this.accept(t, a.headers.get(ue));
907
- return;
908
- }
909
- let o = a.status, s = Be(a.headers.get(ae)), c = o === 413 ? await this.readMaxBytes(a) : void 0;
910
- throw this.reject(o, e.id, t, {
911
- retryAfterMs: s,
912
- maxBytes: c,
913
- advice: yt
914
- });
915
- }
916
- rejectsCredentials(e) {
917
- return e === 401 || e === 403;
918
- }
919
- async attempt(t, n, r, i) {
920
- let a = Date.now() + p, o = new Headers(await this.before(this.resolveHeaders(), a, n));
921
- o.set(e, r.contentType), o.set(re, t.id), o.set(ne, String(t.attempts));
922
- let s = await this.before(this.encodeBody(r.body), a, n);
923
- typeof s != "string" && o.set(d, g);
924
- let c = this.keepalive(s);
925
- return c && i?.(), this.dispatch(n, o, s, a, c);
926
- }
927
- async before(e, t, n) {
928
- try {
929
- return await E(e, Math.max(0, t - Date.now()));
930
- } catch {
931
- throw this.timeout(n);
932
- }
933
- }
934
- timeout(e) {
935
- let t = `The request took longer than ${String(p)} ms.`;
936
- return this.diagnostics.report("transport.timeout", t, { endpoint: e }), new z("timeout", t);
937
- }
938
- async encodeBody(e) {
939
- let { config: t } = this;
940
- return t.compression !== "gzip" || O(e) < 1024 ? e : await this.diagnostics.guardAsync("transport.http_error", "The library could not compress the payload.", () => vt(e)) ?? e;
941
- }
942
- async dispatch(e, t, n, r, i) {
943
- let { config: a } = this, o = new AbortController(), s = setTimeout(() => {
944
- o.abort();
945
- }, Math.max(0, r - Date.now()));
946
- D(s);
947
- try {
948
- return await k(() => fetch(e, {
949
- method: "POST",
950
- headers: t,
951
- body: n,
952
- credentials: a.credentials,
953
- signal: o.signal,
954
- cache: "no-store",
955
- keepalive: i
956
- }));
957
- } catch {
958
- throw o.signal.aborted ? this.timeout(e) : new z(j() ? "transient" : "offline", "A network error occurred.");
959
- } finally {
960
- clearTimeout(s);
961
- }
962
- }
963
- keepalive(e) {
964
- let t = globalThis.document?.visibilityState === "hidden", n = typeof e == "string" ? O(e) : e.byteLength;
965
- return t && n <= 6e4;
966
- }
967
- async readMaxBytes(e) {
968
- return this.diagnostics.guardAsync("transport.http_error", "The library could not read the body of the 413 response.", async () => {
969
- let t = JSON.parse(await e.text());
970
- return typeof t.maxBytes == "number" ? t.maxBytes : void 0;
971
- });
972
- }
973
- async resolveHeaders() {
974
- let { headers: e } = this.config;
975
- if (!e) return {};
976
- if (typeof e != "function") return e;
977
- 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()));
978
- return i && (this.cachedHeaders = {
979
- source: e,
980
- endpoint: t,
981
- metricsEndpoint: n,
982
- tracesEndpoint: r,
983
- headers: i
984
- }), i ?? {};
985
- }
986
- }, xt = class {
987
- storage;
988
- transport;
989
- diagnostics;
990
- config;
991
- batchesPerDrain;
992
- onLoss;
993
- timer = null;
994
- attempt = 0;
995
- draining = !1;
996
- stopped = !1;
997
- maxAttempts;
998
- onOnline = () => {
999
- this.attempt = 0, this.schedule(0);
1000
- };
1001
- constructor(e, t, n, r, i = 20, a = () => void 0) {
1002
- this.storage = e, this.transport = t, this.diagnostics = n, this.config = r, this.batchesPerDrain = i, this.onLoss = a, this.maxAttempts = Ee(r.retention).maxAttempts;
1003
- }
1004
- start() {
1005
- this.stopped = !1, typeof addEventListener < "u" && addEventListener("online", this.onOnline), this.schedule(t);
1006
- }
1007
- nudge() {
1008
- this.draining || this.schedule(f);
1009
- }
1010
- async storeFailed(e, t) {
1011
- let n = this.toStorable(e, t);
1012
- for (let e of n) await this.bounded(this.storage.save(e));
1013
- n.length > 0 && this.nudge();
1014
- }
1015
- stop() {
1016
- this.stopped = !0, typeof removeEventListener < "u" && removeEventListener("online", this.onOnline), this.clearTimer();
1017
- }
1018
- schedule(e) {
1019
- if (this.stopped) return;
1020
- this.clearTimer();
1021
- let t = setTimeout(() => {
1022
- this.drain();
1023
- }, e);
1024
- this.timer = t, D(t);
1025
- }
1026
- bounded(e) {
1027
- return E(e, y);
1028
- }
1029
- clearTimer() {
1030
- this.timer !== null && (clearTimeout(this.timer), this.timer = null);
1031
- }
1032
- backoffMs() {
1033
- let e = Math.min(f * 2 ** this.attempt, b);
1034
- return Math.random() * e;
1035
- }
1036
- async drain() {
1037
- if (!this.draining) {
1038
- this.draining = !0;
1039
- try {
1040
- await De("ui-observability.drain", this.diagnostics, () => this.drainLocked()) !== !0 && this.schedule(t);
1041
- } finally {
1042
- this.draining = !1;
1043
- }
1044
- }
1045
- }
1046
- async drainLocked() {
1047
- try {
1048
- if (!j()) return this.schedule(t), !0;
1049
- let e = await this.bounded(this.storage.take(this.batchesPerDrain));
1050
- if (e.length === 0) return this.schedule(t), !0;
1051
- let n = /* @__PURE__ */ new Set(), r = 0;
1052
- for (let t of e) {
1053
- if (this.stopped) return !0;
1054
- let e = Y(t, this.config);
1055
- if (n.has(e)) continue;
1056
- let i = await this.deliver(t);
1057
- if (i === "stop") return !0;
1058
- i !== "next" && (n.add(e), r = Math.max(r, i));
1059
- }
1060
- return n.size > 0 ? this.schedule(r) : this.schedule(e.length === this.batchesPerDrain ? 0 : t), !0;
1061
- } catch (e) {
1062
- return this.diagnostics.report("storage.degraded", "The drain loop threw an error.", void 0, e), this.attempt++, this.schedule(this.backoffMs()), !0;
1063
- }
1064
- }
1065
- async deliver(e) {
1066
- if (e.attempts >= this.maxAttempts) return this.diagnostics.report("storage.dead_lettered", `The library dropped a batch after ${e.attempts.toString()} attempts.`, {
1067
- batchId: e.id,
1068
- records: e.records.length
1069
- }), this.onLoss("max_attempts", e.records, [e.id]), await this.bounded(this.storage.remove(e.id)), "next";
1070
- try {
1071
- return await this.transport.send(e), await this.bounded(this.storage.remove(e.id)), this.attempt = 0, "next";
1072
- } catch (n) {
1073
- let r = n instanceof z ? n : void 0;
1074
- return r?.kind === "permanent" || r?.kind === "too_large" ? (await this.replaceStored(e, n), "next") : r?.kind === "offline" ? (this.schedule(t), "stop") : r?.kind === "throttled" ? (this.attempt++, r.retryAfterMs ?? this.backoffMs()) : (await this.bounded(this.storage.bumpAttempts(e.id, e.attempts + 1)), this.attempt++, r?.retryAfterMs ?? this.backoffMs());
1075
- }
1076
- }
1077
- async replaceStored(e, t) {
1078
- let n = this.toStorable(e, t);
1079
- await this.bounded(this.storage.remove(e.id));
1080
- for (let e of n) await this.bounded(this.storage.save(e));
1081
- }
1082
- toStorable(e, t) {
1083
- let n = t instanceof z ? t : void 0;
1084
- if (n?.kind === "permanent") return this.onLoss("rejected", e.records, [e.id]), [];
1085
- if (n?.kind !== "too_large") return [e];
1086
- let r = be(e, this.diagnostics);
1087
- return r ? (this.diagnostics.report("transport.batch_split", "The server refused the batch because it is too large.", {
1088
- from: e.records.length,
1089
- to: r[0].records.length,
1090
- serverMaxBytes: n.maxBytes
1091
- }), r) : (this.diagnostics.report("transport.dropped_permanent", "One record is larger than the server limit. The library dropped it.", {
1092
- batchId: e.id,
1093
- bodies: e.records.map((e) => e.body)
1094
- }), this.onLoss("rejected", e.records, [e.id]), []);
1095
- }
1096
- }, Q = [
1097
- "metrics",
1098
- "logs",
1099
- "traces"
1100
- ];
1101
- function St(e) {
1102
- let t = e.attributes[ie];
1103
- return t === "metric" ? "metrics" : t === "span" ? "traces" : "logs";
1104
- }
1105
- var $ = class {
1106
- options;
1107
- onFlush;
1108
- pending = [];
1109
- timer = null;
1110
- constructor(e, t) {
1111
- this.options = e, this.onFlush = t;
1112
- }
1113
- add(e) {
1114
- this.pending.push(e), this.pending.length >= this.options.batchSize ? this.onFlush(this) : this.arm();
1115
- }
1116
- take() {
1117
- this.clearTimer();
1118
- let e = this.pending;
1119
- return this.pending = [], e;
1120
- }
1121
- get size() {
1122
- return this.pending.length;
1123
- }
1124
- stop() {
1125
- this.clearTimer();
1126
- }
1127
- arm() {
1128
- if (this.timer !== null) return;
1129
- let e = setTimeout(() => {
1130
- this.timer = null, this.onFlush(this);
1131
- }, this.options.flushIntervalMs);
1132
- this.timer = e, D(e);
1133
- }
1134
- clearTimer() {
1135
- this.timer !== null && (clearTimeout(this.timer), this.timer = null);
1136
- }
1137
- }, Ct = class {
1138
- transport;
1139
- storage;
1140
- retry;
1141
- diagnostics;
1142
- streams;
1143
- unconfirmed = /* @__PURE__ */ new Map();
1144
- queue = [];
1145
- open = /* @__PURE__ */ new Set();
1146
- ceiling = new he();
1147
- inFlight = 0;
1148
- pumpScheduled = !1;
1149
- stopped = !1;
1150
- constructor(e, t, n, r, i) {
1151
- this.transport = e, this.storage = t, this.retry = n, this.diagnostics = r;
1152
- let a = (e) => {
1153
- this.claim(e);
1154
- };
1155
- this.streams = {
1156
- logs: new $(i.logs, a),
1157
- metrics: new $(i.metrics, a),
1158
- traces: new $(i.traces, a)
1159
- };
1160
- }
1161
- push(e) {
1162
- this.stopped || (e.observedTimeUnixNano = ge(), this.route(e));
1163
- }
1164
- drainForExit() {
1165
- let e = [...this.unconfirmed.values()].flatMap((e) => e.records);
1166
- return this.unconfirmed.clear(), Q.map((t) => this.toBatch([...e.filter((e) => St(e) === t), ...this.streams[t].take()])).filter((e) => e !== null);
1167
- }
1168
- async flushPending() {
1169
- if (!this.stopped) for (let e of Q) this.claim(this.streams[e]);
1170
- await Promise.all([...this.open].map((e) => e.done));
1171
- }
1172
- counts() {
1173
- let e = (e) => {
1174
- let t = 0;
1175
- for (let n of e) t += n.batch.records.length;
1176
- return t;
1177
- }, t = e(this.queue);
1178
- return {
1179
- pending: Q.reduce((e, t) => e + this.streams[t].size, 0) + t,
1180
- inFlight: e(this.open) - t
1181
- };
1182
- }
1183
- destroy() {
1184
- this.stopped = !0;
1185
- for (let e of Q) this.streams[e].stop();
1186
- this.queue.length = 0;
1187
- for (let e of this.open) e.resolve();
1188
- this.open.clear();
1189
- }
1190
- toBatch(e) {
1191
- return e.length === 0 ? null : {
1192
- id: ve(this.diagnostics),
1193
- records: e,
1194
- createdAt: Date.now(),
1195
- attempts: 0
1196
- };
1197
- }
1198
- route(e) {
1199
- try {
1200
- let t = this.streams[St(e)];
1201
- if (!this.ceiling.allow(e)) {
1202
- this.diagnostics.count("record.dropped_by_ceiling");
1203
- return;
1204
- }
1205
- t.add(e);
1206
- } catch (e) {
1207
- this.diagnostics.report("pipeline.crashed", "The pipeline dropped a record before it reached a stream.", void 0, e);
1208
- }
1209
- }
1210
- claim(e) {
1211
- this.enqueue(this.toBatch(e.take()));
1212
- }
1213
- enqueue(e) {
1214
- if (!e) return;
1215
- let t, n = {
1216
- batch: e,
1217
- done: new Promise((e) => {
1218
- t = e;
1219
- }),
1220
- resolve: t
1221
- };
1222
- this.unconfirmed.set(e.id, e), this.open.add(n), this.queue.push(n), !this.pumpScheduled && (this.pumpScheduled = !0, queueMicrotask(() => {
1223
- this.pumpScheduled = !1, this.pump();
1224
- }));
1225
- }
1226
- pump() {
1227
- for (; this.inFlight < 2;) {
1228
- let e = this.queue.shift();
1229
- if (!e) return;
1230
- this.inFlight++, this.run(e);
1231
- }
1232
- }
1233
- async run(e) {
1234
- let { batch: t } = e;
1235
- try {
1236
- await this.dispatch(t);
1237
- } catch (e) {
1238
- this.diagnostics.report("pipeline.crashed", "A dispatch rejected its promise. This must not occur.", { batchId: t.id }, e);
1239
- } finally {
1240
- this.inFlight--, this.open.delete(e), e.resolve(), this.pump();
1241
- }
1242
- }
1243
- async dispatch(e) {
1244
- try {
1245
- let t = this.transport.throttledForMs(e);
1246
- if (t > 0) {
1247
- await this.store(e), this.diagnostics.report("transport.throttled", "The server throttles requests. The pipeline stored the batch and did not send it.", {
1248
- batchId: e.id,
1249
- records: e.records.length,
1250
- throttledMs: t
1251
- });
1252
- return;
1253
- }
1254
- try {
1255
- await this.transport.send(e, () => {
1256
- this.unconfirmed.delete(e.id);
1257
- });
1258
- } catch (t) {
1259
- await this.storeFailed({
1260
- ...e,
1261
- attempts: e.attempts + 1
1262
- }, t), this.diagnostics.report("transport.http_error", "The send failed. The pipeline stored the batch for a retry.", {
1263
- batchId: e.id,
1264
- records: e.records.length
1265
- }, t);
1266
- }
1267
- } finally {
1268
- this.unconfirmed.delete(e.id);
1269
- }
1270
- }
1271
- async storeFailed(e, t) {
1272
- await this.diagnostics.guardAsync("storage.degraded", "The library could not store an undelivered batch.", async () => {
1273
- await this.retry.storeFailed(e, t);
1274
- });
1275
- }
1276
- async store(e) {
1277
- await this.diagnostics.guardAsync("storage.degraded", "The library could not store an undelivered batch.", async () => {
1278
- await E(this.storage.save(e), y), this.retry.nudge();
1279
- });
1280
- }
1281
- }, wt = class e {
1282
- storage;
1283
- retry;
1284
- pipeline;
1285
- exitFlush;
1286
- diagnostics;
1287
- transport;
1288
- constructor(e, t, n, r, i, a) {
1289
- this.storage = e, this.retry = t, this.pipeline = n, this.exitFlush = r, this.diagnostics = i, this.transport = a;
1290
- }
1291
- static async create(t) {
1292
- let { config: n, diagnostics: r } = t, i = await ze(n.storage, de, Ee(n.retention), r, t.onGap), a = (e) => {
1293
- let i = we(e, r);
1294
- i && Te(t.remote, n, i);
1295
- }, { onLoss: o } = t, s = n.transport ? void 0 : new bt(n, r, a, o), c = s ?? new gt(n, r, a, o), l = new xt(i, c, r, n, 20, o), { batching: u } = n, d = new Ct(c, i, l, r, {
1296
- logs: {
1297
- flushIntervalMs: u.logFlushIntervalMs,
1298
- batchSize: u.logBatchSize
1299
- },
1300
- metrics: {
1301
- flushIntervalMs: u.metricFlushIntervalMs,
1302
- batchSize: u.metricBatchSize
1303
- },
1304
- traces: {
1305
- flushIntervalMs: u.logFlushIntervalMs,
1306
- batchSize: u.logBatchSize
1307
- }
1308
- }), f = new _t({
1309
- config: n,
1310
- diagnostics: r,
1311
- drainForExit: () => d.drainForExit(),
1312
- flushAlive: () => d.flushPending(),
1313
- exitHeaders: () => s?.lastHeaders(),
1314
- onLoss: o,
1315
- beforeExit: t.beforeExit
1316
- }), p = !1;
1317
- try {
1318
- f.install(), l.start(), p = !0;
1319
- } finally {
1320
- p || (f.uninstall(), l.stop());
1321
- }
1322
- let m = new e(i, l, d, f, r, c);
1323
- return m.importEmergency(), m;
1324
- }
1325
- push(e) {
1326
- this.pipeline.push(e);
1327
- }
1328
- async flush() {
1329
- await this.pipeline.flushPending();
1330
- }
1331
- async queueDepth() {
1332
- return await this.diagnostics.guardAsync("storage.degraded", "The library could not count the batches in storage.", () => this.storage.count()) ?? 0;
1333
- }
1334
- stats() {
1335
- return {
1336
- ...this.pipeline.counts(),
1337
- lastSendAt: this.transport.lastSendAt()
1338
- };
1339
- }
1340
- async destroy() {
1341
- 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();
1342
- }
1343
- async importEmergency() {
1344
- let e = await this.diagnostics.guardAsync("storage.degraded", "The emergency import failed.", () => Me(this.storage, this.diagnostics));
1345
- e !== void 0 && e > 0 && this.retry.nudge();
1346
- }
1347
- };
1348
- //#endregion
1349
- export { wt as Sender, P as i, L as n, Ne as r, I as t };
1350
-
1351
- //# sourceMappingURL=sender-DFQMF39P.js.map