Skip to content

Commit

Permalink
WIP: update Sentry to v8
Browse files Browse the repository at this point in the history
  • Loading branch information
daniellockyer committed May 9, 2024
1 parent 8c3e5ec commit d9d3868
Show file tree
Hide file tree
Showing 8 changed files with 643 additions and 42 deletions.
4 changes: 0 additions & 4 deletions ghost/core/core/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ const maintenanceMiddleware = function maintenanceMiddleware(req, res, next) {

const rootApp = () => {
const app = express('root');
app.use(sentry.requestHandler);
if (config.get('sentry')?.tracing?.enabled === true) {
app.use(sentry.tracingHandler);
}
app.enable('maintenance');
app.use(maintenanceMiddleware);

Expand Down
11 changes: 6 additions & 5 deletions ghost/core/core/frontend/helpers/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ module.exports = async function get(resource, options) {
try {
const spanName = `{{#get "${resource}"${apiOptionsString}}} ${data.member ? 'member' : 'public'}`;
const result = await Sentry.startSpan({
op: 'frontend.helpers.get',
op: 'frontend.helpers.get',
name: spanName,
tags: {
resource,
Expand All @@ -313,19 +313,20 @@ module.exports = async function get(resource, options) {
if (response[resource] && response[resource].length) {
response[resource].forEach(prepareContextResource);
}

// used for logging details of slow requests
returnedRowsCount = response[resource] && response[resource].length;
span?.setTag('returnedRows', returnedRowsCount);


// span?.setTag('returnedRows', returnedRowsCount);

// block params allows the theme developer to name the data using something like
// `{{#get "posts" as |result pageInfo|}}`
const blockParams = [response[resource]];
if (response.meta && response.meta.pagination) {
response.pagination = response.meta.pagination;
blockParams.push(response.meta.pagination);
}

// Call the main template function
const rendered = options.fn(response, {
data: data,
Expand Down
2 changes: 0 additions & 2 deletions ghost/core/core/frontend/web/middleware/error-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ module.exports.handleThemeResponse = [
prepareError,
// Add cache-control header
prepareErrorCacheControl(),
// Handle the error in Sentry
sentry.errorHandler,
// Format the stack for the user
prepareStack,
// Render the error using theme template
Expand Down
6 changes: 3 additions & 3 deletions ghost/core/core/shared/express.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const debug = require('@tryghost/debug')('shared:express');
const express = require('express');
const {createLazyRouter} = require('express-lazy-router');
const sentry = require('./sentry');
const Sentry = require('@sentry/node');

const lazyLoad = createLazyRouter();

Expand All @@ -20,7 +20,7 @@ module.exports = (name) => {
app.enable('trust proxy');

// Sentry must be our first error handler. Mounting it here means all custom error handlers will come after
app.use(sentry.errorHandler);
Sentry.setupExpressErrorHandler(app);

app.lazyUse = function lazyUse(mountPath, requireFn) {
app.use(mountPath, lazyLoad(() => {
Expand All @@ -39,7 +39,7 @@ module.exports.Router = (name, options) => {
debug('new Router start', name);
const router = express.Router(options);

router.use(sentry.errorHandler);
Sentry.setupExpressErrorHandler(router);

debug('new Router end', name);
return router;
Expand Down
15 changes: 3 additions & 12 deletions ghost/core/core/shared/sentry.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ if (sentryConfig && !sentryConfig.disabled) {

// Enable tracing if sentry.tracing.enabled is true
if (sentryConfig.tracing?.enabled === true) {
sentryInitConfig.integrations.push(new Sentry.Integrations.Http({tracing: true}));
sentryInitConfig.tracesSampleRate = parseFloat(sentryConfig.tracing.sampleRate) || 0.0;

// Enable profiling, if configured, only if tracing is also configured
if (sentryConfig.profiling?.enabled === true) {
// Import Sentry's profiling integration if available
Expand All @@ -147,9 +147,6 @@ if (sentryConfig && !sentryConfig.disabled) {
Sentry.init(sentryInitConfig);

module.exports = {
requestHandler: Sentry.Handlers.requestHandler(),
errorHandler: Sentry.Handlers.errorHandler(),
tracingHandler: Sentry.Handlers.tracingHandler(),
captureException: Sentry.captureException,
captureMessage: Sentry.captureMessage,
beforeSend: beforeSend,
Expand All @@ -158,22 +155,16 @@ if (sentryConfig && !sentryConfig.disabled) {
if (sentryConfig.tracing?.enabled === true) {
const integration = new SentryKnexTracingIntegration(knex);

Sentry.addIntegration(integration);
// Sentry.addIntegration(integration);
}
}
};
} else {
const expressNoop = function (req, res, next) {
next();
};

const noop = () => {};

module.exports = {
requestHandler: expressNoop,
errorHandler: expressNoop,
tracingHandler: expressNoop,
captureException: noop,
captureMessage: noop,
initQueryTracing: noop
};
}
2 changes: 1 addition & 1 deletion ghost/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
},
"dependencies": {
"@extractus/oembed-extractor": "3.2.1",
"@sentry/node": "7.114.0",
"@sentry/node": "8.0.0-rc.2",
"@tryghost/adapter-base-cache": "0.1.12",
"@tryghost/adapter-cache-redis": "0.0.0",
"@tryghost/adapter-manager": "0.0.0",
Expand Down
4 changes: 0 additions & 4 deletions ghost/mw-error-handler/lib/mw-error-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,6 @@ module.exports.handleJSONResponse = sentry => [
module.exports.prepareError,
// Add cache-control header
module.exports.prepareErrorCacheControl(),
// Handle the error in Sentry
sentry.errorHandler,
// Format the stack for the user
module.exports.prepareStack,
// Render the error using JSON format
Expand All @@ -307,8 +305,6 @@ module.exports.handleHTMLResponse = sentry => [
module.exports.prepareError,
// Add cache-control header
module.exports.prepareErrorCacheControl(cacheControlValues.private),
// Handle the error in Sentry
sentry.errorHandler,
// Format the stack for the user
module.exports.prepareStack
];

0 comments on commit d9d3868

Please sign in to comment.