GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - vunetsystems/pyroscope-java: pyroscope java integration

BranchesTags
Open more actions menu
 
 

Latest commit

 

History

270 Commits
270 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
agent
agent
 
 
async-profiler-context
async-profiler-context
 
 
async-profiler-grafana-fork-dist/lib
async-profiler-grafana-fork-dist/lib
 
 
demo
demo
 
 
examples
examples
 
 
gradle/wrapper
gradle/wrapper
 
 
itest/query
itest/query
 
 
scripts
scripts
 
 
.dockerignore
.dockerignore
 
 
.editorconfig
.editorconfig
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
CODEOWNERS
CODEOWNERS
 
 
LICENSE
LICENSE
 
 
Makefile
Makefile
 
 
Pyroscope_Java_Agent_Architecture.docx
Pyroscope_Java_Agent_Architecture.docx
 
 
README.md
README.md
 
 
TODO
TODO
 
 
alpine-test.Dockerfile
alpine-test.Dockerfile
 
 
build.gradle
build.gradle
 
 
docker-compose-itest.yaml
docker-compose-itest.yaml
 
 
gradle.properties
gradle.properties
 
 
gradlew
gradlew
 
 
gradlew.bat
gradlew.bat
 
 
internal.md
internal.md
 
 
settings.gradle
settings.gradle
 
 
ubuntu-test.Dockerfile
ubuntu-test.Dockerfile
 
 

Repository files navigation

Pyroscope Java Agent

Continuous profiling agent for Java applications. Integrates with Pyroscope / Grafana Profiles to collect CPU, allocation, and lock contention profiles with minimal overhead.

Built on top of async-profiler.


Table of Contents

  • Features
  • Distribution
  • Quick Start
  • Installation
  • Configuration
  • Labels & Context
  • OpenTelemetry Integration
  • Profiler Types
  • Building from Source
  • Examples
  • Maintainers

Features

  • CPU profiling via async-profiler (Linux x64/ARM64, macOS x64/ARM64)
  • Windows support via JVM Flight Recorder (JFR)
  • Allocation profiling — track heap allocations above a configurable threshold
  • Lock contention profiling — identify threads blocked on monitors
  • Dynamic labels — annotate profiles with per-request metadata (user ID, request ID, etc.)
  • OpenTelemetry integration — correlate profiles with traces via trace_id / span_id
  • Zero-code-change setup — attach as a JVM agent with no application modifications needed
  • Programmatic API — start/stop profiling and set labels from application code

Distribution

The agent is distributed as a single JAR file pyroscope.jar that bundles native async-profiler libraries for:

  • Linux x64
  • Linux ARM64
  • macOS x64
  • macOS ARM64

On Windows, the agent falls back to the JVM built-in JFR profiler automatically.

Download the latest release from the releases page.


Quick Start

java -javaagent:pyroscope.jar \
  -DPYROSCOPE_APPLICATION_NAME=my-app \
  -DPYROSCOPE_SERVER_ADDRESS=http://localhost:4040 \
  -jar myapp.jar

That's it — the agent starts profiling and uploads data to your Pyroscope server every 10 seconds.


Installation

As a JVM Agent

Attach the agent at startup using the -javaagent flag:

java -javaagent:/path/to/pyroscope.jar \
  -DPYROSCOPE_APPLICATION_NAME=my-service \
  -DPYROSCOPE_SERVER_ADDRESS=http://pyroscope:4040 \
  -jar myapp.jar

Using a Properties File

Create a pyroscope.properties file:

PYROSCOPE_APPLICATION_NAME=my-service
PYROSCOPE_SERVER_ADDRESS=http://localhost:4040
PYROSCOPE_PROFILER_EVENT=itimer
PYROSCOPE_PROFILING_INTERVAL=10ms
PYROSCOPE_UPLOAD_INTERVAL=10s
PYROSCOPE_LOG_LEVEL=info

Then reference it at startup:

