These examples should be used as guidance when configuring Sentry functionality within a project.
Sentry.captureException(error)
to capture an exception and log the error in Sentry.Sentry.startSpan
function to create a spanname
and op
properties should be meaningful for the activities in the call.function TestComponent() {
const handleTestButtonClick = () => {
// Create a transaction/span to measure performance
Sentry.startSpan(
{
op: "ui.click",
name: "Test Button Click",
},
(span) => {
const value = "some config";
const metric = "some metric";
// Metrics can be added to the span
span.setAttribute("config", value);
span.setAttribute("metric", metric);
doSomething();
},
);
};
return (
<button type="button" onClick={handleTestButtonClick}>
Test Sentry
</button>
);
}
name
and op
properties should be meaningful for the activities in the call.async function fetchUserData(userId) {
return Sentry.startSpan(
{
op: "http.client",
name: `GET /api/users/${userId}`,
},
async () => {
const response = await fetch(`/api/users/${userId}`);
const data = await response.json();
return data;
},
);
}
import * as Sentry from "@sentry/nextjs"
Sentry.init({ _experiments: { enableLogs: true } })
const { logger } = Sentry
instrumentation-client.ts
, the server initialization is in sentry.edge.config.ts
and the edge initialization is in sentry.server.config.ts
import * as Sentry from "@sentry/nextjs"
to reference Sentry functionalityimport * as Sentry from "@sentry/nextjs";
Sentry.init({
dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
_experiments: {
enableLogs: true,
},
});
Sentry.init({
dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
integrations: [
// send console.log, console.error, and console.warn calls as logs to Sentry
Sentry.consoleLoggingIntegration({ levels: ["log", "error", "warn"] }),
],
});
logger.fmt
is a template literal function that should be used to bring variables into the structured logs.
logger.trace("Starting database connection", { database: "users" });
logger.debug(logger.fmt`Cache miss for user: ${userId}`);
logger.info("Updated profile", { profileId: 345 });
logger.warn("Rate limit reached for endpoint", {
endpoint: "/api/results/",
isEnterprise: false,
});
logger.error("Failed to process payment", {
orderId: "order_123",
amount: 99.99,
});
logger.fatal("Database connection pool exhausted", {
database: "users",
activeConnections: 100,
});