Skip to content

Initialize Profiler only once for multiple start calls#3081

Merged
ambushwork merged 1 commit into
feature/perfetto-profilingfrom
yl/profiling/profiler-initialise
Dec 31, 2025
Merged

Initialize Profiler only once for multiple start calls#3081
ambushwork merged 1 commit into
feature/perfetto-profilingfrom
yl/profiling/profiler-initialise

Conversation

@ambushwork

Copy link
Copy Markdown
Member

What does this PR do?

This PR takes one step further to support custom profiling.

The current implementation of Profiling.kt is based on application launch profiling, which presumes the Profiling.start is called only once and always called before Profiling.enable.

Which brings following consequences for custom profiling:

  • If Profiling.enable is called before Profiling.start, then ProfilingFeature will be initialized with NoOpProfiler
  • If Profiling.start is called several times, the profiler will be recreated each times, which will be different from the instance held inside ProfilingFeature

Review checklist (to be filled by reviewers)

  • Feature or bugfix MUST have appropriate tests (unit, integration, e2e)
  • Make sure you discussed the feature or bugfix with the maintaining team in an Issue
  • Make sure each commit and the PR mention the Issue number (cf the CONTRIBUTING doc)

@ambushwork
ambushwork requested a review from a team as a code owner December 29, 2025 16:57

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines 48 to 50
val featureSdkCore = sdkCore as FeatureSdkCore
initializeProfiler()
val profilingFeature = ProfilingFeature(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Guard profiler instantiation on pre-Android 15

Enabling profiling now eagerly calls initializeProfiler() before registering the feature, which constructs PerfettoProfiler (uses ProfilingManager/ProfilingResult) immediately. On devices below Build.VERSION_CODES.VANILLA_ICE_CREAM, calling Profiling.enable will now try to load those Android-15 classes and crash with NoClassDefFoundError/verify errors, whereas before enable left the NoOpProfiler in place and only created PerfettoProfiler when start ran. Consider guarding the initialization by API level or deferring it until profiling is actually supported.

Useful? React with 👍 / 👎.

@datadog-official

datadog-official Bot commented Dec 29, 2025

Copy link
Copy Markdown

🎯 Code Coverage
Patch Coverage: 90.91%
Overall Coverage: 66.47% (-0.03%)

View detailed report

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: f9809b4 | Docs | Datadog PR Page | Was this helpful? Give us feedback!

jonathanmos
jonathanmos previously approved these changes Dec 30, 2025
@ambushwork
ambushwork force-pushed the yl/profiling/profiler-initialise branch from 0357e6e to 07a20ca Compare December 30, 2025 11:07
@codecov-commenter

codecov-commenter commented Dec 30, 2025

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 71.28%. Comparing base (f69769c) to head (f9809b4).
⚠️ Report is 22 commits behind head on feature/perfetto-profiling.

Additional details and impacted files
@@                      Coverage Diff                       @@
##           feature/perfetto-profiling    #3081      +/-   ##
==============================================================
+ Coverage                       71.25%   71.28%   +0.04%     
==============================================================
  Files                             893      893              
  Lines                           32694    32698       +4     
  Branches                         5495     5496       +1     
==============================================================
+ Hits                            23293    23308      +15     
+ Misses                           7838     7827      -11     
  Partials                         1563     1563              
Files with missing lines Coverage Δ
...in/java/com/datadog/android/profiling/Profiling.kt 57.14% <100.00%> (+57.14%) ⬆️
...dog/android/profiling/internal/ProfilingStorage.kt 100.00% <100.00%> (ø)

... and 42 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

// Given
val sdkInstanceNames = setOf(fakeInstanceName)

Mockito.mockStatic(ProfilingStorage::class.java)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed? There is no work with ProfilingStorage inside the block.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, Profiling.start will call ProfilingStorage.removeProfilingFlag(context, sdkInstanceNames) inside.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can also be avoided by simply making ProfilingStorage.sharedPreferencesStorage as internal instead of private and simply setting a mock there.

And by doing this we don't need to mock static.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

@ambushwork
ambushwork force-pushed the yl/profiling/profiler-initialise branch from 07a20ca to 47bb279 Compare December 30, 2025 14:44

@Test
fun `M use PerfettoProfiler W enable called but start not called`() {
Mockito.mockStatic(ProfilingStorage::class.java)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mockito.mockStatic(ProfilingStorage::class.java) calls here and below are not needed due to the ProfilingStorage.sharedPreferencesStorage = mockSharedPreferencesStorage call in the setup.

@ambushwork
ambushwork force-pushed the yl/profiling/profiler-initialise branch from 47bb279 to f9809b4 Compare December 31, 2025 09:08
@ambushwork
ambushwork merged commit 500378e into feature/perfetto-profiling Dec 31, 2025
26 of 27 checks passed
@ambushwork
ambushwork deleted the yl/profiling/profiler-initialise branch December 31, 2025 10:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants