Skip to content

Commit 165e6e9

Browse files
authored
examples: add a java span profiling example without the Pyroscope sdk (#3698)
* examples: add a java span profiling example without the Pyroscope sdk * update java span profiles doc
1 parent 5b9ffee commit 165e6e9

File tree

23 files changed

+767
-25
lines changed

23 files changed

+767
-25
lines changed

docs/sources/configure-client/trace-span-profiles/java-span-profiles.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,14 @@ EXPOSE 5000
5252
CMD ["java", "-Dserver.port=5000", "-jar", "./my-app.jar" ]
5353
```
5454

55-
By adding the OTel Java agent and the Pyroscope OTel Java Agent extensions, you can enrich your profiles with span IDs. This makes it possible to query for span-specific profiling data in Grafana Tempo:
55+
By adding the OTel Java agent and the Pyroscope OTel Java Agent extension, you can enrich your profiles with span IDs. This makes it possible to query for span-specific profiling data in Grafana Tempo:
5656

5757
```Dockerfile
5858
# [...]
5959

6060
EXPOSE 5000
6161

6262
## Add required libararies
63-
ADD https://github.com/grafana/pyroscope-java/releases/download/v0.12.2/pyroscope.jar ./pyroscope.jar
6463
ADD https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/download/v1.17.0/opentelemetry-javaagent.jar opentelemetry-javaagent.jar
6564
ADD https://repo1.maven.org/maven2/io/pyroscope/otel/0.10.1.11/otel-0.10.1.11.jar pyroscope-otel.jar
6665

@@ -85,7 +84,7 @@ ENV PYROSCOPE_SERVER_ADDRESS=http://localhost:4040
8584
# ENV PYROSCOPE_BASIC_AUTH_PASSWORD=glc_secret ## Grafana Cloud Password / API Token
8685

8786
## Add the pyroscope and the opentelemetry java-agents
88-
CMD ["java", "-Dserver.port=5000", "-javaagent:./opentelemetry-javaagent.jar", "-javaagent:pyroscope.jar", "-jar", "./my-app.jar" ]
87+
CMD ["java", "-Dserver.port=5000", "-javaagent:./opentelemetry-javaagent.jar", "-jar", "./my-app.jar" ]
8988
```
9089

9190
### Available configuration options
@@ -118,4 +117,4 @@ The profile type or app must be selected for the query to be valid. Grafana does
118117

119118
## Examples
120119

121-
Check out the [examples](https://github.com/grafana/pyroscope/tree/main/examples/tracing/tempo) directory for a complete demo application that shows tracing integration features.
120+
Check out the [examples](https://github.com/grafana/pyroscope/tree/main/examples/tracing/java) directory for a complete demo application that shows tracing integration features.

examples/tracing/java/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
build/
2+
.idea/
3+
.gradle
4+
pyroscope.jar
5+
out/
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,9 @@ COPY --from=builder /opt/app/build/libs/rideshare-1.0-SNAPSHOT.jar /opt/app/buil
4141

4242
WORKDIR /opt/app
4343

44-
ADD https://github.com/grafana/pyroscope-java/releases/download/v0.12.2/pyroscope.jar /opt/app/pyroscope.jar
4544
ADD https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/download/v1.17.0/opentelemetry-javaagent.jar opentelemetry-javaagent.jar
4645
ADD https://repo1.maven.org/maven2/io/pyroscope/otel/0.10.1.11/otel-0.10.1.11.jar pyroscope-otel.jar
4746

4847
EXPOSE 5000
4948

50-
CMD ["java", "-Dserver.port=5000", "-javaagent:./opentelemetry-javaagent.jar", "-javaagent:pyroscope.jar", "-jar", "./build/libs/rideshare-1.0-SNAPSHOT.jar" ]
49+
CMD ["java", "-Dserver.port=5000", "-javaagent:./opentelemetry-javaagent.jar", "-jar", "./build/libs/rideshare-1.0-SNAPSHOT.jar" ]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM python:3.9
2+
3+
RUN pip3 install requests
4+
5+
COPY load-generator.py ./load-generator.py
6+
7+
ENV PYTHONUNBUFFERED=1
8+
9+
CMD [ "python", "load-generator.py" ]

examples/tracing/java/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Span Profiles with Grafana Tempo and Pyroscope
2+
3+
The docker compose consists of:
4+
- The Java Rideshare App
5+
- Tempo
6+
- Pyroscope
7+
- Grafana
8+
9+
The `rideshare` app generate traces and profiling data that should be available in Grafana.
10+
Pyroscope and Tempo datasources are provisioned automatically.
11+
12+
### Build and run
13+
14+
The project can be run locally with the following commands:
15+
16+
```shell
17+
# Pull latest pyroscope and grafana images:
18+
docker pull grafana/pyroscope:latest
19+
docker pull grafana/grafana:latest
20+
21+
docker compose up
22+
```
23+
24+
Navigate to the [Explore page](http://localhost:3000/explore?schemaVersion=1&panes=%7B%22yM9%22:%7B%22datasource%22:%22tempo%22,%22queries%22:%5B%7B%22refId%22:%22A%22,%22datasource%22:%7B%22type%22:%22tempo%22,%22uid%22:%22tempo%22%7D,%22queryType%22:%22traceqlSearch%22,%22limit%22:20,%22tableType%22:%22traces%22,%22filters%22:%5B%7B%22id%22:%22e73a615e%22,%22operator%22:%22%3D%22,%22scope%22:%22span%22%7D,%7B%22id%22:%22service-name%22,%22tag%22:%22service.name%22,%22operator%22:%22%3D%22,%22scope%22:%22resource%22,%22value%22:%5B%22ride-sharing-app%22%5D,%22valueType%22:%22string%22%7D%5D%7D%5D,%22range%22:%7B%22from%22:%22now-6h%22,%22to%22:%22now%22%7D%7D%7D&orgId=1), select a trace and click on a span that has a linked profile:
25+
26+
![image](https://github.com/grafana/otel-profiling-go/assets/12090599/31e33cd1-818b-4116-b952-c9ec7b1fb593)
27+
28+
By default, only the root span gets labeled (the first span created locally): such spans are marked with the _link_ icon
29+
and have `pyroscope.profile.id` attribute set to the corresponding span ID.
30+
Please note that presence of the attribute does not necessarily
31+
indicate that the span has a profile: stack trace samples might not be collected, if the utilized CPU time is
32+
less than the sample interval (10ms).
33+
34+
### Instrumentation
35+
36+
- `rideshare` demo application instrumented with OpenTelemetry: [OTel integration](https://github.com/grafana/otel-profiling-java)
37+
- `pyroscope` itself is instrumented with `opentracing-go` SDK and [`spanprofiler`](https://github.com/grafana/dskit/tree/main/spanprofiler) for profiling integration.
38+
39+
### Grafana Tempo configuration
40+
41+
In order to correlate trace spans with profiling data, the Tempo datasource should have the following configured:
42+
- The profiling data source
43+
- Tags to use when making profiling queries
44+
45+
![image](https://github.com/grafana/pyroscope/assets/12090599/380ac574-a298-440d-acfb-7bc0935a3a7c)
46+
47+
While tags are optional, configuring them is highly recommended for optimizing query performance.
48+
In our example, we configured the `service.name` tag for use in Pyroscope queries as the `service_name` label.
49+
This configuration restricts the data set for lookup, ensuring that queries remain
50+
consistently fast. Note that the tags you configure must be present in the span attributes or resources
51+
for a trace to profiles span link to appear.
52+
53+
Please refer to our [documentation](https://grafana.com/docs/grafana/next/datasources/tempo/configure-tempo-data-source/#trace-to-profiles) for more details.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
plugins {
2+
id("java")
3+
id("org.springframework.boot") version "2.7.0"
4+
id("io.spring.dependency-management") version "1.0.11.RELEASE"
5+
}
6+
7+
group = "org.example"
8+
version = "1.0-SNAPSHOT"
9+
10+
repositories {
11+
mavenCentral()
12+
}
13+
14+
dependencies {
15+
implementation("org.springframework.boot:spring-boot-starter-web")
16+
testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.2")
17+
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.8.2")
18+
}
19+
20+
tasks.getByName<Test>("test") {
21+
useJUnitPlatform()
22+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
version: '3'
2+
services:
3+
pyroscope:
4+
image: grafana/pyroscope
5+
ports:
6+
- "4040:4040"
7+
8+
us-east:
9+
ports:
10+
- "5000"
11+
environment: &env
12+
OTLP_URL: tempo:4318
13+
OTLP_INSECURE: 1
14+
OTEL_TRACES_EXPORTER: otlp
15+
OTEL_EXPORTER_OTLP_ENDPOINT: http://tempo:4317
16+
OTEL_SERVICE_NAME: rideshare.java.push.app
17+
OTEL_METRICS_EXPORTER: none
18+
OTEL_TRACES_SAMPLER: always_on
19+
OTEL_PROPAGATORS: tracecontext
20+
REGION: us-east
21+
PYROSCOPE_LABELS: region=us-east
22+
PYROSCOPE_SERVER_ADDRESS: http://pyroscope:4040
23+
build:
24+
context: .
25+
eu-north:
26+
ports:
27+
- "5000"
28+
environment:
29+
<<: *env
30+
REGION: eu-north
31+
build:
32+
context: .
33+
ap-south:
34+
ports:
35+
- "5000"
36+
environment:
37+
<<: *env
38+
REGION: ap-south
39+
build:
40+
context: .
41+
42+
load-generator:
43+
build:
44+
context: .
45+
dockerfile: Dockerfile.load-generator
46+
47+
grafana:
48+
image: grafana/grafana:latest
49+
environment:
50+
- GF_INSTALL_PLUGINS=grafana-pyroscope-app
51+
- GF_AUTH_ANONYMOUS_ENABLED=true
52+
- GF_AUTH_ANONYMOUS_ORG_ROLE=Admin
53+
- GF_AUTH_DISABLE_LOGIN_FORM=true
54+
- GF_FEATURE_TOGGLES_ENABLE=traceToProfiles tracesEmbeddedFlameGraph
55+
volumes:
56+
- ./grafana-provisioning:/etc/grafana/provisioning
57+
ports:
58+
- "3000:3000"
59+
60+
tempo:
61+
image: grafana/tempo:latest
62+
command: [ "-config.file=/etc/tempo.yml" ]
63+
volumes:
64+
- ./tempo/tempo.yml:/etc/tempo.yml
65+
ports:
66+
- "14268:14268" # jaeger ingest
67+
- "3200:3200" # tempo
68+
- "9095:9095" # tempo grpc
69+
- "4317:4317" # otlp grpc
70+
- "4318:4318" # otlp http
71+
- "9411:9411" # zipkin
58.4 KB
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2.1-bin.zip
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)