Skip to content

Commit 38865c2

Browse files
committed
Merge branch 'main' into markushi/fix/compose-110-api-changes
2 parents 3559ed4 + 37ec571 commit 38865c2

File tree

9 files changed

+210
-13
lines changed

9 files changed

+210
-13
lines changed
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
---
2+
alwaysApply: false
3+
description: JVM Continuous Profiling (sentry-async-profiler)
4+
---
5+
# JVM Continuous Profiling
6+
7+
Use this rule when working on JVM continuous profiling in `sentry-async-profiler` and the related core profiling abstractions in `sentry`.
8+
9+
This area is suitable for LLM work, but do not rely on this rule alone for behavior changes. Always read the implementation and nearby tests first, especially for sampling, lifecycle, rate limiting, and file cleanup behavior.
10+
11+
## Module Structure
12+
13+
- **`sentry-async-profiler`**: standalone module containing the async-profiler integration
14+
- Uses Java `ServiceLoader` discovery
15+
- No direct dependency from core `sentry` module
16+
- Enabled by adding the module as a dependency
17+
18+
- **`sentry` core abstractions**:
19+
- `IContinuousProfiler`: profiler lifecycle interface
20+
- `ProfileChunk`: profile chunk payload sent to Sentry
21+
- `IProfileConverter`: converts JVM JFR files into `SentryProfile`
22+
- `ProfileLifecycle`: controls MANUAL vs TRACE lifecycle
23+
- `ProfilingServiceLoader`: loads profiler and converter implementations via `ServiceLoader`
24+
25+
## Key Classes
26+
27+
### `JavaContinuousProfiler` (`sentry-async-profiler`)
28+
- Wraps the native async-profiler library
29+
- Writes JFR files to `profilingTracesDirPath`
30+
- Rotates chunks periodically via `MAX_CHUNK_DURATION_MILLIS` (currently 10s)
31+
- Implements `RateLimiter.IRateLimitObserver`
32+
- Maintains `rootSpanCounter` for TRACE lifecycle
33+
- Keeps a session-level `profilerId` across chunks until the profiling session ends
34+
- `getChunkId()` currently returns `SentryId.EMPTY_ID`, but emitted `ProfileChunk`s get a fresh chunk id when built in `stop(...)`
35+
36+
### `ProfileChunk`
37+
- Carries `profilerId`, `chunkId`, timestamp, platform, measurements, and a JFR file reference
38+
- Built via `ProfileChunk.Builder`
39+
- For JVM, the JFR file is converted later during envelope item creation, not inside `JavaContinuousProfiler`
40+
41+
### `ProfileLifecycle`
42+
- `MANUAL`: explicit `Sentry.startProfiler()` / `Sentry.stopProfiler()`
43+
- `TRACE`: profiler lifecycle follows active sampled root spans
44+
45+
## Configuration
46+
47+
Continuous profiling is **not** controlled by `profilesSampleRate`.
48+
49+
Key options:
50+
- **`profileSessionSampleRate`**: session-level sample rate for continuous profiling
51+
- **`profileLifecycle`**: `ProfileLifecycle.MANUAL` (default) or `ProfileLifecycle.TRACE`
52+
- **`cacheDirPath`**: base SDK cache directory; profiling traces are written under the derived `profilingTracesDirPath`
53+
- **`profilingTracesHz`**: sampling frequency in Hz (default: 101)
54+
55+
Continuous profiling is enabled when:
56+
- `profilesSampleRate == null`
57+
- `profilesSampler == null`
58+
- `profileSessionSampleRate != null && profileSessionSampleRate > 0`
59+
60+
Example:
61+
62+
```java
63+
options.setProfileSessionSampleRate(1.0);
64+
options.setCacheDirPath("/tmp/sentry-cache");
65+
options.setProfileLifecycle(ProfileLifecycle.MANUAL);
66+
options.setProfilingTracesHz(101);
67+
```
68+
69+
## How It Works
70+
71+
### Initialization
72+
- `InitUtil.initializeProfiler(...)` resolves or creates the profiling traces directory
73+
- `ProfilingServiceLoader.loadContinuousProfiler(...)` uses `ServiceLoader` to find `JavaContinuousProfilerProvider`
74+
- `AsyncProfilerContinuousProfilerProvider` instantiates `JavaContinuousProfiler`
75+
- `ProfilingServiceLoader.loadProfileConverter()` separately loads the `JavaProfileConverterProvider`
76+
77+
### Profiling Flow
78+
79+
**Start**
80+
- Sampling decision is made via `TracesSampler.sampleSessionProfile(...)`
81+
- Sampling is session-based and cached until `reevaluateSampling()`
82+
- Scopes and rate limiter are initialized lazily via `initScopes()`
83+
- Rate limits for `All` or `ProfileChunk` abort startup
84+
- JFR filename is generated under `profilingTracesDirPath`
85+
- async-profiler is started with a command like:
86+
- `start,jfr,event=wall,nobatch,interval=<interval>,file=<path>`
87+
- Automatic chunk stop is scheduled after `MAX_CHUNK_DURATION_MILLIS`
88+
89+
**Chunk Rotation**
90+
- `stop(true)` stops async-profiler and validates the JFR file
91+
- A `ProfileChunk.Builder` is created with:
92+
- current `profilerId`
93+
- a fresh `chunkId`
94+
- trace file
95+
- chunk timestamp
96+
- platform `java`
97+
- Builder is buffered in `payloadBuilders`
98+
- Chunks are sent if scopes are available
99+
- Profiling is restarted for the next chunk
100+
101+
**Stop**
102+
- `MANUAL`: stop immediately, do not restart, reset `profilerId`
103+
- `TRACE`: decrement `rootSpanCounter`; stop only when it reaches 0
104+
- `close(...)` also forces shutdown and resets TRACE state
105+
106+
### Sending and Conversion
107+
- `JavaContinuousProfiler` buffers `ProfileChunk.Builder` instances
108+
- `sendChunks(...)` builds `ProfileChunk` objects and calls `scopes.captureProfileChunk(...)`
109+
- `SentryClient.captureProfileChunk(...)` creates an envelope item
110+
- JVM JFR-to-`SentryProfile` conversion happens in `SentryEnvelopeItem.fromProfileChunk(...)` using the loaded `IProfileConverter`
111+
- Trace files are deleted in the envelope item path after serialization attempts
112+
113+
## TRACE Mode Lifecycle
114+
- `rootSpanCounter` increments when sampled root spans start
115+
- `rootSpanCounter` decrements when root spans finish
116+
- Profiler runs while `rootSpanCounter > 0`
117+
- Multiple concurrent sampled transactions can share the same profiling session
118+
- Be careful when changing lifecycle logic: this area is lock-protected and concurrency-sensitive
119+
120+
## Rate Limiting and Buffering
121+
122+
### Rate Limiting
123+
- Registers as a `RateLimiter.IRateLimitObserver`
124+
- If rate limited for `ProfileChunk` or `All`:
125+
- profiler stops immediately
126+
- it does not auto-restart when the limit expires
127+
- Startup also checks rate limiting before profiling begins
128+
129+
### Buffering / pre-init behavior
130+
- JFR files are written to `profilingTracesDirPath` and marked `deleteOnExit()` when a chunk is accepted
131+
- If scopes are not yet available, `ProfileChunk.Builder`s remain buffered in memory in `payloadBuilders`
132+
- This commonly matters for profiling that starts before SDK scopes are ready
133+
- This is not a dedicated durable offline queue owned by the profiler itself; conversion and final send happen later in the normal client/envelope path
134+
135+
## Extending
136+
137+
To add or replace JVM profiler implementations:
138+
- implement `IContinuousProfiler`
139+
- implement `JavaContinuousProfilerProvider`
140+
- register provider in:
141+
- `META-INF/services/io.sentry.profiling.JavaContinuousProfilerProvider`
142+
143+
To add or replace JVM profile conversion:
144+
- implement `IProfileConverter`
145+
- implement `JavaProfileConverterProvider`
146+
- register provider in:
147+
- `META-INF/services/io.sentry.profiling.JavaProfileConverterProvider`
148+
149+
## Code Locations
150+
151+
Primary implementation:
152+
- `sentry/src/main/java/io/sentry/IContinuousProfiler.java`
153+
- `sentry/src/main/java/io/sentry/ProfileChunk.java`
154+
- `sentry/src/main/java/io/sentry/profiling/ProfilingServiceLoader.java`
155+
- `sentry/src/main/java/io/sentry/util/InitUtil.java`
156+
- `sentry/src/main/java/io/sentry/SentryEnvelopeItem.java`
157+
- `sentry-async-profiler/src/main/java/io/sentry/asyncprofiler/profiling/JavaContinuousProfiler.java`
158+
- `sentry-async-profiler/src/main/java/io/sentry/asyncprofiler/provider/AsyncProfilerContinuousProfilerProvider.java`
159+
- `sentry-async-profiler/src/main/java/io/sentry/asyncprofiler/provider/AsyncProfilerProfileConverterProvider.java`
160+
- `sentry-async-profiler/src/main/java/io/sentry/asyncprofiler/convert/JfrAsyncProfilerToSentryProfileConverter.java`
161+
162+
Tests to read first:
163+
- `sentry-async-profiler/src/test/java/io/sentry/asyncprofiler/profiling/JavaContinuousProfilerTest.kt`
164+
- `sentry-async-profiler/src/test/java/io/sentry/asyncprofiler/JavaContinuousProfilingServiceLoaderTest.kt`
165+
- `sentry-async-profiler/src/test/java/io/sentry/asyncprofiler/convert/JfrAsyncProfilerToSentryProfileConverterTest.kt`
166+
167+
## LLM Guidance
168+
169+
This rule is good enough for orientation, but for actual code changes always verify:
170+
- the sampling path in `TracesSampler`
171+
- continuous profiling enablement in `SentryOptions`
172+
- lifecycle entry points in `Scopes` and `SentryTracer`
173+
- conversion and file deletion behavior in `SentryEnvelopeItem`
174+
- existing tests before changing concurrency or lifecycle semantics

