Skip to content

Commit

Permalink
feat: Add Microservice Pattern, Log aggregation (#2690) (#2719)
Browse files Browse the repository at this point in the history
* feat: Add Microservice Pattern, Log aggregation.

Related: #2690

* docs: Add javaDoc for public methods.

Related: #2690

---------

Co-authored-by: Ilkka Seppälä <iluwatar@users.noreply.github.com>
  • Loading branch information
hwan33 and iluwatar authored Dec 27, 2023
1 parent 5df1fb6 commit cd2dbb7
Show file tree
Hide file tree
Showing 12 changed files with 593 additions and 0 deletions.
59 changes: 59 additions & 0 deletions log-aggregation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
title: Log aggregation
category: Architectural
language: en
tag:
- Microservices
- Extensibility
---

## Intent

Centralize, streamline, and optimize the process of log management so that insights can be quickly
derived, problems can be swiftly identified and resolved, and the system's overall health can be
monitored efficiently.

## Explanation

Real-world example

> AWS CloudWatch aggregates logs from various AWS services for monitoring and alerting.

In plain words

> The primary goal is to consolidate logs from different sources, making them more accessible and
> actionable. Various tools and platforms, such as Elasticsearch-Logstash-Kibana (ELK) stack,
> Splunk,
> Graylog, and others, are employed in these real-world scenarios to facilitate log aggregation.
Wikipedia says

> You have applied the Microservice architecture pattern. The application consists of multiple
> services and service instances that are running on multiple machines. Requests often span multiple
> service instances. Each service instance generates writes information about what it is doing to a
> log file in a standardized format. The log file contains errors, warnings, information and debug
> messages.

## Class diagram

![class diagram](./etc/log-aggregation.png)

## Applicability

1. Distributed Systems and Microservices
- In modern architectures where systems are split into smaller, independent microservices running across multiple servers or even data centers, aggregating logs from all these services is crucial for a holistic view of system health and activity.

2. Troubleshooting and Debugging
- When system failures or unexpected behaviors occur, engineers need consolidated logs to trace and diagnose issues. Log aggregation makes this process efficient by collecting all relevant logs in one place.

3. Security and Compliance Monitoring
- Many industries have regulatory requirements for log retention and analysis. Log aggregation helps in collecting, retaining, and analyzing logs for unauthorized access, potential breaches, and other security threats.

4. Performance Monitoring
- Aggregated logs can be used to identify performance bottlenecks, slow database queries, or service endpoints experiencing high latencies.

## Credits

* [Pattern: Log aggregation](https://microservices.io/patterns/observability/application-logging.html)
Binary file added log-aggregation/etc/log-aggregation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 51 additions & 0 deletions log-aggregation/etc/log-aggregation.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
@startuml

package com.iluwatar.logaggregation {

class App {
+ main(args: String[]) {static}
}

class CentralLogStore {
- logs: ConcurrentLinkedQueue<LogEntry>
+ storeLog(logEntry: LogEntry)
+ displayLogs()
}

class LogAggregator {
- BUFFER_THRESHOLD: int {static}
- centralLogStore: CentralLogStore
- buffer: ConcurrentLinkedQueue<LogEntry>
- minLogLevel: LogLevel
- executorService: ExecutorService
- logCount: AtomicInteger
+ collectLog(logEntry: LogEntry)
+ stop()
}

class LogEntry {
- serviceName: String
- level: LogLevel
- message: String
- timestamp: LocalDateTime
}

enum LogLevel {
DEBUG
INFO
ERROR
}

class LogProducer {
- serviceName: String
- aggregator: LogAggregator
+ generateLog(level: LogLevel, message: String)
}
}

LogProducer --> "-aggregator" LogAggregator
LogAggregator --> "-centralLogStore" CentralLogStore
LogAggregator --> "-buffer" LogEntry
CentralLogStore --> "-logs" LogEntry

@enduml
32 changes: 32 additions & 0 deletions log-aggregation/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.26.0-SNAPSHOT</version>
</parent>

<artifactId>log-aggregation</artifactId>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>
53 changes: 53 additions & 0 deletions log-aggregation/src/main/java/com/iluwatar/logaggregation/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2023 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.logaggregation;

/**
* The main application class responsible for demonstrating the log aggregation mechanism. Creates
* services, generates logs, aggregates, and finally displays the logs.
*/
public class App {

/**
* The entry point of the application.
*
* @param args Command line arguments.
* @throws InterruptedException If any thread has interrupted the current thread.
*/
public static void main(String[] args) throws InterruptedException {
final CentralLogStore centralLogStore = new CentralLogStore();
final LogAggregator aggregator = new LogAggregator(centralLogStore, LogLevel.INFO);

final LogProducer serviceA = new LogProducer("ServiceA", aggregator);
final LogProducer serviceB = new LogProducer("ServiceB", aggregator);

serviceA.generateLog(LogLevel.INFO, "This is an INFO log from ServiceA");
serviceB.generateLog(LogLevel.ERROR, "This is an ERROR log from ServiceB");
serviceA.generateLog(LogLevel.DEBUG, "This is a DEBUG log from ServiceA");

aggregator.stop();
centralLogStore.displayLogs();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2023 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.logaggregation;

import java.util.concurrent.ConcurrentLinkedQueue;
import lombok.extern.slf4j.Slf4j;

/**
* A centralized store for logs. It collects logs from various services and stores them.
* This class is thread-safe, ensuring that logs from different services are safely stored
* concurrently without data races.
*/
@Slf4j
public class CentralLogStore {

private final ConcurrentLinkedQueue<LogEntry> logs = new ConcurrentLinkedQueue<>();

/**
* Stores the given log entry into the central log store.
*
* @param logEntry The log entry to store.
*/
public void storeLog(LogEntry logEntry) {
if (logEntry == null) {
LOGGER.error("Received null log entry. Skipping.");
return;
}
logs.offer(logEntry);
}

/**
* Displays all logs currently stored in the central log store.
*/
public void displayLogs() {
LOGGER.info("----- Centralized Logs -----");
for (LogEntry logEntry : logs) {
LOGGER.info(
logEntry.getTimestamp() + " [" + logEntry.getLevel() + "] " + logEntry.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2023 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.logaggregation;

import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import lombok.extern.slf4j.Slf4j;

/**
* Responsible for collecting and buffering logs from different services.
* Once the logs reach a certain threshold or after a certain time interval,
* they are flushed to the central log store. This class ensures logs are collected
* and processed asynchronously and efficiently, providing both an immediate collection
* and periodic flushing.
*/
@Slf4j
public class LogAggregator {

private static final int BUFFER_THRESHOLD = 3;
private final CentralLogStore centralLogStore;
private final ConcurrentLinkedQueue<LogEntry> buffer = new ConcurrentLinkedQueue<>();
private final LogLevel minLogLevel;
private final ExecutorService executorService = Executors.newSingleThreadExecutor();
private final AtomicInteger logCount = new AtomicInteger(0);

/**
* constructor of LogAggregator.
*
* @param centralLogStore central log store implement
* @param minLogLevel min log level to store log
*/
public LogAggregator(CentralLogStore centralLogStore, LogLevel minLogLevel) {
this.centralLogStore = centralLogStore;
this.minLogLevel = minLogLevel;
startBufferFlusher();
}

/**
* Collects a given log entry, and filters it by the defined log level.
*
* @param logEntry The log entry to collect.
*/
public void collectLog(LogEntry logEntry) {
if (logEntry.getLevel() == null || minLogLevel == null) {
LOGGER.warn("Log level or threshold level is null. Skipping.");
return;
}

if (logEntry.getLevel().compareTo(minLogLevel) < 0) {
LOGGER.debug("Log level below threshold. Skipping.");
return;
}

buffer.offer(logEntry);

if (logCount.incrementAndGet() >= BUFFER_THRESHOLD) {
flushBuffer();
}
}

/**
* Stops the log aggregator service and flushes any remaining logs to
* the central log store.
*
* @throws InterruptedException If any thread has interrupted the current thread.
*/
public void stop() throws InterruptedException {
executorService.shutdownNow();
if (!executorService.awaitTermination(10, TimeUnit.SECONDS)) {
LOGGER.error("Log aggregator did not terminate.");
}
flushBuffer();
}

private void flushBuffer() {
LogEntry logEntry;
while ((logEntry = buffer.poll()) != null) {
centralLogStore.storeLog(logEntry);
logCount.decrementAndGet();
}
}

private void startBufferFlusher() {
executorService.execute(() -> {
while (!Thread.currentThread().isInterrupted()) {
try {
Thread.sleep(5000); // Flush every 5 seconds.
flushBuffer();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
});
}
}
Loading

0 comments on commit cd2dbb7

Please # to comment.