|
@@ -2,11 +2,11 @@
|
|
|
<!-- ReadMe is in progress -->
|
|
|
<!-- TO DO: Add sections for starter guide -->
|
|
|
|
|
|
-This package streamlines the manual instrumentation process of OpenTelemetry for [Spring](https://spring.io/projects/spring-framework) and [Spring Boot](https://spring.io/projects/spring-boot) applications. It will enable you to add traces to requests and database calls with minimal changes to application code. This package will not fully automate your OpenTelemetry instrumentation, instead, it will provide you with better tools to instrument your own code.
|
|
|
+This package streamlines the manual instrumentation process of OpenTelemetry for [Spring](https://spring.io/projects/spring-framework) and [Spring Boot](https://spring.io/projects/spring-boot) applications. It will enable you to add traces to requests and database calls with minimal changes to application code. This package will not fully automate your OpenTelemetry instrumentation, instead, it will provide you with better tools to instrument your own code.
|
|
|
|
|
|
-The [first section](#manual-instrumentation-with-java-sdk) will walk you through span creation and propagation using the OpenTelemetry Java API and [Spring's RestTemplate Http Web Client](https://spring.io/guides/gs/consuming-rest/). This approach will use the "vanilla" OpenTelemetry API to make explicit tracing calls within an application's controller.
|
|
|
+The [first section](#manual-instrumentation-with-java-sdk) will walk you through span creation and propagation using the OpenTelemetry Java API and [Spring's RestTemplate Http Web Client](https://spring.io/guides/gs/consuming-rest/). This approach will use the "vanilla" OpenTelemetry API to make explicit tracing calls within an application's controller.
|
|
|
|
|
|
-The [second section](#manual-instrumentation-using-handlers-and-filters) will build on the first. It will walk you through implementing spring-web handler and filter interfaces to create traces with minimal changes to existing application code. Using the OpenTelemetry API, this approach involves copy and pasting files and a significant amount of manual configurations.
|
|
|
+The [second section](#manual-instrumentation-using-handlers-and-filters) will build on the first. It will walk you through implementing spring-web handler and filter interfaces to create traces with minimal changes to existing application code. Using the OpenTelemetry API, this approach involves copy and pasting files and a significant amount of manual configurations.
|
|
|
|
|
|
The third section will walk you through the annotations and configurations defined in the opentelemetry-instrumentation-spring package. This section will equip you with new tools to streamline the setup and instrumentation of OpenTelemetry on Spring and Spring Boot applications. With these tools you will be able to setup distributed tracing with little to no changes to existing configurations and easily customize traces with minor additions to application code.
|
|
|
|
|
@@ -16,14 +16,14 @@ In this guide we will be using a running example. In section one and two, we wil
|
|
|
|
|
|
## Create two Spring Projects
|
|
|
|
|
|
-Using the [spring project initializer](https://start.spring.io/), we will create two spring projects. Name one project `MainService` and the other `TimeService`. In this example `MainService` will be a client of `TimeService` and they will be dealing with time. Make sure to select maven, Spring Boot 2.3, Java, and add the spring-web dependency. After downloading the two projects include the OpenTelemetry dependencies and configuration listed below.
|
|
|
+Using the [spring project initializer](https://start.spring.io/), we will create two spring projects. Name one project `MainService` and the other `TimeService`. In this example `MainService` will be a client of `TimeService` and they will be dealing with time. Make sure to select maven, Spring Boot 2.3, Java, and add the spring-web dependency. After downloading the two projects include the OpenTelemetry dependencies and configuration listed below.
|
|
|
|
|
|
## Setup for Manual Instrumentation
|
|
|
|
|
|
-Add the dependencies below to enable OpenTelemetry in `MainService` and `TimeService`. The Jaeger and LoggingExporter packages are recommended for exporting traces but are not required. As of May 2020, Jaeger, Zipkin, OTLP, and Logging exporters are supported by opentelemetry-java. Feel free to use whatever exporter you are most comfortable with.
|
|
|
+Add the dependencies below to enable OpenTelemetry in `MainService` and `TimeService`. The Jaeger and LoggingExporter packages are recommended for exporting traces but are not required. As of May 2020, Jaeger, Zipkin, OTLP, and Logging exporters are supported by opentelemetry-java. Feel free to use whatever exporter you are most comfortable with.
|
|
|
|
|
|
### Maven
|
|
|
-
|
|
|
+
|
|
|
#### OpenTelemetry
|
|
|
```xml
|
|
|
<dependency>
|
|
@@ -35,7 +35,7 @@ Add the dependencies below to enable OpenTelemetry in `MainService` and `TimeSer
|
|
|
<groupId>io.opentelemetry</groupId>
|
|
|
<artifactId>opentelemetry-sdk</artifactId>
|
|
|
<version>0.5.0</version>
|
|
|
-</dependency>
|
|
|
+</dependency>
|
|
|
<dependency>
|
|
|
<groupId>io.grpc</groupId>
|
|
|
<artifactId>grpc-context</artifactId>
|
|
@@ -73,7 +73,7 @@ Add the dependencies below to enable OpenTelemetry in `MainService` and `TimeSer
|
|
|
```
|
|
|
|
|
|
### Gradle
|
|
|
-
|
|
|
+
|
|
|
#### OpenTelemetry
|
|
|
```gradle
|
|
|
compile "io.opentelemetry:opentelemetry-api:0.5.0"
|
|
@@ -97,7 +97,7 @@ compile "io.grpc:grpc-netty:1.27.2"
|
|
|
|
|
|
To enable tracing in your OpenTelemetry project configure a Tracer Bean. This bean will be auto wired to controllers to create and propagate spans. This can be seen in the `Tracer otelTracer()` method below. If you plan to use a trace exporter remember to also include it in this configuration class. In section 3 we will use an annotation to set up this configuration.
|
|
|
|
|
|
-A sample OpenTelemetry configuration using LoggingExporter is shown below:
|
|
|
+A sample OpenTelemetry configuration using LoggingExporter is shown below:
|
|
|
|
|
|
```java
|
|
|
import org.springframework.context.annotation.Bean;
|
|
@@ -114,21 +114,21 @@ import io.opentelemetry.exporters.logging.*;
|
|
|
|
|
|
@Configuration
|
|
|
public class OtelConfig {
|
|
|
- private static final tracerName = "fooTracer";
|
|
|
+ private static final tracerName = "fooTracer";
|
|
|
@Bean
|
|
|
public Tracer otelTracer() throws Exception {
|
|
|
final Tracer tracer = OpenTelemetry.getTracer(tracerName);
|
|
|
|
|
|
SpanProcessor logProcessor = SimpleSpanProcessor.newBuilder(new LoggingSpanExporter()).build();
|
|
|
OpenTelemetrySdk.getTracerProvider().addSpanProcessor(logProcessor);
|
|
|
-
|
|
|
+
|
|
|
return tracer;
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
|
|
|
|
|
|
-The file above configures an OpenTelemetry tracer and a span processor. The span processor builds a log exporter which will output spans to the console. Similarly, one could add another exporter, such as the `JaegerExporter`, to visualize traces on a different back-end. Similar to how the `LoggingExporter` is configured, a Jaeger configuration can be added to the `OtelConfig` class above.
|
|
|
+The file above configures an OpenTelemetry tracer and a span processor. The span processor builds a log exporter which will output spans to the console. Similarly, one could add another exporter, such as the `JaegerExporter`, to visualize traces on a different back-end. Similar to how the `LoggingExporter` is configured, a Jaeger configuration can be added to the `OtelConfig` class above.
|
|
|
|
|
|
Sample configuration for a Jaeger Exporter:
|
|
|
|
|
@@ -141,11 +141,11 @@ SpanProcessor jaegerProcessor = SimpleSpanProcessor
|
|
|
.build();
|
|
|
OpenTelemetrySdk.getTracerProvider().addSpanProcessor(jaegerProcessor);
|
|
|
```
|
|
|
-
|
|
|
+
|
|
|
### Project Background
|
|
|
|
|
|
Here we will create REST controllers for `MainService` and `TimeService`.
|
|
|
-`MainService` will send a GET request to `TimeService` to retrieve the current time. After this request is resolved, `MainService` then will append a message to time and return a string to the client.
|
|
|
+`MainService` will send a GET request to `TimeService` to retrieve the current time. After this request is resolved, `MainService` then will append a message to time and return a string to the client.
|
|
|
|
|
|
## Manual Instrumentation with Java SDK
|
|
|
|
|
@@ -190,7 +190,7 @@ import HttpUtils;
|
|
|
public class MainServiceController {
|
|
|
private static int requestCount = 1;
|
|
|
private static final String TIME_SERVICE_URL = "http://localhost:8081/time";
|
|
|
-
|
|
|
+
|
|
|
@Autowired
|
|
|
private Tracer tracer;
|
|
|
|
|
@@ -246,7 +246,7 @@ public class HttpUtils {
|
|
|
headers.set(key, value);
|
|
|
}
|
|
|
};
|
|
|
-
|
|
|
+
|
|
|
@Autowired
|
|
|
private Tracer tracer;
|
|
|
|
|
@@ -314,7 +314,7 @@ public class TimeServiceController {
|
|
|
@GetMapping
|
|
|
public String time() {
|
|
|
Span span = tracer.spanBuilder("time").startSpan();
|
|
|
-
|
|
|
+
|
|
|
try (Scope scope = tracer.withSpan(span)) {
|
|
|
span.addEvent("TimeServiceController Entered");
|
|
|
span.setAttribute("what.am.i", "Tu es une legume");
|
|
@@ -328,17 +328,17 @@ public class TimeServiceController {
|
|
|
|
|
|
### Run MainService and TimeService
|
|
|
|
|
|
-***To view your distributed traces ensure either LogExporter or Jaeger is configured in the OtelConfig.java file***
|
|
|
+***To view your distributed traces ensure either LogExporter or Jaeger is configured in the OtelConfig.java file***
|
|
|
|
|
|
To view traces on the Jaeger UI, deploy a Jaeger Exporter on localhost by running the command in terminal:
|
|
|
|
|
|
-`docker run --rm -it --network=host jaegertracing/all-in-one`
|
|
|
+`docker run --rm -it --network=host jaegertracing/all-in-one`
|
|
|
|
|
|
After running Jaeger locally, navigate to the url below. Make sure to refresh the UI to view the exported traces from the two web services:
|
|
|
|
|
|
`http://localhost:16686`
|
|
|
|
|
|
-Run MainService and TimeService from command line or using an IDE. The end point of interest for MainService is `http://localhost:8080/message` and `http://localhost:8081/time` for TimeService. Entering `localhost:8080/message` in a browser should call MainService and then TimeService, creating a trace.
|
|
|
+Run MainService and TimeService from command line or using an IDE. The end point of interest for MainService is `http://localhost:8080/message` and `http://localhost:8081/time` for TimeService. Entering `localhost:8080/message` in a browser should call MainService and then TimeService, creating a trace.
|
|
|
|
|
|
***Note: The default port for the Apache Tomcat is 8080. On localhost both MainService and TimeService services will attempt to run on this port raising an error. To avoid this add `server.port=8081` to the resources/application.properties file. Ensure the port specified corresponds to port referenced by MainServiceController.TIME_SERVICE_URL. ***
|
|
|
|
|
@@ -346,13 +346,13 @@ Congrats, we just created a distributed service with OpenTelemetry!
|
|
|
|
|
|
## Manual Instrumentation using Handlers and Filters
|
|
|
|
|
|
-In this section, we will implement the javax Servlet Filter interface to wrap all requests to MainService and TimeService controllers in a span.
|
|
|
+In this section, we will implement the javax Servlet Filter interface to wrap all requests to MainService and TimeService controllers in a span.
|
|
|
|
|
|
-We will also use the RestTemplate HTTP client to send requests from MainService to TimeService. To propagate the trace in this request we will also implement the ClientHttpRequestInterceptor interface. This implementation is only required for projects that send outbound requests. In this example it is only required for MainService.
|
|
|
+We will also use the RestTemplate HTTP client to send requests from MainService to TimeService. To propagate the trace in this request we will also implement the ClientHttpRequestInterceptor interface. This implementation is only required for projects that send outbound requests. In this example it is only required for MainService.
|
|
|
|
|
|
### Set up MainService and TimeService
|
|
|
|
|
|
-Using the earlier instructions [create two spring projects](#create-two-spring-projects) and add the required [dependencies and configurations](#setup-for-manual-instrumentation).
|
|
|
+Using the earlier instructions [create two spring projects](#create-two-spring-projects) and add the required [dependencies and configurations](#setup-for-manual-instrumentation).
|
|
|
|
|
|
### Instrumentation of TimeService
|
|
|
|
|
@@ -396,16 +396,16 @@ public class TimeServiceController {
|
|
|
|
|
|
#### Create Controller Filter
|
|
|
|
|
|
-Add the class below to wrap all requests to the TimeServiceController in a span. This class will call the preHandle method before the REST controller is entered and the postHandle method after a response is created.
|
|
|
+Add the class below to wrap all requests to the TimeServiceController in a span. This class will call the preHandle method before the REST controller is entered and the postHandle method after a response is created.
|
|
|
|
|
|
-The preHandle method starts a span for each request. This implementation is shown below:
|
|
|
+The preHandle method starts a span for each request. This implementation is shown below:
|
|
|
|
|
|
```java
|
|
|
|
|
|
@Component
|
|
|
public class ControllerFilter implements Filter {
|
|
|
private static final Logger LOG = Logger.getLogger(ControllerFilter.class.getName());
|
|
|
-
|
|
|
+
|
|
|
@Autowired
|
|
|
Tracer tracer;
|
|
|
|
|
@@ -420,7 +420,7 @@ public class ControllerFilter implements Filter {
|
|
|
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
|
|
|
throws IOException, ServletException {
|
|
|
LOG.info("start doFilter");
|
|
|
-
|
|
|
+
|
|
|
HttpServletRequest req = (HttpServletRequest) request;
|
|
|
Span currentSpan;
|
|
|
try (Scope scope = tracer.withSpan(currentSpan)) {
|
|
@@ -432,10 +432,10 @@ public class ControllerFilter implements Filter {
|
|
|
} finally {
|
|
|
currentSpan.end();
|
|
|
}
|
|
|
-
|
|
|
- LOG.info("end doFilter");
|
|
|
+
|
|
|
+ LOG.info("end doFilter");
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
private Span createSpanWithParent(HttpServletRequest request, Context context) {
|
|
|
return tracer.spanBuilder(request.getRequestURI()).setSpanKind(Span.Kind.SERVER).startSpan();
|
|
|
}
|
|
@@ -478,7 +478,7 @@ public class MainServiceController {
|
|
|
|
|
|
@Autowired
|
|
|
private Tracer tracer;
|
|
|
-
|
|
|
+
|
|
|
@Autowired
|
|
|
private RestTemplate restTemplate;
|
|
|
|
|
@@ -504,7 +504,7 @@ As seen in the setup of TimeService, implement the javax servlet filter interfac
|
|
|
|
|
|
Next, we will configure the ClientHttpRequestInterceptor to intercept all client HTTP requests made using RestTemplate.
|
|
|
|
|
|
-To propagate the span context from MainService to TimeService we must inject the trace parent and trace state into the outgoing request header. In section 1 this was done using the helper class HttpUtils. In this section, we will implement the ClientHttpRequestInterceptor interface and register this interceptor in our application.
|
|
|
+To propagate the span context from MainService to TimeService we must inject the trace parent and trace state into the outgoing request header. In section 1 this was done using the helper class HttpUtils. In this section, we will implement the ClientHttpRequestInterceptor interface and register this interceptor in our application.
|
|
|
|
|
|
Include the two classes below to your MainService project to add this functionality:
|
|
|
|
|
@@ -544,10 +544,10 @@ public class RestTemplateInterceptor implements ClientHttpRequestInterceptor {
|
|
|
@Override
|
|
|
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
|
|
|
ClientHttpRequestExecution execution) throws IOException {
|
|
|
-
|
|
|
+
|
|
|
String spanName = request.getMethodValue() + " " + request.getURI().toString();
|
|
|
Span currentSpan = tracer.spanBuilder(spanName).setSpanKind(Span.Kind.CLIENT).startSpan();
|
|
|
-
|
|
|
+
|
|
|
try (Scope scope = tracer.withSpan(currentSpan)) {
|
|
|
OpenTelemetry.getPropagators().getHttpTextFormat().inject(Context.current(), request, setter);
|
|
|
ClientHttpResponse response = execution.execute(request, body);
|
|
@@ -589,12 +589,12 @@ public class RestClientConfig {
|
|
|
}
|
|
|
```
|
|
|
|
|
|
-### Create a distributed trace
|
|
|
+### Create a distributed trace
|
|
|
|
|
|
-By default Spring Boot runs a Tomcat server on port 8080. This tutorial assumes MainService runs on the default port (8080) and TimeService runs on port 8081. This is because we hard coded the TimeService end point in MainServiceController.TIME_SERVICE_URL. To run TimeServiceApplication on port 8081 include `server.port=8081` in the resources/application.properties file.
|
|
|
+By default Spring Boot runs a Tomcat server on port 8080. This tutorial assumes MainService runs on the default port (8080) and TimeService runs on port 8081. This is because we hard coded the TimeService end point in MainServiceController.TIME_SERVICE_URL. To run TimeServiceApplication on port 8081 include `server.port=8081` in the resources/application.properties file.
|
|
|
|
|
|
-Run both the MainService and TimeService projects in terminal or using an IDE (ex. Eclipse). The end point for MainService should be `http://localhost:8080/message` and `http://localhost:8081/time` for TimeService. Type both urls in a browser and ensure you receive a 200 response.
|
|
|
+Run both the MainService and TimeService projects in terminal or using an IDE (ex. Eclipse). The end point for MainService should be `http://localhost:8080/message` and `http://localhost:8081/time` for TimeService. Type both urls in a browser and ensure you receive a 200 response.
|
|
|
|
|
|
-To visualize this trace add a trace exporter to one or both of your applications. Instructions on how to setup LogExporter and Jaeger can be seen [above](#tracer-configuration).
|
|
|
+To visualize this trace add a trace exporter to one or both of your applications. Instructions on how to setup LogExporter and Jaeger can be seen [above](#tracer-configuration).
|
|
|
|
|
|
To create a sample trace enter `localhost:8080/message` in a browser. This trace should include a span for MainService and a span for TimeService.
|