sdk.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. var {
  2. _optionalChain
  3. } = require('@sentry/utils');
  4. Object.defineProperty(exports, '__esModule', { value: true });
  5. const core = require('@sentry/core');
  6. const utils = require('@sentry/utils');
  7. const index$2 = require('./async/index.js');
  8. const client = require('./client.js');
  9. const console = require('./integrations/console.js');
  10. const context = require('./integrations/context.js');
  11. const contextlines = require('./integrations/contextlines.js');
  12. const http = require('./integrations/http.js');
  13. const index$1 = require('./integrations/local-variables/index.js');
  14. const modules = require('./integrations/modules.js');
  15. const onuncaughtexception = require('./integrations/onuncaughtexception.js');
  16. const onunhandledrejection = require('./integrations/onunhandledrejection.js');
  17. const spotlight = require('./integrations/spotlight.js');
  18. const index = require('./integrations/undici/index.js');
  19. const module$1 = require('./module.js');
  20. const http$1 = require('./transports/http.js');
  21. /* eslint-disable max-lines */
  22. /** @deprecated Use `getDefaultIntegrations(options)` instead. */
  23. const defaultIntegrations = [
  24. // Common
  25. core.inboundFiltersIntegration(),
  26. core.functionToStringIntegration(),
  27. core.linkedErrorsIntegration(),
  28. core.requestDataIntegration(),
  29. // Native Wrappers
  30. console.consoleIntegration(),
  31. http.httpIntegration(),
  32. index.nativeNodeFetchintegration(),
  33. // Global Handlers
  34. onuncaughtexception.onUncaughtExceptionIntegration(),
  35. onunhandledrejection.onUnhandledRejectionIntegration(),
  36. // Event Info
  37. contextlines.contextLinesIntegration(),
  38. index$1.localVariablesIntegration(),
  39. context.nodeContextIntegration(),
  40. modules.modulesIntegration(),
  41. ];
  42. /** Get the default integrations for the Node SDK. */
  43. function getDefaultIntegrations(_options) {
  44. const carrier = core.getMainCarrier();
  45. const autoloadedIntegrations = _optionalChain([carrier, 'access', _ => _.__SENTRY__, 'optionalAccess', _2 => _2.integrations]) || [];
  46. return [
  47. // eslint-disable-next-line deprecation/deprecation
  48. ...defaultIntegrations,
  49. ...autoloadedIntegrations,
  50. ];
  51. }
  52. /**
  53. * The Sentry Node SDK Client.
  54. *
  55. * To use this SDK, call the {@link init} function as early as possible in the
  56. * main entry module. To set context information or send manual events, use the
  57. * provided methods.
  58. *
  59. * @example
  60. * ```
  61. *
  62. * const { init } = require('@sentry/node');
  63. *
  64. * init({
  65. * dsn: '__DSN__',
  66. * // ...
  67. * });
  68. * ```
  69. *
  70. * @example
  71. * ```
  72. *
  73. * const { configureScope } = require('@sentry/node');
  74. * configureScope((scope: Scope) => {
  75. * scope.setExtra({ battery: 0.7 });
  76. * scope.setTag({ user_mode: 'admin' });
  77. * scope.setUser({ id: '4711' });
  78. * });
  79. * ```
  80. *
  81. * @example
  82. * ```
  83. *
  84. * const { addBreadcrumb } = require('@sentry/node');
  85. * addBreadcrumb({
  86. * message: 'My Breadcrumb',
  87. * // ...
  88. * });
  89. * ```
  90. *
  91. * @example
  92. * ```
  93. *
  94. * const Sentry = require('@sentry/node');
  95. * Sentry.captureMessage('Hello, world!');
  96. * Sentry.captureException(new Error('Good bye'));
  97. * Sentry.captureEvent({
  98. * message: 'Manual',
  99. * stacktrace: [
  100. * // ...
  101. * ],
  102. * });
  103. * ```
  104. *
  105. * @see {@link NodeOptions} for documentation on configuration options.
  106. */
  107. // eslint-disable-next-line complexity
  108. function init(options = {}) {
  109. index$2.setNodeAsyncContextStrategy();
  110. if (options.defaultIntegrations === undefined) {
  111. options.defaultIntegrations = getDefaultIntegrations();
  112. }
  113. if (options.dsn === undefined && process.env.SENTRY_DSN) {
  114. options.dsn = process.env.SENTRY_DSN;
  115. }
  116. const sentryTracesSampleRate = process.env.SENTRY_TRACES_SAMPLE_RATE;
  117. if (options.tracesSampleRate === undefined && sentryTracesSampleRate) {
  118. const tracesSampleRate = parseFloat(sentryTracesSampleRate);
  119. if (isFinite(tracesSampleRate)) {
  120. options.tracesSampleRate = tracesSampleRate;
  121. }
  122. }
  123. if (options.release === undefined) {
  124. const detectedRelease = getSentryRelease();
  125. if (detectedRelease !== undefined) {
  126. options.release = detectedRelease;
  127. } else {
  128. // If release is not provided, then we should disable autoSessionTracking
  129. options.autoSessionTracking = false;
  130. }
  131. }
  132. if (options.environment === undefined && process.env.SENTRY_ENVIRONMENT) {
  133. options.environment = process.env.SENTRY_ENVIRONMENT;
  134. }
  135. if (options.autoSessionTracking === undefined && options.dsn !== undefined) {
  136. options.autoSessionTracking = true;
  137. }
  138. if (options.instrumenter === undefined) {
  139. options.instrumenter = 'sentry';
  140. }
  141. // TODO(v7): Refactor this to reduce the logic above
  142. const clientOptions = {
  143. ...options,
  144. stackParser: utils.stackParserFromStackParserOptions(options.stackParser || defaultStackParser),
  145. integrations: core.getIntegrationsToSetup(options),
  146. transport: options.transport || http$1.makeNodeTransport,
  147. };
  148. core.initAndBind(options.clientClass || client.NodeClient, clientOptions);
  149. if (options.autoSessionTracking) {
  150. startSessionTracking();
  151. }
  152. updateScopeFromEnvVariables();
  153. if (options.spotlight) {
  154. const client = core.getClient();
  155. if (client && client.addIntegration) {
  156. // force integrations to be setup even if no DSN was set
  157. // If they have already been added before, they will be ignored anyhow
  158. const integrations = client.getOptions().integrations;
  159. for (const integration of integrations) {
  160. client.addIntegration(integration);
  161. }
  162. client.addIntegration(
  163. spotlight.spotlightIntegration({ sidecarUrl: typeof options.spotlight === 'string' ? options.spotlight : undefined }),
  164. );
  165. }
  166. }
  167. }
  168. /**
  169. * Function that takes an instance of NodeClient and checks if autoSessionTracking option is enabled for that client
  170. */
  171. function isAutoSessionTrackingEnabled(client) {
  172. if (client === undefined) {
  173. return false;
  174. }
  175. const clientOptions = client && client.getOptions();
  176. if (clientOptions && clientOptions.autoSessionTracking !== undefined) {
  177. return clientOptions.autoSessionTracking;
  178. }
  179. return false;
  180. }
  181. /**
  182. * Returns a release dynamically from environment variables.
  183. */
  184. function getSentryRelease(fallback) {
  185. // Always read first as Sentry takes this as precedence
  186. if (process.env.SENTRY_RELEASE) {
  187. return process.env.SENTRY_RELEASE;
  188. }
  189. // This supports the variable that sentry-webpack-plugin injects
  190. if (utils.GLOBAL_OBJ.SENTRY_RELEASE && utils.GLOBAL_OBJ.SENTRY_RELEASE.id) {
  191. return utils.GLOBAL_OBJ.SENTRY_RELEASE.id;
  192. }
  193. return (
  194. // GitHub Actions - https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables
  195. process.env.GITHUB_SHA ||
  196. // Netlify - https://docs.netlify.com/configure-builds/environment-variables/#build-metadata
  197. process.env.COMMIT_REF ||
  198. // Vercel - https://vercel.com/docs/v2/build-step#system-environment-variables
  199. process.env.VERCEL_GIT_COMMIT_SHA ||
  200. process.env.VERCEL_GITHUB_COMMIT_SHA ||
  201. process.env.VERCEL_GITLAB_COMMIT_SHA ||
  202. process.env.VERCEL_BITBUCKET_COMMIT_SHA ||
  203. // Zeit (now known as Vercel)
  204. process.env.ZEIT_GITHUB_COMMIT_SHA ||
  205. process.env.ZEIT_GITLAB_COMMIT_SHA ||
  206. process.env.ZEIT_BITBUCKET_COMMIT_SHA ||
  207. // Cloudflare Pages - https://developers.cloudflare.com/pages/platform/build-configuration/#environment-variables
  208. process.env.CF_PAGES_COMMIT_SHA ||
  209. fallback
  210. );
  211. }
  212. /** Node.js stack parser */
  213. const defaultStackParser = utils.createStackParser(utils.nodeStackLineParser(module$1.createGetModuleFromFilename()));
  214. /**
  215. * Enable automatic Session Tracking for the node process.
  216. */
  217. function startSessionTracking() {
  218. core.startSession();
  219. // Emitted in the case of healthy sessions, error of `mechanism.handled: true` and unhandledrejections because
  220. // The 'beforeExit' event is not emitted for conditions causing explicit termination,
  221. // such as calling process.exit() or uncaught exceptions.
  222. // Ref: https://nodejs.org/api/process.html#process_event_beforeexit
  223. process.on('beforeExit', () => {
  224. const session = core.getIsolationScope().getSession();
  225. const terminalStates = ['exited', 'crashed'];
  226. // Only call endSession, if the Session exists on Scope and SessionStatus is not a
  227. // Terminal Status i.e. Exited or Crashed because
  228. // "When a session is moved away from ok it must not be updated anymore."
  229. // Ref: https://develop.sentry.dev/sdk/sessions/
  230. if (session && !terminalStates.includes(session.status)) {
  231. core.endSession();
  232. }
  233. });
  234. }
  235. /**
  236. * Update scope and propagation context based on environmental variables.
  237. *
  238. * See https://github.com/getsentry/rfcs/blob/main/text/0071-continue-trace-over-process-boundaries.md
  239. * for more details.
  240. */
  241. function updateScopeFromEnvVariables() {
  242. const sentryUseEnvironment = (process.env.SENTRY_USE_ENVIRONMENT || '').toLowerCase();
  243. if (!['false', 'n', 'no', 'off', '0'].includes(sentryUseEnvironment)) {
  244. const sentryTraceEnv = process.env.SENTRY_TRACE;
  245. const baggageEnv = process.env.SENTRY_BAGGAGE;
  246. const propagationContext = utils.propagationContextFromHeaders(sentryTraceEnv, baggageEnv);
  247. core.getCurrentScope().setPropagationContext(propagationContext);
  248. }
  249. }
  250. exports.defaultIntegrations = defaultIntegrations;
  251. exports.defaultStackParser = defaultStackParser;
  252. exports.getDefaultIntegrations = getDefaultIntegrations;
  253. exports.getSentryRelease = getSentryRelease;
  254. exports.init = init;
  255. exports.isAutoSessionTrackingEnabled = isAutoSessionTrackingEnabled;
  256. //# sourceMappingURL=sdk.js.map