Skip to content

Commit 8b364cb

Browse files
committed
moved pet clinic as preparation for intro to instrumentation
1 parent f3dbce7 commit 8b364cb

File tree

8 files changed

+397
-0
lines changed

8 files changed

+397
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
---
2+
title: Installing the OpenTelemetry Collector
3+
linkTitle: 1. OpenTelemetry Collector
4+
weight: 1
5+
---
6+
7+
## 1. Introduction
8+
9+
The OpenTelemetry Collector is the core component of instrumenting infrastructure and applications. Its role is to collect and send:
10+
11+
* Infrastructure metrics (disk, cpu, memory, etc)
12+
* Application Performance Monitoring (APM) traces
13+
* Profiling data
14+
* Host and application logs
15+
16+
Splunk Observability Cloud offers a wizard to walk you through the setup of the Collector on both your infrastructure and applications.
17+
18+
{{% notice title="Delete any existing OpenTelemetry Collectors" style="warning" %}}
19+
If you have completed the Splunk IM workshop, please ensure you have deleted the collector running in Kubernetes before continuing. This can be done by running the following command:
20+
21+
``` bash
22+
helm delete splunk-otel-collector
23+
```
24+
25+
{{% /notice %}}
26+
27+
## 2. Confirm environment variables
28+
29+
To ensure your instance is configured correctly, we need to confirm that the required environment variables for this workshop are set correctly. In your terminal run the following command:
30+
31+
``` bash
32+
env
33+
```
34+
35+
In the output check the following environment variables are present and have values set:
36+
37+
```text
38+
ACCESS_TOKEN
39+
REALM
40+
RUM_TOKEN
41+
```
42+
43+
For this workshop, **all** of the above are required. If any are missing, please contact your instructor.
44+
45+
## 3. Install the OpenTelemetry Collector
46+
47+
We can then go ahead and install the Collector. Some additional parameters are passed to the install script, they are:
48+
49+
* `--with-instrumentation` - This will install the agent from the Splunk distribution of OpenTelemetry Java, which is then loaded automatically when the PetClinic Java application starts up. No configuration is required!
50+
* `--deployment-environment` - Sets the resource attribute `deployment.environment` to the value passed. This is used to filter views in the UI.
51+
* `--enable-profiler` - Enables the profiler for the Java application. This will generate CPU profiles for the application.
52+
* `--enable-profiler-memory` - Enables the profiler for the Java application. This will generate memory profiles for the application.
53+
* `--enable-metrics` - Enables the exporting of Micrometer metrics
54+
55+
``` bash
56+
curl -sSL https://dl.signalfx.com/splunk-otel-collector.sh > /tmp/splunk-otel-collector.sh && \
57+
sudo sh /tmp/splunk-otel-collector.sh --realm $REALM -- $ACCESS_TOKEN --mode agent --without-fluentd --with-instrumentation --deployment-environment $INSTANCE-petclinic --enable-profiler --enable-profiler-memory --enable-metrics
58+
```
59+
60+
{{% notice style="info" title="AWS/EC2 instances" %}}
61+
If you are attempting this workshop on an AWS/EC2 instance you will have to patch the collector to expose the hostname of the instance:
62+
63+
``` bash
64+
sudo sed -i 's/gcp, ecs, ec2, azure, system/system, gcp, ecs, ec2, azure/g' /etc/otel/collector/agent_config.yaml
65+
```
66+
67+
Once the `agent_config.yaml` has been patched, you will need to restart the collector:
68+
69+
``` bash
70+
sudo systemctl restart splunk-otel-collector
71+
```
72+
73+
{{% /notice %}}
74+
Once the installation is completed, you can navigate to the **Hosts with agent installed** dashboard to see the data from your host, **Dashboards → Hosts with agent installed**.
75+
76+
Use the dashboard filter and select `host.name` and type or select the hostname of your virtual machine. Once you see data flowing for your host, we are then ready to get started with the APM component.
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
![APM Environment](../images/apm-environment.png)
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.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
title: 3. Real User Monitoring
3+
weight: 3
4+
---
5+
6+
## 1. Enable RUM
7+
8+
For the Real User Monitoring (RUM) instrumentation, we will add the Open Telemetry Javascript [https://github.com/signalfx/splunk-otel-js-web](https://github.com/signalfx/splunk-otel-js-web) snippet in the pages, we will use the wizard again **Data Management → Add Integration → RUM Instrumentation → Browser Instrumentation**.
9+
10+
Select the preconfigured **RUM ACCESS TOKEN** from the dropdown, and click **Next**. Enter **App name** and **Environment** using the following syntax:
11+
12+
- `[hostname]-petclinic-service` - replacing `[hostname]` with your actual hostname.
13+
- `[hostname]-petclinic-env` - replacing `[hostname]` with your actual hostname.
14+
15+
Then you'll need to select the workshop RUM token and define the application and environment names. The wizard will then show a snippet of HTML code that needs to be placed at the top of the pages in the `<head>` section. In this example, we are using:
16+
17+
- Application Name: `<hostname>-petclinic-service`
18+
- Environment: `<hostname>-petclinic-env`
19+
20+
Copy the generated code snippet in the wizard or copy and edit the snippet below accordingly. You need to replace `<REALM>`, `<RUM_ACCESS_TOKEN>` and `<hostname>` with the actual values.
21+
22+
``` html
23+
<script src="https://cdn.signalfx.com/o11y-gdi-rum/latest/splunk-otel-web.js" crossorigin="anonymous"></script>
24+
<script>
25+
SplunkRum.init({
26+
beaconUrl: "https://rum-ingest.<REALM>.signalfx.com/v1/rum",
27+
rumAuth: "<RUM_ACCESS_TOKEN>",
28+
app: "<hostname>-petclinic-service",
29+
environment: "<hostname>-petclinic-env"
30+
});
31+
</script>
32+
```
33+
34+
The Spring PetClinic application uses a single HTML page as the "layout" page, that is reused across all pages of the application. This is the perfect location to insert the Splunk RUM Instrumentation Library as it will be loaded in all pages automatically.
35+
36+
Let's then edit the layout page:
37+
38+
```bash
39+
vi src/main/resources/templates/fragments/layout.html
40+
```
41+
42+
Next, insert the snippet we generated above in the `<head>` section of the page. Now we need to rebuild the application and run it again:
43+
44+
## 2. Rebuild PetClinic
45+
46+
Run the `maven` command to compile/build/package PetClinic:
47+
48+
```bash
49+
./mvnw package -Dmaven.test.skip=true
50+
```
51+
52+
```bash
53+
java \
54+
-Dserver.port=8083 \
55+
-Dotel.service.name=$(hostname)-petclinic-service \
56+
-Dotel.resource.attributes=version=0.314 \
57+
-jar target/spring-petclinic-*.jar --spring.profiles.active=mysql
58+
```
59+
60+
Then let's visit the application using a browser to generate real-user traffic `http://<VM_IP_ADDRESS>:8083`, now we should see RUM traces being reported.
61+
62+
Let's visit RUM and see some of the traces and metrics **Hamburger Menu → RUM** and you will see some of the Spring PetClinic URLs showing up in the UI.
63+
64+
When you drill down into a RUM trace you will see a link to APM in the spans. Clicking on the trace ID will take you to the corresponding APM trace for the current RUM trace.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
---
2+
title: 4. Log Observer
3+
weight: 4
4+
---
5+
6+
## 1. Introduction
7+
8+
For the Splunk Log Observer component, we will configure the Spring PetClinic application to write logs to a file in the filesystem and configure the Splunk OpenTelemetry Collect to read (tail) that log file and report the information to the Splunk Observability Platform.
9+
10+
## 2. OpenTelemetry Filelog Configuration
11+
12+
We need to configure the Splunk OpenTelemetry Collector to tail the Spring PetClinic log file and report the data to the Splunk Cloud HEC URL.
13+
14+
The Splunk OpenTelemetry Collector uses the [Filelog Receiver](https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/receiver/filelogreceiver/README.md) to consume logs. We will need to edit the collectors' configuration file:
15+
16+
``` bash
17+
sudo vi /etc/otel/collector/agent_config.yaml
18+
```
19+
20+
Under `receivers:` create the Filelog Receiver (make sure to indent correctly):
21+
22+
``` yaml {hl_lines="2-3"}
23+
receivers:
24+
filelog:
25+
include: [/home/ubuntu/spring-petclinic.log]
26+
```
27+
28+
The under the `service:` section, find the `logs:` pipeline, replace `fluentforward` with`filelog` and remove `otlp`(again, make sure to indent correctly):
29+
30+
``` yaml {hl_lines="2-7"}
31+
logs:
32+
receivers: [filelog]
33+
```
34+
35+
Save the file and exit the editor. Next, we need to validate that the HEC Token and HEC URL are configured for the collector to use. We will inspect the `/etc/otel/collector/splunk-otel-collector.conf` file:
36+
37+
```bash
38+
sudo cat /etc/otel/collector/splunk-otel-collector.conf
39+
```
40+
41+
Make sure `SPLUNK_HEC_URL` and `SPLUNK_HEC_TOKEN` have values set, we can then restart the collector to apply the changes:
42+
43+
``` bash
44+
sudo systemctl restart splunk-otel-collector
45+
```
46+
47+
## 3. Logback Settings
48+
49+
The Spring PetClinic application can be configured to use several different Java logging libraries. In this scenario, we are going to use logback. We just need to create a file named `logback.xml` in the configuration folder:
50+
51+
```bash
52+
vi ~/spring-petclinic/src/main/resources/logback.xml
53+
```
54+
55+
Copy and paste the following XML content:
56+
57+
```xml
58+
<?xml version="1.0" encoding="UTF-8"?>
59+
<!DOCTYPE xml>
60+
<configuration scan="true" scanPeriod="30 seconds">
61+
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
62+
<resetJUL>true</resetJUL>
63+
</contextListener>
64+
<logger name="org.springframework.samples.petclinic" level="info"/>
65+
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
66+
<file>/home/ubuntu/spring-petclinic.log</file>
67+
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
68+
<fileNamePattern>springLogFile.%d{yyyy-MM-dd}.log</fileNamePattern>
69+
<maxHistory>5</maxHistory>
70+
<totalSizeCap>1GB</totalSizeCap>
71+
</rollingPolicy>
72+
<encoder>
73+
<pattern>
74+
%d{yyyy-MM-dd HH:mm:ss} - %logger{36} - %msg trace_id=%X{trace_id} span_id=%X{span_id} trace_flags=%X{trace_flags} %n service.name=%property{otel.resource.service.name}, deployment.environment=%property{otel.resource.deployment.environment}: %m%n
75+
</pattern>
76+
</encoder>
77+
</appender>
78+
<root level="info">
79+
<appender-ref ref="file" />
80+
</root>
81+
</configuration>
82+
```
83+
84+
Now we need to rebuild the application and run it again:
85+
86+
```bash
87+
./mvnw package -Dmaven.test.skip=true
88+
```
89+
90+
Once the rebuild has been completed we can then run the application again:
91+
92+
```bash
93+
java \
94+
-Dserver.port=8083 \
95+
-Dotel.service.name=$(hostname)-petclinic-service \
96+
-Dotel.resource.attributes=version=0.314 \
97+
-jar target/spring-petclinic-*.jar --spring.profiles.active=mysql
98+
```
99+
100+
## 4. View Logs
101+
102+
From the left-hand menu click on **Log Observer**. Click on **Index** and select **o11y-workshop-XXX.splunkcloud.com** (where **XXX** will be the realm you are running in). On the right-hand side select **petclinic-workshop** and then click **Apply**. You should see log messages being reported.
103+
104+
Next, click **Add Filter** search for the field `service_name` select the value `<your host name>-petclinic-service` and click `=` (include). You should now see only the log messages from your PetClinic application.
105+
106+
![Log Observer](../images/log-observer.png)
107+
108+
## 4. Summary
109+
110+
This is the end of the exercise and we have certainly covered a lot of ground. At this point, you should have metrics, traces (APM & RUM), logs, database query performance and code profiling being reported into Splunk Observability Cloud. **Congratulations**!

0 commit comments

Comments
 (0)