Automatic Instrumentation
By default, Sentry error events will not get trace context unless you configure the scope with the transaction, as illustrated in the example below.
If you’re adopting Performance in a high-throughput environment, we recommend testing prior to deployment to ensure that your service’s performance characteristics maintain expectations.
Copied
const Sentry = require("@sentry/node");
const Tracing = require("@sentry/tracing");
// Note: You MUST import the package for tracing to work
const http = require("http");
Sentry.init({
dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
integrations: [
// enable HTTP calls tracing
new Sentry.Integrations.Http({ tracing: true }),
],
// We recommend adjusting this value in production, or using tracesSampler
// for finer control
tracesSampleRate: 1.0,
});
const transaction = Sentry.startTransaction({
op: "transaction",
name: "My Transaction",
});
// Note that we set the transaction as the span on the scope.
// This step makes sure that if an error happens during the lifetime of the transaction
// the transaction context will be attached to the error event
Sentry.configureScope(scope => {
scope.setSpan(transaction);
});
let request;
try {
// this should generate an http span
request = http.get("http://sentry.io", res => {
console.log(`STATUS: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
});
// this error event should have trace context
foo();
} catch (err) {
Sentry.captureException(err);
}
request.on("close", () => {
transaction.finish();
});
Retrieving a Transaction
In cases where you want to attach Spans to an already ongoing Transaction you can use Sentry.getCurrentHub().getScope().getTransaction()
. This function will return a Transaction
in case there is a running Transaction otherwise it returns undefined
. If you are using our Express integration by default we attach the Transaction to the Scope. So you could do something like this:
Copied
app.get("/success", function successHandler(req, res) {
const transaction = Sentry.getCurrentHub()
.getScope()
.getTransaction();
if (transaction) {
let span = transaction.startChild({
op: "encode",
description: "parseAvatarImages",
});
// Do something
span.finish();
}
res.status(200).end();
});
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) to suggesting an update ("yeah, this would be better").
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) to suggesting an update ("yeah, this would be better").
- Package:
- npm:@sentry/node
- Version:
- 7.34.0
- Repository:
- https://github.com/getsentry/sentry-javascript