Javascript Forum
As already announced in the Blog Post we were working on rewriting our SDKs to streamline the experience across different environments.
You can find an overview of the SDKs in the readme here: GitHub - getsentry/sentry-javascript: Official Sentry SDKs for JavaScript
You can identify them by the @sentry/*
namespace on NPM. The goal in this new lineup is to provide a more convenient interface and improved consistency between various JavaScript environments.
Updated Interface
import { init, captureMessage } from '@sentry/browser';
init({
dsn: '__DSN__',
// ...
});
captureMessage('Hello, world!');
Library minimal
A new feature of this SDK lineup is the minimal package. It allows library authors add support for a Sentry SDK without having to bundle the entire SDK or being dependent on a specific platform. If the library is included and a Sentry SDK is present, it will automagically work:
import * as Sentry from '@sentry/minimal';
// Add a breadcrumb for future events
Sentry.addBreadcrumb({
message: 'My Breadcrumb',
// ...
});
// Capture exceptions, messages or manual events
Sentry.captureMessage('Hello, world!');
Sentry.captureException(new Error('Good bye'));
Sentry.captureEvent({
message: 'Manual',
stacktrace: [
// ...
],
});
We hope to see library authors adopt this feature in the future to facilitate better error tracking across the ecosystem.
Scope concept
We introduced a new concept we called Scope
. A Scope
holds an isolated state of breadcrumbs, context and other metadata. raven-node
and raven-js
had a similar feature, ambiguously called âcontextâ. There always is a âdefaultâ Scope
which handles all the stuff as we did before you have to do nothing but we also support pushing new a Scope
if you ever want to have isolated context information lets say for example, each request that comes in in a node application should have itâs own Scope
.
To add extra
, tags
or user
to your event you have to call:
import * as Sentry from '@sentry/browser';
// Set user information, as well as tags and further extras
Sentry.configureScope(scope => {
scope.setExtra('battery', 0.7);
scope.setTag('user_mode', 'admin');
scope.setUser({ id: '4711' });
// scope.clear();
});
Sentry.captureMessage("Hello World!"); // This event contains all scope information from the global scope
If you want to have isolated information for only on specific event you can to:
import * as Sentry from '@sentry/browser';
Sentry.getDefaultHub().withScope(() => {
// We are here in an isolated new Scope, we inherited all the stuff from the parent
// but after this functions returns the Scope is popped and removed
Sentry.configureScope(scope => {
scope.setExtra('battery', 0.9); // We overwrite battery extra
});
Sentry.captureMessage("Hello World!"); // This will contain all scope info from before + battery is overwritten just for this message
});
ref: Tracking issue for @sentry/* SDKs ¡ Issue #1281 ¡ getsentry/sentry-javascript ¡ GitHub