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

fix(profiling): Create profiles for start up transactions #3281

Merged
merged 4 commits into from
Sep 15, 2023
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
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