.cursor/rules/overview_dev.mdc

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Use the `fetch_rules` tool to include these rules when working on specific areas
3030

3131
- **`scopes`**: Use when working with:
3232
- Hub/Scope management, forking, or lifecycle
33-
- `Sentry.getCurrentScopes()`, `pushScope()`, `withScope()`
33+
- `Sentry.getCurrentScopes()`, `pushScope()`, `withScope()`
3434
- `ScopeType` (GLOBAL, ISOLATION, CURRENT)
3535
- Thread-local storage, scope bleeding issues
3636
- Migration from Hub API (v7 → v8)
@@ -66,6 +66,18 @@ Use the `fetch_rules` tool to include these rules when working on specific areas
6666
- `SentryMetricsEvent`, `SentryMetricsEvents`
6767
- `SentryOptions.getMetrics()`, `beforeSend` callback
6868

69+
- **`continuous_profiling_jvm`**: Use when working with:
70+
- JVM continuous profiling (`sentry-async-profiler` module)
71+
- `IContinuousProfiler`, `JavaContinuousProfiler`
72+
- `ProfileChunk`, chunk rotation, JFR file handling
73+
- `ProfileLifecycle` (MANUAL vs TRACE modes)
74+
- async-profiler integration, ServiceLoader discovery
75+
- Rate limiting, offline caching, scopes integration
76+
77+
- **Android profiling**: There is currently no dedicated rule for this area yet.
78+
- Inspect the relevant `sentry-android-core` profiling code directly
79+
- Fetch other related rules as needed (for example `options`, `offline`, or `api`)
80+
6981
### Integration & Infrastructure
7082
- **`opentelemetry`**: Use when working with:
7183
- OpenTelemetry modules (`sentry-opentelemetry-*`)
@@ -99,11 +111,13 @@ Use the `fetch_rules` tool to include these rules when working on specific areas
99111
- Public API/apiDump/.api files/binary compatibility/new method → `api`
100112
- Options/SentryOptions/ExternalOptions/ManifestMetadataReader/sentry.properties → `options`
101113
- Scope/Hub/forking → `scopes`
102-
- Duplicate/dedup → `deduplication`
114+
- Duplicate/dedup → `deduplication`
103115
- OpenTelemetry/tracing/spans → `opentelemetry`
104116
- new module/integration/sample → `new_module`
105117
- Cache/offline/network → `offline`
106118
- System test/e2e/sample → `e2e_tests`
107119
- Feature flag/addFeatureFlag/flag evaluation → `feature_flags`
108120
- Metrics/count/distribution/gauge → `metrics`
109121
- PR/pull request/stacked PR/stack → `pr`
122+
- JVM continuous profiling/async-profiler/JFR/ProfileChunk → `continuous_profiling_jvm`
123+
- Android continuous profiling/AndroidProfiler/frame metrics/method tracing → no dedicated rule yet; inspect the code directly

