index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. const core = require('@sentry/core');
  3. const utils = require('@sentry/utils');
  4. function isResponseObject(response) {
  5. return response && (response ).statusCode !== undefined;
  6. }
  7. function isBoomObject(response) {
  8. return response && (response ).isBoom !== undefined;
  9. }
  10. function isErrorEvent(event) {
  11. return event && (event ).error !== undefined;
  12. }
  13. function sendErrorToSentry(errorData) {
  14. core.captureException(errorData, {
  15. mechanism: {
  16. type: 'hapi',
  17. handled: false,
  18. data: {
  19. function: 'hapiErrorPlugin',
  20. },
  21. },
  22. });
  23. }
  24. const hapiErrorPlugin = {
  25. name: 'SentryHapiErrorPlugin',
  26. version: core.SDK_VERSION,
  27. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  28. register: async function (serverArg) {
  29. const server = serverArg ;
  30. server.events.on('request', (request, event) => {
  31. // eslint-disable-next-line deprecation/deprecation
  32. const transaction = core.getActiveTransaction();
  33. if (request.response && isBoomObject(request.response)) {
  34. sendErrorToSentry(request.response);
  35. } else if (isErrorEvent(event)) {
  36. sendErrorToSentry(event.error);
  37. }
  38. if (transaction) {
  39. transaction.setStatus('internal_error');
  40. transaction.end();
  41. }
  42. });
  43. },
  44. };
  45. const hapiTracingPlugin = {
  46. name: 'SentryHapiTracingPlugin',
  47. version: core.SDK_VERSION,
  48. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  49. register: async function (serverArg) {
  50. const server = serverArg ;
  51. server.ext('onPreHandler', (request, h) => {
  52. const transaction = core.continueTrace(
  53. {
  54. sentryTrace: request.headers['sentry-trace'] || undefined,
  55. baggage: request.headers['baggage'] || undefined,
  56. },
  57. transactionContext => {
  58. // eslint-disable-next-line deprecation/deprecation
  59. return core.startTransaction({
  60. ...transactionContext,
  61. op: 'hapi.request',
  62. name: request.route.path,
  63. description: `${request.route.method} ${request.path}`,
  64. });
  65. },
  66. );
  67. // eslint-disable-next-line deprecation/deprecation
  68. core.getCurrentScope().setSpan(transaction);
  69. return h.continue;
  70. });
  71. server.ext('onPreResponse', (request, h) => {
  72. // eslint-disable-next-line deprecation/deprecation
  73. const transaction = core.getActiveTransaction();
  74. if (request.response && isResponseObject(request.response) && transaction) {
  75. const response = request.response ;
  76. response.header('sentry-trace', core.spanToTraceHeader(transaction));
  77. const dynamicSamplingContext = utils.dynamicSamplingContextToSentryBaggageHeader(
  78. core.getDynamicSamplingContextFromSpan(transaction),
  79. );
  80. if (dynamicSamplingContext) {
  81. response.header('baggage', dynamicSamplingContext);
  82. }
  83. }
  84. return h.continue;
  85. });
  86. server.ext('onPostHandler', (request, h) => {
  87. // eslint-disable-next-line deprecation/deprecation
  88. const transaction = core.getActiveTransaction();
  89. if (transaction) {
  90. if (request.response && isResponseObject(request.response)) {
  91. core.setHttpStatus(transaction, request.response.statusCode);
  92. }
  93. transaction.end();
  94. }
  95. return h.continue;
  96. });
  97. },
  98. };
  99. const INTEGRATION_NAME = 'Hapi';
  100. const _hapiIntegration = ((options = {}) => {
  101. const server = options.server ;
  102. return {
  103. name: INTEGRATION_NAME,
  104. setupOnce() {
  105. if (!server) {
  106. return;
  107. }
  108. utils.fill(server, 'start', (originalStart) => {
  109. return async function () {
  110. await this.register(hapiTracingPlugin);
  111. await this.register(hapiErrorPlugin);
  112. const result = originalStart.apply(this);
  113. return result;
  114. };
  115. });
  116. },
  117. };
  118. }) ;
  119. const hapiIntegration = core.defineIntegration(_hapiIntegration);
  120. /**
  121. * Hapi Framework Integration.
  122. * @deprecated Use `hapiIntegration()` instead.
  123. */
  124. // eslint-disable-next-line deprecation/deprecation
  125. const Hapi = core.convertIntegrationFnToClass(INTEGRATION_NAME, hapiIntegration);
  126. // eslint-disable-next-line deprecation/deprecation
  127. exports.Hapi = Hapi;
  128. exports.hapiErrorPlugin = hapiErrorPlugin;
  129. exports.hapiIntegration = hapiIntegration;
  130. exports.hapiTracingPlugin = hapiTracingPlugin;
  131. //# sourceMappingURL=index.js.map