├── .gitignore ├── README.md ├── build.gradle ├── docker ├── Dockerfile ├── app │ └── JaegerTutorial │ │ └── bin │ │ └── start.sh └── build_save.sh ├── gradlew ├── gradlew.bat ├── local └── start.sh ├── settings.gradle └── src └── main └── java └── com └── egoebelbecker └── jaegertutorial ├── SwaggerConfig.java ├── TutorialApplication.java ├── controller └── TutorialController.java ├── model └── Employee.java └── service └── EmployeeService.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### Example user template template 3 | ### Example user template 4 | 5 | # IntelliJ project files 6 | .idea 7 | *.iml 8 | out 9 | gen 10 | .gradle/ 11 | build 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Jaeger Tutorial 2 | 3 | Quick and dirty Spring Boot project with some basic Jaeger tracing enabled. 4 | 5 | 6 | Use gradle build copyLocal to run from local subdirectory. 7 | 8 | Use gradele build copyDeps to run in container. Build image in docker subdirectory. 9 | 10 | 11 | ## Starting Jaeger 12 | 13 | ``` 14 | docker run -d --name jaeger \ 15 | -e COLLECTOR_ZIPKIN_HTTP_PORT=9411 \ 16 | -p 5775:5775/udp \ 17 | -p 6831:6831/udp \ 18 | -p 6832:6832/udp \ 19 | -p 5778:5778 \ 20 | -p 16686:16686 \ 21 | -p 14268:14268 \ 22 | -p 14250:14250 \ 23 | -p 9411:9411 \ 24 | jaegertracing/all-in-one:1.18 25 | ``` 26 | 27 | ## Run with Docker on Mac 28 | 29 | *Prerequisites*: 30 | - Starting Jaeger (above) 31 | 32 | ``` 33 | $ gradle wrapper 34 | $ gradle build copyDeps 35 | $ docker build -t springboot_jaeger_tutorial . 36 | $ docker run --rm \ 37 | --name springboot_jaeger_tutorial \ 38 | -p 8080:8080 \ 39 | -e JAEGER_AGENT_HOST="host.docker.internal" \ 40 | -e JAEGER_AGENT_PORT=6831 \ 41 | springboot_jaeger_tutorial 42 | ``` 43 | 44 | Navigate to: http://localhost:8080/swagger-ui.html#!/tutorial-controller/getAllEmployeesUsingGET 45 | 46 | Click on "Try it out!" 47 | 48 | Navigate to: http://localhost:16686 49 | 50 | Select service: "jaeger tutorial" 51 | 52 | Click on "Find Traces" 53 | 54 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | group 'com.egoebelbecker.jaegertutorial' 2 | version '1.0-SNAPSHOT' 3 | 4 | buildscript { 5 | repositories { 6 | mavenCentral() 7 | } 8 | dependencies { 9 | classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.9.RELEASE") 10 | } 11 | } 12 | 13 | apply plugin: 'java' 14 | apply plugin: 'idea' 15 | apply plugin: 'org.springframework.boot' 16 | 17 | repositories { 18 | mavenCentral() 19 | } 20 | 21 | sourceCompatibility = 1.8 22 | targetCompatibility = 1.8 23 | 24 | 25 | dependencies { 26 | compile("org.springframework.boot:spring-boot-starter-web") 27 | annotationProcessor "org.projectlombok:lombok:1.18.4" 28 | compile "org.projectlombok:lombok:1.18.4" 29 | compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.6.1' 30 | compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.6.1' 31 | compile group: 'io.jaegertracing', name: 'jaeger-client', version:'0.33.1' 32 | testCompile('org.springframework.boot:spring-boot-starter-test') 33 | } 34 | jar { 35 | exclude "*.properties" 36 | exclude "*.xml" 37 | } 38 | 39 | task copyDeps( type: Copy) { 40 | from(configurations.runtime) { 41 | include "*.jar" 42 | exclude { details -> details.file.name.startsWith('maven') } 43 | } 44 | from('build/libs') { 45 | include "*.original" 46 | } 47 | into rootProject.rootDir.getAbsolutePath() + "/docker/app/JaegerTutorial/libs" 48 | rename{ 49 | String fileName -> fileName.replace('.original', '') 50 | } 51 | } 52 | 53 | task copyLocal( type: Copy) { 54 | from(configurations.runtime) { 55 | include "*.jar" 56 | exclude { details -> details.file.name.startsWith('maven') } 57 | } 58 | from('build/libs') { 59 | include "*.original" 60 | } 61 | into rootProject.rootDir.getAbsolutePath() + "/local/libs" 62 | rename{ 63 | String fileName -> fileName.replace('.original', '') 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /docker/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM anapsix/alpine-java:8_server-jre 2 | MAINTAINER Eric Goebelbecker "eric@ericgoebelbecker.com" 3 | ENV JAVA_VERSION 8u31 4 | ENV PACKAGE_ROOT /app 5 | ADD app /app/ 6 | ENTRYPOINT ["/app/JaegerTutorial/bin/start.sh"] 7 | CMD [] -------------------------------------------------------------------------------- /docker/app/JaegerTutorial/bin/start.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | 3 | 4 | export LIBS=/app/JaegerTutorial/libs 5 | export APP=com.egoebelbecker.jaegertutorial.TutorialApplication 6 | export LOG_DIR=${APP_HOME}/logs 7 | export CLASSPATH=${CLASSPATH}:${LIBS}/* 8 | ${JAVA_HOME}/bin/java -enableassertions -cp ${CLASSPATH} ${LOGBACKCFG} ${APP} 9 | 10 | -------------------------------------------------------------------------------- /docker/build_save.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | usage() 4 | { 5 | echo "Usage: $0 release" 6 | exit 1 7 | } 8 | 9 | if [ $# -ne 1 ] ; then 10 | usage 11 | else 12 | export release=$1 13 | fi 14 | 15 | echo "Build and Push jaegertutorial:"${release}" to Docker repo" 16 | 17 | docker build -t jaegertutorial:${release} . 18 | docker tag jaegertutorial:${release} ericgoebelbecker/jaegertutorial:${release} 19 | docker push ericgoebelbecker/jaegertutorial:${release} 20 | 21 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /local/start.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | 3 | 4 | export LIBS=./libs 5 | export APP=com.egoebelbecker.jaegertutorial.TutorialApplication 6 | export LOG_DIR=${APP_HOME}/logs 7 | export CLASSPATH=${CLASSPATH}:${LIBS}/* 8 | java -enableassertions -cp ${CLASSPATH} ${LOGBACKCFG} ${APP} 9 | 10 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'JaegerTutorial' 2 | 3 | -------------------------------------------------------------------------------- /src/main/java/com/egoebelbecker/jaegertutorial/SwaggerConfig.java: -------------------------------------------------------------------------------- 1 | package com.egoebelbecker.jaegertutorial; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; 7 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; 8 | import springfox.documentation.builders.PathSelectors; 9 | import springfox.documentation.builders.RequestHandlerSelectors; 10 | import springfox.documentation.service.ApiInfo; 11 | import springfox.documentation.service.Contact; 12 | import springfox.documentation.spi.DocumentationType; 13 | import springfox.documentation.spring.web.plugins.Docket; 14 | import springfox.documentation.swagger2.annotations.EnableSwagger2; 15 | 16 | 17 | 18 | @Slf4j 19 | @Configuration 20 | @EnableSwagger2 21 | public class SwaggerConfig extends WebMvcConfigurationSupport { 22 | 23 | @Bean 24 | public Docket api() { 25 | return new Docket(DocumentationType.SWAGGER_2) 26 | .select() 27 | .apis(RequestHandlerSelectors.basePackage("com.egoebelbecker.jaegertutorial")) 28 | .paths(PathSelectors.any()) 29 | .build() 30 | .apiInfo(apiInfo()); 31 | } 32 | 33 | private ApiInfo apiInfo() { 34 | return new ApiInfo( 35 | "Jaeger Tutorial", 36 | "API for demonstrating REST traced with Jaeger.", 37 | "1.0", 38 | "", 39 | new Contact("Eric Goebelbecker", "http://www.ericgoebelbecker.com", "eric@ericgoebelbecker.com"), 40 | "", ""); 41 | } 42 | 43 | 44 | @Override 45 | public void addResourceHandlers(ResourceHandlerRegistry registry) { 46 | registry.addResourceHandler("swagger-ui.html") 47 | .addResourceLocations("classpath:/META-INF/resources/"); 48 | 49 | registry.addResourceHandler("/webjars/**") 50 | .addResourceLocations("classpath:/META-INF/resources/webjars/"); 51 | } 52 | 53 | 54 | } 55 | 56 | -------------------------------------------------------------------------------- /src/main/java/com/egoebelbecker/jaegertutorial/TutorialApplication.java: -------------------------------------------------------------------------------- 1 | package com.egoebelbecker.jaegertutorial; 2 | 3 | import com.egoebelbecker.jaegertutorial.controller.TutorialController; 4 | import io.jaegertracing.Configuration; 5 | import io.jaegertracing.internal.JaegerTracer; 6 | import lombok.extern.slf4j.Slf4j; 7 | import org.springframework.boot.SpringApplication; 8 | import org.springframework.boot.autoconfigure.SpringBootApplication; 9 | import org.springframework.context.ApplicationContext; 10 | import org.springframework.context.annotation.Bean; 11 | 12 | @Slf4j 13 | @SpringBootApplication 14 | public class TutorialApplication { 15 | 16 | public static void main(String[] args) { 17 | 18 | ApplicationContext context = SpringApplication.run(TutorialApplication.class, args); 19 | log.info("TutorialApplication started"); 20 | 21 | TutorialController commandController = context.getBean(TutorialController.class); 22 | commandController.init(); 23 | } 24 | 25 | @Bean 26 | public static JaegerTracer getTracer() { 27 | Configuration.SamplerConfiguration samplerConfig = Configuration.SamplerConfiguration.fromEnv().withType("const").withParam(1); 28 | Configuration.ReporterConfiguration reporterConfig = Configuration.ReporterConfiguration.fromEnv().withLogSpans(true); 29 | Configuration config = new Configuration("jaeger tutorial").withSampler(samplerConfig).withReporter(reporterConfig); 30 | return config.getTracer(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/egoebelbecker/jaegertutorial/controller/TutorialController.java: -------------------------------------------------------------------------------- 1 | package com.egoebelbecker.jaegertutorial.controller; 2 | 3 | import com.egoebelbecker.jaegertutorial.model.Employee; 4 | import com.egoebelbecker.jaegertutorial.service.EmployeeService; 5 | import com.google.common.collect.ImmutableMap; 6 | import io.opentracing.Span; 7 | import io.opentracing.Tracer; 8 | import io.swagger.annotations.Api; 9 | import io.swagger.annotations.ApiOperation; 10 | import lombok.extern.slf4j.Slf4j; 11 | import org.springframework.http.HttpStatus; 12 | import org.springframework.http.ResponseEntity; 13 | import org.springframework.web.bind.annotation.*; 14 | 15 | import java.util.Collection; 16 | import java.util.NoSuchElementException; 17 | import java.util.UUID; 18 | 19 | @Slf4j 20 | @RestController 21 | @Api(value = "jaegertutorial", description = "Jaeger Tutorial service") 22 | public class TutorialController { 23 | 24 | private EmployeeService employeeService; 25 | private Tracer tracer; 26 | 27 | public TutorialController(Tracer tracer, EmployeeService employeeService) { 28 | this.tracer = tracer; 29 | this.employeeService = employeeService; 30 | } 31 | 32 | 33 | public void init() { 34 | 35 | // Add some employees 36 | employeeService.addEmployee(Employee.builder() 37 | .employeeId(1) 38 | .email("john@doe.com") 39 | .firstName("John") 40 | .lastName("Doe") 41 | .phone("555-1212") 42 | .build()); 43 | 44 | employeeService.addEmployee(Employee.builder() 45 | .employeeId(2) 46 | .email("jenny@doe.com") 47 | .firstName("Jenny") 48 | .lastName("Doe") 49 | .phone("867-5309") 50 | .build()); 51 | 52 | employeeService.addEmployee(Employee.builder() 53 | .employeeId(3) 54 | .email("clark@doe.com") 55 | .firstName("Clark") 56 | .lastName("Kent") 57 | .phone("555-1213") 58 | .build()); 59 | 60 | } 61 | 62 | 63 | @ApiOperation(value = "Create Employee ", response = ResponseEntity.class) 64 | @RequestMapping(value = "/api/tutorial/1.0/employees", method = RequestMethod.POST) 65 | public ResponseEntity createEmployee(@RequestBody Employee employee) { 66 | 67 | Span span = tracer.buildSpan("create employee").start(); 68 | 69 | HttpStatus status = HttpStatus.FORBIDDEN; 70 | 71 | log.info("Receive Request to add employee {}", employee); 72 | if (employeeService.addEmployee(employee)) { 73 | status = HttpStatus.CREATED; 74 | span.setTag("http.status_code", 201); 75 | } else { 76 | span.setTag("http.status_code", 403); 77 | } 78 | span.finish(); 79 | return new ResponseEntity(null, status); 80 | } 81 | 82 | 83 | @ApiOperation(value = "Get Employee ", response = ResponseEntity.class) 84 | @RequestMapping(value = "/api/tutorial/1.0/employees/{id}", method = RequestMethod.GET) 85 | public ResponseEntity getEmployee(@PathVariable("id") String idString) { 86 | 87 | Employee employee = null; 88 | HttpStatus status = HttpStatus.NOT_FOUND; 89 | 90 | Span span = tracer.buildSpan("get employee").start(); 91 | 92 | try { 93 | int id = Integer.parseInt(idString); 94 | log.info("Received Request for employee {}", id); 95 | employee = employeeService.getEmployee(id) 96 | .orElseThrow(() -> new NoSuchElementException("Employee not found.")); 97 | 98 | status = HttpStatus.OK; 99 | 100 | } catch (NumberFormatException | NoSuchElementException nfe) { 101 | log.error("Error getting employee: ", nfe); 102 | } 103 | span.finish(); 104 | return new ResponseEntity<>(employee, status ); 105 | } 106 | 107 | 108 | @ApiOperation(value = "Get All Employees ", response = ResponseEntity.class) 109 | @RequestMapping(value = "/api/tutorial/1.0/employees", method = RequestMethod.GET) 110 | public ResponseEntity getAllEmployees() { 111 | 112 | Span span = tracer.buildSpan("get employees").start(); 113 | 114 | log.info("Receive Request to Get All Employees"); 115 | Collection employees = employeeService.loadAllEmployees(); 116 | 117 | span.finish(); 118 | return new ResponseEntity<>(employees, HttpStatus.OK); 119 | } 120 | 121 | 122 | @ApiOperation(value = "Update Employee ", response = ResponseEntity.class) 123 | @RequestMapping(value = "/api/tutorial/1.0/employees/{id}", method = RequestMethod.PUT) 124 | public ResponseEntity updateEmployee(@PathVariable("id") String idString, @RequestBody Employee employee) { 125 | 126 | Span span = tracer.buildSpan("update employee").start(); 127 | 128 | HttpStatus status = HttpStatus.NO_CONTENT; 129 | 130 | 131 | try { 132 | int id = Integer.parseInt(idString); 133 | log.info("Received Request to update employee {}", id); 134 | 135 | if (employeeService.updateEmployee(id, employee)) { 136 | status = HttpStatus.OK; 137 | } 138 | } catch (NumberFormatException | NoSuchElementException nfe) { 139 | // Fall through 140 | } 141 | span.finish(); 142 | return new ResponseEntity(null, status); 143 | } 144 | 145 | @ApiOperation(value = "Patch Employee ", response = ResponseEntity.class) 146 | @RequestMapping(value = "/api/tutorial/1.0/employees/{id}", method = RequestMethod.PATCH) 147 | public ResponseEntity patchEmployee(@PathVariable("id") String idString, @RequestBody Employee employee) { 148 | 149 | Span span = tracer.buildSpan("get employees").start(); 150 | 151 | HttpStatus status = HttpStatus.NO_CONTENT; 152 | 153 | try { 154 | int id = Integer.parseInt(idString); 155 | log.info("Received Request to patch employee {}", id); 156 | 157 | if (employeeService.patchEmployee(id, employee)) { 158 | return new ResponseEntity(null, HttpStatus.OK); 159 | } 160 | } catch (NumberFormatException | NoSuchElementException nfe) { 161 | // Fall through 162 | } 163 | 164 | span.finish(); 165 | return new ResponseEntity(null, status); 166 | } 167 | 168 | @ApiOperation(value = "Delete Employee ", response = ResponseEntity.class) 169 | @RequestMapping(value = "/api/tutorial/1.0/employees/{id}", method = RequestMethod.DELETE) 170 | public ResponseEntity deleteEmployee(@PathVariable("id") String idString) { 171 | 172 | Span span = tracer.buildSpan("delete employee").start(); 173 | 174 | HttpStatus status = HttpStatus.NO_CONTENT; 175 | 176 | try { 177 | int id = Integer.parseInt(idString); 178 | log.info("Received Request to delete employee {}", id); 179 | span.log(ImmutableMap.of("event", "delete-request", "value", idString)); 180 | if (employeeService.deleteEmployee(id, span)) { 181 | span.log(ImmutableMap.of("event", "delete-success", "value", idString)); 182 | span.setTag("http.status_code", 200); 183 | status = HttpStatus.OK; 184 | } else { 185 | span.log(ImmutableMap.of("event", "delete-fail", "value", "does not exist")); 186 | span.setTag("http.status_code", 204); 187 | } 188 | } catch (NumberFormatException | NoSuchElementException nfe) { 189 | span.log(ImmutableMap.of("event", "delete-fail", "value", idString)); 190 | span.setTag("http.status_code", 204); 191 | } 192 | 193 | span.finish(); 194 | return new ResponseEntity(null, status); 195 | } 196 | 197 | } 198 | -------------------------------------------------------------------------------- /src/main/java/com/egoebelbecker/jaegertutorial/model/Employee.java: -------------------------------------------------------------------------------- 1 | package com.egoebelbecker.jaegertutorial.model; 2 | 3 | import java.io.Serializable; 4 | 5 | import lombok.AllArgsConstructor; 6 | import lombok.Builder; 7 | import lombok.Data; 8 | import lombok.NoArgsConstructor; 9 | 10 | @Data 11 | @Builder 12 | @NoArgsConstructor 13 | @AllArgsConstructor 14 | public class Employee implements Serializable { 15 | private int employeeId; 16 | private String firstName; 17 | private String lastName; 18 | private String email; 19 | private String phone; 20 | 21 | 22 | 23 | 24 | 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/egoebelbecker/jaegertutorial/service/EmployeeService.java: -------------------------------------------------------------------------------- 1 | package com.egoebelbecker.jaegertutorial.service; 2 | 3 | import com.egoebelbecker.jaegertutorial.model.Employee; 4 | import io.opentracing.Span; 5 | import io.opentracing.Tracer; 6 | import org.springframework.stereotype.Service; 7 | 8 | import java.util.*; 9 | 10 | // This is incredibly optimistic code 11 | @Service 12 | public class EmployeeService { 13 | 14 | //Add jedis 15 | private Map employeeMap = new HashMap<>(); 16 | 17 | private Tracer tracer; 18 | 19 | public EmployeeService(Tracer tracer) { 20 | this.tracer = tracer; 21 | } 22 | 23 | 24 | // Create 25 | public boolean addEmployee(Employee employee) { 26 | if (employeeMap.containsKey(employee.getEmployeeId())) { 27 | return false; 28 | } else { 29 | employeeMap.put(employee.getEmployeeId(), employee); 30 | return true; 31 | } 32 | } 33 | 34 | // Read One 35 | public Optional getEmployee(int id) { 36 | return Optional.ofNullable(employeeMap.get(id)); 37 | } 38 | 39 | // Read all 40 | public Collection loadAllEmployees() { 41 | 42 | return employeeMap.values(); 43 | } 44 | 45 | // Update 46 | public boolean updateEmployee(int id, Employee employee) { 47 | if (employeeMap.containsKey(id)) { 48 | employeeMap.remove(id); 49 | employeeMap.put(employee.getEmployeeId(), employee); 50 | return true; 51 | } else { 52 | return false; 53 | } 54 | 55 | } 56 | 57 | // Update 58 | public boolean patchEmployee(int id, Employee employee) { 59 | 60 | if (employeeMap.containsKey(id)) { 61 | 62 | Employee origEmployee = employeeMap.remove(id); 63 | 64 | if (employee.getEmployeeId() != 0) { 65 | origEmployee.setEmployeeId(employee.getEmployeeId()); 66 | } 67 | 68 | if ((employee.getEmail() != null) && (!employee.getEmail().isEmpty())) { 69 | origEmployee.setEmail(employee.getEmail()); 70 | } 71 | 72 | if ((employee.getFirstName() != null) && (!employee.getFirstName().isEmpty())) { 73 | origEmployee.setFirstName(employee.getFirstName()); 74 | } 75 | 76 | if ((employee.getLastName() != null) && (!employee.getLastName().isEmpty())) { 77 | origEmployee.setLastName(employee.getLastName()); 78 | } 79 | 80 | if ((employee.getPhone() != null) && (!employee.getPhone().isEmpty())) { 81 | origEmployee.setPhone(employee.getPhone()); 82 | } 83 | 84 | employeeMap.put(origEmployee.getEmployeeId(), origEmployee); 85 | return true; 86 | } else { 87 | return false; 88 | } 89 | 90 | } 91 | 92 | // Delete 93 | public boolean deleteEmployee(int id, Span rootSpan) { 94 | 95 | Span span = tracer.buildSpan("service delete employee").asChildOf(rootSpan).start(); 96 | 97 | boolean result = false; 98 | if (employeeMap.containsKey(id)) { 99 | employeeMap.remove(id); 100 | result = true; 101 | } 102 | span.finish(); 103 | return result; 104 | } 105 | 106 | } 107 | --------------------------------------------------------------------------------