UNPKG

@sentry/node

Version:
109 lines (92 loc) 2.78 kB
var { _optionalChain } = require('@sentry/utils'); Object.defineProperty(exports, '__esModule', { value: true }); const http = require('http'); const url = require('url'); const utils = require('@sentry/utils'); /** * Use this integration to send errors and transactions to Spotlight. * * Learn more about spotlight at https://spotlightjs.com * * Important: This integration only works with Node 18 or newer */ class Spotlight { static __initStatic() {this.id = 'Spotlight';} __init() {this.name = Spotlight.id;} constructor(options) {Spotlight.prototype.__init.call(this); this._options = { sidecarUrl: _optionalChain([options, 'optionalAccess', _ => _.sidecarUrl]) || 'http://localhost:8969/stream', }; } /** * JSDoc */ setupOnce() { // empty but otherwise TS complains } /** * Sets up forwarding envelopes to the Spotlight Sidecar */ setup(client) { if (typeof process === 'object' && process.env && process.env.NODE_ENV !== 'development') { utils.logger.warn("[Spotlight] It seems you're not in dev mode. Do you really want to have Spotlight enabled?"); } connectToSpotlight(client, this._options); } }Spotlight.__initStatic(); function connectToSpotlight(client, options) { const spotlightUrl = parseSidecarUrl(options.sidecarUrl); if (!spotlightUrl) { return; } let failedRequests = 0; if (typeof client.on !== 'function') { utils.logger.warn('[Spotlight] Cannot connect to spotlight due to missing method on SDK client (`client.on`)'); return; } client.on('beforeEnvelope', (envelope) => { if (failedRequests > 3) { utils.logger.warn('[Spotlight] Disabled Sentry -> Spotlight integration due to too many failed requests'); return; } const serializedEnvelope = utils.serializeEnvelope(envelope); const req = http.request( { method: 'POST', path: spotlightUrl.pathname, hostname: spotlightUrl.hostname, port: spotlightUrl.port, headers: { 'Content-Type': 'application/x-sentry-envelope', }, }, res => { res.on('data', () => { // Drain socket }); res.on('end', () => { // Drain socket }); res.setEncoding('utf8'); }, ); req.on('error', () => { failedRequests++; utils.logger.warn('[Spotlight] Failed to send envelope to Spotlight Sidecar'); }); req.write(serializedEnvelope); req.end(); }); } function parseSidecarUrl(url$1) { try { return new url.URL(`${url$1}`); } catch (e) { utils.logger.warn(`[Spotlight] Invalid sidecar URL: ${url$1}`); return undefined; } } exports.Spotlight = Spotlight; //# sourceMappingURL=spotlight.js.map