sdk.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { inboundFiltersIntegration, functionToStringIntegration, linkedErrorsIntegration, requestDataIntegration, getIntegrationsToSetup, initAndBind } from '@sentry/core';
  2. import { createStackParser, nodeStackLineParser, stackParserFromStackParserOptions, GLOBAL_OBJ } from '@sentry/utils';
  3. import { setAsyncLocalStorageAsyncContextStrategy } from './async.js';
  4. import { VercelEdgeClient } from './client.js';
  5. import { winterCGFetchIntegration } from './integrations/wintercg-fetch.js';
  6. import { makeEdgeTransport } from './transports/index.js';
  7. import { getVercelEnv } from './utils/vercel.js';
  8. const nodeStackParser = createStackParser(nodeStackLineParser());
  9. /** @deprecated Use `getDefaultIntegrations(options)` instead. */
  10. const defaultIntegrations = [
  11. inboundFiltersIntegration(),
  12. functionToStringIntegration(),
  13. linkedErrorsIntegration(),
  14. winterCGFetchIntegration(),
  15. ];
  16. /** Get the default integrations for the browser SDK. */
  17. function getDefaultIntegrations(options) {
  18. return [
  19. // eslint-disable-next-line deprecation/deprecation
  20. ...defaultIntegrations,
  21. ...(options.sendDefaultPii ? [requestDataIntegration()] : []),
  22. ];
  23. }
  24. /** Inits the Sentry NextJS SDK on the Edge Runtime. */
  25. function init(options = {}) {
  26. setAsyncLocalStorageAsyncContextStrategy();
  27. if (options.defaultIntegrations === undefined) {
  28. options.defaultIntegrations = getDefaultIntegrations(options);
  29. }
  30. if (options.dsn === undefined && process.env.SENTRY_DSN) {
  31. options.dsn = process.env.SENTRY_DSN;
  32. }
  33. if (options.tracesSampleRate === undefined && process.env.SENTRY_TRACES_SAMPLE_RATE) {
  34. const tracesSampleRate = parseFloat(process.env.SENTRY_TRACES_SAMPLE_RATE);
  35. if (isFinite(tracesSampleRate)) {
  36. options.tracesSampleRate = tracesSampleRate;
  37. }
  38. }
  39. if (options.release === undefined) {
  40. const detectedRelease = getSentryRelease();
  41. if (detectedRelease !== undefined) {
  42. options.release = detectedRelease;
  43. } else {
  44. // If release is not provided, then we should disable autoSessionTracking
  45. options.autoSessionTracking = false;
  46. }
  47. }
  48. options.environment =
  49. options.environment || process.env.SENTRY_ENVIRONMENT || getVercelEnv(false) || process.env.NODE_ENV;
  50. if (options.autoSessionTracking === undefined && options.dsn !== undefined) {
  51. options.autoSessionTracking = true;
  52. }
  53. if (options.instrumenter === undefined) {
  54. options.instrumenter = 'sentry';
  55. }
  56. const clientOptions = {
  57. ...options,
  58. stackParser: stackParserFromStackParserOptions(options.stackParser || nodeStackParser),
  59. integrations: getIntegrationsToSetup(options),
  60. transport: options.transport || makeEdgeTransport,
  61. };
  62. initAndBind(VercelEdgeClient, clientOptions);
  63. }
  64. /**
  65. * Returns a release dynamically from environment variables.
  66. */
  67. function getSentryRelease(fallback) {
  68. // Always read first as Sentry takes this as precedence
  69. if (process.env.SENTRY_RELEASE) {
  70. return process.env.SENTRY_RELEASE;
  71. }
  72. // This supports the variable that sentry-webpack-plugin injects
  73. if (GLOBAL_OBJ.SENTRY_RELEASE && GLOBAL_OBJ.SENTRY_RELEASE.id) {
  74. return GLOBAL_OBJ.SENTRY_RELEASE.id;
  75. }
  76. return (
  77. // GitHub Actions - https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables
  78. process.env.GITHUB_SHA ||
  79. // Vercel - https://vercel.com/docs/v2/build-step#system-environment-variables
  80. process.env.VERCEL_GIT_COMMIT_SHA ||
  81. process.env.VERCEL_GITHUB_COMMIT_SHA ||
  82. process.env.VERCEL_GITLAB_COMMIT_SHA ||
  83. process.env.VERCEL_BITBUCKET_COMMIT_SHA ||
  84. // Zeit (now known as Vercel)
  85. process.env.ZEIT_GITHUB_COMMIT_SHA ||
  86. process.env.ZEIT_GITLAB_COMMIT_SHA ||
  87. process.env.ZEIT_BITBUCKET_COMMIT_SHA ||
  88. fallback
  89. );
  90. }
  91. export { defaultIntegrations, getDefaultIntegrations, getSentryRelease, init };
  92. //# sourceMappingURL=sdk.js.map