Skip to content

Commit a09caab

Browse files
committed
Add Chapter 04 working example
1 parent c9aee47 commit a09caab

13 files changed

+795
-1
lines changed

Makefile

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
.PHONY: all
2+
all: opentelemetry-javaagent.jar cache-client-extension dropwizard
3+
4+
opentelemetry-javaagent.jar:
5+
# Download OpenTelemetry Java Agent v1.21.0
6+
curl -o ./opentelemetry-javaagent.jar \
7+
-L https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/download/v1.21.0/opentelemetry-javaagent.jar
8+
9+
.PHONY: all
10+
dropwizard: dropwizard/dropwizard-example/target/example.mv.db
11+
12+
dropwizard/dropwizard-example/pom.xml:
13+
# Clone the repository on version v2.1.1
14+
git clone git@github.com:dropwizard/dropwizard.git --branch v2.1.1 --single-branch
15+
16+
dropwizard/dropwizard-example/target/dropwizard-example-2.1.1.jar: dropwizard/dropwizard-example/pom.xml
17+
# Package the application
18+
cd dropwizard && ./mvnw -Dmaven.test.skip=true package
19+
20+
dropwizard/dropwizard-example/target/example.mv.db: dropwizard/dropwizard-example/target/dropwizard-example-2.1.1.jar
21+
# Prepare the H2 database
22+
cd dropwizard/dropwizard-example && java -jar target/dropwizard-example-2.1.1.jar db migrate example.yml
23+
24+
# Let Gradle (rather than Make) handle file system watching to rebuild if needed
25+
.PHONY: cache-client-extension
26+
cache-client-extension:
27+
cd cache-client-extension && ./gradlew jar
28+
29+
.PHONY: run-without-otel
30+
run-without-otel: dropwizard
31+
cd dropwizard/dropwizard-example && java -jar target/dropwizard-example-2.1.1.jar server example.yml
32+
33+
.PHONY: run-app
34+
run-app: dropwizard opentelemetry-javaagent.jar
35+
cd dropwizard/dropwizard-example && java -javaagent:../../opentelemetry-javaagent.jar \
36+
-Dotel.service.name=dropwizard-example \
37+
-jar target/dropwizard-example-2.1.1.jar server example.yml
38+
39+
.PHONY: run-app-with-extension
40+
run-app-with-extension: dropwizard opentelemetry-javaagent.jar cache-client-extension
41+
cd dropwizard/dropwizard-example && java -javaagent:../../opentelemetry-javaagent.jar \
42+
-Dotel.service.name=dropwizard-example \
43+
-Dotel.javaagent.extensions=../../cache-client-extension/lib/build/libs/cache-client-extension.jar \
44+
-jar target/dropwizard-example-2.1.1.jar server example.yml
45+
46+
.PHONY: run-app-with-logs
47+
run-app-with-logs: dropwizard opentelemetry-javaagent.jar
48+
cd dropwizard/dropwizard-example && java -javaagent:../../opentelemetry-javaagent.jar \
49+
-Dotel.service.name=dropwizard-example -Dotel.logs.exporter=otlp \
50+
-jar target/dropwizard-example-2.1.1.jar server ../../chapter8.example.yml
51+
52+
.PHONY: run-stack
53+
run-stack:
54+
docker compose up
55+
56+
.PHONY: run-all
57+
run-all: dropwizard opentelemetry-javaagent.jar
58+
make -j 2 run-stack run-app
59+
60+
.PHONY: run-all-with-extension
61+
run-all-with-extension: dropwizard opentelemetry-javaagent.jar
62+
make -j 2 run-stack run-app-with-extension
63+
64+
.PHONY: run-all-with-logs
65+
run-all-with-logs: dropwizard opentelemetry-javaagent.jar
66+
make -j 2 run-stack run-app-with-logs
67+
68+
69+
.PHONY: clean
70+
clean:
71+
rm -rf dropwizard
72+
rm -f opentelemetry-javaagent.jar
73+
rm -rf cache-client-extension/lib/build
74+
docker compose rm -f

README.md

+115-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,126 @@
11
# Apress Source Code
22

