GitHub user Yuukadesu created a discussion: Add the function of collecting 
resource utilization metrics for extension services.

###  **Purpose**
By using JAVA packages like `java.lang.management` to collect service load data 
and report it, provide data support for the operation of load balancer。
### **Programs**
Add Usage Class
Used to represent the usage of memory and CPU, and provides the percentUsage() 
method to calculate the percentage of resources.
```
public Usage(double usage, double limit) {
    this.usage = usage;
    this.limit = limit;
}

public float percentUsage() {
        float proportion = 0;
        if (limit > 0) {
           proportion = ((float) usage) / ((float) limit);
        }
        return proportion * 100;
    }
```
Add ServiceLoadDataReport Class and ServiceLoadDataReportGenerator Class
The ServiceLoadDataReport class is used to represent ServiceLoadData, while the 
ServiceLoadDataReportGenerator enables threads to periodically retrieve data 
and report it.
```
public ServiceLoadDataReport(Usage CPU, Usage memory, int weight) {
        this.CPU = CPU;
        Memory = memory;
        Weight = weight;
    }
```
```
static {
        executorService = Executors.newSingleThreadScheduledExecutor();
        systemBean = (OperatingSystemMXBean) 
ManagementFactory.getOperatingSystemMXBean();
        serviceLoadDataReport = new ServiceLoadDataReport();
        totalCPULimit = getTotalCPULimit();
        calculateUsage();
        
executorService.scheduleWithFixedDelay(ServiceLoadDataReportGenerator::checkCPULoad,
 CPU_CHECK_MILLIS, CPU_CHECK_MILLIS, TimeUnit.MILLISECONDS);
        
executorService.scheduleWithFixedDelay(ServiceLoadDataReportGenerator::doCalculateUsage,
 1, 1, TimeUnit.MINUTES);
        
executorService.scheduleWithFixedDelay(ServiceLoadDataReportGenerator::doCalculateMemoryUsage,
 1, 1, TimeUnit.MINUTES);
    }
```

GitHub link: https://github.com/apache/streampipes/discussions/3748

----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: [email protected]

Reply via email to