Skip to content

Commit

Permalink
upgraded sentry utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Leask committed Mar 10, 2024
1 parent 13b6ef6 commit 5676baa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
29 changes: 20 additions & 9 deletions lib/tracing.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// Reference: https://docs.sentry.io/platforms/node/guides/koa/
// https://github.com/getsentry/sentry-javascript/blob/develop/MIGRATION.md

import { callosum, sentinel, utilitas } from 'utilitas';

const _NEED = ['@sentry/node', '@sentry/tracing', 'domain'];
const _NEED = ['@sentry/node', '@sentry/profiling-node', 'domain'];
const log = (str, opts) => utilitas.log(str, import.meta.url, opts);

let sentry, tracing, domain;
let sentry, utils, domain;

// not mandatory, but adding domains does help a lot with breadcrumbs
const requestHandler = (ctx, next) => {
Expand All @@ -32,11 +33,11 @@ const requestHandler = (ctx, next) => {
// this tracing middleware creates a transaction per request
const tracingMiddleWare = async (ctx, next) => {
const reqMethod = (ctx.method || '').toUpperCase();
const reqUrl = ctx.url && tracing.stripUrlQueryAndFragment(ctx.url);
const reqUrl = ctx.url && utils.stripUrlQueryAndFragment(ctx.url);
// connect to trace of upstream app
let traceparentData;
if (ctx.request.get('sentry-trace')) {
traceparentData = tracing.extractTraceparentData(
traceparentData = utils.extractTraceparentData(
ctx.request.get('sentry-trace')
);
}
Expand Down Expand Up @@ -66,10 +67,20 @@ const errorHandler = (err, ctx) => {

const init = async (options, app) => {
if (options) {
sentry || (sentry = await sentinel.init(options));
if (app && (!tracing || !domain)) {
if (!sentry) {
const { nodeProfilingIntegration } = await utilitas.need(
'@sentry/profiling-node', { raw: true }
);
sentry = await sentinel.init({
tracesSampleRate: 1.0, profilesSampleRate: 1.0, ...options,
integrations: [
nodeProfilingIntegration(), ...options.integrations || [],
],
});
}
if (app && (!utils || !domain)) {
/* extractTraceparentData, stripUrlQueryAndFragment, Span */
tracing = await utilitas.need('@sentry/tracing', { raw: true });
utils = await utilitas.need('@sentry/utils', { raw: true });
domain = await utilitas.need('domain', { raw: true });
app.use(requestHandler);
app.use(tracingMiddleWare);
Expand All @@ -78,9 +89,9 @@ const init = async (options, app) => {
}
}
assert(sentry, 'Sentry has not been initialized.', 501);
assert(!app || (tracing && domain),
assert(!app || (utils && domain),
'Sentry Tracing has not been initialized.', 501);
return { sentry, tracing, domain };
return { sentry, utils, domain };
};

export {
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@
"koa-static": "^5.0.0",
"koa-useragent": "^4.1.0",
"multer": "^1.4.4",
"utilitas": "^1995.2.88"
"utilitas": "^1995.3.2"
},
"devDependencies": {
"@google-cloud/storage": "^7.8.0",
"@sentry/node": "^7.106.0",
"@sentry/tracing": "^7.106.0",
"@sentry/profiling-node": "^7.106.0",
"@sentry/utils": "^7.106.0",
"domain": "^0.0.1",
"form-data": "^4.0.0",
"ioredis": "^5.3.2",
Expand Down

0 comments on commit 5676baa

Please sign in to comment.