3-
This repository accompanies [Practical OpenTelemetry: Adopting Open Observability Standards Across Your Organization](https://www.link.springer.com/book/10.1007/9781484290743) by Daniel Gomez Blanco (Apress, 2023).
3+
This repository accompanies [Practical OpenTelemetry: Adopting Open Observability Standards Across Your Organization](https://www.link.springer.com/book/10.1007/9781484290743)
4+
by Daniel Gomez Blanco (Apress, 2023).
45

56
[comment]: #cover
67
![Cover image](9781484290743.jpg)
78

89
Download the files as a zip using the green button, or clone the repository to your machine using Git.
910

11+
## Scope
12+
13+
As mentioned within the introduction, most code snippets in the book are meant
14+
to be considered in isolation to illustrate the concepts discussed in each chapter. The intention is not to create yet
15+
another full-fledged OpenTelemetry demo environment, so their source code has not been made available in this repo. This
16+
is intentional, as OpenTelemetry provides its official OpenTelemetry Demo (available at
17+
https://github.com/open-telemetry/opentelemetry-demo) for this purpose, which allows users to work with OpenTelemetry
18+
instrumented services and visualise the telemetry produced in the observability platform of their choice.
19+
20+
However, *Chapter 4* demonstrates the power of standard instrumentation to automatically generate and export telemetry
21+
from within an existing Java application that has not been previously instrumented with OpenTelemetry. The resources in
22+
this project will make it easy for readers to stand up the examples described in this chapter.
23+
24+
## Installation
25+
26+
This project relies on GNU Make, Git, Java and Docker to run the `dropwizard-example` application, available at
27+
https://github.com/dropwizard/dropwizard. It has been tested with the following versions:
28+
29+
* MacOs Monterrey 12.5.1
30+
* GNU Make 3.81
31+
* Git 2.39.0
32+
* curl 7.79.1
33+
* Docker 20.10.20
34+
* OpenJDK 17
35+
36+
The application and the (minimal) telemetry stack this application spins up consists of:
37+
38+
* Dropwizard v2.1.1
39+
* OpenTelemetry Java Agent 1.21.0
40+
* OpenTelemetry Collector (Contrib) 0.64.0
41+
* Prometheus 2.38.0
42+
* Jaeger (All-in-One Distro) 1.37.0
43+
44+
To install and build the necessary resources:
45+
46+
```shell
47+
make
48+
```
49+
50+
This will clone and build the `dropwizard-example` application. Even though tests are skipped to speed the process, this
51+
Maven build can still a little while to complete (the intention is not to deviate from the `dropwizard-example` build
52+
instructions). This step will also initialise the in-memory database, download the OpenTelemetry Java Agent and build
53+
our `cache-client-extension` processor. It is also possible to execute these steps individually as defined in the
54+
`Makefile`.
55+
56+
Any changes to source code will be automatically rebuilt by running `make` again without the need to delete any
57+
resources. However, to clean up all build artefacts generated by this process, you can run:
58+
59+
```shell
60+
make clean
61+
```
62+
63+
In addition to deleting build artefacts, this command will also delete any stopped Docker containers.
64+
65+
## Running the application
66+
The `Makefile` provides a simple way to run the application in multiple modes, including the snippets in described in
67+
the book:
68+
69+
```shell
70+
# Run the application without OpenTelemetry instrumentation
71+
make run-without-otel
72+
73+
# Run the application with OpenTelemetry instrumentation
74+
make run-app
75+
76+
# Stand up Docker Compose stack with OpenTelemetry Collector, Prometheus and Jaeger
77+
make run-stack
78+
79+
# Run app with OpenTelemetry instrumentation (logs will be produced in the same terminal)
80+
make run-all
81+
82+
# Run app using the cache-client-extension SpanProcessor
83+
make run-app-with-extension
84+
85+
# Run app using the cache-client-extension SpanProcessor and telemetry stack in parallel
86+
make run-all-with-extension
87+
```
88+
89+
As mentioned in Chapter 4, running `dropwizard-example` will generate some `WARN` level logs, as the application comes
90+
pre-configured to push metrics to Graphite, which we won't use in this example. The following links, also cited in
91+
Chapter 4, can be used to send some requests to the application and to evaluate the telemetry produced:
92+
93+
* http://localhost:8080/hello-world: Welcome message from `dropwizard-example`.
94+
* http://localhost:9090: Prometheus web interface, to visualise metrics (with exemplars) produced.
95+
* http://localhost:16686: Jaeger UI, to examine traces for operations being handled by our application.
96+
97+
To interact with the database, as detailed in the example illustrated in Figure 4-2, you can send a request to the
98+
`/people` endpoint as follows:
99+
100+
```shell
101+
curl -H "Content-Type: application/json" -X POST \
102+
-d '{"fullName":"Other Person","jobTitle":"Other Title"}' \
103+
http://localhost:8080/people
104+
```
105+
106+
Navigating to http://localhost:8080/people will show the records currently present in the database.
107+
108+
To showcase the concepts in _Chapter 8 - Logs_, this project also provides the following commands:
109+
110+
```shell
111+
# Run app exporting logs in OTLP to the default OpenTelemetry Collector address
112+
make run-app-with-logs
113+
114+
# Run app exporting logs in OTLP and telemetry stack in parallel
115+
make run-all-with-logs
116+
```
117+
118+
In addition to configure the application to export logs via OTLP, this will use a modified `example.yml` Dropwizard
119+
config file called `chapter8.example.yml` which showcases OpenTelemetry instrumentation adding attributes to the default
120+
`console` logger, as discussed in _Chapter 8 - Logs_. Navigating to
121+
http://localhost:8080/hello-world/date?date=2023-01-15 will generate a request that showcases the use of MDC
122+
instrumentation to annotate log lines with the trace and span ID corresponding to the given request.
123+
10124
## Releases
11125

12126
Release v1.0 corresponds to the code in the published book, without corrections or updates.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
4+
networkTimeout=10000
5+
zipStoreBase=GRADLE_USER_HOME
6+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)