Skip to content

feat(profiling): Drop profiler ids from spans no profile covers - #6015

Draft
markushi wants to merge 4 commits into
mainfrom
feat/drop-invalid-profiler-ids-poll
Draft

feat(profiling): Drop profiler ids from spans no profile covers#6015
markushi wants to merge 4 commits into
mainfrom
feat/drop-invalid-profiler-ids-poll

Conversation

@markushi

Copy link
Copy Markdown
Member

📜 Description

Transactions and spans are tagged with the continuous profiler's profiler_id as soon as they start, but the OS only tells us later whether a Perfetto profile really exists. This PR lets anything tagged with an id that leads nowhere drop the reference before it is sent.

IContinuousProfiler gets one new method:

@NotNull ProfileRecordingState getProfileRecordingState(
    SentryId profilerId, SentryDate startTime, SentryDate endTime);

SentryTracer.finish() asks it once for the root span window and once per child span window, and removes the ProfileContext plus the profiler_id span data only on NOT_RECORDED. RECORDED and UNKNOWN change nothing, so an outcome we do not know never costs a valid link.

PerfettoContinuousProfiler answers from a bounded history of chunk records, guarded by a lock of its own so that a finishing transaction never waits for a chunk start, a chunk stop or an OS callback. PerfettoProfiler creates each record in start() and writes the outcome into it as soon as it knows: an OS error code, a missing or empty trace file, or the result timeout. NOT_RECORDED is final, so a result the OS delivers late cannot revive a chunk that was already given up on.

NoOpContinuousProfiler, AndroidContinuousProfiler and JavaContinuousProfiler answer UNKNOWN, which leaves every non-Perfetto path exactly as it was.

This is an alternative to #5993, which solved the same problem with callbacks from the profiler into the tracer. Pulling the state at send time removes the listener registration, the unregistration on finish, and the tracers that stay registered because they never finish.

💡 Motivation and Context

Rate limiting is the common case on API 35+, and the OS reports it roughly 1 ms after the request. Without this, a rate-limited session produces transactions that link to profiles the backend never receives, which shows up in the UI as dangling profile references. There is no backend logic that removes such references.

💚 How did you test it?

Unit tests, on three levels:

  • ChunkRecordTest — the state machine (NOT_RECORDED is final) and the window arithmetic, including the exact boundaries and a chunk that is still running.
  • PerfettoProfilerTest — the record outcome for an OS error, a null result path, a missing trace file, the result timeout, and a result that arrives after the timeout.
  • PerfettoContinuousProfilerTest — the answer for a running chunk, a collected chunk, a failed chunk, a window after the last chunk, a window that starts before the profiler did, per-chunk judgement across a failed and a recorded chunk, eviction, and close.
  • SentryTracerTest — dropped, kept and unknown outcomes, per-span judgement, a span that never finished, and a profiler_id the SDK did not write.

📝 Checklist

  • I added GH Issue ID & Linear ID
  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.
  • Public API changes reviewed by another Mobile SDK team member or implemented according to the develop docs spec.

🔮 Next steps

Known limits, left out deliberately:

  • PerfettoProfiler.start(long) was public and is now package-private, since it returns the internal chunk record. The class is @ApiStatus.Internal and @RequiresApi(35), but the method was on the published surface in 8.51.0–8.53.0.
  • Only SentryTracer drops the id. OpenTelemetry spans take another path and are not covered. This has no effect today, as only the Android Perfetto profiler ever answers anything but UNKNOWN.
  • Chunks that Sentry itself drops after a valid trace file was produced — a client-side rate limit, or cache eviction while offline — still read as recorded. The scope here is the OS reporting that no profile exists.
  • The chunk history holds 10 chunks, so about 10 minutes. A transaction older than that is judged by the chunks that are left.
  • A profiler id from a profiler instance that a second Sentry.init replaced reads as NOT_RECORDED. Keeping such ids alive belongs to whatever owns the profiler lifecycle across inits, not to the chunk history.

markushi and others added 2 commits August 27, 2026 10:24
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Aug 27, 2026

Copy link
Copy Markdown
Contributor
Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Generated by 🚫 dangerJS against 31b6ce5

@sentry

sentry Bot commented Aug 27, 2026

Copy link
Copy Markdown

📲 Install Builds

Android

🔗 App Name App ID Version Configuration
SDK Size io.sentry.tests.size 8.54.0 (1) release

⚙️ sentry-android Build Distribution Settings

@markushi

Copy link
Copy Markdown
Member Author

@sentry review

@0xadam-brown 0xadam-brown left a comment

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.

