@posthog/browser-common 0.9.2 → 0.9.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client.d.ts +4 -2
- package/dist/config.js +1 -1
- package/dist/config.mjs +1 -1
- package/dist/console-logs.d.ts +15 -0
- package/dist/console-logs.js +317 -0
- package/dist/console-logs.mjs +276 -0
- package/dist/constants.d.ts +16 -0
- package/dist/constants.js +64 -0
- package/dist/constants.mjs +17 -1
- package/dist/extension-tokens.d.ts +4 -0
- package/dist/extension-tokens.js +40 -0
- package/dist/extension-tokens.mjs +2 -0
- package/dist/extension.d.ts +4 -0
- package/dist/feature-flags-config.d.ts +23 -0
- package/dist/feature-flags-config.js +18 -0
- package/dist/feature-flags-config.mjs +1 -0
- package/dist/feature-flags.d.ts +329 -0
- package/dist/feature-flags.js +1222 -0
- package/dist/feature-flags.mjs +1172 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +15 -7
- package/dist/index.mjs +1 -0
- package/dist/logs-config.d.ts +24 -0
- package/dist/logs-config.js +80 -0
- package/dist/logs-config.mjs +42 -0
- package/dist/logs-types.d.ts +31 -0
- package/dist/logs-types.js +46 -0
- package/dist/logs-types.mjs +8 -0
- package/dist/logs.d.ts +85 -0
- package/dist/logs.js +469 -0
- package/dist/logs.mjs +428 -0
- package/dist/token.d.ts +1 -1
- package/dist/types/feature-flags.d.ts +18 -0
- package/dist/types/feature-flags.js +18 -0
- package/dist/types/feature-flags.mjs +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +7 -0
- package/dist/types/index.mjs +1 -0
- package/dist/utils/console-original.d.ts +6 -0
- package/dist/utils/console-original.js +43 -0
- package/dist/utils/console-original.mjs +5 -0
- package/dist/utils/event-utils.d.ts +1 -1
- package/dist/utils/event-utils.js +3 -9
- package/dist/utils/event-utils.mjs +1 -7
- package/dist/utils/logger.js +8 -3
- package/dist/utils/logger.mjs +8 -3
- package/dist/utils/patch.d.ts +3 -0
- package/dist/utils/patch.js +90 -0
- package/dist/utils/patch.mjs +52 -0
- package/dist/utils/request-reachability.d.ts +2 -0
- package/dist/utils/request-reachability.js +61 -0
- package/dist/utils/request-reachability.mjs +20 -0
- package/dist/utils/request-utils.d.ts +1 -2
- package/dist/utils/request-utils.js +3 -14
- package/dist/utils/request-utils.mjs +3 -14
- package/dist/utils/sanitize-url.d.ts +11 -0
- package/dist/utils/sanitize-url.js +55 -0
- package/dist/utils/sanitize-url.mjs +17 -0
- package/dist/utils/timezone.d.ts +1 -0
- package/dist/utils/timezone.js +46 -0
- package/dist/utils/timezone.mjs +8 -0
- package/package.json +59 -3
package/dist/logs.js
ADDED
|
@@ -0,0 +1,469 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = (exports1, getters, values)=>{
|
|
5
|
+
var define = (defs, kind)=>{
|
|
6
|
+
for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
[kind]: defs[key]
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
define(getters, "get");
|
|
12
|
+
define(values, "value");
|
|
13
|
+
};
|
|
14
|
+
})();
|
|
15
|
+
(()=>{
|
|
16
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
17
|
+
})();
|
|
18
|
+
(()=>{
|
|
19
|
+
__webpack_require__.r = (exports1)=>{
|
|
20
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
21
|
+
value: 'Module'
|
|
22
|
+
});
|
|
23
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
24
|
+
value: true
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
})();
|
|
28
|
+
var __webpack_exports__ = {};
|
|
29
|
+
__webpack_require__.r(__webpack_exports__);
|
|
30
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
31
|
+
PostHogLogs: ()=>PostHogLogs,
|
|
32
|
+
RECORDER_MAX_AGE_MS: ()=>RECORDER_MAX_AGE_MS
|
|
33
|
+
});
|
|
34
|
+
const core_namespaceObject = require("@posthog/core");
|
|
35
|
+
const promise_utils_js_namespaceObject = require("./utils/promise-utils.js");
|
|
36
|
+
const general_utils_js_namespaceObject = require("./utils/general-utils.js");
|
|
37
|
+
const logger_js_namespaceObject = require("./utils/logger.js");
|
|
38
|
+
const external_logs_config_js_namespaceObject = require("./logs-config.js");
|
|
39
|
+
const external_logs_types_js_namespaceObject = require("./logs-types.js");
|
|
40
|
+
const patch_js_namespaceObject = require("./utils/patch.js");
|
|
41
|
+
const console_original_js_namespaceObject = require("./utils/console-original.js");
|
|
42
|
+
const request_reachability_js_namespaceObject = require("./utils/request-reachability.js");
|
|
43
|
+
const RECORDER_MAX_AGE_MS = 30000;
|
|
44
|
+
const CONSOLE_SCOPE_NAME = 'console';
|
|
45
|
+
const LOGS_SEND_TIMEOUT_MS = 90000;
|
|
46
|
+
const MAX_CONSECUTIVE_STATUS_ZERO_FAILURES = 3;
|
|
47
|
+
const HANDLED_LOGS_REQUEST_ERROR = '__posthogHandledLogsRequestError';
|
|
48
|
+
const markLogsRequestErrorAsHandled = (error, fallbackMessage)=>{
|
|
49
|
+
const handledError = error instanceof Error ? error : new Error(fallbackMessage);
|
|
50
|
+
handledError[HANDLED_LOGS_REQUEST_ERROR] = true;
|
|
51
|
+
return handledError;
|
|
52
|
+
};
|
|
53
|
+
const isHandledLogsRequestError = (error)=>!!error && 'object' == typeof error && true === error[HANDLED_LOGS_REQUEST_ERROR];
|
|
54
|
+
class PostHogLogs {
|
|
55
|
+
get _coreLogger() {
|
|
56
|
+
return {
|
|
57
|
+
...this._logger,
|
|
58
|
+
error: (...args)=>{
|
|
59
|
+
if (!args.some(isHandledLogsRequestError)) this._logger.error(...args);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
constructor(_configSource, _getSdkContext){
|
|
64
|
+
this._configSource = _configSource;
|
|
65
|
+
this._getSdkContext = _getSdkContext;
|
|
66
|
+
this.name = 'logs';
|
|
67
|
+
this._isLogsEnabled = false;
|
|
68
|
+
this._isLoaded = false;
|
|
69
|
+
this._isLoading = false;
|
|
70
|
+
this._logger = (0, logger_js_namespaceObject.createLogger)('[logs]');
|
|
71
|
+
this._queue = [];
|
|
72
|
+
this._consoleQueue = [];
|
|
73
|
+
this._consecutiveStatusZeroFailures = 0;
|
|
74
|
+
this._setupStarted = false;
|
|
75
|
+
this._disposed = false;
|
|
76
|
+
this._consoleBuffer = [];
|
|
77
|
+
this._consoleRecorderUnpatchers = [];
|
|
78
|
+
this._isRecordingConsole = false;
|
|
79
|
+
this._isRecordingConsoleEntry = false;
|
|
80
|
+
this._listeningForReconnect = false;
|
|
81
|
+
this._onReconnect = ()=>{
|
|
82
|
+
if (this._disposed) return;
|
|
83
|
+
this._consecutiveStatusZeroFailures = 0;
|
|
84
|
+
this._core?.onReconnect();
|
|
85
|
+
this._consoleCore?.onReconnect();
|
|
86
|
+
};
|
|
87
|
+
this._isLogsEnabled = !!_configSource.get()?.captureConsoleLogs;
|
|
88
|
+
}
|
|
89
|
+
get _isRequestReady() {
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
_getConsoleLoader() {}
|
|
93
|
+
_listenForReconnect() {
|
|
94
|
+
if (this._listeningForReconnect) return;
|
|
95
|
+
try {
|
|
96
|
+
this._window = "u" < typeof window ? void 0 : window;
|
|
97
|
+
} catch {
|
|
98
|
+
this._window = void 0;
|
|
99
|
+
}
|
|
100
|
+
if (this._window) {
|
|
101
|
+
(0, general_utils_js_namespaceObject.addEventListener)(this._window, 'online', this._onReconnect);
|
|
102
|
+
this._listeningForReconnect = true;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
_buildCore(getQueue, setQueue, opts, scopeName) {
|
|
106
|
+
this._listenForReconnect();
|
|
107
|
+
const config = (0, external_logs_config_js_namespaceObject.resolveLogsConfig)(this._configSource.get(), opts);
|
|
108
|
+
const core = new core_namespaceObject.PostHogLogs(this._createHost(getQueue, setQueue), config, this._coreLogger, ()=>this._getSdkContext(), (fn)=>fn(), void 0, scopeName);
|
|
109
|
+
return [
|
|
110
|
+
core,
|
|
111
|
+
config
|
|
112
|
+
];
|
|
113
|
+
}
|
|
114
|
+
_getCore() {
|
|
115
|
+
const logsConfig = this._configSource.get();
|
|
116
|
+
if (!this._core || this._resolvedFrom !== logsConfig) {
|
|
117
|
+
this._core?.reset();
|
|
118
|
+
this._resolvedFrom = logsConfig;
|
|
119
|
+
[this._core, this._resolvedConfig] = this._buildCore(()=>this._queue, (q)=>{
|
|
120
|
+
this._queue = q;
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
return this._core;
|
|
124
|
+
}
|
|
125
|
+
_getConsoleCore() {
|
|
126
|
+
const logsConfig = this._configSource.get();
|
|
127
|
+
if (!this._consoleCore || this._consoleResolvedFrom !== logsConfig) {
|
|
128
|
+
this._consoleCore?.reset();
|
|
129
|
+
this._consoleResolvedFrom = logsConfig;
|
|
130
|
+
[this._consoleCore, this._consoleResolvedConfig] = this._buildCore(()=>this._consoleQueue, (q)=>{
|
|
131
|
+
this._consoleQueue = q;
|
|
132
|
+
}, {
|
|
133
|
+
serviceNameDefault: 'posthog-browser-logs',
|
|
134
|
+
consoleCapture: true
|
|
135
|
+
}, CONSOLE_SCOPE_NAME);
|
|
136
|
+
}
|
|
137
|
+
return this._consoleCore;
|
|
138
|
+
}
|
|
139
|
+
setup(client) {
|
|
140
|
+
if (this._disposed || this._setupStarted) return;
|
|
141
|
+
this._setupStarted = true;
|
|
142
|
+
this._client = client;
|
|
143
|
+
this._logger = client.logger.createLogger('[logs]');
|
|
144
|
+
return (0, promise_utils_js_namespaceObject.continueWith)(client.kv.initialize(), ()=>{
|
|
145
|
+
if (!this._disposed) this._finishSetup(client);
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
_finishSetup(client) {
|
|
149
|
+
this._listenForReconnect();
|
|
150
|
+
if (this._configSource.get()?.captureConsoleLogs) this._isLogsEnabled = true;
|
|
151
|
+
if (this._isLogsEnabled || this._configSource.remoteConfigWillArrive && client.kv.get(this._configSource.captureHintKey)) this._startConsoleRecorder();
|
|
152
|
+
let replayedEnabledConfig = false;
|
|
153
|
+
const subscription = client.onRemoteConfig((result)=>{
|
|
154
|
+
replayedEnabledConfig = result.ok && result.config.logs?.captureConsoleLogs === true;
|
|
155
|
+
this.onRemoteConfig(result);
|
|
156
|
+
});
|
|
157
|
+
if (this._disposed) return void subscription.dispose();
|
|
158
|
+
this._remoteConfigSubscription = subscription;
|
|
159
|
+
if (!replayedEnabledConfig) this.loadIfEnabled();
|
|
160
|
+
}
|
|
161
|
+
dispose() {
|
|
162
|
+
if (this._disposed) return;
|
|
163
|
+
this._disposed = true;
|
|
164
|
+
this._stopConsoleRecorder();
|
|
165
|
+
this._remoteConfigSubscription?.dispose();
|
|
166
|
+
this._remoteConfigSubscription = void 0;
|
|
167
|
+
this._client = void 0;
|
|
168
|
+
this._isLoading = false;
|
|
169
|
+
this._window?.removeEventListener('online', this._onReconnect);
|
|
170
|
+
this._consoleLogsDispose?.();
|
|
171
|
+
this._consoleLogsDispose = void 0;
|
|
172
|
+
this._core?.clearQueue();
|
|
173
|
+
this._consoleCore?.clearQueue();
|
|
174
|
+
this._core?.reset();
|
|
175
|
+
this._consoleCore?.reset();
|
|
176
|
+
}
|
|
177
|
+
onRemoteConfig(result) {
|
|
178
|
+
if (this._disposed) return;
|
|
179
|
+
const logCapture = result.ok ? result.config.logs?.captureConsoleLogs : void 0;
|
|
180
|
+
if ((0, core_namespaceObject.isNullish)(logCapture)) return void this._stopRecorderStartedByPersistedHint();
|
|
181
|
+
this._client?.kv.set(this._configSource.captureHintKey, !!logCapture);
|
|
182
|
+
if (!logCapture) return void this._stopRecorderStartedByPersistedHint();
|
|
183
|
+
this._isLogsEnabled = true;
|
|
184
|
+
if (!this._isLoaded) this._startConsoleRecorder();
|
|
185
|
+
this.loadIfEnabled();
|
|
186
|
+
}
|
|
187
|
+
reset() {
|
|
188
|
+
this._stopConsoleRecorder();
|
|
189
|
+
this._core?.clearQueue();
|
|
190
|
+
this._queue = [];
|
|
191
|
+
this._core?.reset();
|
|
192
|
+
this._consoleCore?.clearQueue();
|
|
193
|
+
this._consoleQueue = [];
|
|
194
|
+
this._consoleCore?.reset();
|
|
195
|
+
this._consecutiveStatusZeroFailures = 0;
|
|
196
|
+
}
|
|
197
|
+
captureLog(options) {
|
|
198
|
+
if (!this._disposed && this._client) this._getCore().captureLog(options);
|
|
199
|
+
}
|
|
200
|
+
captureConsoleLog(options) {
|
|
201
|
+
if (!this._disposed && this._client) this._getConsoleCore().captureLog(options);
|
|
202
|
+
}
|
|
203
|
+
captureBufferedConsoleLog(options, context, occurredAtMs) {
|
|
204
|
+
if (!this._disposed && this._client) this._getConsoleCore().captureLog(options, {
|
|
205
|
+
context,
|
|
206
|
+
occurredAtMs
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
_onOptOut() {
|
|
210
|
+
this._stopConsoleRecorder();
|
|
211
|
+
this._consoleCore?.clearQueue();
|
|
212
|
+
this._consoleQueue = [];
|
|
213
|
+
}
|
|
214
|
+
_stopRecorderStartedByPersistedHint() {
|
|
215
|
+
if (this._isLogsEnabled) return;
|
|
216
|
+
this._stopConsoleRecorder();
|
|
217
|
+
}
|
|
218
|
+
_startConsoleRecorder() {
|
|
219
|
+
if (this._isRecordingConsole || !this._window?.console) return;
|
|
220
|
+
const maxBufferSize = (0, external_logs_config_js_namespaceObject.resolveLogsConfig)(this._configSource.get()).maxBufferSize;
|
|
221
|
+
let active = true;
|
|
222
|
+
this._consoleRecorderUnpatchers.push(()=>{
|
|
223
|
+
active = false;
|
|
224
|
+
});
|
|
225
|
+
for (const level of external_logs_types_js_namespaceObject.BUFFERED_CONSOLE_LEVELS){
|
|
226
|
+
let trueOriginal;
|
|
227
|
+
try {
|
|
228
|
+
trueOriginal = (0, console_original_js_namespaceObject.originalConsoleMethod)(this._window.console[level]);
|
|
229
|
+
} catch {
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
if (trueOriginal) this._consoleRecorderUnpatchers.push((0, patch_js_namespaceObject.patch)(this._window.console, level, (next)=>{
|
|
233
|
+
const wrapped = (...args)=>{
|
|
234
|
+
try {
|
|
235
|
+
if (active) this._recordConsoleEntry(level, args, maxBufferSize);
|
|
236
|
+
} catch {}
|
|
237
|
+
return next.apply(this._window.console, args);
|
|
238
|
+
};
|
|
239
|
+
wrapped.__rrweb_original__ = trueOriginal;
|
|
240
|
+
return wrapped;
|
|
241
|
+
}));
|
|
242
|
+
}
|
|
243
|
+
this._isRecordingConsole = true;
|
|
244
|
+
this._consoleRecorderTimeout = setTimeout(()=>{
|
|
245
|
+
this._stopConsoleRecorder();
|
|
246
|
+
}, RECORDER_MAX_AGE_MS);
|
|
247
|
+
}
|
|
248
|
+
_recordConsoleEntry(level, args, maxBufferSize) {
|
|
249
|
+
if (!this._isRecordingConsole || this._isRecordingConsoleEntry || 0 === args.length) return;
|
|
250
|
+
if (!this._client?.canCapture) return void this._stopConsoleRecorder();
|
|
251
|
+
if (this._consoleBuffer.length >= maxBufferSize) return;
|
|
252
|
+
this._isRecordingConsoleEntry = true;
|
|
253
|
+
try {
|
|
254
|
+
this._consoleBuffer.push({
|
|
255
|
+
level,
|
|
256
|
+
args,
|
|
257
|
+
occurredAtMs: Date.now(),
|
|
258
|
+
context: this._getSdkContext()
|
|
259
|
+
});
|
|
260
|
+
} finally{
|
|
261
|
+
this._isRecordingConsoleEntry = false;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
_stopConsoleRecorder() {
|
|
265
|
+
this._consoleBuffer = [];
|
|
266
|
+
if (!this._isRecordingConsole) return;
|
|
267
|
+
this._isRecordingConsole = false;
|
|
268
|
+
if (this._consoleRecorderTimeout) {
|
|
269
|
+
clearTimeout(this._consoleRecorderTimeout);
|
|
270
|
+
this._consoleRecorderTimeout = void 0;
|
|
271
|
+
}
|
|
272
|
+
for (const unpatch of this._consoleRecorderUnpatchers)unpatch();
|
|
273
|
+
this._consoleRecorderUnpatchers = [];
|
|
274
|
+
}
|
|
275
|
+
_takeConsoleBuffer() {
|
|
276
|
+
const buffered = this._consoleBuffer;
|
|
277
|
+
this._stopConsoleRecorder();
|
|
278
|
+
return buffered;
|
|
279
|
+
}
|
|
280
|
+
get logger() {
|
|
281
|
+
if (!this._capture_logger) this._capture_logger = {
|
|
282
|
+
trace: (body, attributes)=>this.captureLog({
|
|
283
|
+
body,
|
|
284
|
+
level: 'trace',
|
|
285
|
+
attributes
|
|
286
|
+
}),
|
|
287
|
+
debug: (body, attributes)=>this.captureLog({
|
|
288
|
+
body,
|
|
289
|
+
level: 'debug',
|
|
290
|
+
attributes
|
|
291
|
+
}),
|
|
292
|
+
info: (body, attributes)=>this.captureLog({
|
|
293
|
+
body,
|
|
294
|
+
level: 'info',
|
|
295
|
+
attributes
|
|
296
|
+
}),
|
|
297
|
+
warn: (body, attributes)=>this.captureLog({
|
|
298
|
+
body,
|
|
299
|
+
level: 'warn',
|
|
300
|
+
attributes
|
|
301
|
+
}),
|
|
302
|
+
error: (body, attributes)=>this.captureLog({
|
|
303
|
+
body,
|
|
304
|
+
level: 'error',
|
|
305
|
+
attributes
|
|
306
|
+
}),
|
|
307
|
+
fatal: (body, attributes)=>this.captureLog({
|
|
308
|
+
body,
|
|
309
|
+
level: 'fatal',
|
|
310
|
+
attributes
|
|
311
|
+
})
|
|
312
|
+
};
|
|
313
|
+
return this._capture_logger;
|
|
314
|
+
}
|
|
315
|
+
flushLogs(transport) {
|
|
316
|
+
if (!this._client) return;
|
|
317
|
+
if (transport) return void this._flushViaTransport(transport);
|
|
318
|
+
if (this._core) this._core.flush().catch((err)=>this._logFlushError(err));
|
|
319
|
+
if (this._consoleCore) this._consoleCore.flush().catch((err)=>this._logFlushError(err));
|
|
320
|
+
}
|
|
321
|
+
async flush() {
|
|
322
|
+
const programmatic = this._core?.flush().catch((error)=>this._logFlushError(error));
|
|
323
|
+
const console = this._consoleCore?.flush().catch((error)=>this._logFlushError(error));
|
|
324
|
+
await programmatic;
|
|
325
|
+
await console;
|
|
326
|
+
}
|
|
327
|
+
_logFlushError(error) {
|
|
328
|
+
if (!isHandledLogsRequestError(error)) this._logger.error('PostHog logs flush failed:', error);
|
|
329
|
+
}
|
|
330
|
+
loadIfEnabled() {
|
|
331
|
+
if (this._disposed || !this._isLogsEnabled || this._isLoaded || this._isLoading) return;
|
|
332
|
+
const loadConsole = this._getConsoleLoader();
|
|
333
|
+
if (!loadConsole) return void this._stopConsoleRecorder();
|
|
334
|
+
this._isLoading = true;
|
|
335
|
+
try {
|
|
336
|
+
loadConsole((err, logsExtension)=>{
|
|
337
|
+
this._isLoading = false;
|
|
338
|
+
if (this._disposed || !this._isLogsEnabled) return;
|
|
339
|
+
if (err || !logsExtension) {
|
|
340
|
+
this._logger.error("Could not load logs script", err);
|
|
341
|
+
this._stopConsoleRecorder();
|
|
342
|
+
} else {
|
|
343
|
+
const buffered = this._takeConsoleBuffer();
|
|
344
|
+
this._consoleLogsDispose = logsExtension.initialize(this._client);
|
|
345
|
+
this._isLoaded = true;
|
|
346
|
+
if (buffered.length > 0) logsExtension.replay(this._client, buffered);
|
|
347
|
+
}
|
|
348
|
+
});
|
|
349
|
+
} catch (error) {
|
|
350
|
+
this._isLoading = false;
|
|
351
|
+
throw error;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
_createHost(getQueue, setQueue) {
|
|
355
|
+
const client = this._client;
|
|
356
|
+
return {
|
|
357
|
+
get isDisabled () {
|
|
358
|
+
return false;
|
|
359
|
+
},
|
|
360
|
+
get optedOut () {
|
|
361
|
+
return !client.canCapture;
|
|
362
|
+
},
|
|
363
|
+
getPersistedProperty: (key)=>key === core_namespaceObject.PostHogPersistedProperty.LogsQueue ? getQueue() : void 0,
|
|
364
|
+
setPersistedProperty: (key, value)=>{
|
|
365
|
+
if (key === core_namespaceObject.PostHogPersistedProperty.LogsQueue) setQueue(value ?? []);
|
|
366
|
+
},
|
|
367
|
+
_sendLogsBatch: (payload)=>this._sendLogsBatch(payload),
|
|
368
|
+
getLibraryId: ()=>client.library.name,
|
|
369
|
+
getLibraryVersion: ()=>client.library.version
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
_sendLogsBatch(payload) {
|
|
373
|
+
return new Promise((resolve)=>{
|
|
374
|
+
if ((0, request_reachability_js_namespaceObject.isStatusZeroFailureCircuitBreakerTripped)(this._consecutiveStatusZeroFailures, MAX_CONSECUTIVE_STATUS_ZERO_FAILURES)) return void resolve({
|
|
375
|
+
kind: 'fatal',
|
|
376
|
+
error: markLogsRequestErrorAsHandled(void 0, 'logs endpoint is unreachable, dropping batch')
|
|
377
|
+
});
|
|
378
|
+
let settled = false;
|
|
379
|
+
const settle = (outcome)=>{
|
|
380
|
+
if (settled) return;
|
|
381
|
+
settled = true;
|
|
382
|
+
clearTimeout(timer);
|
|
383
|
+
resolve(outcome);
|
|
384
|
+
};
|
|
385
|
+
const timer = setTimeout(()=>{
|
|
386
|
+
this._logger.warn('Logs request timed out before receiving a response');
|
|
387
|
+
settle({
|
|
388
|
+
kind: 'retry-later',
|
|
389
|
+
error: markLogsRequestErrorAsHandled(void 0, 'logs request timed out')
|
|
390
|
+
});
|
|
391
|
+
}, LOGS_SEND_TIMEOUT_MS);
|
|
392
|
+
const isRequestReady = this._isRequestReady;
|
|
393
|
+
this._client.sendRequest('/i/v1/logs', {
|
|
394
|
+
method: 'POST',
|
|
395
|
+
query: {
|
|
396
|
+
token: this._client.projectToken
|
|
397
|
+
},
|
|
398
|
+
body: payload,
|
|
399
|
+
compression: 'best-available',
|
|
400
|
+
timeoutMs: 60000
|
|
401
|
+
}).then((response)=>{
|
|
402
|
+
const status = response.statusCode;
|
|
403
|
+
this._trackEndpointReachability(status, isRequestReady);
|
|
404
|
+
if (status >= 200 && status < 300) settle({
|
|
405
|
+
kind: 'ok'
|
|
406
|
+
});
|
|
407
|
+
else if (413 === status) settle({
|
|
408
|
+
kind: 'too-large'
|
|
409
|
+
});
|
|
410
|
+
else if (0 === status || 408 === status || 429 === status || status >= 500) if (0 === status) {
|
|
411
|
+
if (!response.error) this._logger.warn('Logs request failed before receiving an HTTP response');
|
|
412
|
+
settle({
|
|
413
|
+
kind: 'retry-later',
|
|
414
|
+
error: markLogsRequestErrorAsHandled(response.error, 'logs request failed before receiving an HTTP response')
|
|
415
|
+
});
|
|
416
|
+
} else settle({
|
|
417
|
+
kind: 'retry-later',
|
|
418
|
+
error: response.error ?? new Error(`logs request failed with status ${status}`)
|
|
419
|
+
});
|
|
420
|
+
else settle({
|
|
421
|
+
kind: 'fatal',
|
|
422
|
+
error: new Error(`logs request failed with status ${status}`)
|
|
423
|
+
});
|
|
424
|
+
}, (error)=>{
|
|
425
|
+
settle({
|
|
426
|
+
kind: 'retry-later',
|
|
427
|
+
error
|
|
428
|
+
});
|
|
429
|
+
});
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
_trackEndpointReachability(statusCode, isRequestReady) {
|
|
433
|
+
if (0 === statusCode && !isRequestReady) return;
|
|
434
|
+
this._consecutiveStatusZeroFailures = (0, request_reachability_js_namespaceObject.updateStatusZeroFailureCount)(statusCode, this._consecutiveStatusZeroFailures, MAX_CONSECUTIVE_STATUS_ZERO_FAILURES, ()=>this._logger.warn('Log requests are failing before receiving an HTTP response; this can happen due to network issues, CORS, browser blocking, or ad blockers. Stopped sending logs; will try again when connectivity changes.'));
|
|
435
|
+
}
|
|
436
|
+
_flushViaTransport(transport) {
|
|
437
|
+
if (this._queue.length > 0) this._drainQueueViaTransport(transport, this._queue, this._resolvedConfig, this._client.library.name, (q)=>{
|
|
438
|
+
this._queue = q;
|
|
439
|
+
});
|
|
440
|
+
if (this._consoleQueue.length > 0) this._drainQueueViaTransport(transport, this._consoleQueue, this._consoleResolvedConfig, CONSOLE_SCOPE_NAME, (q)=>{
|
|
441
|
+
this._consoleQueue = q;
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
_drainQueueViaTransport(transport, queue, config, scopeName, setQueue) {
|
|
445
|
+
if (0 === queue.length) return;
|
|
446
|
+
const records = queue.map((e)=>e.record);
|
|
447
|
+
setQueue([]);
|
|
448
|
+
const payload = (0, core_namespaceObject.buildOtlpLogsPayload)(records, (0, core_namespaceObject.buildResourceAttributes)(config, this._client.library.name, this._client.library.version), scopeName, this._client.library.version);
|
|
449
|
+
this._client.sendRequest('/i/v1/logs', {
|
|
450
|
+
method: 'POST',
|
|
451
|
+
query: {
|
|
452
|
+
token: this._client.projectToken
|
|
453
|
+
},
|
|
454
|
+
body: payload,
|
|
455
|
+
compression: 'best-available',
|
|
456
|
+
transport,
|
|
457
|
+
timeoutMs: 60000
|
|
458
|
+
}).catch((error)=>this._logFlushError(error));
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
exports.PostHogLogs = __webpack_exports__.PostHogLogs;
|
|
462
|
+
exports.RECORDER_MAX_AGE_MS = __webpack_exports__.RECORDER_MAX_AGE_MS;
|
|
463
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
464
|
+
"PostHogLogs",
|
|
465
|
+
"RECORDER_MAX_AGE_MS"
|
|
466
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
467
|
+
Object.defineProperty(exports, '__esModule', {
|
|
468
|
+
value: true
|
|
469
|
+
});
|