-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add quickstart example with java (#3281)
* chore: add quickstart example with java * update: readme with details about Java implementation
- Loading branch information
1 parent
18c779f
commit ba8d9dd
Showing
18 changed files
with
661 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.DS_Store | ||
.gradle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Quick Start - Java app with OpenTelemetry and Tracetest | ||
|
||
> [Read the detailed recipe for setting up OpenTelemetry Collector with Tractest in our documentation.](https://docs.tracetest.io/examples-tutorials/recipes/running-tracetest-without-a-trace-data-store) | ||
This is a simple quick start on how to configure a Java app (with [Spring](https://spring.io/)) to use OpenTelemetry instrumentation with traces, and Tracetest for enhancing your e2e and integration tests with trace-based testing. | ||
|
||
The Java API was created with OpenTelemetry instrumentation using the [OpenTelemetry with Java guidelines](https://opentelemetry.io/docs/instrumentation/java/getting-started/). | ||
|
||
To run it just execute `docker compose up` on this folder. | ||
|
||
Feel free to check out the [docs](https://docs.tracetest.io/), and join our [Discord Community](https://discord.gg/8MtcMrQNbX) for more info! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
version: '3' | ||
services: | ||
app: | ||
image: quick-start-java | ||
build: ./quick_start_api/. | ||
platform: linux/amd64 | ||
extra_hosts: | ||
- "host.docker.internal:host-gateway" | ||
ports: | ||
- "8080:8080" | ||
environment: | ||
OTEL_SERVICE_NAME: quick_start_java | ||
OTEL_TRACES_EXPORTER: otlp | ||
OTEL_METRICS_EXPORTER: none | ||
OTEL_LOGS_EXPORTER: none | ||
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: http://otel-collector:4317 | ||
|
||
tracetest: | ||
image: kubeshop/tracetest:latest | ||
platform: linux/amd64 | ||
volumes: | ||
- type: bind | ||
source: ./tracetest/tracetest-config.yaml | ||
target: /app/tracetest.yaml | ||
- type: bind | ||
source: ./tracetest/tracetest-provision.yaml | ||
target: /app/provisioning.yaml | ||
ports: | ||
- 11633:11633 | ||
command: --provisioning-file /app/provisioning.yaml | ||
depends_on: | ||
postgres: | ||
condition: service_healthy | ||
otel-collector: | ||
condition: service_started | ||
healthcheck: | ||
test: ["CMD", "wget", "--spider", "localhost:11633"] | ||
interval: 1s | ||
timeout: 3s | ||
retries: 60 | ||
environment: | ||
TRACETEST_DEV: ${TRACETEST_DEV} | ||
|
||
postgres: | ||
image: postgres:14 | ||
environment: | ||
POSTGRES_PASSWORD: postgres | ||
POSTGRES_USER: postgres | ||
healthcheck: | ||
test: pg_isready -U "$$POSTGRES_USER" -d "$$POSTGRES_DB" | ||
interval: 1s | ||
timeout: 5s | ||
retries: 60 | ||
|
||
otel-collector: | ||
image: otel/opentelemetry-collector-contrib:0.59.0 | ||
command: | ||
- "--config" | ||
- "/otel-local-config.yaml" | ||
volumes: | ||
- ./tracetest/collector.config.yaml:/otel-local-config.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# | ||
# https://help.github.com/articles/dealing-with-line-endings/ | ||
# | ||
# Linux start script should use lf | ||
/gradlew text eol=lf | ||
|
||
# These are Windows script files and should use crlf | ||
*.bat text eol=crlf | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Ignore Gradle project-specific cache directory | ||
.gradle | ||
|
||
# Ignore Gradle build output directory | ||
build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#based on https://github.com/open-telemetry/opentelemetry-demo/blob/main/src/adservice/Dockerfile | ||
|
||
FROM eclipse-temurin:17-jdk AS builder | ||
|
||
WORKDIR /usr/src/app/ | ||
|
||
COPY ./gradlew* ./settings.gradle* . | ||
COPY ./gradle ./gradle | ||
COPY ./app ./app | ||
|
||
RUN ./gradlew | ||
RUN ./gradlew assemble | ||
|
||
FROM eclipse-temurin:17-jre | ||
|
||
WORKDIR /usr/src/app/ | ||
|
||
COPY --from=builder /usr/src/app/ ./ | ||
|
||
ENTRYPOINT [ "java", "-jar", "./app/build/libs/app.jar" ] |
54 changes: 54 additions & 0 deletions
54
examples/quick-start-java/quick_start_api/app/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
plugins { | ||
id("java") | ||
id("org.springframework.boot") version "3.0.6" | ||
id("io.spring.dependency-management") version "1.1.0" | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
implementation("org.springframework.boot:spring-boot-starter-web") | ||
|
||
// OpenTelemetry core | ||
implementation(platform("io.opentelemetry:opentelemetry-bom:1.31.0")) | ||
implementation("io.opentelemetry:opentelemetry-api") | ||
implementation("io.opentelemetry:opentelemetry-sdk") | ||
implementation("io.opentelemetry:opentelemetry-exporter-otlp") | ||
implementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure") | ||
implementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure-spi") | ||
|
||
// OpenTelemetry instrumentation | ||
implementation(platform("io.opentelemetry.instrumentation:opentelemetry-instrumentation-bom-alpha:1.31.0-alpha")) | ||
implementation("io.opentelemetry.instrumentation:opentelemetry-runtime-telemetry-java8") | ||
implementation("io.opentelemetry.instrumentation:opentelemetry-log4j-appender-2.17") | ||
implementation("io.opentelemetry.instrumentation:opentelemetry-spring-webmvc-6.0") | ||
|
||
// implementation(platform("io.opentelemetry:opentelemetry-bom:1.31.0")) | ||
// implementation("io.opentelemetry:opentelemetry-api:1.31.0") | ||
// implementation("io.opentelemetry:opentelemetry-sdk:1.31.0") | ||
// implementation("io.opentelemetry:opentelemetry-sdk-metrics:1.31.0") | ||
// implementation("io.opentelemetry.semconv:opentelemetry-semconv:1.21.0-alpha") | ||
// implementation("io.opentelemetry:opentelemetry-exporter-otlp:1.31.0") | ||
// implementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure:1.31.0") | ||
// implementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure-spi:1.31.0") | ||
|
||
// implementation("io.opentelemetry.instrumentation:opentelemetry-spring-webmvc-6.0") | ||
} | ||
|
||
java { | ||
toolchain { | ||
languageVersion.set(JavaLanguageVersion.of(17)) | ||
} | ||
} | ||
|
||
sourceSets { | ||
main { | ||
java.setSrcDirs(setOf(".")) | ||
} | ||
} | ||
|
||
ext { | ||
set("opentelemetry.version", "1.31.0") | ||
} |
41 changes: 41 additions & 0 deletions
41
...s/quick-start-java/quick_start_api/app/src/main/java/quick_start_api/HelloController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package quick_start_api; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.server.ResponseStatusException; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.api.trace.Tracer; | ||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.context.Scope; | ||
|
||
@RestController | ||
public class HelloController { | ||
private final Tracer tracer; | ||
|
||
@GetMapping("/hello") | ||
public String index() { | ||
Span span = tracer.spanBuilder("hello").startSpan(); | ||
|
||
try (Scope scope = span.makeCurrent()) { | ||
return "hello"; | ||
} catch(Throwable t) { | ||
span.recordException(t); | ||
throw t; | ||
} finally { | ||
span.end(); | ||
} | ||
} | ||
|
||
@Autowired | ||
HelloController(OpenTelemetry openTelemetry) { | ||
tracer = openTelemetry.getTracer(HelloController.class.getName(), "0.1.0"); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...k-start-java/quick_start_api/app/src/main/java/quick_start_api/QuickStartApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package quick_start_api; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.Banner; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
import jakarta.servlet.Filter; | ||
|
||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.sdk.OpenTelemetrySdk; | ||
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; | ||
import io.opentelemetry.instrumentation.spring.webmvc.v6_0.SpringWebMvcTelemetry; | ||
|
||
@SpringBootApplication | ||
public class QuickStartApplication { | ||
private static volatile OpenTelemetry openTelemetry = OpenTelemetry.noop(); | ||
|
||
public static void main(String[] args) { | ||
OpenTelemetrySdk openTelemetrySdk = AutoConfiguredOpenTelemetrySdk.builder().build().getOpenTelemetrySdk(); | ||
QuickStartApplication.openTelemetry = openTelemetrySdk; | ||
|
||
SpringApplication app = new SpringApplication(QuickStartApplication.class); | ||
app.setBannerMode(Banner.Mode.OFF); | ||
app.run(args); | ||
} | ||
|
||
@Bean | ||
public OpenTelemetry openTelemetry() { | ||
return openTelemetry; | ||
} | ||
|
||
@Bean | ||
public Filter webMvcTracingFilter(OpenTelemetry openTelemetry) { | ||
return SpringWebMvcTelemetry.create(openTelemetry).createServletFilter(); | ||
} | ||
} |
Binary file added
BIN
+62.2 KB
examples/quick-start-java/quick_start_api/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
7 changes: 7 additions & 0 deletions
7
examples/quick-start-java/quick_start_api/gradle/wrapper/gradle-wrapper.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip | ||
networkTimeout=10000 | ||
validateDistributionUrl=true | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
Oops, something went wrong.