Skip to content

Commit

Permalink
ref(profiling-node): Remove usage of getCurrentHub (getsentry#11275)
Browse files Browse the repository at this point in the history
Building on top of
getsentry#11239, this removes
usage of `getCurrentHub` in `profiling-node` package by passing in the
defined client into the function.

This also removes the `createProfilingEventFromTransaction`, which was
totally unused.
  • Loading branch information
AbhiPrasad authored and cadesalaberry committed Apr 19, 2024
1 parent 2972ae9 commit be5678d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 55 deletions.
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

0 comments on commit be5678d

Please sign in to comment.