Skip to content

feat(node): Add Hapi Integration #9539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 18, 2023
Merged

feat(node): Add Hapi Integration #9539

merged 5 commits into from
Dec 18, 2023

Conversation

onurtemizkan
Copy link
Collaborator

@onurtemizkan onurtemizkan commented Nov 13, 2023

Resolves: #9344

Adds a new node integration for Hapi framework.

Also exports a Hapi plugin to capture errors when the tracing instrumentation from node-experimental is used.

Can be used with node-experimental (Sample Error Event) like:

const Sentry = require('@sentry/node-experimental');

Sentry.init({
  dsn: '__DSN__',
  tracesSampleRate: 1.0,
});

const Hapi = require('@hapi/hapi');

const init = async () => {
    const server = Hapi.server({
        port: 3000,
        host: 'localhost'
    });

    await server.register(Sentry.hapiErrorPlugin)

    server.route({
        method: 'GET',
        path: '/',
        handler: (request, h) => {
            throw new Error('My Hapi Sentry error!');
        }
    });

    await server.start();
};

Also can be used from @sentry/node with tracing (Errored Transaction, Successful Transaction) and error tracking (Event) like:

'use strict';

const Sentry = require('@sentry/node');
const Hapi = require('@hapi/hapi');


const init = async () => {
    const server = Hapi.server({
        port: 3000,
        host: 'localhost'
    });

    Sentry.init({
      dsn: '__DSN__',
      tracesSampleRate: 1.0,
      integrations: [
        new Sentry.Integrations.Hapi({server}),
      ],
      debug: true,
    });

    server.route({
        method: 'GET',
        path: '/',
        handler: (request, h) => {
            return 'Hello World!';
        }
    });

    await server.start();
};

@onurtemizkan onurtemizkan self-assigned this Nov 13, 2023
@@ -0,0 +1,163 @@
import type { Boom } from '@hapi/boom';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not/cannot use types like these, as then we need to add hapi as a dependency 😬 let's just inline the types we need in here!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍 21beab3

Comment on lines +68 to +99
const sentryRequest = https.request(
sentryIngestUrl,
{ headers: proxyRequest.headers, method: proxyRequest.method },
sentryResponse => {
sentryResponse.addListener('data', (chunk: Buffer) => {
proxyResponse.write(chunk, 'binary');
sentryResponseChunks.push(chunk);
});

sentryResponse.addListener('end', () => {
eventCallbackListeners.forEach(listener => {
const rawSentryResponseBody = Buffer.concat(sentryResponseChunks).toString();

const data: SentryRequestCallbackData = {
envelope: parseEnvelope(proxyRequestBody, new TextEncoder(), new TextDecoder()),
rawProxyRequestBody: proxyRequestBody,
rawSentryResponseBody,
sentryResponseStatusCode: sentryResponse.statusCode,
};

listener(Buffer.from(JSON.stringify(data)).toString('base64'));
});
proxyResponse.end();
});

sentryResponse.addListener('error', err => {
throw err;
});

proxyResponse.writeHead(sentryResponse.statusCode || 500, sentryResponse.headers);
},
);

Check failure

Code scanning / CodeQL

Server-side request forgery

The [URL](1) of this request depends on a [user-provided value](2).
Copy link
Contributor

github-actions bot commented Nov 17, 2023

size-limit report 📦

Path Size
@sentry/browser (incl. Tracing, Replay) - Webpack (gzipped) 66.02 KB (0%)
@sentry/browser (incl. Tracing, Replay) - Webpack with treeshaking flags (gzipped) 56.21 KB (0%)
@sentry/browser (incl. Tracing) - Webpack (gzipped) 31.19 KB (0%)
@sentry/browser - Webpack (gzipped) 21.4 KB (0%)
@sentry/browser (incl. Tracing, Replay) - ES6 CDN Bundle (gzipped) 62.65 KB (0%)
@sentry/browser (incl. Tracing) - ES6 CDN Bundle (gzipped) 29.47 KB (0%)
@sentry/browser - ES6 CDN Bundle (gzipped) 21.55 KB (0%)
@sentry/browser (incl. Tracing, Replay) - ES6 CDN Bundle (minified & uncompressed) 197.27 KB (0%)
@sentry/browser (incl. Tracing) - ES6 CDN Bundle (minified & uncompressed) 89.15 KB (0%)
@sentry/browser - ES6 CDN Bundle (minified & uncompressed) 64.12 KB (0%)
@sentry/browser (incl. Tracing) - ES5 CDN Bundle (gzipped) 32.12 KB (0%)
@sentry/react (incl. Tracing, Replay) - Webpack (gzipped) 66.43 KB (0%)
@sentry/react - Webpack (gzipped) 21.45 KB (0%)
@sentry/nextjs Client (incl. Tracing, Replay) - Webpack (gzipped) 83.16 KB (0%)
@sentry/nextjs Client - Webpack (gzipped) 48.32 KB (0%)
@sentry-internal/feedback - Webpack (gzipped) 16.19 KB (0%)

@onurtemizkan onurtemizkan force-pushed the onur/hapi-integration branch 3 times, most recently from 0729c7f to c5c9d9c Compare November 18, 2023 06:05
@onurtemizkan onurtemizkan marked this pull request as ready for review November 18, 2023 06:24
}

function sendErrorToSentry(errorData: object): void {
captureException(errorData, scope => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self: if we merge #9590 first, update this later!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, merged this in yet, so we can refactor this to this instead:

captureException(errorData, { 
  mechanism: {
        type: 'hapi',
        handled: false,
        data: {
          function: 'hapiErrorPlugin',
        },
      }
});


export const hapiErrorPlugin = {
name: 'SentryHapiErrorPlugin',
version: '0.0.1',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's make this SDK_VERSION, which we can import from core?

/* eslint-disable @typescript-eslint/no-empty-interface */
/* eslint-disable @typescript-eslint/no-namespace */

// Vendored and simplified from:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we possibly add the versions here we vendored from, may make future updating easier? 😅

Copy link
Member

@mydea mydea left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some small comments, but overall this looks great! nice E2E tests 🎉

@onurtemizkan
Copy link
Collaborator Author

Thanks for the review @mydea. I updated the PR 👍

Copy link
Member

@mydea mydea left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

back from my vacation, this looks great, thank you!

@mydea mydea merged commit c0c5eca into develop Dec 18, 2023
@mydea mydea deleted the onur/hapi-integration branch December 18, 2023 10:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Supportability of the Hapi Framework Integration
2 participants