Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy instrumentation quickstart out of java-docs-samples #283

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions examples/instrumentation-quickstart/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
HELP.md
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### VS Code ###
.vscode/

### Docker ###
Dockerfile
docker-compose*.yaml
otel-collector-config.yaml
37 changes: 37 additions & 0 deletions examples/instrumentation-quickstart/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
HELP.md
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.6/apache-maven-3.9.6-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar
38 changes: 38 additions & 0 deletions examples/instrumentation-quickstart/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Adapted from https://spring.io/guides/topicals/spring-boot-docker/#_multi_stage_build
# syntax=docker/dockerfile:experimental
FROM eclipse-temurin:17.0.9_9-jdk-alpine as build
WORKDIR /workspace/app

COPY mvnw .
COPY .mvn .mvn
COPY pom.xml .
COPY src src

RUN --mount=type=cache,target=/root/.m2 ./mvnw install -DskipTests
RUN mkdir -p target/dependency && (cd target/dependency; jar -xf ../*.jar)

FROM eclipse-temurin:17.0.9_9-jdk-alpine
VOLUME /tmp
ARG DEPENDENCY=/workspace/app/target/dependency
COPY --from=build ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY --from=build ${DEPENDENCY}/META-INF /app/META-INF
COPY --from=build ${DEPENDENCY}/BOOT-INF/classes /app
# [START opentelemetry_instrumentation_javaagent_dockerfile]
RUN wget -O /opentelemetry-javaagent.jar https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/download/v1.31.0/opentelemetry-javaagent.jar
CMD sh -c "java -javaagent:/opentelemetry-javaagent.jar -cp app:app/lib/* com.example.demo.DemoApplication \
2>&1 | tee /var/log/app.log"
# [END opentelemetry_instrumentation_javaagent_dockerfile]
64 changes: 64 additions & 0 deletions examples/instrumentation-quickstart/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# OpenTelemetry Spring Boot instrumentation example

This sample is a Spring Boot application instrumented with the [OpenTelemetry java
agent](https://opentelemetry.io/docs/instrumentation/java/automatic/). This is a java version
of [this golang
sample](https://github.com/GoogleCloudPlatform/golang-samples/tree/main/opentelemetry/instrumentation).
It uses docker compose to orchestrate running the application and sending it some requests.

The Java code is a basic Spring Boot application with two endpoints
- `/multi` makes a few requests to `/single` on localhost
- `/single` sleeps for a short time to simulate work

Docker compose also runs the OpenTelemetry collector, set up to receive telemetry from the Java
application and parse its logs from a shared volume. Finally, a loadgen container sends
requests to the Java app.

## Permissions

This sample writes to Cloud Logging, Cloud Monitoring, and Cloud Trace. Grant yourself the
following roles to run the example:
- `roles/logging.logWriter` – see https://cloud.google.com/logging/docs/access-control#permissions_and_roles
- `roles/monitoring.metricWriter` – see https://cloud.google.com/monitoring/access-control#predefined_roles
- `roles/cloudtrace.agent` – see https://cloud.google.com/trace/docs/iam#trace-roles

## Running the example

### Cloud Shell or GCE

```sh
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
cd java-docs-samples/opentelemetry/spring-boot-instrumentation/
docker compose up --abort-on-container-exit
```

### Locally with Application Default Credentials


First Create local credentials by running the following command and following the
oauth2 flow (read more about the command [here][auth_command]):

gcloud auth application-default login

Set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable with `export GOOGLE_APPLICATION_CREDENTIALS="$HOME/.config/gcloud/application_default_credentials.json"`
or manually set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to point to a service
account key JSON file path.

Learn more at [Setting Up Authentication for Server to Server Production Applications][ADC].

*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.

Then run the example:

```sh
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
cd java-docs-samples/opentelemetry/spring-boot-instrumentation/

# Lets collector read mounted config
export USERID="$(id -u)"
# Specify the project ID
export GOOGLE_CLOUD_PROJECT=<your project id>
docker compose -f docker-compose.yaml -f docker-compose.adc.yaml up --abort-on-container-exit
```

[auth_command]: https://cloud.google.com/sdk/gcloud/reference/beta/auth/application-default/login
33 changes: 33 additions & 0 deletions examples/instrumentation-quickstart/docker-compose.adc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Use this compose file along with docker-compose.yaml to pass Application Default
# Credentials from the host into the collector container:
#
# ```
# export GOOGLE_APPLICATION_CREDENTIALS=$HOME/.config/gcloud/application_default_credentials.json
# docker compose -f docker-compose.yaml -f docker-compose.adc.yaml up
# ```

version: "3"

services:
otelcol:
# If the collector does not have permission to read the mounted volumes, set
# USERID=$(id -u) to run the container as the current user
user: ${USERID}
volumes:
- ${GOOGLE_APPLICATION_CREDENTIALS:-/dev/null}:/tmp/keys/gcp-credentials.json:ro
environment:
- GOOGLE_APPLICATION_CREDENTIALS=/tmp/keys/gcp-credentials.json
50 changes: 50 additions & 0 deletions examples/instrumentation-quickstart/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

version: "3"

services:
app:
build: .
environment:
- OTEL_EXPORTER_OTLP_ENDPOINT=http://otelcol:4317
- OTEL_SERVICE_NAME=otel-quickstart-spring-boot
- OTEL_METRIC_EXPORT_INTERVAL=5000
- GOOGLE_CLOUD_PROJECT=${GOOGLE_CLOUD_PROJECT?}
volumes:
- logs:/var/log:rw
depends_on:
- "otelcol"
otelcol:
image: otel/opentelemetry-collector-contrib:0.92.0
volumes:
- ./otel-collector-config.yaml:/etc/otelcol-contrib/config.yaml:ro
- logs:/var/log:ro
environment:
- GOOGLE_CLOUD_PROJECT=${GOOGLE_CLOUD_PROJECT?}
loadgen:
image: golang:1.21
command:
[
"go",
"run",
"github.com/rakyll/hey@latest",
"-c=2",
"-q=1",
"http://app:8080/multi",
]
depends_on:
- "app"
volumes:
logs:
Loading
Loading