Skip to content

Commit

Permalink
Sentry in dev mode removed
Browse files Browse the repository at this point in the history
  • Loading branch information
rzafari42 committed Jul 25, 2023
1 parent 29b3a88 commit 83c1508
Showing 1 changed file with 33 additions and 35 deletions.
68 changes: 33 additions & 35 deletions api/src/utils/sentry.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as Sentry from "@sentry/node";
import { stripUrlQueryAndFragment } from "@sentry/utils";
import config from "config";
import packageJson from "../../package.json";
import * as Sentry from '@sentry/node'
import { stripUrlQueryAndFragment } from '@sentry/utils'
import config from 'config'
import packageJson from '../../package.json'

Sentry.init({
dsn: config.sentryApi,
environment: process.env.NODE_ENV || "developpement",
environment: process.env.NODE_ENV || null,
release: `${packageJson.name}@${packageJson.version}`,

integrations: [
Expand All @@ -18,12 +18,12 @@ Sentry.init({
// We recommend adjusting this value in production, or using tracesSampler
// for finer control
tracesSampleRate: 1.0,
});
})

export const requestHandler = (ctx, next) => {
return new Promise((resolve, reject) => {
Sentry.runWithAsyncContext(async () => {
const hub = Sentry.getCurrentHub();
const hub = Sentry.getCurrentHub()
hub.configureScope((scope) =>
scope.addEventProcessor((event) =>
Sentry.addRequestDataToEvent(event, ctx.request, {
Expand All @@ -32,56 +32,54 @@ export const requestHandler = (ctx, next) => {
},
})
)
);
)

try {
await next();
await next()
} catch (err) {
reject(err);
reject(err)
}
resolve();
});
});
};
resolve()
})
})
}

// this tracing middleware creates a transaction per request
export const tracingMiddleWare = async (ctx, next) => {
const reqMethod = (ctx.method || "").toUpperCase();
const reqUrl = ctx.url && stripUrlQueryAndFragment(ctx.url);
const reqMethod = (ctx.method || '').toUpperCase()
const reqUrl = ctx.url && stripUrlQueryAndFragment(ctx.url)

// connect to trace of upstream app
let traceparentData;
if (ctx.request.get("sentry-trace")) {
traceparentData = Sentry.extractTraceparentData(
ctx.request.get("sentry-trace")
);
let traceparentData
if (ctx.request.get('sentry-trace')) {
traceparentData = Sentry.extractTraceparentData(ctx.request.get('sentry-trace'))
}

const transaction = Sentry.startTransaction({
name: `${reqMethod} ${reqUrl}`,
op: "http.server",
op: 'http.server',
...traceparentData,
});
})

ctx.__sentry_transaction = transaction;
ctx.__sentry_transaction = transaction

// We put the transaction on the scope so users can attach children to it
Sentry.getCurrentHub().configureScope((scope) => {
scope.setSpan(transaction);
});
scope.setSpan(transaction)
})

ctx.res.on("finish", () => {
ctx.res.on('finish', () => {
// Push `transaction.finish` to the next event loop so open spans have a chance to finish before the transaction closes
setImmediate(() => {
// if using koa router, a nicer way to capture transaction using the matched route
if (ctx._matchedRoute) {
const mountPath = ctx.mountPath || "";
transaction.setName(`${reqMethod} ${mountPath}${ctx._matchedRoute}`);
const mountPath = ctx.mountPath || ''
transaction.setName(`${reqMethod} ${mountPath}${ctx._matchedRoute}`)
}
transaction.setHttpStatus(ctx.status);
transaction.finish();
});
});
transaction.setHttpStatus(ctx.status)
transaction.finish()
})
})

await next();
};
await next()
}

0 comments on commit 83c1508

Please sign in to comment.