The request-tracker-spring-boot-starter is a Spring Boot Starter dependency that transparently creates a Servlet Filter in a Spring Boot Application that collect some information from request and response Servlet objects and performs a custom action asynchronously (in a new thread).
The custom action is defined by application that uses request-tracker-spring-boot-starter through the creation of a bean that implements de interface RequestTrackerActionAdapter.java.
The following data is tracked and sent to custom action as a json string:
- url: url requested composed by protocol, server name, port number and server path
- queryString: query string that is contained in the request URL after the path
- method: HTTP method with which this request was made
- status: status code of HTTP response
- remoteHost: fully qualified name of the client or the last proxy that sent the request
- remotePort: Internet Protocol (IP) source port of the client or last proxy that sent the request
- protocol: ame and version of the protocol the request uses in the form protocol/majorVersion
- timestamp: timestamp of instant that RequestTrackerFilter is executed
To use this lib in your Spring Boot Application, follow the steps below:
- Add the request-tracker-spring-boot-starter dependency in your project
If you are using Maven, add this fragment in your pom.xml file:
<dependency>
<groupId>dev.rogeriofbrito</groupId>
<artifactId>request-tracker-spring-boot-starter</artifactId>
<version>{version}</version>
</dependency>
For others build automation tools, see the proper way to add the dependency in MVN Repository site.
- Implement the interface RequestTrackerActionAdapter
public class MyRequestTrackerAction implements RequestTrackerActionAdapter {
@Override
public Thread doAction(String requestTrackerElementsJson) {
return new Thread(() -> {
// do something...
});
}
}
- Create a bean with the class that implements RequestTrackerActionAdapter
@Configuration
public class RequestTrackerConfiguration {
@Bean
public MyRequestTrackerAction myRequestTrackerAction() {
return new MyRequestTrackerAction();
}
}
@Component
public class RestTemplateRequestTrackerAction implements RequestTrackerActionAdapter {
@Autowired
private RestTemplate restTemplate;
@Value("${app.url}")
private String url;
@Override
public Thread send(String requestTrackerElementsJson) {
return new Thread(() -> restTemplate.postForEntity(url, requestTrackerElementsJson, Void.class));
}
}
@Component
public class SqsRequestTrackerAction implements RequestTrackerActionAdapter {
@Autowired
private AmazonSQS amazonSQS;
@Value("${app.queueUrl}")
private String queueUrl;
@Override
public Thread send(String requestTrackerElementsJson) {
return new Thread(() -> amazonSQS.sendMessage(queueUrl, requestTrackerElementsJson));
}
}
@Component
public class FirehoseRequestTrackerAction implements RequestTrackerActionAdapter {
@Autowired
private AmazonKinesisFirehoseClient firehoseClient;
@Value("${app.deliveryStreamName}")
private String deliveryStreamName;
@Override
public Thread send(String requestTrackerElementsJson) {
return new Thread(() -> {
PutRecordRequest putRecordRequest = new PutRecordRequest();
putRecordRequest.setDeliveryStreamName(deliveryStreamName);
String data = requestTrackerElementsJson + "\n";
Record record = new Record().withData(ByteBuffer.wrap(data.getBytes()));
putRecordRequest.setRecord(record);
firehoseClient.putRecord(putRecordRequest);
});
}
}