Skip to content

Commit 4927f40

Browse files
author
Emanuele Palazzetti
authored
Merge pull request #95 from palazzem/spring-boot-example
[example] update Spring Boot example with a simplified documentation
2 parents d0e24e7 + f9012c4 commit 4927f40

File tree

5 files changed

+51
-86
lines changed

5 files changed

+51
-86
lines changed
Lines changed: 32 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,60 @@
1-
## Spring-boot + JDBC example
2-
### Purpose
1+
## Spring-boot
32

4-
This project aims at demonstrating how to instrument legacy code based on the SpringBoot framework
5-
and a JDBC connection.
3+
This project provides a simple API using [Spring Boot][1]. The framework is supported and auto-instrumentation is used
4+
to trace the endpoints.
65

7-
We are using the [opentracing contributions](https://github.com/opentracing-contrib) in order to trace:
8-
* All the JDBC queries and calls to the DB
9-
* All incoming HTTP calls
10-
11-
12-
The instrumentation is injected via the Datadog Java Agent. Java agents allows developers to execute
13-
some code before starting the legacy application. All operations for instrumenting the code are done
14-
at this time.
6+
[1]: https://projects.spring.io/spring-boot/
157

168
### Run the demo
179

18-
The demo consistes into a very simple backend backed by an in-memory db. The Spring Boot application exposes
19-
2 endpoints that can be reached via an HTTP request.
20-
2110
#### Prerequisites
22-
1. Run the latest version of Datadog Agent. For instance, you can run it through a docker container:
23-
`docker run -d --name dd-agent -v /var/run/docker.sock:/var/run/docker.sock:ro -v /proc/:/host/proc/:ro -v /sys/fs/cgroup/:/host/sys/fs/cgroup:ro -e API_KEY=<YOUR-API-KEY> -e SD_BACKEND=docker -p 8126:8126 -P datadog/docker-dd-agent:latest`
24-
25-
#### Run the application
2611

27-
The first step is to edit the Spring configuration file and the Datadog Tracer file and check if the
28-
connection properties are okay.
29-
30-
```properties
31-
# file: src/resources/application.properties
32-
33-
spring.datasource.driver-class-name=io.opentracing.contrib.jdbc.TracingDriver
34-
spring.datasource.url=jdbc:tracing:h2:mem:spring-test
12+
Be sure to build the project so that the latest version of ``dd-trace-java`` components are used. You can build
13+
all libraries and examples launching from the ``dd-trace-java`` root folder:
14+
```bash
15+
./gradlew clean shadowJar
3516
```
3617

37-
```yaml
38-
# file: src/resources/dd-trace.yaml
39-
40-
# Service name used if none is provided in the app
41-
defaultServiceName: spring-app
42-
43-
# The writer to use.
44-
writer:
45-
type: DDAgentWriter
46-
host: localhost
47-
port: 8126
48-
49-
# The sampler to use.
50-
sampler:
51-
type: AllSampler
18+
Then you can launch the Datadog agent as follows:
19+
```bash
20+
cd dd-trace-examples/spring-boot-jdbc
21+
DD_API_KEY=<your_datadog_api_key> docker-compose up -d
5222
```
5323

54-
Then, is to run the Spring Application along the Datadog Java Agent.
55-
56-
- So first download the `jar` file from the main repository.
57-
58-
*NOTE:* While in beta, the latest version is best found on the [Snapshot Repo](https://oss.jfrog.org/artifactory/oss-snapshot-local/com/datadoghq/).
59-
60-
```
61-
# download the latest published version:
62-
wget -O dd-java-agent.jar 'https://search.maven.org/remote_content?g=com.datadoghq&a=dd-java-agent&v=LATEST'
63-
```
24+
A valid ``DD_API_KEY`` is required to post collected traces to the Datadog backend.
6425

65-
- Then add the following JVM argument when launching your application (in IDE, using Maven/Gradle run or simply in collaboration with the `>java -jar` command):
26+
#### Run the application
6627

67-
```
68-
-javaagent:/path/to/the/dd-java-agent.jar
28+
To launch the application, just:
29+
```bash
30+
./gradlew bootRun
6931
```
7032

71-
- Finally, build and run the application with the `javaagent` option. (Note, actual flag set in `spring-boot-jdbc.gradle`)
72-
73-
```
74-
cd path/to/dd-trace-examples/spring-boot-jdbc
75-
./gradlew bootRun -Pjavaagent=/path/to/the/dd-java-agent.jar
76-
```
33+
The ``bootRun`` Gradle command appends automatically the ``-javaagent`` argument, so that you don't need to specify
34+
the path of the Java Agent. Gradle executes the ``:dd-trace-examples:spring-boot-jdbc:bootRun`` task until you
35+
stop it.
7736

7837
### Generate traces
7938

80-
Once the application runs. Go to the following urls:
81-
82-
* [http://localhost:8080/demo/add?name=foo&email=bar](http://localhost:8080/demo/add?name=foo&email=bar)
83-
* [http://localhost:8080/demo/all](http://localhost:8080/demo/all)
39+
Once the Gradle task is running. Go to the following urls:
8440

85-
Then, you will able to see traces directly to the DataDog UI.
41+
* [http://localhost:8080/user/add?name=foo&email=bar](http://localhost:8080/user/add?name=foo&email=bar)
42+
* [http://localhost:8080/user/all](http://localhost:8080/user/all)
8643

87-
![Datadog APM](apm.png)
88-
89-
### How did we instrument this project?
90-
91-
If you want to instrument your own code (Spring-boot and JDBC based), here is how you should do it.
92-
93-
#### Dependencies
44+
Then get back to Datadog and wait a bit to see a trace coming.
9445

9546
#### Auto-instrumentation with the `dd-trace-agent`
9647

97-
The instrumentation is entirely done by the datadog agent which embed a set of rules that automatically recognizes & instruments:
48+
The instrumentation is entirely done by the datadog agent which embed a set of rules that automatically recognizes &
49+
instruments:
9850

9951
- The java servlet filters
52+
- The JDBC driver
10053

101-
The datadog agent embeds the [open tracing java agent](https://github.com/opentracing-contrib/java-agent).
54+
The Java Agent embeds the [OpenTracing Java Agent](https://github.com/opentracing-contrib/java-agent).
10255

103-
JDBC is not automatically instrumented yet by the agent. So you should follow the instructions on the next section.
56+
#### Note for JDBC tracing configuration
10457

105-
#### JDBC tracing configuration
106-
107-
Just edit your `application.properties` [file](src/main/resources/application.properties)
108-
and add `:tracing` right after the `jdbc` string as shown below.
109-
110-
```properties
111-
spring.datasource.url= jdbc:tracing:mysql://localhost:3306/springdb
112-
```
58+
JDBC is not automatically instrumented by the Java Agent, so we changed the `application.properties`
59+
[file](src/main/resources/application.properties) to use the OpenTracing Driver. Without this step in your
60+
applications, the JDBC driver will not be instrumented.
-112 KB
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
ddagent:
2+
image: datadog/docker-dd-agent
3+
environment:
4+
- DD_BIND_HOST=0.0.0.0
5+
- DD_API_KEY
6+
ports:
7+
- "127.0.0.1:8126:8126"
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
# you must set the following so that OpenTracing traced driver is used
12
spring.datasource.driver-class-name=io.opentracing.contrib.jdbc.TracingDriver
23
spring.datasource.url=jdbc:tracing:h2:mem:spring-test
3-
#spring.datasource.driver-class-name=org.h2.Driver
4-
#spring.datasource.url=jdbc:h2:mem:spring-test
4+
5+
# set the logging level
56
logging.level.root=INFO
67
logging.level.com.datadoghq.trace=DEBUG
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defaultServiceName: example-spring-boot
2+
3+
writer:
4+
type: DDAgentWriter
5+
host: localhost
6+
port: 8126
7+
8+
sampler:
9+
type: AllSampler

0 commit comments

Comments
 (0)