-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New Koa configuration #1804
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
New Koa configuration #1804
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
622b381
New Koa configuration
arthurM2x c8cbcd3
Update src/collections/_documentation/performance-monitoring/configur…
arthurM2x c770779
Update src/collections/_documentation/performance-monitoring/configur…
arthurM2x 20894a5
Update src/collections/_documentation/performance-monitoring/configur…
arthurM2x File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
src/collections/_documentation/performance-monitoring/configuration/koa.md
This file contains hidden or 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,103 @@ | ||
### Koa | ||
|
||
Creates and attach a transaction to each context | ||
|
||
```javascript | ||
const Sentry = require('@sentry/node') | ||
const { Span } = require('@sentry/apm') | ||
const Koa = require('koa') | ||
const app = new Koa() | ||
const domain = require('domain') | ||
|
||
Sentry.init({ | ||
dsn: "___PUBLIC_DSN___", | ||
tracesSampleRate: 0.25 | ||
}) | ||
|
||
// not mandatory, but adding domains do help a lot with breadcrumbs | ||
const requestHandler = (ctx, next) => { | ||
return new Promise((resolve, reject) => { | ||
const local = domain.create() | ||
local.add(ctx) | ||
local.on('error', (err) => { | ||
ctx.status = err.status || 500 | ||
ctx.body = err.message | ||
ctx.app.emit('error', err, ctx) | ||
}) | ||
local.run(async() => { | ||
Sentry.getCurrentHub().configureScope(scope => | ||
scope.addEventProcessor((event) => Sentry.Handlers.parseRequest(event, ctx.request, {user: false})) | ||
) | ||
await next() | ||
resolve() | ||
}) | ||
}) | ||
} | ||
|
||
// this tracing middleware creates a transaction per request | ||
const tracingMiddleWare = async (ctx, next) => { | ||
// captures span of upstream app | ||
const sentryTraceId = ctx.request.get('sentry-trace') | ||
let traceId | ||
let parentSpanId | ||
if (sentryTraceId) { | ||
const span = Span.fromTraceparent(sentryTraceId) | ||
if (span) { | ||
traceId = span.traceId | ||
parentSpanId = span.parentSpanId | ||
} | ||
} | ||
const transaction = Sentry.startTransaction({ | ||
name: `${ctx.method} ${ctx.url}`, | ||
op: 'http.server', | ||
parentSpanId, | ||
traceId, | ||
}) | ||
ctx.__sentry_transaction = transaction | ||
await next() | ||
|
||
// if using koa router, a nicer way to capture transaction using the matched route | ||
if (ctx._matchedRoute) { | ||
const mountPath = ctx.mountPath || '' | ||
transaction.setName(`${ctx.method} ${mountPath}${ctx._matchedRoute}`) | ||
} | ||
transaction.setHttpStatus(ctx.status) | ||
transaction.finish() | ||
} | ||
|
||
app.use(requestHandler) | ||
app.use(tracingMiddleWare) | ||
|
||
// usual error handler | ||
app.on('error', (err, ctx) => { | ||
Sentry.withScope(function (scope) { | ||
scope.addEventProcessor(function (event) { | ||
return Sentry.Handlers.parseRequest(event, ctx.request) | ||
}) | ||
Sentry.captureException(err) | ||
}) | ||
}) | ||
// the rest of your app | ||
``` | ||
|
||
#### Subsequent manual child transactions | ||
|
||
The following example creates a transaction for a part of the code that contains an expensive operation, and sends the result to Sentry, you will need to use the transaction stored in the context | ||
|
||
```javascript | ||
const myMiddleware = async (ctx, next) => { | ||
let span | ||
const transaction = ctx.__sentry_transaction | ||
if (transaction) { | ||
span = transaction.startChild({ | ||
description: route, | ||
op: 'myMiddleware', | ||
}) | ||
} | ||
await myExpensiveOperation() | ||
if (span) { | ||
span.finish() | ||
} | ||
return next() | ||
} | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.