123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- Object.defineProperty(exports, '__esModule', { value: true });
- const core = require('@sentry/core');
- const utils = require('@sentry/utils');
- function isResponseObject(response) {
- return response && (response ).statusCode !== undefined;
- }
- function isBoomObject(response) {
- return response && (response ).isBoom !== undefined;
- }
- function isErrorEvent(event) {
- return event && (event ).error !== undefined;
- }
- function sendErrorToSentry(errorData) {
- core.captureException(errorData, {
- mechanism: {
- type: 'hapi',
- handled: false,
- data: {
- function: 'hapiErrorPlugin',
- },
- },
- });
- }
- const hapiErrorPlugin = {
- name: 'SentryHapiErrorPlugin',
- version: core.SDK_VERSION,
-
- register: async function (serverArg) {
- const server = serverArg ;
- server.events.on('request', (request, event) => {
-
- const transaction = core.getActiveTransaction();
- if (request.response && isBoomObject(request.response)) {
- sendErrorToSentry(request.response);
- } else if (isErrorEvent(event)) {
- sendErrorToSentry(event.error);
- }
- if (transaction) {
- transaction.setStatus('internal_error');
- transaction.end();
- }
- });
- },
- };
- const hapiTracingPlugin = {
- name: 'SentryHapiTracingPlugin',
- version: core.SDK_VERSION,
-
- register: async function (serverArg) {
- const server = serverArg ;
- server.ext('onPreHandler', (request, h) => {
- const transaction = core.continueTrace(
- {
- sentryTrace: request.headers['sentry-trace'] || undefined,
- baggage: request.headers['baggage'] || undefined,
- },
- transactionContext => {
-
- return core.startTransaction({
- ...transactionContext,
- op: 'hapi.request',
- name: request.route.path,
- description: `${request.route.method} ${request.path}`,
- });
- },
- );
-
- core.getCurrentScope().setSpan(transaction);
- return h.continue;
- });
- server.ext('onPreResponse', (request, h) => {
-
- const transaction = core.getActiveTransaction();
- if (request.response && isResponseObject(request.response) && transaction) {
- const response = request.response ;
- response.header('sentry-trace', core.spanToTraceHeader(transaction));
- const dynamicSamplingContext = utils.dynamicSamplingContextToSentryBaggageHeader(
- core.getDynamicSamplingContextFromSpan(transaction),
- );
- if (dynamicSamplingContext) {
- response.header('baggage', dynamicSamplingContext);
- }
- }
- return h.continue;
- });
- server.ext('onPostHandler', (request, h) => {
-
- const transaction = core.getActiveTransaction();
- if (transaction) {
- if (request.response && isResponseObject(request.response)) {
- core.setHttpStatus(transaction, request.response.statusCode);
- }
- transaction.end();
- }
- return h.continue;
- });
- },
- };
- const INTEGRATION_NAME = 'Hapi';
- const _hapiIntegration = ((options = {}) => {
- const server = options.server ;
- return {
- name: INTEGRATION_NAME,
- setupOnce() {
- if (!server) {
- return;
- }
- utils.fill(server, 'start', (originalStart) => {
- return async function () {
- await this.register(hapiTracingPlugin);
- await this.register(hapiErrorPlugin);
- const result = originalStart.apply(this);
- return result;
- };
- });
- },
- };
- }) ;
- const hapiIntegration = core.defineIntegration(_hapiIntegration);
- const Hapi = core.convertIntegrationFnToClass(INTEGRATION_NAME, hapiIntegration);
- exports.Hapi = Hapi;
- exports.hapiErrorPlugin = hapiErrorPlugin;
- exports.hapiIntegration = hapiIntegration;
- exports.hapiTracingPlugin = hapiTracingPlugin;
|