Skip to content

Commit

Permalink
fix: console log uncaught exceptions (#159)
Browse files Browse the repository at this point in the history
sdk should stdout the exception before exit
  • Loading branch information
wrn14897 authored Jun 25, 2024
1 parent 6b6ddd2 commit d332c32
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/khaki-melons-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@hyperdx/instrumentation-exception': patch
'@hyperdx/node-opentelemetry': patch
---

fix: console log uncaught exceptions
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { consoleSandbox } from '@sentry/utils';
import { diag } from '@opentelemetry/api';

const DEFAULT_SHUTDOWN_TIMEOUT = 2000;
Expand All @@ -21,7 +22,10 @@ export async function logAndExitProcess(
error: Error,
forceFlush: () => Promise<void>,
): Promise<void> {
diag.error('Exiting process due to fatal error', error);
consoleSandbox(() => {
// eslint-disable-next-line no-console
console.error(error);
});

try {
await Promise.race([
Expand Down
39 changes: 39 additions & 0 deletions packages/node-opentelemetry/examples/dummy_hardcrash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const express = require('express');
const HyperDX = require('../build/src');

HyperDX.init({
apiKey: '',
service: 'hard-crash',
});

// Crash immediately
har();

// unhandledRejection
// new Promise((resolve, reject) => {
// reject(new Error('🦄🦄🦄🦄'));
// });

const PORT = parseInt(process.env.PORT || '7788');
const app = express();

app.get('/uncaught', async (req, res) => {
setTimeout(() => {
throw new Error('💥💥💥💥');
}, 2000);
res.send('Uncaught exception in 2 seconds');
});

app.get('/crash', (req, res) => {
throw new Error('🧨🧨🧨🧨');
});

app.get('/', (req, res) => {
res.send('Hello World!');
});

// HyperDX.setupExpressErrorHandler(app);

app.listen(PORT, () => {
console.log(`Listening for requests on http://localhost:${PORT}`);
});
35 changes: 35 additions & 0 deletions packages/node-opentelemetry/examples/dummy_redis4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const express = require('express');
const Redis = require('redis');
const HyperDX = require('../build/src');

HyperDX.init({
apiKey: '',
service: 'dummy_redis4',
});

const PORT = parseInt(process.env.PORT || '7788');
const app = express();

app.use(express.json());
app.get('/', async (req, res) => {
const client = await Redis.createClient({
host: 'localhost',
port: 6379,
}).connect();
// await redis.commandsExecutor('set', 'foo', 'bar');
await client.set('foo1', 'bar1');
await client.get('foo1');
// await redis.sendCommand(['SET', 'foo1', 'bar1']);
// await redis.sendCommand(['GET', 'foo1']);
// const [setKeyReply, otherKeyValue] = await client
// .multi()
// .set('key', 'value')
// .get('another-key')
// .exec(); // ['OK', 'another-value']
await client.disconnect();
res.send('Hello World!!!');
});

app.listen(PORT, () => {
console.log(`Listening for requests on http://localhost:${PORT}`);
});

0 comments on commit d332c32

Please sign in to comment.