java -javaagent:pyroscope.jar \
  -Dpyroscope.config=/path/to/pyroscope.properties \
  -jar myapp.jar

Programmatic API

import io.pyroscope.javaagent.PyroscopeAgent;
import io.pyroscope.javaagent.config.Config;
import io.pyroscope.javaagent.EventType;

Config config = new Config.Builder()
    .setApplicationName("my-service")
    .setServerAddress("http://localhost:4040")
    .setProfilingEvent(EventType.ITIMER)
    .build();

PyroscopeAgent.start(config);

// ... your application runs ...

PyroscopeAgent.stop();

Configuration

Configuration is resolved in the following priority order:

  1. System properties (-Dproperty=value)
  2. Environment variables (PYROSCOPE_*)
  3. Properties file (-Dpyroscope.config=/path/to/file)
  4. Defaults

Core Settings

Variable Default Description
PYROSCOPE_APPLICATION_NAME (required) Application name shown in Pyroscope UI
PYROSCOPE_SERVER_ADDRESS http://localhost:4040 Pyroscope server endpoint
PYROSCOPE_AGENT_ENABLED true Enable or disable the agent
PYROSCOPE_LOG_LEVEL info Log verbosity: debug, info, warn, error

Profiling Settings

Variable Default Description
PYROSCOPE_PROFILER_TYPE ASYNC Profiler backend: ASYNC or JFR
PYROSCOPE_PROFILER_EVENT ITIMER CPU profiling event: ITIMER, CTIMER, CPU
PYROSCOPE_PROFILING_INTERVAL 10ms Sampling frequency
PYROSCOPE_PROFILER_ALLOC (disabled) Allocation tracking threshold (e.g. 512k)
PYROSCOPE_PROFILER_LOCK (disabled) Lock contention threshold (e.g. 10ms)
PYROSCOPE_JAVA_STACK_DEPTH_MAX 2048 Maximum stack trace depth

Export Settings

Variable Default Description
PYROSCOPE_UPLOAD_INTERVAL 10s How often to upload profiles
PYROSCOPE_FORMAT JFR Export format: JFR or PPROF
PYROSCOPE_PUSH_QUEUE_CAPACITY 8 In-memory snapshot buffer size
PYROSCOPE_INGEST_MAX_TRIES 8 Upload retry attempts (exponential backoff)
PYROSCOPE_PROFILE_EXPORT_TIMEOUT 10s Timeout per upload request

Authentication

Variable Default Description
PYROSCOPE_AUTH_TOKEN (optional) Bearer token for Grafana Cloud
PYROSCOPE_BASIC_AUTH_USER (optional) Basic auth username
PYROSCOPE_BASIC_AUTH_PASSWORD (optional) Basic auth password

Static Labels

Attach fixed key-value metadata to all profiles from this instance:

-DPYROSCOPE_LABELS=env=production,region=us-east-1,version=2.1.0

Labels & Context

Labels allow you to slice profiles by arbitrary dimensions in the Pyroscope UI — for example, by request ID, user ID, or endpoint.

Static Labels

Set once at startup, applied to all profiles:

import io.pyroscope.labels.v2.Pyroscope;

Pyroscope.setStaticLabels(Map.of(
    "region", "us-east-1",
    "version", "2.1.0"
));

Scoped Context (Per-Request)

Use ScopedContext to attach labels for the duration of a code block. Labels are removed automatically when the block exits.

import io.pyroscope.labels.v2.ScopedContext;
import io.pyroscope.labels.v2.LabelsSet;

// Try-with-resources (recommended)
try (ScopedContext ctx = new ScopedContext(
        new LabelsSet("request_id", "abc123", "user_id", "42"))) {
    handleRequest();
} // labels removed here

// Or with a lambda
Pyroscope.LabelsWrapper.run(
    new LabelsSet("endpoint", "/checkout"),
    () -> processCheckout()
);

Constant Context (Long-lived / High-cardinality)

For contexts that outlive a single request:

