|
| 1 | +--- |
| 2 | +title: Zero Configuration Auto Instrumentation for Java |
| 3 | +linkTitle: 2. Zero Configuration |
| 4 | +weight: 2 |
| 5 | +--- |
| 6 | + |
| 7 | +## 1. Spring PetClinic Application |
| 8 | + |
| 9 | +The first thing we need to set up APM is... well, an application. For this exercise, we will use the Spring PetClinic application. This is a very popular sample Java application built with Spring framework (Springboot). |
| 10 | + |
| 11 | +First, clone the PetClinic GitHub repository, and then we will compile, build, package and test the application: |
| 12 | + |
| 13 | +```bash |
| 14 | +git clone https://github.com/spring-projects/spring-petclinic |
| 15 | +``` |
| 16 | + |
| 17 | +Change into the `spring-petclinic` directory (and checkout a specific commit): |
| 18 | + |
| 19 | +```bash |
| 20 | +cd spring-petclinic && git checkout 276880e |
| 21 | +``` |
| 22 | + |
| 23 | +Start a MySQL database for PetClinic to use: |
| 24 | + |
| 25 | +```bash |
| 26 | +docker run -d -e MYSQL_USER=petclinic -e MYSQL_PASSWORD=petclinic -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=petclinic -p 3306:3306 docker.io/mysql:5.7.8 |
| 27 | +``` |
| 28 | + |
| 29 | +Next, we will start a Docker container running Locust that will generate some simple traffic to the PetClinic application. Locust is a simple load-testing tool that can be used to generate traffic to a web application. |
| 30 | + |
| 31 | +```bash |
| 32 | +docker run --network="host" -d -p 8090:8090 -v /home/ubuntu/workshop/petclinic:/mnt/locust docker.io/locustio/locust -f /mnt/locust/locustfile.py --headless -u 1 -r 1 -H http://127.0.0.1:8083 |
| 33 | +``` |
| 34 | + |
| 35 | +Next, run the `maven` command to compile/build/package PetClinic: |
| 36 | + |
| 37 | +```bash |
| 38 | +./mvnw package -Dmaven.test.skip=true |
| 39 | +``` |
| 40 | + |
| 41 | +{{% notice style="info" %}} |
| 42 | +This will take a few minutes the first time you run, `maven` will download a lot of dependencies before it compiles the application. Future executions will be a lot quicker. |
| 43 | +{{% /notice %}} |
| 44 | + |
| 45 | +Once the compilation is complete, you can run the application with the following command. Notice that we are passing the `mysql` profile to the application, this will tell the application to use the MySQL database we started earlier. We are also setting the `otel.service.name` to a logical service name that will also be used in the UI for filtering: |
| 46 | + |
| 47 | +```bash |
| 48 | +java \ |
| 49 | +-Dserver.port=8083 \ |
| 50 | +-Dotel.service.name=$(hostname)-petclinic-service \ |
| 51 | +-jar target/spring-petclinic-*.jar --spring.profiles.active=mysql |
| 52 | +``` |
| 53 | + |
| 54 | +You can validate if the application is running by visiting `http://<VM_IP_ADDRESS>:8083`. |
| 55 | + |
| 56 | +## 2. AlwaysOn Profiling and Metrics |
| 57 | + |
| 58 | +When we installed the collector we configured it to enable AlwaysOn Profiling and Metrics. This means that the collector will automatically generate CPU and Memory profiles for the application and send them to Splunk Observability Cloud. |
| 59 | + |
| 60 | +When you start the PetClinic application you will see the collector automatically detect the application and instrument it for traces and profiling. |
| 61 | + |
| 62 | +{{% tab title="Example output" %}} |
| 63 | + |
| 64 | +``` text {wrap="false"} |
| 65 | +Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/lib/splunk-instrumentation/splunk-otel-javaagent.jar -Dsplunk.profiler.enabled=true -Dsplunk.profiler.memory.enabled=true |
| 66 | +OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended |
| 67 | +[otel.javaagent 2023-06-26 13:32:04:661 +0100] [main] INFO io.opentelemetry.javaagent.tooling.VersionLogger - opentelemetry-javaagent - version: splunk-1.25.0-otel-1.27.0 |
| 68 | +[otel.javaagent 2023-06-26 13:32:05:294 +0100] [main] INFO com.splunk.javaagent.shaded.io.micrometer.core.instrument.push.PushMeterRegistry - publishing metrics for SignalFxMeterRegistry every 30s |
| 69 | +[otel.javaagent 2023-06-26 13:32:07:043 +0100] [main] INFO com.splunk.opentelemetry.profiler.ConfigurationLogger - ----------------------- |
| 70 | +[otel.javaagent 2023-06-26 13:32:07:044 +0100] [main] INFO com.splunk.opentelemetry.profiler.ConfigurationLogger - Profiler configuration: |
| 71 | +[otel.javaagent 2023-06-26 13:32:07:047 +0100] [main] INFO com.splunk.opentelemetry.profiler.ConfigurationLogger - splunk.profiler.enabled : true |
| 72 | +[otel.javaagent 2023-06-26 13:32:07:048 +0100] [main] INFO com.splunk.opentelemetry.profiler.ConfigurationLogger - splunk.profiler.directory : /tmp |
| 73 | +[otel.javaagent 2023-06-26 13:32:07:049 +0100] [main] INFO com.splunk.opentelemetry.profiler.ConfigurationLogger - splunk.profiler.recording.duration : 20s |
| 74 | +[otel.javaagent 2023-06-26 13:32:07:050 +0100] [main] INFO com.splunk.opentelemetry.profiler.ConfigurationLogger - splunk.profiler.keep-files : false |
| 75 | +[otel.javaagent 2023-06-26 13:32:07:051 +0100] [main] INFO com.splunk.opentelemetry.profiler.ConfigurationLogger - splunk.profiler.logs-endpoint : null |
| 76 | +[otel.javaagent 2023-06-26 13:32:07:053 +0100] [main] INFO com.splunk.opentelemetry.profiler.ConfigurationLogger - otel.exporter.otlp.endpoint : null |
| 77 | +[otel.javaagent 2023-06-26 13:32:07:054 +0100] [main] INFO com.splunk.opentelemetry.profiler.ConfigurationLogger - splunk.profiler.memory.enabled : true |
| 78 | +[otel.javaagent 2023-06-26 13:32:07:055 +0100] [main] INFO com.splunk.opentelemetry.profiler.ConfigurationLogger - splunk.profiler.tlab.enabled : true |
| 79 | +[otel.javaagent 2023-06-26 13:32:07:056 +0100] [main] INFO com.splunk.opentelemetry.profiler.ConfigurationLogger - splunk.profiler.memory.event.rate : 150/s |
| 80 | +[otel.javaagent 2023-06-26 13:32:07:057 +0100] [main] INFO com.splunk.opentelemetry.profiler.ConfigurationLogger - splunk.profiler.call.stack.interval : PT10S |
| 81 | +[otel.javaagent 2023-06-26 13:32:07:058 +0100] [main] INFO com.splunk.opentelemetry.profiler.ConfigurationLogger - splunk.profiler.include.internal.stacks : false |
| 82 | +[otel.javaagent 2023-06-26 13:32:07:059 +0100] [main] INFO com.splunk.opentelemetry.profiler.ConfigurationLogger - splunk.profiler.tracing.stacks.only : false |
| 83 | +[otel.javaagent 2023-06-26 13:32:07:059 +0100] [main] INFO com.splunk.opentelemetry.profiler.ConfigurationLogger - ----------------------- |
| 84 | +[otel.javaagent 2023-06-26 13:32:07:060 +0100] [main] INFO com.splunk.opentelemetry.profiler.JfrActivator - Profiler is active. |
| 85 | +``` |
| 86 | + |
| 87 | +{{% /tab %}} |
| 88 | + |
| 89 | +## 3. Review Profiling Data Collection |
| 90 | + |
| 91 | +You can now visit the Splunk APM UI and examine the application components, traces, profiling, DB Query performance and metrics. From the left-hand menu **APM** → Explore**, click the environment dropdown and select your environment (which is prefixed with the hostname of your instance). |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | +Once your validation is complete you can stop the application by pressing `Ctrl-c`. |
| 96 | + |
| 97 | +## 4. Adding Resource Attributes to Spans |
| 98 | + |
| 99 | +Resource attributes can be added to every reported span. For example `version=0.314`. A comma-separated list of resource attributes can also be defined e.g. `key1=val1,key2=val2`. |
| 100 | + |
| 101 | +Let's launch the PetClinic again using a new resource attribute. Note, that adding resource attributes to the run command will override what was defined when we installed the collector. Let's add a new resource attribute `version=0.314`: |
| 102 | + |
| 103 | +```bash |
| 104 | +java \ |
| 105 | +-Dserver.port=8083 \ |
| 106 | +-Dotel.service.name=$INSTANCE-petclinic-service \ |
| 107 | +-Dotel.resource.attributes=version=0.314 \ |
| 108 | +-jar target/spring-petclinic-*.jar --spring.profiles.active=mysql |
| 109 | +``` |
| 110 | + |
| 111 | +Back in the Splunk APM UI we can drill down on a recent trace and see the new `version` attribute in a span. |
0 commit comments