Skip to content

Commit 0a199d7

Browse files
Copilottrask
andcommitted
Convert reference application to use OpenTelemetry Java agent instead of manual SDK configuration
Co-authored-by: trask <[email protected]>
1 parent c357ae1 commit 0a199d7

File tree

5 files changed

+42
-140
lines changed

5 files changed

+42
-140
lines changed

reference-application/Dockerfile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ FROM openjdk:17-jdk-slim
22

33
WORKDIR /app
44

5-
# Copy the JAR file
6-
COPY build/libs/*.jar app.jar
5+
# Copy the JAR file and agent
6+
COPY build/libs/app.jar /app.jar
7+
COPY build/agent/opentelemetry-javaagent.jar /opentelemetry-javaagent.jar
78

89
# Expose the port
910
EXPOSE 8080
1011

11-
# Run the application
12-
ENTRYPOINT ["java", "-jar", "app.jar"]
12+
# Run the application with the Java agent
13+
ENTRYPOINT ["java", "-javaagent:/opentelemetry-javaagent.jar", "-jar", "/app.jar"]

reference-application/README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ This application showcases:
1515

1616
## Application Overview
1717

18-
The reference application is a dice rolling service that simulates various scenarios to demonstrate OpenTelemetry capabilities:
18+
The reference application is a dice rolling service that demonstrates OpenTelemetry capabilities using the **OpenTelemetry Java Agent** for automatic instrumentation and manual instrumentation examples:
1919

2020
### Endpoints
2121

@@ -45,8 +45,11 @@ The reference application is a dice rolling service that simulates various scena
4545
### Running with Console Output
4646

4747
```shell
48-
# Build and run with console logging
49-
../gradlew bootRun
48+
# Build the application with the Java agent
49+
../gradlew bootJar
50+
51+
# Run with the Java agent for automatic instrumentation
52+
java -javaagent:build/agent/opentelemetry-javaagent.jar -jar build/libs/app.jar
5053
```
5154

5255
Then test the endpoints:
@@ -59,6 +62,9 @@ curl http://localhost:8080/fibonacci?n=10
5962
### Running with OpenTelemetry Collector
6063

6164
```shell
65+
# Build the application
66+
../gradlew bootJar
67+
6268
# Start the collector and application
6369
docker-compose up --build
6470
```
@@ -84,13 +90,9 @@ export OTEL_METRICS_EXPORTER=otlp
8490
export OTEL_LOGS_EXPORTER=otlp
8591
```
8692

87-
### Programmatic Configuration
93+
### Java Agent Configuration
8894

89-
See `src/main/java/io/opentelemetry/example/config/` for examples of:
90-
- Manual SDK initialization
91-
- Custom span processors and exporters
92-
- Resource configuration
93-
- Sampling configuration
95+
The application uses the OpenTelemetry Java Agent which automatically configures instrumentation based on environment variables and system properties. All standard OpenTelemetry configuration options are supported.
9496

9597
### Declarative Configuration
9698

reference-application/build.gradle.kts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import org.springframework.boot.gradle.plugin.SpringBootPlugin
2+
import org.springframework.boot.gradle.tasks.bundling.BootJar
23

34
plugins {
45
id("java")
56
id("org.springframework.boot") version "3.5.6"
6-
id("io.spring.dependency-management") version "1.1.6"
77
}
88

99
val moduleName by extra { "io.opentelemetry.examples.reference-application" }
@@ -12,35 +12,41 @@ repositories {
1212
mavenCentral()
1313
}
1414

15+
val agent = configurations.create("agent")
16+
1517
dependencies {
1618
implementation(platform(SpringBootPlugin.BOM_COORDINATES))
17-
implementation(platform("io.opentelemetry.instrumentation:opentelemetry-instrumentation-bom:2.20.1"))
19+
implementation("io.opentelemetry:opentelemetry-api")
1820

1921
// Spring Boot
2022
implementation("org.springframework.boot:spring-boot-starter-web")
2123
implementation("org.springframework.boot:spring-boot-starter-actuator")
2224

23-
// OpenTelemetry SDK and API
24-
implementation("io.opentelemetry:opentelemetry-api")
25-
implementation("io.opentelemetry:opentelemetry-sdk")
26-
implementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure")
27-
28-
// OpenTelemetry Exporters
29-
implementation("io.opentelemetry:opentelemetry-exporter-otlp")
30-
implementation("io.opentelemetry:opentelemetry-exporter-logging")
31-
implementation("io.opentelemetry:opentelemetry-exporter-prometheus")
32-
33-
// OpenTelemetry Instrumentation - use manual configuration instead of starter
25+
// OpenTelemetry Instrumentation - use logback appender for logs
3426
implementation("io.opentelemetry.instrumentation:opentelemetry-logback-appender-1.0")
3527

3628
// Micrometer for additional metrics
3729
implementation("io.micrometer:micrometer-registry-prometheus")
3830

31+
// Java agent
32+
agent("io.opentelemetry.javaagent:opentelemetry-javaagent:2.20.1")
33+
3934
// Testing
4035
testImplementation("org.springframework.boot:spring-boot-starter-test")
4136
testImplementation("io.opentelemetry:opentelemetry-sdk-testing")
4237
}
4338

39+
val copyAgent = tasks.register<Copy>("copyAgent") {
40+
from(agent.singleFile)
41+
into(layout.buildDirectory.dir("agent"))
42+
rename("opentelemetry-javaagent-.*\\.jar", "opentelemetry-javaagent.jar")
43+
}
44+
45+
tasks.named<BootJar>("bootJar") {
46+
dependsOn(copyAgent)
47+
archiveFileName = "app.jar"
48+
}
49+
4450
tasks.withType<Test> {
4551
useJUnitPlatform()
4652
}

reference-application/src/main/java/io/opentelemetry/example/ReferenceApplication.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import io.micrometer.core.instrument.Counter;
44
import io.micrometer.core.instrument.MeterRegistry;
55
import io.micrometer.core.instrument.Timer;
6+
import io.opentelemetry.api.GlobalOpenTelemetry;
7+
import io.opentelemetry.api.OpenTelemetry;
68
import org.springframework.boot.SpringApplication;
79
import org.springframework.boot.autoconfigure.SpringBootApplication;
810
import org.springframework.context.annotation.Bean;
@@ -14,6 +16,11 @@ public static void main(String[] args) {
1416
SpringApplication.run(ReferenceApplication.class, args);
1517
}
1618

19+
@Bean
20+
public OpenTelemetry openTelemetry() {
21+
return GlobalOpenTelemetry.get();
22+
}
23+
1724
@Bean
1825
public Counter diceRollCounter(MeterRegistry meterRegistry) {
1926
return Counter.builder("dice_rolls_total")

reference-application/src/main/java/io/opentelemetry/example/config/OpenTelemetryConfig.java

Lines changed: 0 additions & 114 deletions
This file was deleted.

0 commit comments

Comments
 (0)