-
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.
refactor(store-indexer): clean up middleware (#2163)
- Loading branch information
Showing
14 changed files
with
196 additions
and
213 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
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
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
File renamed without changes.
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,37 @@ | ||
import { Middleware } from "koa"; | ||
|
||
type HealthcheckOptions = { | ||
isHealthy?: () => boolean; | ||
isReady?: () => boolean; | ||
}; | ||
|
||
/** | ||
* Middleware to add Kubernetes healthcheck endpoints | ||
*/ | ||
export function healthcheck({ isHealthy, isReady }: HealthcheckOptions = {}): Middleware { | ||
return async function healthcheckMiddleware(ctx, next): Promise<void> { | ||
if (ctx.path === "/healthz") { | ||
if (isHealthy == null || isHealthy()) { | ||
ctx.status = 200; | ||
ctx.body = "healthy"; | ||
} else { | ||
ctx.status = 503; | ||
ctx.body = "not healthy"; | ||
} | ||
return; | ||
} | ||
|
||
if (ctx.path === "/readyz") { | ||
if (isReady == null || isReady()) { | ||
ctx.status = 200; | ||
ctx.body = "ready"; | ||
} else { | ||
ctx.status = 503; | ||
ctx.body = "not ready"; | ||
} | ||
return; | ||
} | ||
|
||
await next(); | ||
}; | ||
} |
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,12 @@ | ||
import { Middleware } from "koa"; | ||
|
||
export function helloWorld(): Middleware { | ||
return async function helloWorldMiddleware(ctx, next): Promise<void> { | ||
if (ctx.path === "/") { | ||
ctx.status = 200; | ||
ctx.body = "emit HelloWorld();"; | ||
return; | ||
} | ||
await next(); | ||
}; | ||
} |
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,101 @@ | ||
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"; | ||
import compose from "koa-compose"; | ||
|
||
export function errorHandler(): Koa.Middleware { | ||
return async function errorHandlerMiddleware(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 function requestHandler(): Koa.Middleware { | ||
return async function requestHandlerMiddleware(ctx, next) { | ||
await Sentry.runWithAsyncContext(async () => { | ||
const hub = Sentry.getCurrentHub(); | ||
hub.configureScope((scope) => | ||
scope.addEventProcessor((event) => | ||
Sentry.addRequestDataToEvent(event, ctx.request, { | ||
include: { | ||
user: false, | ||
}, | ||
}) | ||
) | ||
); | ||
await next(); | ||
}); | ||
}; | ||
} | ||
|
||
export function tracing(): Koa.Middleware { | ||
// creates a Sentry transaction per request | ||
return async function tracingMiddleware(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(); | ||
}; | ||
} | ||
|
||
export function sentry(dsn: string): Koa.Middleware { | ||
debug("Initializing Sentry"); | ||
Sentry.init({ | ||
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, | ||
}); | ||
|
||
return compose([errorHandler(), requestHandler(), tracing()]); | ||
} |
Oops, something went wrong.
whoops, duplicated this line on accident