Skip to content

Commit

Permalink
fix(profiling): Create profiles for start up transactions (#3281)
Browse files Browse the repository at this point in the history
  • Loading branch information
krystofwoldrich authored Sep 15, 2023
1 parent be4595b commit 4cc74aa
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 118 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Fixes

- Create profiles for start up transactions ([#3281](https://github.com/getsentry/sentry-react-native/pull/3281))

### Dependencies

- Bump CLI from v2.20.5 to v2.20.7 ([#3265](https://github.com/getsentry/sentry-react-native/pull/3265), [#3273](https://github.com/getsentry/sentry-react-native/pull/3273))
Expand Down
41 changes: 26 additions & 15 deletions src/js/profiling/integration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { Envelope, Event, EventProcessor, Hub, Integration, Profile, Transaction } from '@sentry/types';
import type { Hub } from '@sentry/core';
import { getActiveTransaction } from '@sentry/core';
import type { Envelope, Event, EventProcessor, Integration, Profile, Transaction } from '@sentry/types';
import { logger, uuid4 } from '@sentry/utils';

import { isHermesEnabled } from '../utils/environment';
Expand Down Expand Up @@ -51,21 +53,10 @@ export class HermesProfiling implements Integration {
return;
}

client.on('startTransaction', (transaction: Transaction) => {
this._finishCurrentProfile();
this._startCurrentProfileForActiveTransaction();
client.on('startTransaction', this._startCurrentProfile);

const shouldStartProfiling = this._shouldStartProfiling(transaction);
if (!shouldStartProfiling) {
return;
}

this._currentProfileTimeout = setTimeout(this._finishCurrentProfile, MAX_PROFILE_DURATION_MS);
this._startNewProfile(transaction);
});

client.on('finishTransaction', () => {
this._finishCurrentProfile();
});
client.on('finishTransaction', this._finishCurrentProfile);

client.on('beforeEnvelope', (envelope: Envelope) => {
if (!PROFILE_QUEUE.size()) {
Expand All @@ -89,6 +80,26 @@ export class HermesProfiling implements Integration {
});
}

private _startCurrentProfileForActiveTransaction = (): void => {
if (this._currentProfile) {
return;
}
const transaction = this._getCurrentHub && getActiveTransaction(this._getCurrentHub());
transaction && this._startCurrentProfile(transaction);
};

private _startCurrentProfile = (transaction: Transaction): void => {
this._finishCurrentProfile();

const shouldStartProfiling = this._shouldStartProfiling(transaction);
if (!shouldStartProfiling) {
return;
}

this._currentProfileTimeout = setTimeout(this._finishCurrentProfile, MAX_PROFILE_DURATION_MS);
this._startNewProfile(transaction);
};

private _shouldStartProfiling = (transaction: Transaction): boolean => {
if (!transaction.sampled) {
logger.log('[Profiling] Transaction is not sampled, skipping profiling');
Expand Down
6 changes: 3 additions & 3 deletions src/js/sdk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ export function init(passedOptions: ReactNativeOptions): void {
if (options.enableNative) {
defaultIntegrations.push(new DeviceContext());
}
if (options._experiments && typeof options._experiments.profilesSampleRate === 'number') {
defaultIntegrations.push(new HermesProfiling());
}
if (hasTracingEnabled(options) && options.enableAutoPerformanceTracing) {
defaultIntegrations.push(new ReactNativeTracing());
}
Expand All @@ -133,9 +136,6 @@ export function init(passedOptions: ReactNativeOptions): void {
if (options.enableCaptureFailedRequests) {
defaultIntegrations.push(new HttpClient());
}
if (options._experiments && typeof options._experiments.profilesSampleRate === 'number') {
defaultIntegrations.push(new HermesProfiling());
}
}

options.integrations = getIntegrationsToSetup({
Expand Down
Loading

0 comments on commit 4cc74aa

Please sign in to comment.