Set Up Profiling

Learn how to enable profiling in your app if it is not already set up.

通过性能分析,Sentry 通过在各种环境中采样程序的调用栈来跟踪软件的性能。此功能收集关于代码的功能级信息,使你能够微调程序的性能。Sentry 的性能分析器捕获函数调用及其确切位置,对它们进行聚合,并显示程序中最常见的代码路径。这突出了你可以优化的区域,以帮助提高代码性能和用户满意度,同时降低成本。

Profiling depends on Sentry’s Tracing product being enabled beforehand. To enable tracing in the SDK:

In AndroidManifest.xml:

Copied
<application>
  <meta-data
    android:name="io.sentry.dsn"
    android:value="https://examplePublicKey@o0.ingest.sentry.io/0"
  />
  <meta-data
    android:name="io.sentry.traces.sample-rate"
    android:value="1.0"
  />
</application>

Check out the tracing setup documentation for more detailed information on how to configure sampling. Setting the sample rate to 1.0 means all transactions will be captured.

By default, some transactions will be created automatically for common operations like loading a view controller/activity and app startup.

Android profiling is available starting in SDK version 6.16.0 and is supported on API level 22 and up. App start profiling is available starting in SDK version 7.3.0.

In AndroidManifest.xml:

Copied
<application>
  <meta-data
    android:name="io.sentry.dsn"
    android:value="https://examplePublicKey@o0.ingest.sentry.io/0"
  />
  <meta-data
    android:name="io.sentry.traces.sample-rate"
    android:value="1.0"
  />
  <meta-data
    android:name="io.sentry.traces.profiling.sample-rate"
    android:value="1.0"
  />
  <meta-data
    android:name="io.sentry.traces.profiling.enable-app-start"
    android:value="true"
  />
</application>

The io.sentry.traces.profiling.sample-rate setting is relative to the io.sentry.traces.sample-rate setting.

When app start profiling is enabled, the whole app start process is profiled. This includes all methods from any ContentProvider, the Application class, and the first Activity, until the first automatic Activity transaction is finished. App start profiling can be enabled with the manifest option io.sentry.traces.profiling.enable-app-start as shown above, and it will respect the io.sentry.traces.sample-rate and the io.sentry.traces.profiling.sample-rate. If you prefer to use a sampling function, the SDK sets the isForNextAppStart field on the TransactionContext to specify it will be used for the next app start profiling.

The SDK won't run app start profiling the very first time the app runs, as the SDK won't have read the options by the time the profile should run. The SDK will set the isForNextAppStart flag in TransactionContext if app start profiling is enabled.