
Backend
Traffic-Guard
A logging system that sends every HTTP request and response of an API through Kafka to Elasticsearch. A servlet filter collects the record and publishes it to Kafka with a correlation id, a consumer writes it to Elasticsearch, and you search it in Kibana.
Problem
When an API receives many requests per second, the answer to 'what did that request send, what came back, how long did it take' tends to get lost between log lines. Recording every request in detail also risks slowing the API down because of the recording itself. I built Traffic Guard to separate the two: leave as little work as possible on the request path, and move the heavy indexing behind Kafka.
Highlights
- TrafficMonitorFilter (a OncePerRequestFilter) reads the request and response through ContentCaching wrappers and records method, URI, client IP, status, duration, headers and both bodies, together with a random UUID correlation id.
- A failure in recording cannot break the request: the logging sits in a try/catch and the body is always copied back to the response. The /actuator, Swagger and api-docs paths are excluded.
- The event is sent asynchronously with KafkaTemplate to the traffic.log.events topic (3 partitions), keyed by the correlation id; a send failure is only logged.
- A consumer in the traffic-guard-group group takes events as a list and writes them to the traffic-logs index with a single saveAll. Fields are mapped with types: method and correlationId as keyword, clientIp as ip, durationMs as long, timestamp as date.
- A small order API exists to produce traffic worth recording. The payment simulator throws a random timeout in about 30 percent of calls; an order goes from PENDING to either COMPLETED or FAILED.
- Docker Compose brings up Kafka and Zookeeper, Elasticsearch with security enabled, Kibana and PostgreSQL. Settings come from a Git repository through Spring Cloud Config Server.
Architecture and notes
The flow is this: the request enters the filter, goes down the chain wrapped, and when the response is ready the filter builds the record and hands it to the producer. The Kafka topic acts as a buffer between the request path and indexing; even if the consumer slows down or is stopped the API keeps answering, and events wait in the topic. The correlation id is both the Kafka key and the Elasticsearch document id, so a repeated event overwrites itself. In Kibana you can find a single request by its id, or filter by status code and duration.
Saying it 'does not affect the request path' would not be quite accurate, though. Only the Kafka send is asynchronous; collecting headers and turning bodies into strings still happens on the request thread, and bodies are buffered in memory. There are other gaps: headers, including Authorization, are stored without masking, body size is not capped, the consumer logs and drops a batch when it fails (no retry or dead-letter), and the tests only check that the application starts. For real use I would fix the masking first, then that error path.
Live from GitHub
- Stars
- 0
- Forks
- 0
- Last updated
Languages
- Java100%
Fetched from the public GitHub API and refreshed hourly.
Technologies
Request flow
Hover or focus a component to see what it is.