Nice! Much easier to follow, IMO. A few initial comments. Haven't checked for any threading issues. But the APIs are looking clean 💯


private final @NotNull ArrayDeque<ChunkRecord> chunkHistory =
new ArrayDeque<>(MAX_CHUNK_HISTORY_SIZE);
private @Nullable ChunkRecord currentChunk = null;

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.

l: Can we just use chunkHistory.peekLast() instead?

if (chunk.getProfilerId().equals(profilerId)
&& chunk.overlaps(startTime, endTime)
&& chunk.getRecordingState() != ProfileRecordingState.NOT_RECORDED) {
return ProfileRecordingState.RECORDED;

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.

m: Thoughts about updating our approach to match the following policy?

Scenario Returned value
No chunks have ever run UNKNOWN
No recorded chunk overlaps window but one or more overlapping chunk remain undecided UNKNOWN
At least one overlapping chunk was recorded RECORDED
Overlapping chunks exist, but all failed NOT_RECORDED
History exists, but no chunk for that profiler/window overlaps NOT_RECORDED

We could implement it like this:

 @Override                                                                                                                                                                                                                                                                                                                                               
     public @NotNull ProfileRecordingState getProfileRecordingState(                                                                                                                                                                                                                                                                                         
         final @NotNull SentryId profilerId,                                                                                                                                                                                                                                                                                                                 
         final @NotNull SentryDate startTime,                                                                                                                                                                                                                                                                                                                
         final @NotNull SentryDate endTime) {                                                                                                                                                                                                                                                                                                                
       try (final @NotNull ISentryLifecycleToken ignored = chunkHistoryLock.acquire()) {                                                                                                                                                                                                                                                                     
         if (chunkHistory.isEmpty()) {                                                                                                                                                                                                                                                                                                                       
           return ProfileRecordingState.UNKNOWN;                                                                                                                                                                                                                                                                                                             
         }                                                                                                                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                                                                                                                             
         boolean hasUnknownOverlappingChunk = false;                                                                                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                                                                                                                             
         for (final @NotNull ChunkRecord chunk : chunkHistory) {                                                                                                                                                                                                                                                                                             
           if (!chunk.getProfilerId().equals(profilerId) || !chunk.overlaps(startTime, endTime)) {                                                                                                                                                                                                                                                           
             continue;                                                                                                                                                                                                                                                                                                                                       
           }                                                                                                                                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                                                                                                                             
           final @NotNull ProfileRecordingState state = chunk.getRecordingState();                                                                                                                                                                                                                                                                           
           if (state == ProfileRecordingState.RECORDED) {                                                                                                                                                                                                                                                                                                    
             return ProfileRecordingState.RECORDED;                                                                                                                                                                                                                                                                                                          
           }                                                                                                                                                                                                                                                                                                                                                 
           if (state == ProfileRecordingState.UNKNOWN) {                                                                                                                                                                                                                                                                                                     
             hasUnknownOverlappingChunk = true;                                                                                                                                                                                                                                                                                                              
           }                                                                                                                                                                                                                                                                                                                                                 
         }                  

         if (hasUnknownOverlappingChunk) {                                                                                                                                                                                                                                                                                                                   
           return ProfileRecordingState.UNKNOWN;                                                                                                                                                                                                                                                                                                             
         }                                                                                                                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                                                                                                                             
         return ProfileRecordingState.NOT_RECORDED;                                                                                                                                                                                                                                                                                                          
       }                                                                                                                                                                                                                                                                                                                                                     
     }                       

That'd let us avoid returning RECORDED in situations where we don't actually know yet. (Doesn't change current behavior b/c we only act on NOT_RECORDED, but it could matter going forward.)

final @Nullable ChunkRecord chunkRecord =
perfettoProfiler.start(startProfileChunkTimestamp, profilerId, MAX_CHUNK_DURATION_MILLIS);
if (chunkRecord == null) {
profilerId = SentryId.EMPTY_ID;

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.

m: What's the thinking behind updating the profilerId?

Fwiw, I would've figured we'd keep the profilerId intact and let the ProfileRecordingState inform the outside world about the success vs failure of recording – but perhaps I'm missing something?

if (record != null) {
final int errorCode = result.getErrorCode();
record.setRecordingState(
errorCode == ProfilingResult.ERROR_NONE

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.

m: Should this be:

if (record != null && result.getErrorCode() != ProfilingResult.ERROR_NONE) {                                                                                                                                                                                                                                                                          
         record.setRecordingState(ProfileRecordingState.NOT_RECORDED);                                                                                                                                                                                                                                                                                       
       }   

Worried that we're claiming the profile has been recorded because we haven't seen an error.

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.

2 participants