Skip to content

Commit 0d3898b

Browse files
authored
Update java examples with latest SDKs (#3812)
* Update java span-profiles example with latest SDKs * Bump java SDK version in a few more examples, improve docs further
1 parent 466aaa8 commit 0d3898b

File tree

16 files changed

+46
-35
lines changed

16 files changed

+46
-35
lines changed

docs/sources/configure-client/language-sdks/java.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ First, add the Pyroscope dependency:
5151
<dependency>
5252
<groupId>io.pyroscope</groupId>
5353
<artifactId>agent</artifactId>
54-
<version>0.15.2</version>
54+
<version>0.16.0</version>
5555
</dependency>
5656
```
5757

5858
```gradle
59-
implementation("io.pyroscope:agent:0.15.2")
59+
implementation("io.pyroscope:agent:0.16.0")
6060
```
6161

6262
{{< /code >}}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ weight: 103
77

88
# Span profiles with Traces to profiles for .NET
99

10-
Span Profiles represents a shift in profiling methodology.
10+
Span Profiles represent a shift in profiling methodology.
1111
Traditional continuous profiling provides an application-wide view over fixed intervals.
1212
In contrast, Span Profiles delivers focused, dynamic analysis on specific execution scopes within applications, such as individual requests or specific trace spans.
1313

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ weight: 100
1010

1111
# Span profiles with Traces to profiles for Go
1212

13-
Span Profiles represents a major shift in profiling methodology, enabling deeper analysis of both tracing and profiling data.
13+
Span Profiles represent a major shift in profiling methodology, enabling deeper analysis of both tracing and profiling data.
1414
Traditional continuous profiling provides an application-wide view over fixed intervals.
1515
In contrast, Span Profiles delivers focused, dynamic analysis on specific execution scopes within applications, such as individual requests or specific trace spans.
1616

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ weight: 101
77

88
# Span profiles with Traces to profiles for Java
99

10-
Span Profiles represents a major shift in profiling methodology, enabling deeper analysis of both tracing and profiling data.
10+
Span Profiles represent a major shift in profiling methodology, enabling deeper analysis of both tracing and profiling data.
1111
Traditional continuous profiling provides an application-wide view over fixed intervals.
1212
In contrast, Span Profiles delivers focused, dynamic analysis on specific execution scopes within applications, such as individual requests or specific trace spans.
1313

@@ -40,7 +40,8 @@ Your applications must be instrumented for profiling and tracing before you can
4040

4141
## Configure the otel-profiling-java package
4242

43-
To start collecting Span Profiles for your Java application, you need to include [otel-profiling-java](https://github.com/pyroscope-io/otel-profiling-java) as an extension to your application.
43+
To start collecting Span Profiles for your Java application, you need to include [otel-profiling-java](https://github.com/pyroscope-io/otel-profiling-java) as an extension
44+
for the [OTel Java agent](https://opentelemetry.io/docs/zero-code/java/agent/).
4445

4546
Assuming you have this sample application Docker image:
4647

@@ -61,7 +62,7 @@ EXPOSE 5000
6162

6263
## Add required libararies
6364
ADD https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/download/v1.17.0/opentelemetry-javaagent.jar opentelemetry-javaagent.jar
64-
ADD https://repo1.maven.org/maven2/io/pyroscope/otel/0.10.1.11/otel-0.10.1.11.jar pyroscope-otel.jar
65+
ADD https://repo1.maven.org/maven2/io/pyroscope/otel/0.11.0/otel-0.11.0.jar pyroscope-otel.jar
6566

6667
ENV PYROSCOPE_APPLICATION_NAME=my-app
6768
ENV PYROSCOPE_FORMAT=jfr
@@ -83,14 +84,14 @@ ENV PYROSCOPE_SERVER_ADDRESS=http://localhost:4040
8384
# ENV PYROSCOPE_BASIC_AUTH_USER=123 ## Grafana Cloud Username
8485
# ENV PYROSCOPE_BASIC_AUTH_PASSWORD=glc_secret ## Grafana Cloud Password / API Token
8586

86-
## Add the pyroscope and the opentelemetry java-agents
87+
## Add the opentelemetry java agent
8788
CMD ["java", "-Dserver.port=5000", "-javaagent:./opentelemetry-javaagent.jar", "-jar", "./my-app.jar" ]
8889
```
8990

9091
### Available configuration options
9192

9293
| Flag | Description | Default |
93-
| -------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------- |
94+
|----------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------|
9495
| `otel.pyroscope.start.profiling` | Boolean flag to start PyroscopeAgent. Set to false if you want to start the PyroscopeAgent manually. | `true` |
9596
| `otel.pyroscope.root.span.only` | Boolean flag. When enabled, the tracer will annotate only the first span created locally (the root span), but the profile will include samples of all the nested spans. This may be helpful in case if the trace consists of multiple spans shorter than 10ms and profiler can't collect and annotate samples properly. | `true` |
9697
| `otel.pyroscope.add.span.name` | Boolean flag. Controls whether the span name added to profile labels. | `true` |

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ weight: 104
77

88
# Span profiles with Traces to profiles for Python
99

10-
Span Profiles represents a major shift in profiling methodology, enabling deeper analysis of both tracing and profiling data.
10+
Span Profiles represent a major shift in profiling methodology, enabling deeper analysis of both tracing and profiling data.
1111
Traditional continuous profiling provides an application-wide view over fixed intervals.
1212
In contrast, Span Profiles delivers focused, dynamic analysis on specific execution scopes within applications, such as individual requests or specific trace spans.
1313

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ weight: 102
77

88
# Span profiles with Traces to profiles for Ruby
99

10-
Span Profiles represents a major shift in profiling methodology, enabling deeper analysis of both tracing and profiling data.
10+
Span Profiles represent a major shift in profiling methodology, enabling deeper analysis of both tracing and profiling data.
1111
Traditional continuous profiling provides an application-wide view over fixed intervals.
1212
In contrast, Span Profiles delivers focused, dynamic analysis on specific execution scopes within applications, such as individual requests or specific trace spans.
1313

examples/language-sdk-instrumentation/java/fib/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ WORKDIR /opt/app
44

55
RUN apt-get update && apt-get install ca-certificates -y && update-ca-certificates && apt-get install -y git
66

7-
ADD https://github.com/grafana/pyroscope-java/releases/download/v0.15.2/pyroscope.jar /opt/app/pyroscope.jar
7+
ADD https://github.com/grafana/pyroscope-java/releases/download/v0.16.0/pyroscope.jar /opt/app/pyroscope.jar
88

99
COPY Main.java ./
1010

examples/language-sdk-instrumentation/java/rideshare/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ COPY --from=builder /opt/app/build/libs/rideshare-1.0-SNAPSHOT.jar /opt/app/buil
3737

3838
WORKDIR /opt/app
3939

40-
ADD https://github.com/grafana/pyroscope-java/releases/download/v0.15.2/pyroscope.jar /opt/app/pyroscope.jar
40+
ADD https://github.com/grafana/pyroscope-java/releases/download/v0.16.0/pyroscope.jar /opt/app/pyroscope.jar
4141

4242
CMD sh -c "exec java -Dserver.port=${RIDESHARE_LISTEN_PORT} -javaagent:pyroscope.jar -jar ./build/libs/rideshare-1.0-SNAPSHOT.jar"

examples/language-sdk-instrumentation/java/rideshare/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ repositories {
1212
}
1313

1414
dependencies {
15-
implementation("io.pyroscope:agent:0.15.2")
15+
implementation("io.pyroscope:agent:0.16.0")
1616
implementation("org.springframework.boot:spring-boot-starter-web")
1717
testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.2")
1818
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.8.2")

examples/language-sdk-instrumentation/java/simple/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ FROM openjdk:11.0.11-jdk
22

33
WORKDIR /opt/app
44

5-
ADD https://github.com/grafana/pyroscope-java/releases/download/v0.15.2/pyroscope.jar /opt/app/pyroscope.jar
5+
ADD https://github.com/grafana/pyroscope-java/releases/download/v0.16.0/pyroscope.jar /opt/app/pyroscope.jar
66

77
COPY Main.java ./Main.java
88
RUN javac Main.java

examples/tracing/java/Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ COPY --from=builder /opt/app/build/libs/rideshare-1.0-SNAPSHOT.jar /opt/app/buil
4242
WORKDIR /opt/app
4343

4444
ADD https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/download/v1.17.0/opentelemetry-javaagent.jar opentelemetry-javaagent.jar
45-
ADD https://repo1.maven.org/maven2/io/pyroscope/otel/0.10.1.11/otel-0.10.1.11.jar pyroscope-otel.jar
45+
ADD https://repo1.maven.org/maven2/io/pyroscope/agent/0.16.0/agent-0.16.0.jar pyroscope.jar
46+
ADD https://repo1.maven.org/maven2/io/pyroscope/otel/0.11.0/otel-0.11.0.jar pyroscope-otel.jar
4647

4748
EXPOSE 5000
4849

49-
CMD ["java", "-Dserver.port=5000", "-javaagent:./opentelemetry-javaagent.jar", "-jar", "./build/libs/rideshare-1.0-SNAPSHOT.jar" ]
50+
CMD ["java", "-Dserver.port=5000", "-javaagent:./pyroscope.jar", "-javaagent:./opentelemetry-javaagent.jar", "-jar", "./build/libs/rideshare-1.0-SNAPSHOT.jar" ]

examples/tracing/java/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,34 @@ The docker compose consists of:
77
- Grafana
88

99
The `rideshare` app generate traces and profiling data that should be available in Grafana.
10-
Pyroscope and Tempo datasources are provisioned automatically.
10+
Datasources for Pyroscope and Tempo are provisioned automatically.
1111

1212
### Build and run
1313

1414
The project can be run locally with the following commands:
1515

1616
```shell
17-
# Pull latest pyroscope and grafana images:
17+
# (optionally) pull latest pyroscope and grafana images:
1818
docker pull grafana/pyroscope:latest
1919
docker pull grafana/grafana:latest
2020

21-
docker compose up
21+
# build and run the example
22+
docker compose up --build
2223
```
2324

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+
Navigate to the [Explore page](http://localhost:3000/explore?schemaVersion=1&panes=%7B%22f36%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%22rideshare.java.push.app%22%5D,%22valueType%22:%22string%22%7D%5D%7D%5D,%22range%22:%7B%22from%22:%22now-15m%22,%22to%22:%22now%22%7D%7D%7D&orgId=1), select a trace and click on a span that has a linked profile:
2526

2627
![image](https://github.com/grafana/otel-profiling-go/assets/12090599/31e33cd1-818b-4116-b952-c9ec7b1fb593)
2728

2829
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+
and have the `pyroscope.profile.id` attribute set to the corresponding span ID.
3031
Please note that presence of the attribute does not necessarily
3132
indicate that the span has a profile: stack trace samples might not be collected, if the utilized CPU time is
3233
less than the sample interval (10ms).
3334

3435
### Instrumentation
3536

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.
37+
The `rideshare` demo application is instrumented with OpenTelemetry: [OTel integration](https://github.com/grafana/otel-profiling-java)
3838

3939
### Grafana Tempo configuration
4040

examples/tracing/java/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ repositories {
1212
}
1313

1414
dependencies {
15+
implementation("io.pyroscope:agent:0.16.0")
16+
1517
implementation("org.springframework.boot:spring-boot-starter-web")
1618
testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.2")
1719
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.8.2")

examples/tracing/java/docker-compose.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ services:
1717
OTEL_TRACES_SAMPLER: always_on
1818
OTEL_PROPAGATORS: tracecontext
1919
REGION: us-east
20-
PYROSCOPE_LABELS: region=us-east
2120
PYROSCOPE_SERVER_ADDRESS: http://pyroscope:4040
2221
build:
2322
context: .

examples/tracing/java/src/main/java/org/example/rideshare/Main.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.example.rideshare;
22

3+
import io.pyroscope.labels.Pyroscope;
34
import org.springframework.boot.SpringApplication;
45
import org.springframework.boot.autoconfigure.SpringBootApplication;
56

@@ -8,6 +9,9 @@
89
@SpringBootApplication
910
public class Main {
1011
public static void main(String[] args) {
12+
Pyroscope.setStaticLabels(Map.of(
13+
"region", System.getenv("REGION"),
14+
"hostname", System.getenv("HOSTNAME")));
1115
SpringApplication.run(Main.class, args);
1216
}
1317
}

examples/tracing/java/src/main/java/org/example/rideshare/OrderService.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.example.rideshare;
22

3+
import io.pyroscope.labels.LabelsSet;
4+
import io.pyroscope.labels.Pyroscope;
35
import org.springframework.stereotype.Service;
46

57
import java.time.Duration;
@@ -14,16 +16,18 @@ public class OrderService {
1416
public static final Duration OP_DURATION = Duration.of(200, ChronoUnit.MILLIS);
1517

1618
public synchronized void findNearestVehicle(int searchRadius, String vehicle) {
17-
AtomicLong i = new AtomicLong();
18-
Instant end = Instant.now()
19-
.plus(OP_DURATION.multipliedBy(searchRadius));
20-
while (Instant.now().compareTo(end) <= 0) {
21-
i.incrementAndGet();
22-
}
23-
24-
if (vehicle.equals("car")) {
25-
checkDriverAvailability(searchRadius);
26-
}
19+
Pyroscope.LabelsWrapper.run(new LabelsSet("vehicle", vehicle), () -> {
20+
AtomicLong i = new AtomicLong();
21+
Instant end = Instant.now()
22+
.plus(OP_DURATION.multipliedBy(searchRadius));
23+
while (Instant.now().compareTo(end) <= 0) {
24+
i.incrementAndGet();
25+
}
26+
27+
if (vehicle.equals("car")) {
28+
checkDriverAvailability(searchRadius);
29+
}
30+
});
2731
}
2832

2933
private void checkDriverAvailability(int searchRadius) {

0 commit comments

Comments
 (0)