sentry-java / rules
Cursor rule1.4k starsChanged 7 months agoMITView on GitHub
JVM Continuous Profiling (sentry-async-profiler)
Trending
Quiet this year.about the whole repository
Who's talking about getsentry/sentry-java
- getsentry/sentry“[Mobile Replay] Capture Network request/response bodies”GitHub · Feb 5, 2025 · 48 reactions
- quarkusio/quarkus“Sentry tracing with automatic instrumentation”GitHub · Aug 31, 2021 · 19 reactions
- getsentry/sentry-dart“Support Source Context”GitHub · Oct 25, 2020 · 18 reactions
- getsentry/sentry-dotnet“OpenTelemetry Support”GitHub · Nov 25, 2022 · 11 reactions
- getsentry/sentry-dotnet“Feature: Session Replay support for .NET MAUI”GitHub · Jan 30, 2023 · 8 reactions
The biggest 5 of 18 mentions linking to the repository that our crawl found. Each line is their own headline.
See all 18
Talked about on GitHub (18 times).
On GitHub · 18
- Feb 5, 2025[Mobile Replay] Capture Network request/response bodies — getsentry/sentry48 reactions · 13 comments
- Aug 31, 2021Sentry tracing with automatic instrumentation — quarkusio/quarkus19 reactions · 20 comments
- Oct 25, 2020Support Source Context — getsentry/sentry-dart18 reactions · 24 comments
- Nov 25, 2022OpenTelemetry Support — getsentry/sentry-dotnet11 reactions · 4 comments
- Jan 30, 2023Feature: Session Replay support for .NET MAUI — getsentry/sentry-dotnet8 reactions · 9 comments
13 more on GitHub
- Mar 19, 2025Add UI Profiling Support (converted to project) — getsentry/sentry-react-native5 reactions · 3 comments
- Jan 20, 2021Proposal (Performance): Some convenience API to wrap a Promise with a Span/Transaction — getsentry/sentry-javascript5 reactions · 2 comments
- May 1, 2025feat: Initial Android support — getsentry/sentry-godot4 reactions · 3 comments
- Mar 25, 2024feat(replay): Add Mobile Replay Alpha — getsentry/sentry-react-native4 reactions · 8 comments
- Feb 8, 2023Add support for Sentry — spring-io/start.spring.io4 reactions · 6 comments
- May 4, 2022Figure out how to implement hints — getsentry/sentry-javascript4 reactions · 5 comments
- Dec 10, 2020Add basic support for attachments — getsentry/sentry-cocoa4 reactions
- Jul 17, 2025Bump io.sentry.android.gradle from 4.10.0 to 5.8.0 — woocommerce/woocommerce-android3 reactions · 2 comments
- Jul 23, 2024feat: User Feedback widget/form — getsentry/sentry-cocoa3 reactions · 5 comments
- May 1, 2024Remove `enableTracing` (and update docs accordingly) — getsentry/team-sdks3 reactions · 7 comments
- Feb 1, 2024[Epic] Mobile Session Replay — getsentry/sentry3 reactions · 2 comments
- Nov 20, 2023Tracing: W3C Trace Context Support — getsentry/team-sdks3 reactions · 9 comments
- Jan 12, 2023Allow more granular auto installation — getsentry/sentry-android-gradle-plugin3 reactions · 3 comments
Where files are headingNew Cursor rule files have more rules about planning and tracking tasks, and fewer about code style.
Of the Cursor rule files we read, the 1,533 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.
In this repository17 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- .cursor/rules/api.mdcCursor rule
- .cursor/rules/deduplication.mdcCursor rule
- .cursor/rules/e2e_tests.mdcCursor rule
- .cursor/rules/feature_flags.mdcCursor rule
- .cursor/rules/metrics.mdcCursor rule
- .cursor/rules/new_module.mdcCursor rule
- .cursor/rules/offline.mdcCursor rule
- .cursor/rules/opentelemetry.mdcCursor rule
- .cursor/rules/options.mdcCursor rule
- .cursor/rules/queues.mdcCursor rule
- .cursor/rules/scopes.mdcCursor rule
- btrace-perfettoSkill
- check-code-attributionSkill
- create-java-prSkill
- testSkill
The file
We split this continuous_profiling_jvm.mdc into the rules it gives an agent
4 rules. About 0 are common, 0 are distinctive, and 4 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
4
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 7
Use this rule when working on JVM continuous profiling in sentry-async-profiler and the related core profiling abstractions in sentry.
- Very specific: line 15
No direct dependency from core sentry module
- Very specific: line 124
If rate limited for ProfileChunk or All:
- Very specific: line 131
If scopes are not yet available, ProfileChunk.Builders remain buffered in memory in payloadBuilders
≈ “Common” is an estimate: at least 1 in 5 of the 3,030 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
---
alwaysApply: false
description: JVM Continuous Profiling (sentry-async-profiler)
---
# JVM Continuous Profiling
Use this rule when working on JVM continuous profiling in `sentry-async-profiler` and the related core profiling abstractions in `sentry`.
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.
## Module Structure
- **`sentry-async-profiler`**: standalone module containing the async-profiler integration
- Uses Java `ServiceLoader` discovery
- No direct dependency from core `sentry` module
- Enabled by adding the module as a dependency
- **`sentry` core abstractions**:
- `IContinuousProfiler`: profiler lifecycle interface
- `ProfileChunk`: profile chunk payload sent to Sentry
- `IProfileConverter`: converts JVM JFR files into `SentryProfile`
- `ProfileLifecycle`: controls MANUAL vs TRACE lifecycle
- `ProfilingServiceLoader`: loads profiler and converter implementations via `ServiceLoader`
## Key Classes
### `JavaContinuousProfiler` (`sentry-async-profiler`)
- Wraps the native async-profiler library
- Writes JFR files to `profilingTracesDirPath`
- Rotates chunks periodically via `MAX_CHUNK_DURATION_MILLIS` (currently 10s)
- Implements `RateLimiter.IRateLimitObserver`
- Maintains `rootSpanCounter` for TRACE lifecycle
- Keeps a session-level `profilerId` across chunks until the profiling session ends
- `getChunkId()` currently returns `SentryId.EMPTY_ID`, but emitted `ProfileChunk`s get a fresh chunk id when built in `stop(...)`
### `ProfileChunk`
- Carries `profilerId`, `chunkId`, timestamp, platform, measurements, and a JFR file reference
- Built via `ProfileChunk.Builder`
- For JVM, the JFR file is converted later during envelope item creation, not inside `JavaContinuousProfiler`
### `ProfileLifecycle`
- `MANUAL`: explicit `Sentry.startProfiler()` / `Sentry.stopProfiler()`
- `TRACE`: profiler lifecycle follows active sampled root spans
## Configuration
Continuous profiling is **not** controlled by `profilesSampleRate`.
Key options:
- **`profileSessionSampleRate`**: session-level sample rate for continuous profiling
- **`profileLifecycle`**: `ProfileLifecycle.MANUAL` (default) or `ProfileLifecycle.TRACE`
- **`cacheDirPath`**: base SDK cache directory; profiling traces are written under the derived `profilingTracesDirPath`
- **`profilingTracesHz`**: sampling frequency in Hz (default: 101)
Continuous profiling is enabled when:
- `profilesSampleRate == null`
- `profilesSampler == null`
- `profileSessionSampleRate != null && profileSessionSampleRate > 0`
Example:
```java
options.setProfileSessionSampleRate(1.0);
options.setCacheDirPath("/tmp/sentry-cache");
options.setProfileLifecycle(ProfileLifecycle.MANUAL);
options.setProfilingTracesHz(101);
```
## How It Works
### Initialization
- `InitUtil.initializeProfiler(...)` resolves or creates the profiling traces directory
- `ProfilingServiceLoader.loadContinuousProfiler(...)` uses `ServiceLoader` to find `JavaContinuousProfilerProvider`
- `AsyncProfilerContinuousProfilerProvider` instantiates `JavaContinuousProfiler`
- `ProfilingServiceLoader.loadProfileConverter()` separately loads the `JavaProfileConverterProvider`
### Profiling Flow
**Start**
- Sampling decision is made via `TracesSampler.sampleSessionProfile(...)`
- Sampling is session-based and cached until `reevaluateSampling()`
- Scopes and rate limiter are initialized lazily via `initScopes()`
- Rate limits for `All` or `ProfileChunk` abort startup
- JFR filename is generated under `profilingTracesDirPath`
- async-profiler is started with a command like:
- `start,jfr,event=wall,nobatch,interval=<interval>,file=<path>`
- Automatic chunk stop is scheduled after `MAX_CHUNK_DURATION_MILLIS`
**Chunk Rotation**
- `stop(true)` stops async-profiler and validates the JFR file
- A `ProfileChunk.Builder` is created with:
- current `profilerId`
- a fresh `chunkId`
- trace file
- chunk timestamp
- platform `java`
- Builder is buffered in `payloadBuilders`
- Chunks are sent if scopes are available
- Profiling is restarted for the next chunk
**Stop**
- `MANUAL`: stop immediately, do not restart, reset `profilerId`
- `TRACE`: decrement `rootSpanCounter`; stop only when it reaches 0
- `close(...)` also forces shutdown and resets TRACE state
### Sending and Conversion
- `JavaContinuousProfiler` buffers `ProfileChunk.Builder` instances
- `sendChunks(...)` builds `ProfileChunk` objects and calls `scopes.captureProfileChunk(...)`
- `SentryClient.captureProfileChunk(...)` creates an envelope item
- JVM JFR-to-`SentryProfile` conversion happens in `SentryEnvelopeItem.fromProfileChunk(...)` using the loaded `IProfileConverter`
- Trace files are deleted in the envelope item path after serialization attempts
## TRACE Mode Lifecycle
- `rootSpanCounter` increments when sampled root spans start
- `rootSpanCounter` decrements when root spans finish
- Profiler runs while `rootSpanCounter > 0`
- Multiple concurrent sampled transactions can share the same profiling session
- Be careful when changing lifecycle logic: this area is lock-protected and concurrency-sensitive
## Rate Limiting and Buffering
### Rate Limiting
- Registers as a `RateLimiter.IRateLimitObserver`
- If rate limited for `ProfileChunk` or `All`:
- profiler stops immediately
- it does not auto-restart when the limit expires
- Startup also checks rate limiting before profiling begins
### Buffering / pre-init behavior
- JFR files are written to `profilingTracesDirPath` and marked `deleteOnExit()` when a chunk is accepted
- If scopes are not yet available, `ProfileChunk.Builder`s remain buffered in memory in `payloadBuilders`
- This commonly matters for profiling that starts before SDK scopes are ready
- 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
## Extending
To add or replace JVM profiler implementations:
- implement `IContinuousProfiler`
- implement `JavaContinuousProfilerProvider`
- register provider in:
- `META-INF/services/io.sentry.profiling.JavaContinuousProfilerProvider`
To add or replace JVM profile conversion:
- implement `IProfileConverter`
- implement `JavaProfileConverterProvider`
- register provider in:
- `META-INF/services/io.sentry.profiling.JavaProfileConverterProvider`
## Code Locations
Primary implementation:
- `sentry/src/main/java/io/sentry/IContinuousProfiler.java`
- `sentry/src/main/java/io/sentry/ProfileChunk.java`
- `sentry/src/main/java/io/sentry/profiling/ProfilingServiceLoader.java`
- `sentry/src/main/java/io/sentry/util/InitUtil.java`
- `sentry/src/main/java/io/sentry/SentryEnvelopeItem.java`
- `sentry-async-profiler/src/main/java/io/sentry/asyncprofiler/profiling/JavaContinuousProfiler.java`
- `sentry-async-profiler/src/main/java/io/sentry/asyncprofiler/provider/AsyncProfilerContinuousProfilerProvider.java`
- `sentry-async-profiler/src/main/java/io/sentry/asyncprofiler/provider/AsyncProfilerProfileConverterProvider.java`
- `sentry-async-profiler/src/main/java/io/sentry/asyncprofiler/convert/JfrAsyncProfilerToSentryProfileConverter.java`
Tests to read first:
- `sentry-async-profiler/src/test/java/io/sentry/asyncprofiler/profiling/JavaContinuousProfilerTest.kt`
- `sentry-async-profiler/src/test/java/io/sentry/asyncprofiler/JavaContinuousProfilingServiceLoaderTest.kt`
- `sentry-async-profiler/src/test/java/io/sentry/asyncprofiler/convert/JfrAsyncProfilerToSentryProfileConverterTest.kt`
## LLM Guidance
This rule is good enough for orientation, but for actual code changes always verify:
- the sampling path in `TracesSampler`
- continuous profiling enablement in `SentryOptions`
- lifecycle entry points in `Scopes` and `SentryTracer`
- conversion and file deletion behavior in `SentryEnvelopeItem`
- existing tests before changing concurrency or lifecycle semantics
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

