|
1 | | -## Spring-boot + JDBC example |
2 | | -### Purpose |
| 1 | +## Spring-boot |
3 | 2 |
|
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. |
6 | 5 |
|
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/ |
15 | 7 |
|
16 | 8 | ### Run the demo |
17 | 9 |
|
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 | | - |
21 | 10 | #### 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 |
26 | 11 |
|
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 |
35 | 16 | ``` |
36 | 17 |
|
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 |
52 | 22 | ``` |
53 | 23 |
|
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. |
64 | 25 |
|
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 |
66 | 27 |
|
67 | | -``` |
68 | | --javaagent:/path/to/the/dd-java-agent.jar |
| 28 | +To launch the application, just: |
| 29 | +```bash |
| 30 | +./gradlew bootRun |
69 | 31 | ``` |
70 | 32 |
|
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. |
77 | 36 |
|
78 | 37 | ### Generate traces |
79 | 38 |
|
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: |
84 | 40 |
|
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) |
86 | 43 |
|
87 | | - |
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. |
94 | 45 |
|
95 | 46 | #### Auto-instrumentation with the `dd-trace-agent` |
96 | 47 |
|
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: |
98 | 50 |
|
99 | 51 | - The java servlet filters |
| 52 | +- The JDBC driver |
100 | 53 |
|
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). |
102 | 55 |
|
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 |
104 | 57 |
|
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. |
0 commit comments