Important: This documentation is about an older version. It's relevant only to the release noted, many of the features and functions have been updated or replaced. Please view the current version.
Profiling Java using Grafana Alloy or Agent
Grafana Alloy and Grafana Agent in Flow mode support Java profiling.
The collector configuration file is composed of components that are used to collect, transform, and send data. Alloy configuration files use the Alloy configuration syntax. Agent Flow files use the River language.
Caution
Grafana Alloy is the new name for our distribution of the OTel collector. Grafana Agent has been deprecated and is in Long-Term Support (LTS) through October 31, 2025. Grafana Agent will reach an End-of-Life (EOL) on November 1, 2025. Read more about why we recommend migrating to Grafana Alloy.
Configure the components
The pyroscope.java
component is used to continuously profile Java processes running on the local Linux OS
using async-profiler.
pyroscope.java "java" {
profiling_config {
interval = "15s"
alloc = "512k"
cpu = true
lock = "10ms"
sample_rate = 100
}
forward_to = [pyroscope.write.endpoint.receiver]
targets = discovery.relabel.java.output
}
Using the targets
argument, you can specify which processes and containers to profile on the machine. The targets
can be from discovery.process
component. You can use discovery.process
join argument to join process targets with
extra discoveries such as dicovery.kubernetes
, discovery.docker
and discovery.dockerswarm
.
You can use the discovery.relabel
component to relabel discovered targets and set your own labels . For more
information, see Components.
The forward_to
parameter should point to a pyroscope.write
component to send the collected profiles to your
Pyroscope Server or Grafana Cloud.
The special label __process_pid__
must always be present in each target of targets
and corresponds to the PID
of
the process to profile.
The special label service_name
is required and must always be present.
If service_name
isn’t specified, pyroscope.java
attempts to infer it from discovery meta labels.
If service_name
isn’t specified and couldn’t be inferred, then it’s set to unspecified
.
The profiling_config
block describes how async-profiler is invoked.
It supports the following arguments:
For more information on async-profiler configuration, see profiler-options.
Set privileges for the collector
You must run the collector, either Grafana Alloy (recommended) or Agent (legacy), as root and inside host pid
namespace for the pyroscope.java
and discover.process
components to work.
Start the collector
To start Grafana Alloy v1.2: Replace configuration.alloy
with your configuration file name:
alloy run configuration.alloy
To start Grafana Alloy v1.0/1.1: Replace configuration.alloy
with your configuration file name:
alloy run --stability.level=public-preview configuration.alloy
The stability.level
option is required for pyroscope.scrape
with Alloy v1.0 or v1.1. For more information about stability.level
, refer to The run command documentation.
To start Grafana Agent, replace configuration.river
with your configuration filename:
grafana-agent-flow run configuration.river
Send data to Grafana Cloud Profiles
When sending to Grafana Cloud Profiles, you can use the following pyroscope.write
component configuration which uses environment variables.
Ensure that you have appropriately configured the GC_URL
, GC_USER
, and GC_PASSWORD
environment variables.
pyroscope.write "endpoint" {
endpoint {
basic_auth {
password = env("GC_PASSWORD")
username = env("GC_USER")
}
url = env("GC_URL")
}
}
Examples
Profiling local process
Profiling docker containers
Profiling Kubernetes pods
References
For more information: