mirror of
https://github.com/MrUnknownDE/utools.git
synced 2026-04-19 06:03:45 +02:00
edit some sentry in traceroute
This commit is contained in:
@@ -37,11 +37,11 @@ router.get('/', (req, res) => {
|
|||||||
return res.status(403).json({ success: false, error: 'Operations on private or local IP addresses are not allowed.' });
|
return res.status(403).json({ success: false, error: 'Operations on private or local IP addresses are not allowed.' });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the transaction created by Sentry's tracingHandler middleware
|
// Add specific context for this request to the current Sentry scope
|
||||||
const scope = Sentry.getCurrentScope();
|
// Errors/Messages captured later in this request handler will have this context.
|
||||||
const transaction = scope?.getTransaction();
|
Sentry.configureScope(scope => {
|
||||||
// Add specific context for this request if needed
|
scope.setContext("traceroute_details", { targetIp: targetIp, requestIp: requestIp });
|
||||||
scope?.setContext("traceroute_details", { targetIp: targetIp });
|
});
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -59,18 +59,6 @@ router.get('/', (req, res) => {
|
|||||||
|
|
||||||
let buffer = '';
|
let buffer = '';
|
||||||
|
|
||||||
// Flag to ensure transaction is only finished once
|
|
||||||
let transactionFinished = false;
|
|
||||||
const finishTransaction = (status) => {
|
|
||||||
if (!transactionFinished && transaction) {
|
|
||||||
transaction.setStatus(status);
|
|
||||||
transaction.finish();
|
|
||||||
transactionFinished = true;
|
|
||||||
logger.debug({ requestIp, targetIp, status }, 'Sentry transaction finished.');
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
const sendEvent = (event, data) => {
|
const sendEvent = (event, data) => {
|
||||||
try {
|
try {
|
||||||
if (!res.writableEnded) {
|
if (!res.writableEnded) {
|
||||||
@@ -89,8 +77,7 @@ router.get('/', (req, res) => {
|
|||||||
Sentry.captureException(e, { level: 'warning', extra: { requestIp, targetIp, event } });
|
Sentry.captureException(e, { level: 'warning', extra: { requestIp, targetIp, event } });
|
||||||
if (proc && !proc.killed) proc.kill();
|
if (proc && !proc.killed) proc.kill();
|
||||||
if (!res.writableEnded) res.end();
|
if (!res.writableEnded) res.end();
|
||||||
// Finish transaction here as well, as the request handling failed
|
// No manual transaction finishing needed here
|
||||||
finishTransaction('internal_error');
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -120,10 +107,10 @@ router.get('/', (req, res) => {
|
|||||||
proc.on('error', (err) => {
|
proc.on('error', (err) => {
|
||||||
const errorMsg = getErrorMessage(err, 'Failed to start traceroute command due to an unknown error.');
|
const errorMsg = getErrorMessage(err, 'Failed to start traceroute command due to an unknown error.');
|
||||||
logger.error({ requestIp, targetIp, error: errorMsg }, `Failed to start traceroute command`);
|
logger.error({ requestIp, targetIp, error: errorMsg }, `Failed to start traceroute command`);
|
||||||
Sentry.captureException(err, { extra: { requestIp, targetIp } });
|
Sentry.captureException(err, { extra: { requestIp, targetIp } }); // Capture original error
|
||||||
sendEvent('error', { error: `Failed to start traceroute: ${errorMsg}` });
|
sendEvent('error', { error: `Failed to start traceroute: ${errorMsg}` });
|
||||||
if (!res.writableEnded) res.end();
|
if (!res.writableEnded) res.end();
|
||||||
finishTransaction('internal_error'); // Finish transaction on process error
|
// No manual transaction finishing needed here
|
||||||
});
|
});
|
||||||
|
|
||||||
proc.on('close', (code) => {
|
proc.on('close', (code) => {
|
||||||
@@ -133,35 +120,35 @@ router.get('/', (req, res) => {
|
|||||||
else if (buffer.trim()) sendEvent('info', { message: buffer.trim() });
|
else if (buffer.trim()) sendEvent('info', { message: buffer.trim() });
|
||||||
}
|
}
|
||||||
|
|
||||||
let status = 'ok';
|
|
||||||
if (code !== 0) {
|
if (code !== 0) {
|
||||||
const errorMsg = `Traceroute command failed with exit code ${code}`;
|
const errorMsg = `Traceroute command failed with exit code ${code}`;
|
||||||
logger.error({ requestIp, targetIp, exitCode: code }, errorMsg);
|
logger.error({ requestIp, targetIp, exitCode: code }, errorMsg);
|
||||||
Sentry.captureMessage('Traceroute command failed', { level: 'error', extra: { requestIp, targetIp, exitCode: code } });
|
Sentry.captureMessage('Traceroute command failed', { level: 'error', extra: { requestIp, targetIp, exitCode: code } });
|
||||||
sendEvent('error', { error: errorMsg });
|
sendEvent('error', { error: errorMsg });
|
||||||
status = 'unknown_error';
|
// Transaction status will be inferred by Sentry based on errors captured
|
||||||
} else {
|
} else {
|
||||||
logger.info({ requestIp, targetIp }, `Traceroute stream completed successfully.`);
|
logger.info({ requestIp, targetIp }, `Traceroute stream completed successfully.`);
|
||||||
|
// Transaction status will likely be 'ok' if no errors were captured
|
||||||
}
|
}
|
||||||
sendEvent('end', { exitCode: code });
|
sendEvent('end', { exitCode: code });
|
||||||
if (!res.writableEnded) res.end();
|
if (!res.writableEnded) res.end();
|
||||||
finishTransaction(status); // Finish transaction on process close
|
// No manual transaction finishing needed here
|
||||||
});
|
});
|
||||||
|
|
||||||
req.on('close', () => {
|
req.on('close', () => {
|
||||||
logger.info({ requestIp, targetIp }, 'Client disconnected from traceroute stream, killing process.');
|
logger.info({ requestIp, targetIp }, 'Client disconnected from traceroute stream, killing process.');
|
||||||
if (proc && !proc.killed) proc.kill();
|
if (proc && !proc.killed) proc.kill();
|
||||||
if (!res.writableEnded) res.end();
|
if (!res.writableEnded) res.end();
|
||||||
finishTransaction('cancelled'); // Finish transaction on client disconnect
|
// Sentry transaction might be marked as 'cancelled' automatically or based on timeout
|
||||||
|
// No manual transaction finishing needed here
|
||||||
});
|
});
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// This catch handles errors during the initial setup (e.g., spawn fails immediately)
|
// This catch handles errors during the initial setup (e.g., spawn fails immediately)
|
||||||
const errorMsg = getErrorMessage(error, 'Failed to initiate traceroute due to an internal server error.');
|
const errorMsg = getErrorMessage(error, 'Failed to initiate traceroute due to an internal server error.');
|
||||||
logger.error({ requestIp, targetIp, error: errorMsg, stack: error.stack }, 'Error setting up traceroute stream');
|
logger.error({ requestIp, targetIp, error: errorMsg, stack: error.stack }, 'Error setting up traceroute stream');
|
||||||
Sentry.captureException(error, { extra: { requestIp, targetIp } });
|
Sentry.captureException(error, { extra: { requestIp, targetIp } }); // Capture original error
|
||||||
// Finish the transaction if an error occurs during setup
|
// No manual transaction finishing needed here
|
||||||
finishTransaction('internal_error');
|
|
||||||
|
|
||||||
if (!res.headersSent) {
|
if (!res.headersSent) {
|
||||||
res.status(500).json({ success: false, error: `Failed to initiate traceroute: ${errorMsg}` });
|
res.status(500).json({ success: false, error: `Failed to initiate traceroute: ${errorMsg}` });
|
||||||
|
|||||||
Reference in New Issue
Block a user