spotlight.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. const http = require('http');
  3. const url = require('url');
  4. const core = require('@sentry/core');
  5. const utils = require('@sentry/utils');
  6. const INTEGRATION_NAME = 'Spotlight';
  7. const _spotlightIntegration = ((options = {}) => {
  8. const _options = {
  9. sidecarUrl: options.sidecarUrl || 'http://localhost:8969/stream',
  10. };
  11. return {
  12. name: INTEGRATION_NAME,
  13. // TODO v8: Remove this
  14. setupOnce() {}, // eslint-disable-line @typescript-eslint/no-empty-function
  15. setup(client) {
  16. if (typeof process === 'object' && process.env && process.env.NODE_ENV !== 'development') {
  17. utils.logger.warn("[Spotlight] It seems you're not in dev mode. Do you really want to have Spotlight enabled?");
  18. }
  19. connectToSpotlight(client, _options);
  20. },
  21. };
  22. }) ;
  23. const spotlightIntegration = core.defineIntegration(_spotlightIntegration);
  24. /**
  25. * Use this integration to send errors and transactions to Spotlight.
  26. *
  27. * Learn more about spotlight at https://spotlightjs.com
  28. *
  29. * Important: This integration only works with Node 18 or newer.
  30. *
  31. * @deprecated Use `spotlightIntegration()` instead.
  32. */
  33. // eslint-disable-next-line deprecation/deprecation
  34. const Spotlight = core.convertIntegrationFnToClass(INTEGRATION_NAME, spotlightIntegration)
  35. ;
  36. // eslint-disable-next-line deprecation/deprecation
  37. function connectToSpotlight(client, options) {
  38. const spotlightUrl = parseSidecarUrl(options.sidecarUrl);
  39. if (!spotlightUrl) {
  40. return;
  41. }
  42. let failedRequests = 0;
  43. if (typeof client.on !== 'function') {
  44. utils.logger.warn('[Spotlight] Cannot connect to spotlight due to missing method on SDK client (`client.on`)');
  45. return;
  46. }
  47. client.on('beforeEnvelope', (envelope) => {
  48. if (failedRequests > 3) {
  49. utils.logger.warn('[Spotlight] Disabled Sentry -> Spotlight integration due to too many failed requests');
  50. return;
  51. }
  52. const serializedEnvelope = utils.serializeEnvelope(envelope);
  53. const request = getNativeHttpRequest();
  54. const req = request(
  55. {
  56. method: 'POST',
  57. path: spotlightUrl.pathname,
  58. hostname: spotlightUrl.hostname,
  59. port: spotlightUrl.port,
  60. headers: {
  61. 'Content-Type': 'application/x-sentry-envelope',
  62. },
  63. },
  64. res => {
  65. res.on('data', () => {
  66. // Drain socket
  67. });
  68. res.on('end', () => {
  69. // Drain socket
  70. });
  71. res.setEncoding('utf8');
  72. },
  73. );
  74. req.on('error', () => {
  75. failedRequests++;
  76. utils.logger.warn('[Spotlight] Failed to send envelope to Spotlight Sidecar');
  77. });
  78. req.write(serializedEnvelope);
  79. req.end();
  80. });
  81. }
  82. function parseSidecarUrl(url$1) {
  83. try {
  84. return new url.URL(`${url$1}`);
  85. } catch (e) {
  86. utils.logger.warn(`[Spotlight] Invalid sidecar URL: ${url$1}`);
  87. return undefined;
  88. }
  89. }
  90. /**
  91. * We want to get an unpatched http request implementation to avoid capturing our own calls.
  92. */
  93. function getNativeHttpRequest() {
  94. const { request } = http;
  95. if (isWrapped(request)) {
  96. return request.__sentry_original__;
  97. }
  98. return request;
  99. }
  100. function isWrapped(impl) {
  101. return '__sentry_original__' in impl;
  102. }
  103. exports.Spotlight = Spotlight;
  104. exports.getNativeHttpRequest = getNativeHttpRequest;
  105. exports.spotlightIntegration = spotlightIntegration;
  106. //# sourceMappingURL=spotlight.js.map