|
| 1 | +import '@sentry/tracing'; // Allow people to call tracing API methods without explicitly importing the tracing package. |
| 2 | + |
| 3 | +import { getCurrentHub, getIntegrationsToSetup, initAndBind, Integrations as CoreIntegrations } from '@sentry/core'; |
| 4 | +import type { Options } from '@sentry/types'; |
| 5 | +import { |
| 6 | + createStackParser, |
| 7 | + GLOBAL_OBJ, |
| 8 | + logger, |
| 9 | + nodeStackLineParser, |
| 10 | + stackParserFromStackParserOptions, |
| 11 | +} from '@sentry/utils'; |
| 12 | + |
| 13 | +import { EdgeClient } from './edgeclient'; |
| 14 | +import { makeEdgeTransport } from './transport'; |
| 15 | + |
| 16 | +const nodeStackParser = createStackParser(nodeStackLineParser()); |
| 17 | + |
| 18 | +export const defaultIntegrations = [new CoreIntegrations.InboundFilters(), new CoreIntegrations.FunctionToString()]; |
| 19 | + |
| 20 | +export type EdgeOptions = Options; |
| 21 | + |
| 22 | +/** Inits the Sentry NextJS SDK on the Edge Runtime. */ |
| 23 | +export function init(options: EdgeOptions = {}): void { |
| 24 | + if (options.defaultIntegrations === undefined) { |
| 25 | + options.defaultIntegrations = defaultIntegrations; |
| 26 | + } |
| 27 | + |
| 28 | + if (options.dsn === undefined && process.env.SENTRY_DSN) { |
| 29 | + options.dsn = process.env.SENTRY_DSN; |
| 30 | + } |
| 31 | + |
| 32 | + if (options.tracesSampleRate === undefined && process.env.SENTRY_TRACES_SAMPLE_RATE) { |
| 33 | + const tracesSampleRate = parseFloat(process.env.SENTRY_TRACES_SAMPLE_RATE); |
| 34 | + if (isFinite(tracesSampleRate)) { |
| 35 | + options.tracesSampleRate = tracesSampleRate; |
| 36 | + } |
| 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 | + |
| 49 | + if (options.environment === undefined && process.env.SENTRY_ENVIRONMENT) { |
| 50 | + options.environment = process.env.SENTRY_ENVIRONMENT; |
| 51 | + } |
| 52 | + |
| 53 | + if (options.autoSessionTracking === undefined && options.dsn !== undefined) { |
| 54 | + options.autoSessionTracking = true; |
| 55 | + } |
| 56 | + |
| 57 | + if (options.instrumenter === undefined) { |
| 58 | + options.instrumenter = 'sentry'; |
| 59 | + } |
| 60 | + |
| 61 | + const clientOptions = { |
| 62 | + ...options, |
| 63 | + stackParser: stackParserFromStackParserOptions(options.stackParser || nodeStackParser), |
| 64 | + integrations: getIntegrationsToSetup(options), |
| 65 | + transport: options.transport || makeEdgeTransport, |
| 66 | + }; |
| 67 | + |
| 68 | + initAndBind(EdgeClient, clientOptions); |
| 69 | + |
| 70 | + // TODO?: Sessiontracking |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * Returns a release dynamically from environment variables. |
| 75 | + */ |
| 76 | +export function getSentryRelease(fallback?: string): string | undefined { |
| 77 | + // Always read first as Sentry takes this as precedence |
| 78 | + if (process.env.SENTRY_RELEASE) { |
| 79 | + return process.env.SENTRY_RELEASE; |
| 80 | + } |
| 81 | + |
| 82 | + // This supports the variable that sentry-webpack-plugin injects |
| 83 | + if (GLOBAL_OBJ.SENTRY_RELEASE && GLOBAL_OBJ.SENTRY_RELEASE.id) { |
| 84 | + return GLOBAL_OBJ.SENTRY_RELEASE.id; |
| 85 | + } |
| 86 | + |
| 87 | + return ( |
| 88 | + // GitHub Actions - https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables |
| 89 | + process.env.GITHUB_SHA || |
| 90 | + // Netlify - https://docs.netlify.com/configure-builds/environment-variables/#build-metadata |
| 91 | + process.env.COMMIT_REF || |
| 92 | + // Vercel - https://vercel.com/docs/v2/build-step#system-environment-variables |
| 93 | + process.env.VERCEL_GIT_COMMIT_SHA || |
| 94 | + process.env.VERCEL_GITHUB_COMMIT_SHA || |
| 95 | + process.env.VERCEL_GITLAB_COMMIT_SHA || |
| 96 | + process.env.VERCEL_BITBUCKET_COMMIT_SHA || |
| 97 | + // Zeit (now known as Vercel) |
| 98 | + process.env.ZEIT_GITHUB_COMMIT_SHA || |
| 99 | + process.env.ZEIT_GITLAB_COMMIT_SHA || |
| 100 | + process.env.ZEIT_BITBUCKET_COMMIT_SHA || |
| 101 | + fallback |
| 102 | + ); |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Call `close()` on the current client, if there is one. See {@link Client.close}. |
| 107 | + * |
| 108 | + * @param timeout Maximum time in ms the client should wait to flush its event queue before shutting down. Omitting this |
| 109 | + * parameter will cause the client to wait until all events are sent before disabling itself. |
| 110 | + * @returns A promise which resolves to `true` if the queue successfully drains before the timeout, or `false` if it |
| 111 | + * doesn't (or if there's no client defined). |
| 112 | + */ |
| 113 | +export async function close(timeout?: number): Promise<boolean> { |
| 114 | + const client = getCurrentHub().getClient<EdgeClient>(); |
| 115 | + if (client) { |
| 116 | + return client.close(timeout); |
| 117 | + } |
| 118 | + __DEBUG_BUILD__ && logger.warn('Cannot flush events and disable SDK. No client defined.'); |
| 119 | + return Promise.resolve(false); |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * Call `flush()` on the current client, if there is one. See {@link Client.flush}. |
| 124 | + * |
| 125 | + * @param timeout Maximum time in ms the client should wait to flush its event queue. Omitting this parameter will cause |
| 126 | + * the client to wait until all events are sent before resolving the promise. |
| 127 | + * @returns A promise which resolves to `true` if the queue successfully drains before the timeout, or `false` if it |
| 128 | + * doesn't (or if there's no client defined). |
| 129 | + */ |
| 130 | +export async function flush(timeout?: number): Promise<boolean> { |
| 131 | + const client = getCurrentHub().getClient<EdgeClient>(); |
| 132 | + if (client) { |
| 133 | + return client.flush(timeout); |
| 134 | + } |
| 135 | + __DEBUG_BUILD__ && logger.warn('Cannot flush events. No client defined.'); |
| 136 | + return Promise.resolve(false); |
| 137 | +} |
| 138 | + |
| 139 | +/** |
| 140 | + * This is the getter for lastEventId. |
| 141 | + * |
| 142 | + * @returns The last event id of a captured event. |
| 143 | + */ |
| 144 | +export function lastEventId(): string | undefined { |
| 145 | + return getCurrentHub().lastEventId(); |
| 146 | +} |
| 147 | + |
| 148 | +export * from '@sentry/core'; |
0 commit comments