Skip to content

Commit

Permalink
fix: Sentry config
Browse files Browse the repository at this point in the history
  • Loading branch information
tokebe committed Nov 10, 2023
1 parent 8c19c3a commit e7db685
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 77 deletions.
51 changes: 27 additions & 24 deletions src/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ module.exports = class Config {
windowMs: 1 * 60 * 1000, //1min
max: process.env.MAX_QUERIES_PER_MIN || 6000,
});
this.app.use("/", fastLimiter)
this.app.use("/", fastLimiter);
this.app.use("/v1/query", slowLimiter);
this.app.use("/v1/team/:team_name/query", medLimiter);
this.app.use("/v1/team/:smartapiID/query", medLimiter);
Expand All @@ -90,33 +90,36 @@ module.exports = class Config {
this.app.use("/v1/asyncquery", fastLimiter);
this.app.use("/v1/team/:teamName/asyncquery", fastLimiter);
this.app.use("/v1/smartapi/:smartapiID/asyncquery", fastLimiter);
this.app.use("/queues", fastLimiter)
this.app.use("/queues", fastLimiter);
}

setSentry() {
// use SENTRY_DSN environment variable
Sentry.init({
// dsn: "https://5297933ef0f6487c9fd66532bb1fcefe@o4505444772806656.ingest.sentry.io/4505449737420800",
integrations: [
// enable HTTP calls tracing
new Sentry.Integrations.Http({ tracing: true }),
// enable Express.js middleware tracing
new Sentry.Integrations.Express({ app: this.app }),
// Automatically instrument Node.js libraries and frameworks
...Sentry.autoDiscoverNodePerformanceMonitoringIntegrations(),
],
try {
Sentry.init({
integrations: [
// enable HTTP calls tracing
new Sentry.Integrations.Http({ tracing: true }),
// enable Express.js middleware tracing
new Sentry.Integrations.Express({ app: this.app }),
// Automatically instrument Node.js libraries and frameworks
...Sentry.autoDiscoverNodePerformanceMonitoringIntegrations(),
],

// Set tracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production
tracesSampleRate: process.env.EXPRESS_SAMPLE_RATE ? parseFloat(process.env.EXPRESS_SAMPLE_RATE) : 1.0,
environment: process.env.INSTANCE_ENV,
});

// RequestHandler creates a separate execution context, so that all
// transactions/spans/breadcrumbs are isolated across requests
this.app.use(Sentry.Handlers.requestHandler({ user: false }));
// TracingHandler creates a trace for every incoming request
this.app.use(Sentry.Handlers.tracingHandler());
// Set tracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production
tracesSampleRate: process.env.EXPRESS_SAMPLE_RATE ? parseFloat(process.env.EXPRESS_SAMPLE_RATE) : 1.0,
environment: process.env.INSTANCE_ENV,
});
// RequestHandler creates a separate execution context, so that all
// transactions/spans/breadcrumbs are isolated across requests
this.app.use(Sentry.Handlers.requestHandler({ user: false }));
// TracingHandler creates a trace for every incoming request
this.app.use(Sentry.Handlers.tracingHandler());
} catch (error) {
debug("Sentry init error. This does not affect execution.");
debug(error);
}
}
};
4 changes: 2 additions & 2 deletions src/controllers/async/asyncquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { Readable } = require("stream");
const chunker = require("stream-chunker");
const { parser } = require("stream-json");
const Assembler = require("stream-json/Assembler");
const Sentry = require("@sentry/node");
const { Telemetry } = require("@biothings-explorer/utils");
const ErrorHandler = require("../../middlewares/error.js");

