-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(store-indexer): setup Sentry middleware in indexer (#2054)
- Loading branch information
Showing
5 changed files
with
571 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@latticexyz/store-indexer": patch | ||
--- | ||
|
||
Added a Sentry middleware and `SENTRY_DNS` environment variable to the postgres indexer. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import * as Sentry from "@sentry/node"; | ||
import { ProfilingIntegration } from "@sentry/profiling-node"; | ||
import { stripUrlQueryAndFragment } from "@sentry/utils"; | ||
import { debug } from "./debug"; | ||
import Koa from "koa"; | ||
|
||
Sentry.init({ | ||
dsn: process.env.SENTRY_DSN, | ||
integrations: [ | ||
// Automatically instrument Node.js libraries and frameworks | ||
...Sentry.autoDiscoverNodePerformanceMonitoringIntegrations(), | ||
new ProfilingIntegration(), | ||
], | ||
// Performance Monitoring | ||
tracesSampleRate: 1.0, | ||
// Set sampling rate for profiling - this is relative to tracesSampleRate | ||
profilesSampleRate: 1.0, | ||
}); | ||
|
||
const requestHandler: Koa.Middleware = (ctx, next) => { | ||
return new Promise<void>((resolve, reject) => { | ||
Sentry.runWithAsyncContext(async () => { | ||
const hub = Sentry.getCurrentHub(); | ||
hub.configureScope((scope) => | ||
scope.addEventProcessor((event) => | ||
Sentry.addRequestDataToEvent(event, ctx.request, { | ||
include: { | ||
user: false, | ||
}, | ||
}) | ||
) | ||
); | ||
|
||
try { | ||
await next(); | ||
} catch (err) { | ||
reject(err); | ||
} | ||
resolve(); | ||
}); | ||
}); | ||
}; | ||
|
||
// This tracing middleware creates a transaction per request | ||
const tracingMiddleWare: Koa.Middleware = async (ctx, next) => { | ||
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")); | ||
} | ||
|
||
const transaction = Sentry.startTransaction({ | ||
name: `${reqMethod} ${reqUrl}`, | ||
op: "http.server", | ||
...traceparentData, | ||
}); | ||
|
||
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); | ||
}); | ||
|
||
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 you're using koa router, set the matched route as transaction name | ||
if (ctx._matchedRoute) { | ||
const mountPath = ctx.mountPath || ""; | ||
transaction.setName(`${reqMethod} ${mountPath}${ctx._matchedRoute}`); | ||
} | ||
|
||
transaction.setHttpStatus(ctx.status); | ||
transaction.finish(); | ||
}); | ||
}); | ||
|
||
await next(); | ||
}; | ||
|
||
const errorHandler: Koa.Middleware = async (ctx, next) => { | ||
try { | ||
await next(); | ||
} catch (err) { | ||
Sentry.withScope((scope) => { | ||
scope.addEventProcessor((event) => { | ||
return Sentry.addRequestDataToEvent(event, ctx.request); | ||
}); | ||
Sentry.captureException(err); | ||
}); | ||
throw err; | ||
} | ||
}; | ||
|
||
export const registerSentryMiddlewares = (server: Koa): void => { | ||
debug("Registering Sentry middlewares"); | ||
|
||
server.use(errorHandler); | ||
server.use(requestHandler); | ||
server.use(tracingMiddleWare); | ||
}; |
Oops, something went wrong.