This sample is a Spring Boot application instrumented with the OpenTelemetry java agent. This is a java version of this golang sample. 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
/multimakes a few requests to/singleon localhost/singlesleeps 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.
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_rolesroles/monitoring.metricWriter– see https://cloud.google.com/monitoring/access-control#predefined_rolesroles/cloudtrace.agent– see https://cloud.google.com/trace/docs/iam#trace-roles
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
cd java-docs-samples/opentelemetry/spring-boot-instrumentation/
docker compose up --abort-on-container-exitFirst Create local credentials by running the following command and following the oauth2 flow (read more about the command here):
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:
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