.github/workflows/agp-matrix.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ jobs:
6060

6161
- name: Create AVD and generate snapshot for caching
6262
if: steps.avd-cache.outputs.cache-hit != 'true'
63-
uses: reactivecircus/android-emulator-runner@b530d96654c385303d652368551fb075bc2f0b6b # pin@v2
63+
uses: reactivecircus/android-emulator-runner@e89f39f1abbbd05b1113a29cf4db69e7540cae5a # pin@v2
6464
with:
6565
api-level: 30
6666
target: aosp_atd
@@ -79,7 +79,7 @@ jobs:
7979

8080
# We tried to use the cache action to cache gradle stuff, but it made tests slower and timeout
8181
- name: Run instrumentation tests
82-
uses: reactivecircus/android-emulator-runner@b530d96654c385303d652368551fb075bc2f0b6b # pin@v2
82+
uses: reactivecircus/android-emulator-runner@e89f39f1abbbd05b1113a29cf4db69e7540cae5a # pin@v2
8383
with:
8484
api-level: 30
8585
target: aosp_atd

.github/workflows/changes-in-high-risk-code.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
- uses: actions/checkout@v6
2020
- name: Get changed files
2121
id: changes
22-
uses: dorny/paths-filter@de90cc6fb38fc0963ad72b210f1f284cd68cea36 # v3.0.2
22+
uses: dorny/paths-filter@fbd0ab8f3e69293af611ebaee6363fc25e6d187d # v4.0.1
2323
with:
2424
token: ${{ github.token }}
2525
filters: .github/file-filters.yml

