Track service requests through multiple hops using a UUID.
This project uses servlet filters, Jersey client filters, and Logback's MDC to keep track of an ID through multiple hops. The servlet and client filters will intercept a request and automatically generate an ID if none exists. The client filter adds the ID as a request header. The servlet filter intercepts the ID.
There's a Dropwizard bundle that will add in the servlet filter for you. Jersey client filters have to be manually added to your client classes.
Add the following dependency into your pom.xml
<dependency>
<groupId>com.serviceenabled</groupId>
<artifactId>dropwizard-request-tracker</artifactId>
<version>0.2.0</version>
</dependency>
Add RequestTrackerConfiguration
into your application's configuration class.
public class ExampleConfiguration extends Configuration {
private RequestTrackerConfiguration requestTrackerConfiguration = new RequestTrackerConfiguration();
public RequestTrackerConfiguration getRequestTrackerConfiguration() {
return requestTrackerConfiguration;
}
public void setRequestTrackerConfiguration(RequestTrackerConfiguration configuration) {
this.requestTrackerConfiguration = configuration;
}
}
Add the RequestTrackerBundle
to your application
bootstrap.addBundle(new RequestTrackerBundle<ExampleConfiguration>() {
@Override
public RequestTrackerConfiguration getRequestTrackerConfiguration(BundleConfiguration configuration) {
return configuration.getRequestTrackerConfiguration();
}
});
Add the client filter to your client classes
client.addFilter(new RequestTrackerClientFilter(configuration.getRequestTrackerConfiguration()));
If you'd like the ID to be output in your logs, add %X{Request-Tracker}
to your logFormat.
and mvn clean install
The RequestTrackerConfiguration
sets the HTTP header name to X-Request-Tracker
and the MDC key to Request-Tracker
by default. These can be overridden in your YAML configuration.
By default UuidSupplier
is used by the bundle and filters. The provided bundle and filters provide constructors for you to pass in your own custom ID supplier. Your custom ID supplier must implement Guava's Supplier<String>
. Here's an example ID supplier:
import com.google.common.base.Supplier;
public class CustomIdSupplier implements Supplier<String> {
@Override
public String get() {
return "12345";
}
}