Refactor newSentryTransaction to use improved API

This change is based on a suggestion by Sentry staff. It requires SDK version > 8.4.0

https://github.com/getsentry/sentry-javascript/issues/12116#issuecomment-2132812315
https://github.com/getsentry/sentry-javascript/pull/12138
This commit is contained in:
Sebastian Serth
2024-05-27 10:36:24 +02:00
committed by Sebastian Serth
parent 016971f4c2
commit a99d86068a

View File

@ -205,23 +205,19 @@ var CodeOceanEditor = {
newSentryTransaction: function (initiator, callback) { newSentryTransaction: function (initiator, callback) {
// based on Sentry recommendation. // based on Sentry recommendation.
// See https://github.com/getsentry/sentry-javascript/issues/12116 // See https://github.com/getsentry/sentry-javascript/issues/12116
return Sentry.continueTrace({ sentryTrace: '', baggage: '' }, () => { return Sentry.startNewTrace(() => {
// inside of this we have a new trace! const cause = initiator.data('cause') || initiator.prop('id');
return Sentry.withActiveSpan(null, () => { return Sentry.startSpan({name: cause, op: "transaction"}, async () => {
// inside of this there is no parent span, no matter what! // Execute the desired custom code
const cause = initiator.data('cause') || initiator.prop('id'); try {
return Sentry.startSpan({name: cause, op: "transaction", forceTransaction: true}, async () => { return await callback();
// Execute the desired custom code } catch (error) {
try { // WebSocket errors are handled in `showWebsocketError` already.
return await callback(); if (error.target instanceof WebSocket) return;
} catch (error) {
// WebSocket errors are handled in `showWebsocketError` already.
if (error.target instanceof WebSocket) return;
console.error(JSON.stringify(error)); console.error(JSON.stringify(error));
Sentry.captureException(JSON.stringify(error), {mechanism: {handled: false}}); Sentry.captureException(JSON.stringify(error), {mechanism: {handled: false}});
} }
});
}); });
}); });
}, },