.github/workflows/codeql-analysis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
cache-encryption-key: ${{ secrets.GRADLE_ENCRYPTION_KEY }}
3737

3838
- name: Initialize CodeQL
39-
uses: github/codeql-action/init@89a39a4e59826350b863aa6b6252a07ad50cf83e # pin@v2
39+
uses: github/codeql-action/init@0d579ffd059c29b07949a3cce3983f0780820c98 # pin@v2
4040
with:
4141
languages: 'java'
4242

@@ -45,4 +45,4 @@ jobs:
4545
./gradlew buildForCodeQL --no-build-cache
4646
4747
- name: Perform CodeQL Analysis
48-
uses: github/codeql-action/analyze@89a39a4e59826350b863aa6b6252a07ad50cf83e # pin@v2
48+
uses: github/codeql-action/analyze@0d579ffd059c29b07949a3cce3983f0780820c98 # pin@v2

.github/workflows/integration-tests-ui-critical.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ jobs:
100100

101101
- name: Create AVD and generate snapshot for caching
102102
if: steps.avd-cache.outputs.cache-hit != 'true'
103-
uses: reactivecircus/android-emulator-runner@b530d96654c385303d652368551fb075bc2f0b6b # pin@v2
103+
uses: reactivecircus/android-emulator-runner@e89f39f1abbbd05b1113a29cf4db69e7540cae5a # pin@v2
104104
with:
105105
api-level: ${{ matrix.api-level }}
106106
target: ${{ matrix.target }}
@@ -124,7 +124,7 @@ jobs:
124124
version: ${{env.MAESTRO_VERSION}}
125125

