├── .gitattributes ├── .gitignore ├── 9781484290743.jpg ├── Contributing.md ├── LICENSE.txt ├── Makefile ├── README.md ├── cache-client-extension ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── lib │ ├── build.gradle │ └── src │ │ └── main │ │ └── java │ │ └── com │ │ └── example │ │ ├── CacheClientCustomizer.java │ │ └── CacheClientSpanProcessor.java └── settings.gradle ├── chapter8.example.yml ├── docker-compose.yml └── otel-collector.yml /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore Gradle project-specific cache directory 2 | .gradle 3 | 4 | # Ignore build files 5 | dropwizard 6 | opentelemetry-javaagent.jar 7 | cache-client-extension/lib/build 8 | 9 | # Ignore IntelliJ Idea files 10 | .idea 11 | 12 | # Ignore DS_Store 13 | .DS_Store 14 | -------------------------------------------------------------------------------- /9781484290743.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/practical-open-telemetry/a55b3292901162baf28fb7a2bd53ffde7a0d9ceb/9781484290743.jpg -------------------------------------------------------------------------------- /Contributing.md: -------------------------------------------------------------------------------- 1 | # Contributing to Apress Source Code 2 | 3 | Copyright for Apress source code belongs to the author(s). However, under fair use you are encouraged to fork and contribute minor corrections and updates for the benefit of the author(s) and other readers. 4 | 5 | ## How to Contribute 6 | 7 | 1. Make sure you have a GitHub account. 8 | 2. Fork the repository for the relevant book. 9 | 3. Create a new branch on which to make your change, e.g. 10 | `git checkout -b my_code_contribution` 11 | 4. Commit your change. Include a commit message describing the correction. Please note that if your commit message is not clear, the correction will not be accepted. 12 | 5. Submit a pull request. 13 | 14 | Thank you for your contribution! -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Freeware License, some rights reserved 2 | 3 | Copyright (c) 2023 Daniel Gomez Blanco 4 | 5 | Permission is hereby granted, free of charge, to anyone obtaining a copy 6 | of this software and associated documentation files (the "Software"), 7 | to work with the Software within the limits of freeware distribution and fair use. 8 | This includes the rights to use, copy, and modify the Software for personal use. 9 | Users are also allowed and encouraged to submit corrections and modifications 10 | to the Software for the benefit of other users. 11 | 12 | It is not allowed to reuse, modify, or redistribute the Software for 13 | commercial use in any way, or for a user’s educational materials such as books 14 | or blog articles without prior permission from the copyright holder. 15 | 16 | The above copyright notice and this permission notice need to be included 17 | in all copies or substantial portions of the software. 18 | 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | SOFTWARE. 26 | 27 | 28 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 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: -------------------------------------------------------------------------------- 1 | # Apress Source Code 2 | 3 | This repository accompanies [Practical OpenTelemetry: Adopting Open Observability Standards Across Your Organization](https://link.springer.com/book/10.1007/978-1-4842-9075-0) 4 | by Daniel Gomez Blanco (Apress, 2023). 5 | 6 | [comment]: #cover 7 | ![Cover image](9781484290743.jpg) 8 | 9 | Download the files as a zip using the green button, or clone the repository to your machine using Git. 10 | 11 | ## Scope 12 | 13 | As mentioned within the _Introduction_, most code snippets in the book are meant to be considered in isolation to 14 | illustrate the concepts discussed in each chapter. The intention is not to create yet another full-fledged 15 | OpenTelemetry demo environment, so their source code has not been made available in this repo. This is intentional, 16 | 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 they're most familiar with. 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 said 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, while in the root directory of this project, run: 45 | 46 | ```shell 47 | make 48 | ``` 49 | 50 | This will clone and build the `dropwizard-example` application. Even though tests are skipped to speed up the process, 51 | this Maven build can still a little while to complete (the intention is not to deviate from the `dropwizard-example` 52 | build instructions). This step will also initialise the in-memory database, download the OpenTelemetry Java Agent 53 | and build our `cache-client-extension` processor. It is also possible to execute these steps individually as defined 54 | in the `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 described in 67 | _Chapter 4_: 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_, 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_. 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 | 124 | ## Releases 125 | 126 | Release v1.0 corresponds to the code in the published book, without corrections or updates. 127 | 128 | ## Contributions 129 | 130 | See the file Contributing.md for more information on how you can contribute to this repository. 131 | -------------------------------------------------------------------------------- /cache-client-extension/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/practical-open-telemetry/a55b3292901162baf28fb7a2bd53ffde7a0d9ceb/cache-client-extension/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /cache-client-extension/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 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 7 | -------------------------------------------------------------------------------- /cache-client-extension/gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Copyright © 2015-2021 the original authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | # 21 | # Gradle start up script for POSIX generated by Gradle. 22 | # 23 | # Important for running: 24 | # 25 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is 26 | # noncompliant, but you have some other compliant shell such as ksh or 27 | # bash, then to run this script, type that shell name before the whole 28 | # command line, like: 29 | # 30 | # ksh Gradle 31 | # 32 | # Busybox and similar reduced shells will NOT work, because this script 33 | # requires all of these POSIX shell features: 34 | # * functions; 35 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}», 36 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»; 37 | # * compound commands having a testable exit status, especially «case»; 38 | # * various built-in commands including «command», «set», and «ulimit». 39 | # 40 | # Important for patching: 41 | # 42 | # (2) This script targets any POSIX shell, so it avoids extensions provided 43 | # by Bash, Ksh, etc; in particular arrays are avoided. 44 | # 45 | # The "traditional" practice of packing multiple parameters into a 46 | # space-separated string is a well documented source of bugs and security 47 | # problems, so this is (mostly) avoided, by progressively accumulating 48 | # options in "$@", and eventually passing that to Java. 49 | # 50 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, 51 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; 52 | # see the in-line comments for details. 53 | # 54 | # There are tweaks for specific operating systems such as AIX, CygWin, 55 | # Darwin, MinGW, and NonStop. 56 | # 57 | # (3) This script is generated from the Groovy template 58 | # https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt 59 | # within the Gradle project. 60 | # 61 | # You can find Gradle at https://github.com/gradle/gradle/. 62 | # 63 | ############################################################################## 64 | 65 | # Attempt to set APP_HOME 66 | 67 | # Resolve links: $0 may be a link 68 | app_path=$0 69 | 70 | # Need this for daisy-chained symlinks. 71 | while 72 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path 73 | [ -h "$app_path" ] 74 | do 75 | ls=$( ls -ld "$app_path" ) 76 | link=${ls#*' -> '} 77 | case $link in #( 78 | /*) app_path=$link ;; #( 79 | *) app_path=$APP_HOME$link ;; 80 | esac 81 | done 82 | 83 | # This is normally unused 84 | # shellcheck disable=SC2034 85 | APP_BASE_NAME=${0##*/} 86 | APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit 87 | 88 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 89 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 90 | 91 | # Use the maximum available, or set MAX_FD != -1 to use that value. 92 | MAX_FD=maximum 93 | 94 | warn () { 95 | echo "$*" 96 | } >&2 97 | 98 | die () { 99 | echo 100 | echo "$*" 101 | echo 102 | exit 1 103 | } >&2 104 | 105 | # OS specific support (must be 'true' or 'false'). 106 | cygwin=false 107 | msys=false 108 | darwin=false 109 | nonstop=false 110 | case "$( uname )" in #( 111 | CYGWIN* ) cygwin=true ;; #( 112 | Darwin* ) darwin=true ;; #( 113 | MSYS* | MINGW* ) msys=true ;; #( 114 | NONSTOP* ) nonstop=true ;; 115 | esac 116 | 117 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 118 | 119 | 120 | # Determine the Java command to use to start the JVM. 121 | if [ -n "$JAVA_HOME" ] ; then 122 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 123 | # IBM's JDK on AIX uses strange locations for the executables 124 | JAVACMD=$JAVA_HOME/jre/sh/java 125 | else 126 | JAVACMD=$JAVA_HOME/bin/java 127 | fi 128 | if [ ! -x "$JAVACMD" ] ; then 129 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 130 | 131 | Please set the JAVA_HOME variable in your environment to match the 132 | location of your Java installation." 133 | fi 134 | else 135 | JAVACMD=java 136 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 137 | 138 | Please set the JAVA_HOME variable in your environment to match the 139 | location of your Java installation." 140 | fi 141 | 142 | # Increase the maximum file descriptors if we can. 143 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then 144 | case $MAX_FD in #( 145 | max*) 146 | # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. 147 | # shellcheck disable=SC3045 148 | MAX_FD=$( ulimit -H -n ) || 149 | warn "Could not query maximum file descriptor limit" 150 | esac 151 | case $MAX_FD in #( 152 | '' | soft) :;; #( 153 | *) 154 | # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. 155 | # shellcheck disable=SC3045 156 | ulimit -n "$MAX_FD" || 157 | warn "Could not set maximum file descriptor limit to $MAX_FD" 158 | esac 159 | fi 160 | 161 | # Collect all arguments for the java command, stacking in reverse order: 162 | # * args from the command line 163 | # * the main class name 164 | # * -classpath 165 | # * -D...appname settings 166 | # * --module-path (only if needed) 167 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. 168 | 169 | # For Cygwin or MSYS, switch paths to Windows format before running java 170 | if "$cygwin" || "$msys" ; then 171 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) 172 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) 173 | 174 | JAVACMD=$( cygpath --unix "$JAVACMD" ) 175 | 176 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 177 | for arg do 178 | if 179 | case $arg in #( 180 | -*) false ;; # don't mess with options #( 181 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath 182 | [ -e "$t" ] ;; #( 183 | *) false ;; 184 | esac 185 | then 186 | arg=$( cygpath --path --ignore --mixed "$arg" ) 187 | fi 188 | # Roll the args list around exactly as many times as the number of 189 | # args, so each arg winds up back in the position where it started, but 190 | # possibly modified. 191 | # 192 | # NB: a `for` loop captures its iteration list before it begins, so 193 | # changing the positional parameters here affects neither the number of 194 | # iterations, nor the values presented in `arg`. 195 | shift # remove old arg 196 | set -- "$@" "$arg" # push replacement arg 197 | done 198 | fi 199 | 200 | # Collect all arguments for the java command; 201 | # * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of 202 | # shell script including quotes and variable substitutions, so put them in 203 | # double quotes to make sure that they get re-expanded; and 204 | # * put everything else in single quotes, so that it's not re-expanded. 205 | 206 | set -- \ 207 | "-Dorg.gradle.appname=$APP_BASE_NAME" \ 208 | -classpath "$CLASSPATH" \ 209 | org.gradle.wrapper.GradleWrapperMain \ 210 | "$@" 211 | 212 | # Stop when "xargs" is not available. 213 | if ! command -v xargs >/dev/null 2>&1 214 | then 215 | die "xargs is not available" 216 | fi 217 | 218 | # Use "xargs" to parse quoted args. 219 | # 220 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed. 221 | # 222 | # In Bash we could simply go: 223 | # 224 | # readarray ARGS < <( xargs -n1 <<<"$var" ) && 225 | # set -- "${ARGS[@]}" "$@" 226 | # 227 | # but POSIX shell has neither arrays nor command substitution, so instead we 228 | # post-process each arg (as a line of input to sed) to backslash-escape any 229 | # character that might be a shell metacharacter, then use eval to reverse 230 | # that process (while maintaining the separation between arguments), and wrap 231 | # the whole thing up as a single "set" statement. 232 | # 233 | # This will of course break if any of these variables contains a newline or 234 | # an unmatched quote. 235 | # 236 | 237 | eval "set -- $( 238 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | 239 | xargs -n1 | 240 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | 241 | tr '\n' ' ' 242 | )" '"$@"' 243 | 244 | exec "$JAVACMD" "$@" 245 | -------------------------------------------------------------------------------- /cache-client-extension/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%"=="" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%"=="" set DIRNAME=. 29 | @rem This is normally unused 30 | set APP_BASE_NAME=%~n0 31 | set APP_HOME=%DIRNAME% 32 | 33 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 34 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 35 | 36 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 37 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 38 | 39 | @rem Find java.exe 40 | if defined JAVA_HOME goto findJavaFromJavaHome 41 | 42 | set JAVA_EXE=java.exe 43 | %JAVA_EXE% -version >NUL 2>&1 44 | if %ERRORLEVEL% equ 0 goto execute 45 | 46 | echo. 47 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 48 | echo. 49 | echo Please set the JAVA_HOME variable in your environment to match the 50 | echo location of your Java installation. 51 | 52 | goto fail 53 | 54 | :findJavaFromJavaHome 55 | set JAVA_HOME=%JAVA_HOME:"=% 56 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 57 | 58 | if exist "%JAVA_EXE%" goto execute 59 | 60 | echo. 61 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 62 | echo. 63 | echo Please set the JAVA_HOME variable in your environment to match the 64 | echo location of your Java installation. 65 | 66 | goto fail 67 | 68 | :execute 69 | @rem Setup the command line 70 | 71 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 72 | 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if %ERRORLEVEL% equ 0 goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | set EXIT_CODE=%ERRORLEVEL% 85 | if %EXIT_CODE% equ 0 set EXIT_CODE=1 86 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% 87 | exit /b %EXIT_CODE% 88 | 89 | :mainEnd 90 | if "%OS%"=="Windows_NT" endlocal 91 | 92 | :omega 93 | -------------------------------------------------------------------------------- /cache-client-extension/lib/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | } 4 | 5 | repositories { 6 | // Use Maven Central for resolving dependencies. 7 | mavenCentral() 8 | } 9 | 10 | dependencies { 11 | // Dependencies marked as compileOnly as classes are provided by Java agent at runtime 12 | compileOnly platform("io.opentelemetry:opentelemetry-bom:1.21.0") 13 | compileOnly('io.opentelemetry:opentelemetry-api') 14 | compileOnly('io.opentelemetry:opentelemetry-sdk') 15 | compileOnly("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure-spi") 16 | 17 | compileOnly platform("io.opentelemetry:opentelemetry-bom-alpha:1.21.0-alpha") 18 | compileOnly ("io.opentelemetry:opentelemetry-semconv") 19 | 20 | // Use Google's AutoService to generate SPI files 21 | annotationProcessor 'com.google.auto.service:auto-service:1.0-rc5' 22 | compileOnly 'com.google.auto.service:auto-service:1.0-rc5' 23 | } 24 | 25 | project.archivesBaseName = 'cache-client-extension' 26 | jar {} 27 | -------------------------------------------------------------------------------- /cache-client-extension/lib/src/main/java/com/example/CacheClientCustomizer.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.google.auto.service.AutoService; 4 | import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer; 5 | import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider; 6 | import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; 7 | import io.opentelemetry.sdk.trace.SdkTracerProviderBuilder; 8 | 9 | /** 10 | * CacheClientCustomizer implements the {@link AutoConfigurationCustomizerProvider} interface to add a span processor 11 | * to the configured trace provider. 12 | */ 13 | @AutoService(AutoConfigurationCustomizerProvider.class) 14 | public class CacheClientCustomizer implements AutoConfigurationCustomizerProvider { 15 | 16 | @Override 17 | public void customize(AutoConfigurationCustomizer customizer) { 18 | customizer.addTracerProviderCustomizer(this::configureTracerProvider); 19 | } 20 | 21 | private SdkTracerProviderBuilder configureTracerProvider(SdkTracerProviderBuilder builder, 22 | ConfigProperties config) { 23 | return builder.addSpanProcessor(new CacheClientSpanProcessor()); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /cache-client-extension/lib/src/main/java/com/example/CacheClientSpanProcessor.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import io.opentelemetry.api.common.AttributeKey; 4 | import io.opentelemetry.api.trace.SpanKind; 5 | import io.opentelemetry.context.Context; 6 | import io.opentelemetry.sdk.common.CompletableResultCode; 7 | import io.opentelemetry.sdk.trace.ReadWriteSpan; 8 | import io.opentelemetry.sdk.trace.ReadableSpan; 9 | import io.opentelemetry.sdk.trace.SpanProcessor; 10 | 11 | import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NET_PEER_NAME; 12 | 13 | /** 14 | * CacheClientSpanProcessor implements the {@link SpanProcessor} interface to add a http.is_cache_client attribute when 15 | * a {@code Span} is started matching a given span kind and net.peer.name. This allows OpenTelemetry Collectors to 16 | * easily unset the status code for this type of spans if the result is 404. 17 | * 18 | * The reason the status of the span cannot be modified within the onEnd() method is that, at this point, the span 19 | * is finished and cannot be modified. In future releases of OpenTelemetry this may be possible if a beforeEnd() hook 20 | * is made available (see https://github.com/open-telemetry/opentelemetry-specification/issues/1089). 21 | */ 22 | public class CacheClientSpanProcessor implements SpanProcessor { 23 | 24 | @Override 25 | public void onStart(Context parentContext, ReadWriteSpan span) { 26 | // Mark the span as a "cache client" span if it's a client span calling a given service 27 | if (span.getKind().equals(SpanKind.CLIENT) && "cache-proxy.example.com".equals(span.getAttribute(NET_PEER_NAME))) { 28 | span.setAttribute(AttributeKey.booleanKey("http.is_cache_client"), true); 29 | } 30 | 31 | // For demo purposes, we add another custom attribute on every span processed by this processor 32 | span.setAttribute(AttributeKey.stringKey("custom_processor"), "com.example.Spanprocessor"); 33 | } 34 | 35 | @Override 36 | public boolean isStartRequired() { 37 | return true; 38 | } 39 | 40 | @Override 41 | public void onEnd(ReadableSpan span) {} 42 | 43 | @Override 44 | public boolean isEndRequired() { 45 | return false; 46 | } 47 | 48 | @Override 49 | public CompletableResultCode shutdown() { 50 | return CompletableResultCode.ofSuccess(); 51 | } 52 | 53 | @Override 54 | public CompletableResultCode forceFlush() { 55 | return CompletableResultCode.ofSuccess(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /cache-client-extension/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'cache-client-extension' 2 | include('lib') 3 | -------------------------------------------------------------------------------- /chapter8.example.yml: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # This config file is the config file used in Chapter 8 of Practical 3 | # OpenTelemetry. It is a copy of the default `example.yml` used within the 4 | # official dropwizard-example@2.1.1 (https://github.com/dropwizard/dropwizard) 5 | # replacing the `logging` block for a block demonstrating OpenTelemetry 6 | # instrumentation. 7 | ############################################################################### 8 | 9 | template: Hello, %s! 10 | 11 | defaultName: ${DW_DEFAULT_NAME:-Stranger} 12 | 13 | # Database settings. 14 | database: 15 | 16 | # the name of your JDBC driver 17 | driverClass: org.h2.Driver 18 | 19 | # the username 20 | user: sa 21 | 22 | # the password 23 | password: sa 24 | 25 | # the JDBC URL 26 | url: jdbc:h2:./target/example 27 | 28 | # use the simple server factory if you only want to run on a single port 29 | #server: 30 | # type: simple 31 | # connector: 32 | # type: http 33 | # port: 8080 34 | 35 | server: 36 | # softNofileLimit: 1000 37 | # hardNofileLimit: 1000 38 | applicationConnectors: 39 | - type: http 40 | port: 8080 41 | - type: https 42 | port: 8443 43 | keyStorePath: example.keystore 44 | keyStorePassword: example 45 | #this requires the alpn-boot library on the JVM's boot classpath 46 | #- type: h2 47 | # port: 8445 48 | # keyStorePath: example.keystore 49 | # keyStorePassword: example 50 | adminConnectors: 51 | - type: http 52 | port: 8081 53 | - type: https 54 | port: 8444 55 | keyStorePath: example.keystore 56 | keyStorePassword: example 57 | 58 | # Logging settings. 59 | logging: 60 | appenders: 61 | - type: console 62 | logFormat: "%-6level [%d{HH:mm:ss.SSS}] 63 | [%t] %logger{5} - %X{code} %msg 64 | trace_id=%X{trace_id} span_id=%X{span_id} 65 | trace_flags=%X{trace_flags}%n" 66 | 67 | # the key needs to match the configuration key of the renderer (ViewRenderer::getConfigurationKey) 68 | viewRendererConfiguration: 69 | freemarker: 70 | strict_syntax: yes 71 | whitespace_stripping: yes 72 | 73 | metrics: 74 | reporters: 75 | - type: graphite 76 | host: localhost 77 | port: 2003 78 | prefix: example 79 | frequency: 1m 80 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: "3" 2 | 3 | services: 4 | jaeger: 5 | image: jaegertracing/all-in-one:1.37.0 6 | ports: 7 | - "16686:16686" 8 | - "14250" 9 | 10 | prometheus: 11 | image: prom/prometheus:v2.38.0 12 | command: 13 | - --web.enable-remote-write-receiver 14 | - --enable-feature=exemplar-storage # Required for Chapter 7 - Exemplars 15 | volumes: 16 | - /dev/null:/prometheus/prometheus.yml 17 | ports: 18 | - "9090:9090" 19 | 20 | otel-collector: 21 | image: otel/opentelemetry-collector-contrib:0.64.0 22 | command: 23 | - --config=/etc/otel-collector.yml 24 | volumes: 25 | - ./otel-collector.yml:/etc/otel-collector.yml 26 | ports: 27 | - "4317:4317" 28 | depends_on: 29 | - jaeger 30 | - prometheus 31 | -------------------------------------------------------------------------------- /otel-collector.yml: -------------------------------------------------------------------------------- 1 | receivers: 2 | otlp: 3 | protocols: 4 | grpc: 5 | 6 | processors: 7 | batch: 8 | # Unsets error codes for cache client 404s marked by cache-client-extension. 9 | # This allows collectors to be independent of the decision of a service owner to mark a client span as a type of 10 | # cache-like client or not. 11 | span/unset_cache_client_404: 12 | include: 13 | attributes: 14 | - Key: http.status_code 15 | Value: 404 16 | - Key: http.is_cache_client 17 | Value: true 18 | match_type: strict 19 | status: 20 | code: "Unset" 21 | 22 | exporters: 23 | jaeger: 24 | endpoint: jaeger:14250 25 | tls: 26 | insecure: true 27 | prometheusremotewrite: 28 | endpoint: http://prometheus:9090/api/v1/write 29 | logging: 30 | verbosity: detailed 31 | 32 | service: 33 | pipelines: 34 | traces: 35 | receivers: [otlp] 36 | processors: [batch, span/unset_cache_client_404] 37 | exporters: [jaeger] 38 | metrics: 39 | receivers: [otlp] 40 | processors: [batch] 41 | exporters: [prometheusremotewrite] 42 | logs: 43 | receivers: [otlp] 44 | processors: [batch] 45 | exporters: [logging] 46 | --------------------------------------------------------------------------------