feat: Sentry in the API, the worker, the Site and the Kanban (off until the DSNs are set)
app/observability.py starts sentry-sdk for the API and the worker when SENTRY_DSN_API is set, with send_default_pii off and a scrub that strips cookies, tokens, bodies, CPF/CNPJ and e-mails. The browser loads the official Sentry bundle from our own origin (web/vendor, provenance in its README) and starts with SENTRY_DSN_WEB from /api/session or /api/operator/me; every failed API answer the customer or the team gets is reported with its path, status and message, never the request body. The CSP allows Blyzer's Sentry ingest host. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
120
web/observability.js
Normal file
120
web/observability.js
Normal file
@@ -0,0 +1,120 @@
|
||||
/* Sentry for the Site and the Kanban, used only when the DSN arrives from the
|
||||
API. It loads after web/vendor/sentry.min.js and never fetches anything
|
||||
itself; DSNs are public by design, so an empty one leaves everything off.
|
||||
|
||||
beforeSend follows the server (app/observability.py): no cookies, credential
|
||||
headers, request bodies or signed/token query strings, and CPF/CNPJ and
|
||||
e-mails replaced by [removido]. */
|
||||
(function () {
|
||||
const EMAIL = /[^\s@]+@[^\s@]+\.[A-Za-z]{2,}/g;
|
||||
const DOCUMENT = /(?<!\d)(?:\d{2}\.?\d{3}\.?\d{3}\/?\d{4}-?\d{2}|\d{3}\.?\d{3}\.?\d{3}-?\d{2})(?!\d)/g;
|
||||
const SENSITIVE_PARAM = /token|signature|x-amz/i;
|
||||
const SENSITIVE_HEADER = /^(cookie|authorization|x-.*token.*)$/i;
|
||||
const REDACTED = '[removido]';
|
||||
let started = false;
|
||||
let app = '';
|
||||
|
||||
const clean = value => String(value == null ? '' : value)
|
||||
.replace(EMAIL, REDACTED).replace(DOCUMENT, REDACTED);
|
||||
|
||||
function scrubQuery(query) {
|
||||
if (!query) return query;
|
||||
return String(query).split('&').filter(part => {
|
||||
let key = part.split('=')[0] || '';
|
||||
try { key = decodeURIComponent(key.replace(/\+/g, ' ')); } catch (error) { /* keep the raw key */ }
|
||||
return !SENSITIVE_PARAM.test(key);
|
||||
}).join('&');
|
||||
}
|
||||
|
||||
function scrubText(value) {
|
||||
if (typeof value !== 'string') return value;
|
||||
value = clean(value);
|
||||
const at = value.indexOf('?');
|
||||
return at < 0 ? value : value.slice(0, at + 1) + scrubQuery(value.slice(at + 1));
|
||||
}
|
||||
|
||||
function scrubUrl(url) {
|
||||
if (typeof url !== 'string') return url;
|
||||
const at = url.indexOf('?');
|
||||
return at < 0 ? url : url.slice(0, at + 1) + scrubQuery(url.slice(at + 1));
|
||||
}
|
||||
|
||||
function scrubValue(value) {
|
||||
if (typeof value === 'string') return scrubText(value);
|
||||
if (Array.isArray(value)) return value.map(scrubValue);
|
||||
if (value && typeof value === 'object') {
|
||||
const out = {};
|
||||
for (const [key, item] of Object.entries(value)) out[key] = scrubValue(item);
|
||||
return out;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function scrubRequest(request) {
|
||||
if (!request || typeof request !== 'object') return;
|
||||
delete request.cookies;
|
||||
delete request.data;
|
||||
delete request.body;
|
||||
if (request.headers && typeof request.headers === 'object') {
|
||||
for (const key of Object.keys(request.headers)) {
|
||||
if (SENSITIVE_HEADER.test(key)) delete request.headers[key];
|
||||
}
|
||||
}
|
||||
if (request.query_string) request.query_string = scrubQuery(request.query_string);
|
||||
if (request.url) request.url = scrubUrl(request.url);
|
||||
}
|
||||
|
||||
function scrub(event) {
|
||||
if (!event || typeof event !== 'object') return event;
|
||||
scrubRequest(event.request);
|
||||
if (typeof event.message === 'string') event.message = scrubText(event.message);
|
||||
if (event.logentry && typeof event.logentry.message === 'string')
|
||||
event.logentry.message = scrubText(event.logentry.message);
|
||||
if (event.exception && Array.isArray(event.exception.values)) {
|
||||
for (const value of event.exception.values) {
|
||||
if (value && typeof value.value === 'string') value.value = scrubText(value.value);
|
||||
}
|
||||
}
|
||||
let breadcrumbs = event.breadcrumbs;
|
||||
if (breadcrumbs && !Array.isArray(breadcrumbs)) breadcrumbs = breadcrumbs.values;
|
||||
if (Array.isArray(breadcrumbs)) {
|
||||
for (const crumb of breadcrumbs) {
|
||||
if (!crumb || typeof crumb !== 'object') continue;
|
||||
if (typeof crumb.message === 'string') crumb.message = scrubText(crumb.message);
|
||||
if (crumb.data) crumb.data = scrubValue(crumb.data);
|
||||
}
|
||||
}
|
||||
if (event.extra) event.extra = scrubValue(event.extra);
|
||||
if (event.user) event.user = scrubValue(event.user);
|
||||
return event;
|
||||
}
|
||||
|
||||
function start(dsn, component) {
|
||||
if (started || !dsn || typeof Sentry === 'undefined') return;
|
||||
started = true;
|
||||
app = component || '';
|
||||
const options = {dsn, sendDefaultPii: false, beforeSend: scrub};
|
||||
if (window.dtfEnvironment) options.environment = window.dtfEnvironment;
|
||||
if (window.dtfRelease) options.release = window.dtfRelease;
|
||||
try {
|
||||
Sentry.init(options);
|
||||
Sentry.setTag('app', app);
|
||||
} catch (error) {
|
||||
started = false;
|
||||
}
|
||||
}
|
||||
|
||||
function apiError(path, status, message, detail) {
|
||||
if (!started || status === 401 || typeof Sentry === 'undefined') return;
|
||||
try {
|
||||
const extra = {path, status, page: location.pathname};
|
||||
if (status === 422 && detail) extra.detail = detail;
|
||||
Sentry.captureMessage(clean('API ' + status + ' ' + path + ': ' + (message || '')),
|
||||
{level: 'error', tags: {app}, extra});
|
||||
} catch (error) {
|
||||
// Reporting an error must never break the page.
|
||||
}
|
||||
}
|
||||
|
||||
window.dtfObservability = {start, apiError, scrub};
|
||||
})();
|
||||
Reference in New Issue
Block a user