Away with your Errors - A Short Tutorial on Running Sentry with Deno
Geert-Jan Zwiers
Posted on June 28, 2022
Cover image credit: Arc'blroth; image link; CC-BY-SA-4.0
Recently, I decided to see if one can run the Sentry SDK for JavaScript on Deno. If you have not heard of Sentry, it is a tool for tracking errors and can be especially useful when working in complex environments such as cloud-native apps with multiple containerized services. Sentry can be used to capture errors and warnings and send them to a centralized dashboard, providing an overview of what might be going awry at any given time.
Although there seems to be no dedicated Sentry module for Deno just yet, Deno comes with the option to run a program in Node compatibility mode. This mode allows running 'hybrid' programs, meaning you can install certain Node packages like you normally would with npm, and then use them alongside code written for Deno. Quite an impressive amount of Node programs can work this way, though not everything. Mostly, it is meant to ease the transition to Deno for Node developers. In this tutorial, I will show how to use Deno's Node compatibility mode to run the Sentry SDK and send an application error to the Sentry dashboard.
Requirements
- Deno installed on your machine. You can get it from deno.land.
- Node and npm installed on your machine. You can get them from nodejs.org
- A Sentry account. They have a free plan available for developers which will do nicely for the purposes of this tutorial. They also have a trial for the more advanced plans in case you're interested in exploring that.
Project Setup
Start by making a new directory for the program. Because we want to use the Node compatibility mode with Deno, we do need to install the npm packages that we want to use, so in the new directory run npm init -y
for a default package.json
and npm install @sentry/node @sentry/tracing
to install the Sentry SDK. You should now have a directory with a package.json
, package-lock.json
and the node_modules
directory containing the node dependencies.
The Code
Create a new file main.mjs
. This will be a JavaScript module in which we will test that the Sentry integration works. First, we import Sentry from the npm package like we would in Node:
import * as Sentry from "@sentry/node";
Sentry recommends running Sentry.init
as early on as possible in the program, so we will do that next. For this we need to login to Sentry, go to Projects and click Create Project. Choose Node.js as the platform (Deno is not yet listed as an option here) and give a name to the project. On the next page Sentry will show a quickstart, copy the following lines into main.mjs
:
Sentry.init({
dsn: "<my-sentry-dsn>",
tracesSampleRate: 1.0,
});
Replace <my-sentry-dsn>
with the actual Data Source Name (DSN) shown in the quickstart. This string is used by Sentry to figure out where to send events to.
Now we can add some code to generate a test event and see if it is sent to Sentry successfully. Change the contents of main.mjs
to the following:
import * as Sentry from "@sentry/node";
import * as log from "https://deno.land/std@0.145.0/log/mod.ts";
Sentry.init({
dsn: "<my-sentry-dsn>",
tracesSampleRate: 1.0,
});
async function testEvent() {
log.info("Sending test event to Sentry.");
try {
throw new Error("Nope.");
} catch (e) {
Sentry.captureException(e);
await Sentry.flush();
}
}
await testEvent();
Notice the log
import from the Deno standard library (std
).
To understand what is happening, it is important to know that Deno and Node use different systems to resolve JavaScript modules. Node relies heavily on the node_modules directory while Deno utilizes URLs. With Node compatibility mode it is possible to use both 'styles' together in a file.
When you're ready, run the following command from a terminal:
deno run --compat --allow-read --allow-env --allow-net --unstable main.mjs
The important thing here is the --compat
option, which enables the Node compatibility mode. If you are interested in how this works behind the scenes, you can find more info on it in the deno manual.
When you run the command, you will probably see a few warning messages in the terminal:
Not implemented: process.on("uncaughtException")
Not implemented: process.on("unhandledRejection")
These are Node methods of the process
global that are not available in Deno compatibility mode (yet). This does mean that certain parts of the Sentry SDK are not fully compatible with Deno at the moment, but luckily it does not matter for the example shown in this tutorial. After the program has run, go to the Sentry dashboard, refresh the page (or enable real-time updates) and you should see the error appear:
This short tutorial covered how to use the Sentry SDK for JavaScript in a Deno program and run it in Node compatibility mode. This shows how many Node programs can be re-used in Deno, which is very beneficial to speeding up development until the Deno ecosystem has grown enough that it provides a module of its own.
Posted on June 28, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 29, 2024