Skip to content
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

ref(profiling-node): Remove usage of getCurrentHub #11275

Merged
merged 1 commit into from
Mar 25, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/profiling-node/src/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export const _nodeProfilingIntegration = (() => {

// Remove the profile from the queue.
PROFILE_QUEUE.splice(profileIndex, 1);
const profile = createProfilingEvent(cpuProfile, profiledTransaction);
const profile = createProfilingEvent(client, cpuProfile, profiledTransaction);

if (client.emit && profile) {
const integrations =
Expand Down
62 changes: 8 additions & 54 deletions packages/profiling-node/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import * as os from 'os';
import type { Context, Envelope, Event, StackFrame, StackParser } from '@sentry/types';
import type { Client, Context, Envelope, Event, StackFrame, StackParser } from '@sentry/types';
import { env, versions } from 'process';
import { isMainThread, threadId } from 'worker_threads';

import { getCurrentHub } from '@sentry/node';
import { GLOBAL_OBJ, forEachEnvelopeItem, logger } from '@sentry/utils';

import { DEBUG_BUILD } from './debug-build';
import type { Profile, ProfiledEvent, RawThreadCpuProfile, ThreadCpuProfile } from './types';
import type { Profile, RawThreadCpuProfile, ThreadCpuProfile } from './types';
import type { DebugImage } from './types';

// We require the file because if we import it, it will be included in the bundle.
Expand Down Expand Up @@ -61,59 +60,18 @@ export function enrichWithThreadInformation(profile: ThreadCpuProfile | RawThrea
};
}

/**
* Creates a profiling event envelope from a Sentry event. If profile does not pass
* validation, returns null.
* @param {Event}
* @returns {Profile | null}
*/
export function createProfilingEventFromTransaction(event: ProfiledEvent): Profile | null {
if (event.type !== 'transaction') {
// createProfilingEventEnvelope should only be called for transactions,
// we type guard this behavior with isProfiledTransactionEvent.
throw new TypeError('Profiling events may only be attached to transactions, this should never occur.');
}

const rawProfile = event.sdkProcessingMetadata['profile'];
if (rawProfile === undefined || rawProfile === null) {
throw new TypeError(
`Cannot construct profiling event envelope without a valid profile. Got ${rawProfile} instead.`,
);
}

if (!rawProfile.profile_id) {
throw new TypeError(
`Cannot construct profiling event envelope without a valid profile id. Got ${rawProfile.profile_id} instead.`,
);
}

if (!isValidProfile(rawProfile)) {
return null;
}

return createProfilePayload(rawProfile, {
release: event.release ?? '',
environment: event.environment ?? '',
event_id: event.event_id ?? '',
transaction: event.transaction ?? '',
start_timestamp: event.start_timestamp ? event.start_timestamp * 1000 : Date.now(),
trace_id: event.contexts?.['trace']?.['trace_id'] ?? '',
profile_id: rawProfile.profile_id,
});
}

/**
* Creates a profiling envelope item, if the profile does not pass validation, returns null.
* @param {RawThreadCpuProfile}
* @param {Event}
* @returns {Profile | null}
*/
export function createProfilingEvent(profile: RawThreadCpuProfile, event: Event): Profile | null {
export function createProfilingEvent(client: Client, profile: RawThreadCpuProfile, event: Event): Profile | null {
if (!isValidProfile(profile)) {
return null;
}

return createProfilePayload(profile, {
return createProfilePayload(client, profile, {
release: event.release ?? '',
environment: event.environment ?? '',
event_id: event.event_id ?? '',
Expand All @@ -132,6 +90,7 @@ export function createProfilingEvent(profile: RawThreadCpuProfile, event: Event)
*/

function createProfilePayload(
client: Client,
cpuProfile: RawThreadCpuProfile,
{
release,
Expand Down Expand Up @@ -185,7 +144,7 @@ function createProfilePayload(
is_emulator: false,
},
debug_meta: {
images: applyDebugMetadata(cpuProfile.resources),
images: applyDebugMetadata(client, cpuProfile.resources),
},
profile: enrichedThreadProfile,
transaction: {
Expand Down Expand Up @@ -310,18 +269,13 @@ const debugIdStackParserCache = new WeakMap<StackParser, Map<string, StackFrame[
* @param {string[]} resource_paths
* @returns {DebugImage[]}
*/
export function applyDebugMetadata(resource_paths: ReadonlyArray<string>): DebugImage[] {
export function applyDebugMetadata(client: Client, resource_paths: ReadonlyArray<string>): DebugImage[] {
const debugIdMap = GLOBAL_OBJ._sentryDebugIds;

if (!debugIdMap) {
return [];
}

// eslint-disable-next-line deprecation/deprecation
const hub = getCurrentHub();
// eslint-disable-next-line deprecation/deprecation
const client = hub.getClient();
const options = client && client.getOptions();
const options = client.getOptions();

if (!options || !options.stackParser) {
return [];
Expand Down