1 | import { areCookiesAuthorized, Batch, combine, commonInit, getTimestamp, HttpRequest, } from '@datadog/browser-core';
|
2 | import { buildEnv } from './buildEnv';
|
3 | import { startLoggerSession } from './loggerSession';
|
4 | export function startLogs(userConfiguration, errorLogger, getGlobalContext) {
|
5 | var isCollectingError = userConfiguration.forwardErrorsToLogs !== false;
|
6 | var _a = commonInit(userConfiguration, buildEnv, isCollectingError), configuration = _a.configuration, internalMonitoring = _a.internalMonitoring, errorObservable = _a.errorObservable;
|
7 | var session = startLoggerSession(configuration, areCookiesAuthorized(configuration.cookieOptions));
|
8 | return doStartLogs(configuration, errorObservable, internalMonitoring, session, errorLogger, getGlobalContext);
|
9 | }
|
10 | export function doStartLogs(configuration, errorObservable, internalMonitoring, session, errorLogger, getGlobalContext) {
|
11 | internalMonitoring.setExternalContextProvider(function () {
|
12 | return combine({ session_id: session.getId() }, getGlobalContext(), getRUMInternalContext());
|
13 | });
|
14 | var batch = startLoggerBatch(configuration, session);
|
15 | errorObservable.subscribe(function (e) {
|
16 | return errorLogger.error(e.message, combine({ date: getTimestamp(e.startTime) }, e.context, getRUMInternalContext(e.startTime)));
|
17 | });
|
18 | return function (message, currentContext) {
|
19 | if (session.isTracked()) {
|
20 | batch.add(message, currentContext);
|
21 | }
|
22 | };
|
23 | }
|
24 | function startLoggerBatch(configuration, session) {
|
25 | var primaryBatch = createLoggerBatch(configuration.logsEndpoint);
|
26 | var replicaBatch;
|
27 | if (configuration.replica !== undefined) {
|
28 | replicaBatch = createLoggerBatch(configuration.replica.logsEndpoint);
|
29 | }
|
30 | function createLoggerBatch(endpointUrl) {
|
31 | return new Batch(new HttpRequest(endpointUrl, configuration.batchBytesLimit), configuration.maxBatchSize, configuration.batchBytesLimit, configuration.maxMessageSize, configuration.flushTimeout);
|
32 | }
|
33 | return {
|
34 | add: function (message, currentContext) {
|
35 | var contextualizedMessage = assembleMessageContexts({ service: configuration.service, session_id: session.getId() }, currentContext, getRUMInternalContext(), message);
|
36 | primaryBatch.add(contextualizedMessage);
|
37 | if (replicaBatch) {
|
38 | replicaBatch.add(contextualizedMessage);
|
39 | }
|
40 | },
|
41 | };
|
42 | }
|
43 | export function assembleMessageContexts(defaultContext, currentContext, rumInternalContext, message) {
|
44 | return combine(defaultContext, currentContext, rumInternalContext, message);
|
45 | }
|
46 | function getRUMInternalContext(startTime) {
|
47 | var rum = window.DD_RUM;
|
48 | return rum && rum.getInternalContext ? rum.getInternalContext(startTime) : undefined;
|
49 | }
|
50 |
|
\ | No newline at end of file |