This document describes how continuous profiling works on Android when the SDK
captures traces through the OS-level android.os.ProfilingManager
API (available on API 35+), and how a captured profile chunk flows all the way
from the device to a downloadable profile in Sentry.
Perfetto is Google's tracing framework for Android and Linux, and
the tooling Android itself is instrumented with. Its
callstack sampler interrupts the
app at a fixed frequency, records the native and Java call stacks of the running threads,
and writes them to a binary .pftrace file (a serialized
Perfetto protobuf).
Starting with Android 15, apps can request such traces at
runtime via ProfilingManager without root or adb, which is what makes on-device
continuous profiling possible.
Useful Perfetto references:
- Perfetto docs: https://perfetto.dev/docs/
- CPU profiling with Perfetto: https://perfetto.dev/docs/getting-started/cpu-profiling
- Trace format (
TracePacketproto): https://perfetto.dev/docs/reference/trace-packet-proto - Perfetto UI (to open a downloaded
.pftrace): https://ui.perfetto.dev/
Profile chunks travel the standard ingestion path described in general/pipeline.md — SDK envelope, Relay (Sentry's ingestion proxy), Kafka, a monolith processing task, then storage and a read API. Read that first; the rest of this document covers only where Perfetto deviates from it.
The deviations are:
- The envelope item carries JSON and raw binary in one payload, subdivided by a
meta_lengthheader rather than base64-encoding the trace (details). - Relay converts the Perfetto trace into the existing Sample v2 profile format, and
additionally keeps the raw
.pftracein the object store so it can be downloaded later (details).
flowchart TD
subgraph device["Android device — sentry-java"]
PM[android.os.ProfilingManager]
PP[PerfettoProfiler]
PCP[PerfettoContinuousProfiler]
PC[ProfileChunk]
ENV["Envelope item<br/>[JSON metadata][raw .pftrace]<br/>header: meta_length"]
PM --> PP --> PCP --> PC --> ENV
end
subgraph relay["Relay (processing mode)"]
SPLIT[Split payload at meta_length]
CONV[Convert Perfetto → Sample v2]
OS1[Upload raw .pftrace to object store]
KAFKA[["Kafka topic: profiles<br/>ProfileChunkKafkaMessage<br/>(Sample v2 + attachment stored_id)"]]
SPLIT --> CONV --> KAFKA
SPLIT --> OS1
end
subgraph monolith["Monolith — getsentry/sentry"]
TASK[process_profile_task]
SYM[Symbolicate / deobfuscate]
VR[vroomrs: parse + normalize]
OS2[(Object store)]
SNUBA[(Snuba: function metrics)]
DB[(ProfileChunkAttachment row)]
TASK --> SYM --> VR
VR --> OS2
VR --> SNUBA
TASK --> DB
end
ENV -->|envelope| relay
KAFKA --> TASK
OS1 -.stored_id.-> DB
VROOM[getsentry/vroom<br/>serve + merge flamegraphs]
OS2 --> VROOM
SNUBA --> VROOM
On API 35+, AndroidOptionsInitializer
wires up PerfettoContinuousProfiler automatically. On older devices profiling is disabled
by default. Applications can temporarily opt into the legacy Debug-based
AndroidContinuousProfiler
with the deprecated enableLegacyProfiling option (manifest key
io.sentry.profiling.enable-legacy-profiling). The legacy profiler can cause Android Runtime
crashes and will be removed in version 10. Only continuous profiling is supported on the
Perfetto path — transaction-based and app-start profiling are not.
Continuous profiling emits a stream of independent ProfileChunks
rather than one profile per transaction. PerfettoContinuousProfiler drives a chained
loop: each chunk runs for MAX_CHUNK_DURATION_MILLIS (60s) via PerfettoProfiler, which
calls ProfilingManager.requestProfiling(PROFILING_TYPE_STACK_SAMPLING, …) at
PROFILING_FREQUENCY_HZ (101 Hz). When a chunk's trace file is ready, a new chunk starts,
so profiling runs continuously.
A chunk keeps a stable profilerId across the session and a per-chunk chunkId. When the
OS produces the trace file, the profiler builds a ProfileChunk tagged with the Perfetto
content type:
ProfileChunk.Builder(profilerId, chunkId, measurements, traceFile, timestamp, ProfileChunk.PLATFORM_ANDROID)
.setContentType(ProfileChunk.CONTENT_TYPE_PERFETTO) // "application/x-perfetto-trace"
.build()The chunk is captured via scopes.captureProfileChunk(...) and sent as its own envelope
with item type SentryItemType.ProfileChunk
(wire name profile_chunk).
A legacy chunk base64-encodes its trace into the ProfileChunk JSON. A Perfetto chunk is
much larger, so SentryClient instead
routes it through the new SentryEnvelopeItem.fromPerfettoProfileChunk(...) factory, which
avoids base64 by sending the raw binary alongside the JSON.
The trick is a single envelope item whose payload concatenates the JSON metadata and
the raw .pftrace bytes with no delimiter:
[ProfileChunk JSON bytes][raw .pftrace binary bytes]
A new meta_length property on the envelope item header
tells the server where the JSON ends and the binary begins. The standard envelope item
structure (header line + newline + payload) is unchanged; meta_length simply subdivides
the payload:
{"type":"profile_chunk","content_type":"application/x-perfetto-trace","filename":"…","length":<total>,"meta_length":<json bytes>}
<ProfileChunk JSON><raw perfetto binary>
length— total payload size (JSON + binary), as for any envelope item.meta_length— byte length of the JSON prefix. It is only known after the payload is serialized, so the header computes it lazily (via aCallable<Integer>) and omits the field entirely for non-Perfetto items, keeping the change backward compatible.
In processing mode Relay:
- Splits the compound item payload at
meta_lengthinto(metadata JSON, raw profile)and readscontent_type: "perfetto"from the metadata. - Converts the binary Perfetto trace into the existing Sample v2 profile JSON
format (
relay_profiling::expand_perfetto(...), backed by a checked-in subset of the Perfetto protobuf definitions). - Uploads the raw
.pftraceblob to object store (usecaseprofiles, keyed per org/project, with an attachment-retention TTL). - Produces a
ProfileChunkKafkaMessageto theprofilesKafka topic. The message carries the expanded Sample v2 JSON aspayloadplus anattachmentsarray, where each attachment records:name(e.g.profile.perfetto),content_type(e.g.application/x-perfetto-trace),stored_id— the object store key of the uploaded raw blob.
{
"organization_id": 1,
"project_id": 42,
"received": 1720000000,
"retention_days": 30,
"payload": "<expanded Sample v2 profile JSON>",
"attachments": [
{
"name": "profile.perfetto",
"content_type": "application/x-perfetto-trace",
"stored_id": "<object store key of the raw .pftrace blob>"
}
]
}The monolith later uses stored_id to fetch the raw trace back.
process_profile_task (in src/sentry/profiles/task.py) consumes the profiles topic.
Because Relay already converted the trace to Sample v2, the task treats a Perfetto chunk
like any other: deobfuscate, hand it to vroomrs to parse and normalize
(vroomrs.profile_chunk_from_json_str(...)), compress and store it, and emit function
metrics to Snuba.
The Perfetto-specific step is the last one: for each attachment on the message the task
persists a lightweight ProfileChunkAttachment row — project_id, profiler_id,
chunk_id, name, content_type, and the stored_id object store key. The row exists so
the raw trace can be downloaded by ID without exposing the stored_id.
Flamegraphs themselves are served by getsentry/vroom, which reads the stored chunks and
the Snuba-indexed metadata and merges several chunks into one flamegraph. The endpoint
lives in the monolith and passes the request through.
Older Android SDKs emit the legacy Android trace format tagged as a "faulty" version=2,
and the pipeline historically keyed off the platform rather than the version. To
distinguish legacy from Sample v2 chunks, ProfileChunk carries a dedicated version
field, and both vroom and vroomrs now dispatch on it instead of the platform:
- Version
""or2.android-trace→ legacy Android trace format. - Any other version → Sample v2.
The monolith exposes two feature-gated endpoints:
- List attachments —
GET /organizations/{org}/profiling/chunk-attachments/(sentry-api-0-organization-profiling-chunk-attachments). Requires aprojectandprofiler_id; resolves the visiblechunk_ids (same logic as the flamegraph) and returns the matchingProfileChunkAttachmentmetadata. - Download —
GET /projects/{org}/{project}/profiling/chunks/{profiler_id}/{chunk_id}/attachments/{attachment_id}/?download(sentry-api-0-project-profiling-chunk-attachment). The?downloadparam is required; it streams the raw blob back from object store via the storedstored_id. Access requires the org's configured attachments role, analogous to generic event attachments.
In the flamegraph UI, a toolbar button (added for continuous profiles when the feature is enabled and at least one attachment exists) lists and provides a way to download these traces.
- SDK: sentry-java#5251 — Android
ProfilingManager(Perfetto) support - Relay: #5659, #5932, #6099, #6102 — Perfetto parsing, pipeline, and object-store routing
- vroom: #672 — version dispatch for Android trace profiles
- vroomrs: #93 — accept Android profiles in Sample v2 format
- Monolith: sentry#118029 (chunk attachments + endpoints), sentry#118071 (flamegraph download button)