Skip to content

feat(node-experimental): Move defaultStackParser & getSentryRelease #10722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions packages/node-experimental/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,21 @@ export * as Handlers from './sdk/handlers';
export type { Span } from './types';

export { startSpan, startSpanManual, startInactiveSpan, getActiveSpan, withActiveSpan } from '@sentry/opentelemetry';
export { getClient } from './sdk/api';
export { getClient, getSentryRelease, defaultStackParser } from './sdk/api';
export { createGetModuleFromFilename } from './utils/module';
// eslint-disable-next-line deprecation/deprecation
export { getCurrentHub } from './sdk/hub';

export {
addBreadcrumb,
isInitialized,
makeNodeTransport,
defaultStackParser,
getSentryRelease,
getGlobalScope,
addRequestDataToEvent,
DEFAULT_USER_INCLUDES,
extractRequestData,
// eslint-disable-next-line deprecation/deprecation
getModuleFromFilename,
createGetModuleFromFilename,
close,
createTransport,
flush,
Expand Down
41 changes: 40 additions & 1 deletion packages/node-experimental/src/sdk/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// PUBLIC APIS

import { getCurrentScope } from '@sentry/core';
import type { Client } from '@sentry/types';
import type { Client, StackParser } from '@sentry/types';
import { GLOBAL_OBJ, createStackParser, nodeStackLineParser } from '@sentry/utils';
import { createGetModuleFromFilename } from '../utils/module';

/** Get the currently active client. */
export function getClient<C extends Client>(): C {
Expand All @@ -15,3 +17,40 @@ export function getClient<C extends Client>(): C {
// TODO otherwise ensure we use a noop client
return {} as C;
}

/**
* Returns a release dynamically from environment variables.
*/
export function getSentryRelease(fallback?: string): string | undefined {
// Always read first as Sentry takes this as precedence
if (process.env.SENTRY_RELEASE) {
return process.env.SENTRY_RELEASE;
}

// This supports the variable that sentry-webpack-plugin injects
if (GLOBAL_OBJ.SENTRY_RELEASE && GLOBAL_OBJ.SENTRY_RELEASE.id) {
return GLOBAL_OBJ.SENTRY_RELEASE.id;
}

return (
// GitHub Actions - https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables
process.env.GITHUB_SHA ||
// Netlify - https://docs.netlify.com/configure-builds/environment-variables/#build-metadata
process.env.COMMIT_REF ||
// Vercel - https://vercel.com/docs/v2/build-step#system-environment-variables
process.env.VERCEL_GIT_COMMIT_SHA ||
process.env.VERCEL_GITHUB_COMMIT_SHA ||
process.env.VERCEL_GITLAB_COMMIT_SHA ||
process.env.VERCEL_BITBUCKET_COMMIT_SHA ||
// Zeit (now known as Vercel)
process.env.ZEIT_GITHUB_COMMIT_SHA ||
process.env.ZEIT_GITLAB_COMMIT_SHA ||
process.env.ZEIT_BITBUCKET_COMMIT_SHA ||
// Cloudflare Pages - https://developers.cloudflare.com/pages/platform/build-configuration/#environment-variables
process.env.CF_PAGES_COMMIT_SHA ||
fallback
);
}

/** Node.js stack parser */
export const defaultStackParser: StackParser = createStackParser(nodeStackLineParser(createGetModuleFromFilename()));
8 changes: 2 additions & 6 deletions packages/node-experimental/src/sdk/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ import {
hasTracingEnabled,
startSession,
} from '@sentry/core';
import {
defaultStackParser,
getDefaultIntegrations as getDefaultNodeIntegrations,
getSentryRelease,
spotlightIntegration,
} from '@sentry/node';
import { getDefaultIntegrations as getDefaultNodeIntegrations, spotlightIntegration } from '@sentry/node';
import { setOpenTelemetryContextAsyncContextStrategy } from '@sentry/opentelemetry';
import type { Client, Integration, Options } from '@sentry/types';
import {
Expand All @@ -28,6 +23,7 @@ import { httpIntegration } from '../integrations/http';
import { nativeNodeFetchIntegration } from '../integrations/node-fetch';
import { makeNodeTransport } from '../transports';
import type { NodeClientOptions, NodeOptions } from '../types';
import { defaultStackParser, getSentryRelease } from './api';
import { NodeClient } from './client';
import { initOtel } from './initOtel';

Expand Down
57 changes: 57 additions & 0 deletions packages/node-experimental/src/utils/module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { posix, sep } from 'path';
import { dirname } from '@sentry/utils';

/** normalizes Windows paths */
function normalizeWindowsPath(path: string): string {
return path
.replace(/^[A-Z]:/, '') // remove Windows-style prefix
.replace(/\\/g, '/'); // replace all `\` instances with `/`
}

/** Creates a function that gets the module name from a filename */
export function createGetModuleFromFilename(
basePath: string = process.argv[1] ? dirname(process.argv[1]) : process.cwd(),
isWindows: boolean = sep === '\\',
): (filename: string | undefined) => string | undefined {
const normalizedBase = isWindows ? normalizeWindowsPath(basePath) : basePath;

return (filename: string | undefined) => {
if (!filename) {
return;
}

const normalizedFilename = isWindows ? normalizeWindowsPath(filename) : filename;

// eslint-disable-next-line prefer-const
let { dir, base: file, ext } = posix.parse(normalizedFilename);

if (ext === '.js' || ext === '.mjs' || ext === '.cjs') {
file = file.slice(0, ext.length * -1);
}

if (!dir) {
// No dirname whatsoever
dir = '.';
}

const n = dir.lastIndexOf('/node_modules');
if (n > -1) {
return `${dir.slice(n + 14).replace(/\//g, '.')}:${file}`;
}

// Let's see if it's a part of the main module
// To be a part of main module, it has to share the same base
if (dir.startsWith(normalizedBase)) {
let moduleName = dir.slice(normalizedBase.length + 1).replace(/\//g, '.');

if (moduleName) {
moduleName += ':';
}
moduleName += file;

return moduleName;
}

return file;
};
}