ConstantContext ctx = Pyroscope.LabelsWrapper.registerConstant("worker-thread-1");
// ... later ...
ctx.close();

OpenTelemetry Integration

The agent can inject trace_id and span_id into profiles so you can jump from a slow trace span directly to the matching flame graph in Pyroscope.

Setup

Download pyroscope-otel-extension.jar from the releases page, then add it to the OpenTelemetry Java agent:

java \
  -javaagent:opentelemetry-javaagent.jar \
  -Dotel.javaagent.extensions=pyroscope-otel-extension.jar \
  -DPYROSCOPE_APPLICATION_NAME=my-traced-service \
  -DPYROSCOPE_SERVER_ADDRESS=http://pyroscope:4040 \
  -jar myapp.jar

No code changes are required. The PyroscopeSpanProcessor automatically injects trace context into each profile sample.

Manual Setup (without OTel agent)

If you manage spans manually:

import io.pyroscope.otel.PyroscopeSpanProcessor;

SdkTracerProvider provider = SdkTracerProvider.builder()
    .addSpanProcessor(new PyroscopeSpanProcessor())
    .build();

Profiler Types

ASYNC (default — Linux & macOS)

Uses async-profiler via native JNI. Provides accurate CPU profiling without safepoint bias. Supports:

  • ITIMER — interval timer (default, works without root)
  • CTIMER — CPU timer (better accuracy on some systems)
  • CPU — perf_events based (requires perf_event_paranoid=1)

JFR (Windows & fallback)

Uses the JVM built-in Java Flight Recorder. Works on all platforms but requires Java 11+. Enable explicitly:

-DPYROSCOPE_PROFILER_TYPE=JFR

You can supply a custom JFR configuration file:

-DPYROSCOPE_JFR_PROFILER_SETTINGS=/path/to/custom.jfc

Building from Source

Prerequisites

  • JDK 8+ (JDK 11+ required for the demo/OTel module)
  • Gradle (wrapper included)

Build the Agent JAR

./gradlew shadowJar

Output: agent/build/libs/pyroscope.jar

Build the OTel Extension JAR

./gradlew :demo:shadowJar

Output: demo/build/libs/pyroscope-otel-extension.jar

Run Tests

./gradlew test

Run Integration Tests

make itest

Examples

Docker Compose

A complete example with a demo app and Pyroscope server:

services:
  pyroscope:
    image: grafana/pyroscope:latest
    ports:
      - "4040:4040"

  app:
    image: my-java-app
    environment:
      PYROSCOPE_APPLICATION_NAME: my-app
      PYROSCOPE_SERVER_ADDRESS: http://pyroscope:4040
      PYROSCOPE_PROFILER_EVENT: itimer
      PYROSCOPE_PROFILING_INTERVAL: 10ms

See the examples/ directory for complete Docker Compose configurations.

Allocation Profiling

Track heap allocations to find GC pressure:

java -javaagent:pyroscope.jar \
  -DPYROSCOPE_APPLICATION_NAME=my-app \
  -DPYROSCOPE_PROFILER_ALLOC=512k \
  -jar myapp.jar

Lock Contention Profiling

Find threads blocked waiting for monitors:

java -javaagent:pyroscope.jar \
  -DPYROSCOPE_APPLICATION_NAME=my-app \
  -DPYROSCOPE_PROFILER_LOCK=10ms \
  -jar myapp.jar

Grafana Cloud

java -javaagent:pyroscope.jar \
  -DPYROSCOPE_APPLICATION_NAME=my-app \
  -DPYROSCOPE_SERVER_ADDRESS=https://profiles-prod-001.grafana.net \
  -DPYROSCOPE_AUTH_TOKEN=<your-grafana-cloud-token> \
  -jar myapp.jar

Maintainers

This package is maintained by @grafana/pyroscope-java. Mention this team on issues or PRs for feedback.

About

pyroscope java integration

Resources

Readme
License
Activity
Custom properties

Stars

0 stars

Watchers

0 watching

Forks

1 fork
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.