Skip to content

Commit 5296524

Browse files
authored
OpenTelemetry Spring Boot example (#8865)
OpenTelemetry Spring Boot example Add assertions to integration test based on collector prometheus text Uses Spring WebClient instead for Java8 Adds region tags to Dockerfile and logback.xml
1 parent 1c1a2b3 commit 5296524

19 files changed

Lines changed: 1316 additions & 0 deletions

File tree

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,4 @@
100100
/retail @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-retail-team
101101
/unittests @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/serverless-runtimes
102102
/bigquery/bigquerydatatransfer @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/bigquery-data-connectors
103+
/opentelemetry @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/opentelemetry-ops
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
HELP.md
2+
.gradle
3+
build/
4+
!gradle/wrapper/gradle-wrapper.jar
5+
!**/src/main/**/build/
6+
!**/src/test/**/build/
7+
8+
### STS ###
9+
.apt_generated
10+
.classpath
11+
.factorypath
12+
.project
13+
.settings
14+
.springBeans
15+
.sts4-cache
16+
bin/
17+
!**/src/main/**/bin/
18+
!**/src/test/**/bin/
19+
20+
### VS Code ###
21+
.vscode/
22+
23+
### Docker ###
24+
Dockerfile
25+
docker-compose*.yaml
26+
otel-collector-config.yaml
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
HELP.md
2+
.gradle
3+
build/
4+
!gradle/wrapper/gradle-wrapper.jar
5+
!**/src/main/**/build/
6+
!**/src/test/**/build/
7+
8+
### STS ###
9+
.apt_generated
10+
.classpath
11+
.factorypath
12+
.project
13+
.settings
14+
.springBeans
15+
.sts4-cache
16+
bin/
17+
!**/src/main/**/bin/
18+
!**/src/test/**/bin/
19+
20+
### IntelliJ IDEA ###
21+
.idea
22+
*.iws
23+
*.iml
24+
*.ipr
25+
out/
26+
!**/src/main/**/out/
27+
!**/src/test/**/out/
28+
29+
### NetBeans ###
30+
/nbproject/private/
31+
/nbbuild/
32+
/dist/
33+
/nbdist/
34+
/.nb-gradle/
35+
36+
### VS Code ###
37+
.vscode/
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip
2+
wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Adapted from https://spring.io/guides/topicals/spring-boot-docker/#_multi_stage_build
16+
# syntax=docker/dockerfile:experimental
17+
FROM eclipse-temurin:17-jdk-alpine as build
18+
WORKDIR /workspace/app
19+
20+
COPY mvnw .
21+
COPY .mvn .mvn
22+
COPY pom.xml .
23+
COPY src src
24+
25+
RUN --mount=type=cache,target=/root/.m2 ./mvnw install -DskipTests
26+
RUN mkdir -p target/dependency && (cd target/dependency; jar -xf ../*.jar)
27+
28+
FROM eclipse-temurin:17-jdk-alpine
29+
VOLUME /tmp
30+
ARG DEPENDENCY=/workspace/app/target/dependency
31+
COPY --from=build ${DEPENDENCY}/BOOT-INF/lib /app/lib
32+
COPY --from=build ${DEPENDENCY}/META-INF /app/META-INF
33+
COPY --from=build ${DEPENDENCY}/BOOT-INF/classes /app
34+
# [START opentelemetry_instrumentation_javaagent_dockerfile]
35+
RUN wget -O /opentelemetry-javaagent.jar https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/download/v1.31.0/opentelemetry-javaagent.jar
36+
CMD sh -c "java -javaagent:/opentelemetry-javaagent.jar -cp app:app/lib/* com.example.demo.DemoApplication \
37+
2>&1 | tee /var/log/app.log"
38+
# [END opentelemetry_instrumentation_javaagent_dockerfile]
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# OpenTelemetry Spring Boot instrumentation example
2+
3+
This sample is a Spring Boot application instrumented with the [OpenTelemetry java
4+
agent](https://opentelemetry.io/docs/instrumentation/java/automatic/). This is a java version
5+
of [this golang
6+
sample](https://github.com/GoogleCloudPlatform/golang-samples/tree/main/opentelemetry/instrumentation).
7+
It uses docker compose to orchestrate running the application and sending it some requests.
8+
9+
The Java code is a basic Spring Boot application with two endpoints
10+
- `/multi` makes a few requests to `/single` on localhost
11+
- `/single` sleeps for a short time to simulate work
12+
13+
Docker compose also runs the OpenTelemetry collector, set up to receive telemetry from the Java
14+
application and parse its logs from a shared volume. Finally, a loadgen container sends
15+
requests to the Java app.
16+
17+
## Permissions
18+
19+
This sample writes to Cloud Logging, Cloud Monitoring, and Cloud Trace. Grant yourself the
20+
following roles to run the example:
21+
- `roles/logging.logWriter` – see https://cloud.google.com/logging/docs/access-control#permissions_and_roles
22+
- `roles/monitoring.metricWriter` – see https://cloud.google.com/monitoring/access-control#predefined_roles
23+
- `roles/cloudtrace.agent` – see https://cloud.google.com/trace/docs/iam#trace-roles
24+
25+
## Running the example
26+
27+
### Cloud Shell or GCE
28+
29+
```sh
30+
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
31+
cd java-docs-samples/opentelemetry/spring-boot-instrumentation/
32+
docker compose up --abort-on-container-exit
33+
```
34+
35+
### Locally with Application Default Credentials
36+
37+
38+
First Create local credentials by running the following command and following the
39+
oauth2 flow (read more about the command [here][auth_command]):
40+
41+
gcloud auth application-default login
42+
43+
Set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable with `export GOOGLE_APPLICATION_CREDENTIALS="$HOME/.config/gcloud/application_default_credentials.json"`
44+
or manually set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to point to a service
45+
account key JSON file path.
46+
47+
Learn more at [Setting Up Authentication for Server to Server Production Applications][ADC].
48+
49+
*Note:* Application Default Credentials is able to implicitly find the credentials as long as the application is running on Compute Engine, Kubernetes Engine, App Engine, or Cloud Functions.
50+
51+
Then run the example:
52+
53+
```sh
54+
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
55+
cd java-docs-samples/opentelemetry/spring-boot-instrumentation/
56+
57+
# Lets collector read mounted config
58+
export USERID="$(id -u)"
59+
# Specify the project ID
60+
export GOOGLE_CLOUD_PROJECT=<your project id>
61+
docker compose -f docker-compose.yaml -f docker-compose.adc.yaml up --abort-on-container-exit
62+
```
63+
64+
[auth_command]: https://cloud.google.com/sdk/gcloud/reference/beta/auth/application-default/login
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Use this compose file along with docker-compose.yaml to pass Application Default
16+
# Credentials from the host into the collector container:
17+
#
18+
# ```
19+
# export GOOGLE_APPLICATION_CREDENTIALS=$HOME/.config/gcloud/application_default_credentials.json
20+
# docker compose -f docker-compose.yaml -f docker-compose.adc.yaml up
21+
# ```
22+
23+
version: "3"
24+
25+
services:
26+
otelcol:
27+
# If the collector does not have permission to read the mounted volumes, set
28+
# USERID=$(id -u) to run the container as the current user
29+
user: ${USERID}
30+
volumes:
31+
- ${GOOGLE_APPLICATION_CREDENTIALS:-/dev/null}:/tmp/keys/gcp-credentials.json:ro
32+
environment:
33+
- GOOGLE_APPLICATION_CREDENTIALS=/tmp/keys/gcp-credentials.json
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
version: "3"
16+
17+
services:
18+
app:
19+
build: .
20+
environment:
21+
- OTEL_EXPORTER_OTLP_ENDPOINT=http://otelcol:4317
22+
- OTEL_SERVICE_NAME=otel-quickstart-spring-boot
23+
- OTEL_METRIC_EXPORT_INTERVAL=5000
24+
- GOOGLE_CLOUD_PROJECT=${GOOGLE_CLOUD_PROJECT?}
25+
volumes:
26+
- logs:/var/log:rw
27+
depends_on:
28+
- "otelcol"
29+
otelcol:
30+
image: otel/opentelemetry-collector-contrib:0.88.0
31+
volumes:
32+
- ./otel-collector-config.yaml:/etc/otelcol-contrib/config.yaml:ro
33+
- logs:/var/log:ro
34+
environment:
35+
- GOOGLE_CLOUD_PROJECT=${GOOGLE_CLOUD_PROJECT?}
36+
loadgen:
37+
image: golang:1.21
38+
command:
39+
[
40+
"go",
41+
"run",
42+
"github.com/rakyll/hey@latest",
43+
"-c=2",
44+
"-q=1",
45+
"http://app:8080/multi",
46+
]
47+
depends_on:
48+
- "app"
49+
volumes:
50+
logs:

0 commit comments

Comments
 (0)