|
| 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 |
0 commit comments