Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions AdminPanel/server/sources/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@

'use strict';

require('../../../Common/sources/reporters').init('adminpanel');
const moduleReloader = require('../../../Common/sources/moduleReloader');
const config = moduleReloader.requireConfigWithRuntime();
const logger = require('../../../Common/sources/logger');
const operationContext = require('../../../Common/sources/operationContext');
const tenantManager = require('../../../Common/sources/tenantManager');
const license = require('../../../Common/sources/license');
Expand Down Expand Up @@ -178,6 +180,13 @@ app.use((err, req, res, _next) => {
res.sendStatus(err.status || err.statusCode || 500);
});

process.on('uncaughtException', err => {
operationContext.global.logger.error('uncaughtException:%s', err.stack);
logger.shutdown(() => {
process.exit(1);
});
});

server.listen(port, () => {
operationContext.global.logger.warn('AdminPanel server listening on port %d', port);
});
Expand Down
158 changes: 158 additions & 0 deletions Common/npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"@aws-sdk/client-s3": "3.637.0",
"@aws-sdk/s3-request-presigner": "3.370.0",
"@azure/storage-blob": "12.27.0",
"@sentry/node": "10.75.3",
"@smithy/node-http-handler": "4.0.3",
"amqplib": "0.10.7",
"axios": "1.8.3",
Expand Down
16 changes: 15 additions & 1 deletion Common/sources/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const fs = require('fs');

const log4js = require('log4js');
const layouts = require('log4js/lib/layouts');
const reporters = require('./reporters');
const logConfigPath = config.get('log.filePath');
const logOptions = config.get('log.options');

Expand Down Expand Up @@ -77,9 +78,22 @@ log4js.addLayout('patternWithTokens', cfg => {
const cachedLogConfig = JSON.parse(fs.readFileSync(logConfigPath, 'utf8'));
let curLogConfig = cachedLogConfig;

function withReporterAppenders(logConfig) {
const reporterAppenders = reporters.appenders();
const names = Object.keys(reporterAppenders);
if (names.length === 0) {
return logConfig;
}
const categories = {};
for (const [name, category] of Object.entries(logConfig.categories)) {
categories[name] = {...category, appenders: [...category.appenders, ...names]};
}
return {...logConfig, appenders: {...logConfig.appenders, ...reporterAppenders}, categories};
}

function configureLogger(options) {
const mergedOptions = config.util.extendDeep({}, cachedLogConfig, options);
log4js.configure(mergedOptions);
log4js.configure(withReporterAppenders(mergedOptions));
curLogConfig = mergedOptions;
}
configureLogger(logOptions);
Expand Down
25 changes: 25 additions & 0 deletions Common/sources/reporters/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* SPDX-FileCopyrightText: 2026 Euro-Office contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
'use strict';

// Backends are required statically so pkg bundles them
const backends = [require('./sentry')];

/**
* Initializes all configured error reporting backends. Must run before any other require so backends can instrument modules.
* @param {string} service - process name, e.g. docservice or converter
*/
function init(service) {
backends.forEach(backend => backend.init(service));
}

/**
* @returns {object} log4js appenders of the enabled backends, keyed by backend name
*/
function appenders() {
return Object.fromEntries(backends.filter(backend => backend.isEnabled()).map(backend => [backend.name, {type: backend}]));
}

module.exports = {init, appenders};
84 changes: 84 additions & 0 deletions Common/sources/reporters/sentry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* SPDX-FileCopyrightText: 2026 Euro-Office contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
'use strict';

const util = require('util');
const constants = require('../constants');

const SECRETS = [
[/([?&](?:token|doc)=)[^&#\s"'\\]*/gi, '$1[Filtered]'],
[/("authorization"\s*:\s*")[^"]*/gi, '$1[Filtered]'],
[/(Bearer\s+)[^\s"'\\]+/gi, '$1[Filtered]']
];

let Sentry = null;

/**
* Initializes the Sentry SDK when SENTRY_DSN is set. Must run before any other require so the SDK can instrument modules.
* @param {string} service - process name, reported as the `service` tag
*/
function init(service) {
if (!process.env.SENTRY_DSN || Sentry) {
return;
}
Sentry = require('@sentry/node');
const {buildVersion} = require('../commondefines');
Sentry.init({
dsn: process.env.SENTRY_DSN,
release: process.env.SENTRY_RELEASE || `documentserver@${buildVersion}`,
sendDefaultPii: false,
initialScope: {tags: {service}},
// Crashes are reported through the existing uncaughtException handlers, which log the error and flush via logger.shutdown
integrations: defaults => defaults.filter(i => i.name !== 'OnUncaughtException' && i.name !== 'OnUnhandledRejection'),
beforeSend: scrub
});
}

function isEnabled() {
return Sentry !== null;
}

function scrub(event) {
const json = SECRETS.reduce((str, [pattern, replacement]) => str.replace(pattern, replacement), JSON.stringify(event));
return JSON.parse(json);
}

/**
* log4js appender module: reports events of level ERROR and above.
*/
function configure(config, layouts, findAppender, levels) {
const appender = logEvent => {
if (!logEvent.level.isGreaterThanOrEqualTo(levels.ERROR)) {
return;
}
const [template] = logEvent.data;
const error = logEvent.data.find(arg => arg instanceof Error);
const message = util.format(...logEvent.data);
const {TENANT, DOCID} = logEvent.context || {};
const context = {
level: logEvent.level.isGreaterThanOrEqualTo(levels.FATAL) ? 'fatal' : 'error',
tags: {logger: logEvent.categoryName},
extra: {message}
};
if (TENANT) {
context.tags.tenant = TENANT;
}
if (DOCID && DOCID !== constants.DEFAULT_DOC_ID) {
context.tags.docId = DOCID;
}
if (typeof template === 'string') {
context.fingerprint = [template];
}
if (error) {
Sentry.captureException(error, context);
} else {
Sentry.captureMessage(message, context);
}
};
appender.shutdown = done => Sentry.close(2000).then(() => done(), done);
return appender;
}

module.exports = {name: 'sentry', init, isEnabled, scrub, configure};
1 change: 1 addition & 0 deletions DocService/sources/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

'use strict';

require('./../../Common/sources/reporters').init('docservice');
const moduleReloader = require('./../../Common/sources/moduleReloader');
const config = moduleReloader.requireConfigWithRuntime();
//process.env.NODE_ENV = config.get('services.CoAuthoring.server.mode');
Expand Down
1 change: 1 addition & 0 deletions FileConverter/sources/convertermaster.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

'use strict';

require('./../../Common/sources/reporters').init('converter');
const cluster = require('cluster');
const moduleReloader = require('./../../Common/sources/moduleReloader');
const config = moduleReloader.requireConfigWithRuntime();
Expand Down
1 change: 1 addition & 0 deletions SpellChecker/sources/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

'use strict';

require('./../../Common/sources/reporters').init('spellchecker');
const cluster = require('cluster');
const config = require('config').get('SpellChecker');

Expand Down
Loading
Loading