Skip to content

Commit

Permalink
docs(examples): Grafana agent java example (#2930)
Browse files Browse the repository at this point in the history
* move to subfolder

* add java example
  • Loading branch information
korniltsev authored Jan 17, 2024
1 parent 2402565 commit 40ccc05
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ You'll need to configure the Grafana agent for things like profiling configurati

You can find a list of [arguments](https://grafana.com/docs/agent/latest/flow/reference/components/pyroscope.scrape/#arguments) and [supported blocks](https://grafana.com/docs/agent/latest/flow/reference/components/pyroscope.scrape/#blocks) in the [Grafana Agent documentation for pyroscope](https://grafana.com/docs/agent/latest/flow/reference/components/pyroscope.scrape/)

Refer to [config file](./agent/config/config.river) to see an example of how to configure Grafana Agent to send profiling data to Pyroscope.
Refer to [config file](agent/config/config.river) to see an example of how to configure Grafana Agent to send profiling data to Pyroscope.

### 2. Run Grafana agent, Grafana and Pyroscope

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
21 changes: 21 additions & 0 deletions examples/grafana-agent/java/FastSlow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import java.util.concurrent.*;

public class FastSlow {

public static long fib(int n) {
if (n < 2) {
return n;
}
return fib(n - 1) + fib(n - 2);
}

public static void main(String[] args) throws ExecutionException, InterruptedException {
ExecutorService e = Executors.newSingleThreadExecutor();
e.submit(() -> {
while (true) {
fib(26);
fib(24);
}
});
}
}
56 changes: 56 additions & 0 deletions examples/grafana-agent/java/config.river
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
logging {
level = "debug"
format = "logfmt"
}

discovery.process "all" {
// join kuberenetes targets with process targets on container_id to have k8s labels
// join = discovery.kubernetes.containers.targets
}

discovery.relabel "java" {
targets = discovery.process.all.targets
rule {
source_labels = ["__meta_process_exe"]
action = "keep"
regex = ".*/java$"
}
rule {
source_labels = ["__meta_process_commandline"]
regex = "java FastSlow"
action = "keep"
}
rule {
action = "replace"
target_label = "service_name"
replacement = "java-fast-slow-fibonacci"
}
}

pyroscope.java "java" {
profiling_config {
interval = "15s"
alloc = "512k"
cpu = true
lock = "10ms"
sample_rate = 100
}
forward_to = [pyroscope.write.example.receiver]
targets = discovery.relabel.java.output
}

pyroscope.write "example" {
// Send metrics to a locally running Pyroscope instance.
endpoint {
url = "http://pyroscope:4040"

// To send data to Grafana Cloud you'll need to provide username and password.
// basic_auth {
// username = "myuser"
// password = "mypassword"
// }
}
external_labels = {
"env" = "example",
}
}
26 changes: 26 additions & 0 deletions examples/grafana-agent/java/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
version: '3.9'
services:
java:
build:
context: .
dockerfile: java.Dockerfile
pyroscope:
image: 'grafana/pyroscope:latest'
ports:
- 4040:4040

agent:
image: grafana/agent:main
volumes:
- ./config.river:/etc/agent-config/config.river
command:
- run
- /etc/agent-config/config.river
- --server.http.listen-addr=0.0.0.0:12345
environment:
HOSTNAME: agent
AGENT_MODE: flow
ports:
- "12345:12345"
privileged: true
pid: host
6 changes: 6 additions & 0 deletions examples/grafana-agent/java/java.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM openjdk:17-jdk-slim

ADD ./FastSlow.java /FastSlow.java
RUN javac FastSlow.java

CMD ["java", "FastSlow"]

0 comments on commit 40ccc05

Please sign in to comment.