exports.asyncquery = async (req, res, next, queueData, queryQueue) => {
Expand Down Expand Up @@ -157,7 +157,7 @@ exports.asyncqueryResponse = async (handler, callback_url, jobID = null, jobURL
console.error(e);

if (ErrorHandler.shouldHandleError(e)) {
Sentry.captureException(e);
Telemetry.captureException(e);
}

//shape error > will be handled below
Expand Down
80 changes: 53 additions & 27 deletions src/controllers/threading/taskHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,32 @@ const Sentry = require("@sentry/node");
const { ProfilingIntegration } = require("@sentry/profiling-node");

// use SENTRY_DSN environment variable
Sentry.init({
// dsn: "https://5297933ef0f6487c9fd66532bb1fcefe@o4505444772806656.ingest.sentry.io/4505449737420800",
integrations: [
// Automatically instrument Node.js libraries and frameworks
...Sentry.autoDiscoverNodePerformanceMonitoringIntegrations(),
new ProfilingIntegration(),
],
environment: process.env.INSTANCE_ENV,
debug: true,
normalizeDepth: 6,
maxBreadcrumbs: 500,
// Set tracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production
tracesSampleRate: process.env.THREAD_SAMPLE_RATE ? parseFloat(process.env.THREAD_SAMPLE_RATE) : 1.0,
profilesSampleRate: process.env.THREAD_PROFILE_RATE ? parseFloat(process.env.THREAD_PROFILE_RATE) : 1.0, // Profiling sample rate is relative to tracesSampleRate,
_experiments: {
maxProfileDurationMs: 6 * 60 * 1000, // max profiling duration of 6 minutes (technically "beta" feature)
},
});
try {
Sentry.init({
integrations: [
// Automatically instrument Node.js libraries and frameworks
...Sentry.autoDiscoverNodePerformanceMonitoringIntegrations(),
new ProfilingIntegration(),
// enable HTTP calls tracing
new Sentry.Integrations.Http({ tracing: true }),
],
environment: process.env.INSTANCE_ENV,
debug: true,
normalizeDepth: 6,
maxBreadcrumbs: 500,
// Set tracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production
tracesSampleRate: process.env.THREAD_SAMPLE_RATE ? parseFloat(process.env.THREAD_SAMPLE_RATE) : 1.0,
profilesSampleRate: process.env.THREAD_PROFILE_RATE ? parseFloat(process.env.THREAD_PROFILE_RATE) : 1.0, // Profiling sample rate is relative to tracesSampleRate,
_experiments: {
maxProfileDurationMs: 6 * 60 * 1000, // max profiling duration of 6 minutes (technically "beta" feature)
},
});
} catch (error) {
debug("Sentry init error. This does not affect execution.");
debug(error);
}

const runTask = async ({ req, route, port, job: { jobId, queueName } = {} }) => {
debug(`Worker thread ${threadId} beginning ${workerData.queue} task.`);
Expand All @@ -52,17 +58,37 @@ const runTask = async ({ req, route, port, job: { jobId, queueName } = {} }) =>
global.job = await queue.getJob(jobId);
}

const transaction = Sentry.startTransaction({ name: route });
transaction.setData("request", req.data.queryGraph);
Sentry.getCurrentHub().configureScope(scope => {
scope.clearBreadcrumbs();
scope.setSpan(transaction);
});
let transaction;
try {
const routeNames = {
query_v1: "EXEC /v1/query",
query_v1_by_api: "EXEC /v1/smartapi/:/query",
query_v1_by_team: "EXEC /v1/team/:/query",
asyncquery_status: "EXEC /v1/asyncquery_status",
asyncquery_v1: "EXEC /v1/asyncquery",
asyncquery_v1_by_api: "EXEC /v1/smartapi/:/asyncquery",
asyncquery_v1_by_team: "EXEC /v1/team/:/asyncquery",
};
transaction = Sentry.startTransaction({ name: routeNames[route] });
transaction.setData("request", req.data.queryGraph);
Sentry.getCurrentHub().configureScope(scope => {
scope.clearBreadcrumbs();
scope.setSpan(transaction);
});
} catch (error) {
debug("Sentry transaction start error. This does not affect execution.");
debug(error);
}

const completedTask = await tasks[route](req);
await Promise.all(global.cachingTasks);

transaction.finish();
try {
transaction.finish();
} catch (error) {
debug("Sentry transaction finish error. This does not affect execution.");
debug(error);
}

debug(`Worker thread ${threadId} completed ${workerData.queue} task.`);

Expand Down
53 changes: 29 additions & 24 deletions src/middlewares/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,35 @@ class ErrorHandler {

setRoutes(app) {
// first pass through sentry
app.use(
Sentry.Handlers.errorHandler({
shouldHandleError(error) {
// Do not capture non-server errors
if (error.status && error.status < 500) {
return false;
}
if (error instanceof swaggerValidation.InputValidationError || error.name === "InputValidationError") {
return false;
}
if (
error instanceof QueryGraphHandler.InvalidQueryGraphError ||
error.stack.includes("InvalidQueryGraphError") ||
error.name === "InvalidQueryGraphError"
) {
return false;
}
if (error.name === "QueryAborted") {
return false;
}
return true;
},
}),
);
try {
app.use(
Sentry.Handlers.errorHandler({
shouldHandleError(error) {
// Do not capture non-server errors
if (error.status && error.status < 500) {
return false;
}
if (error instanceof swaggerValidation.InputValidationError || error.name === "InputValidationError") {
return false;
}
if (
error instanceof QueryGraphHandler.InvalidQueryGraphError ||
error.stack.includes("InvalidQueryGraphError") ||
error.name === "InvalidQueryGraphError"
) {
return false;
}
if (error.name === "QueryAborted") {
return false;
}
return true;
},
}),
);
} catch (error) {
debug("Sentry express config error. This does not affect execution.");
debug(error);
}

app.use((error, req, res, next) => {
const json = {
Expand Down

0 comments on commit e7db685

Please sign in to comment.