126126
- name: Run tests
127-
uses: reactivecircus/android-emulator-runner@b530d96654c385303d652368551fb075bc2f0b6b # pin@v2.35.0
127+
uses: reactivecircus/android-emulator-runner@e89f39f1abbbd05b1113a29cf4db69e7540cae5a # pin@v2.37.0
128128
with:
129129
api-level: ${{ matrix.api-level }}
130130
target: ${{ matrix.target }}

.github/workflows/release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
steps:
2424
- name: Get auth token
2525
id: token
26-
uses: actions/create-github-app-token@29824e69f54612133e76f7eaac726eef6c875baf # v2.2.1
26+
uses: actions/create-github-app-token@f8d387b68d61c58ab83c6c016672934102569859 # v3.0.0
2727
with:
2828
app-id: ${{ vars.SENTRY_RELEASE_BOT_CLIENT_ID }}
2929
private-key: ${{ secrets.SENTRY_RELEASE_BOT_PRIVATE_KEY }}
@@ -34,7 +34,7 @@ jobs:
3434
fetch-depth: 0
3535
submodules: 'recursive'
3636
- name: Prepare release
37-
uses: getsentry/craft@d4cfac9d25d1fc72c9241e5d22aff559a114e4e9 # v2
37+
uses: getsentry/craft@013a7b2113c2cac0ff32d5180cfeaefc7c9ce5b6 # v2
3838
env:
3939
GITHUB_TOKEN: ${{ steps.token.outputs.token }}
4040
with:

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66

77
- Support masking/unmasking and click/scroll detection for Jetpack Compose 1.10+ ([#5189](https://github.com/getsentry/sentry-java/pull/5189))
88

9+
### Dependencies
10+
11+
- Bump Native SDK from v0.13.1 to v0.13.2 ([#5181](https://github.com/getsentry/sentry-java/pull/5181))
12+
- [changelog](https://github.com/getsentry/sentry-native/blob/master/CHANGELOG.md#0132)
13+
- [diff](https://github.com/getsentry/sentry-native/compare/0.13.1...0.13.2)
14+
- Bump `com.abovevacant:epitaph` to `0.1.1` to avoid old D8/R8 dexing crashes in downstream Android builds on old AGP versions such as 7.4.x. ([#5200](https://github.com/getsentry/sentry-java/pull/5200))
15+
- [changelog](https://github.com/abovevacant/epitaph/blob/main/CHANGELOG.md#011---2026-03-16)
16+
- [diff](https://github.com/abovevacant/epitaph/compare/v0.1.0...v0.1.1)
17+
918
## 8.35.0
1019

1120
### Fixes

gradle/libs.versions.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,12 @@ otel-javaagent-extension-api = { module = "io.opentelemetry.javaagent:openteleme
143143
otel-semconv = { module = "io.opentelemetry.semconv:opentelemetry-semconv", version.ref = "otelSemanticConventions" }
144144
otel-semconv-incubating = { module = "io.opentelemetry.semconv:opentelemetry-semconv-incubating", version.ref = "otelSemanticConventionsAlpha" }
145145
p6spy = { module = "p6spy:p6spy", version = "3.9.1" }
146-
epitaph = { module = "com.abovevacant:epitaph", version = "0.1.0" }
146+
epitaph = { module = "com.abovevacant:epitaph", version = "0.1.1" }
147147
quartz = { module = "org.quartz-scheduler:quartz", version = "2.3.0" }
148148
reactor-core = { module = "io.projectreactor:reactor-core", version = "3.5.3" }
149149
retrofit = { module = "com.squareup.retrofit2:retrofit", version.ref = "retrofit" }
150150
retrofit-gson = { module = "com.squareup.retrofit2:converter-gson", version.ref = "retrofit" }
151-
sentry-native-ndk = { module = "io.sentry:sentry-native-ndk", version = "0.13.1" }
151+
sentry-native-ndk = { module = "io.sentry:sentry-native-ndk", version = "0.13.2" }
152152
servlet-api = { module = "javax.servlet:javax.servlet-api", version = "3.1.0" }
153153
servlet-jakarta-api = { module = "jakarta.servlet:jakarta.servlet-api", version = "6.1.0" }
154154
slf4j-api = { module = "org.slf4j:slf4j-api", version.ref = "slf4j" }

0 commit comments

Comments
 (0)