diff --git a/exporter/src/main/kotlin/lab/monilabexporterex/MonilabExporterExApplication.kt b/exporter/src/main/kotlin/lab/monilabexporterex/MonilabExporterExApplication.kt index d132362..dc2cb7f 100644 --- a/exporter/src/main/kotlin/lab/monilabexporterex/MonilabExporterExApplication.kt +++ b/exporter/src/main/kotlin/lab/monilabexporterex/MonilabExporterExApplication.kt @@ -2,8 +2,10 @@ package lab.monilabexporterex import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.runApplication +import org.springframework.scheduling.annotation.EnableScheduling @SpringBootApplication +@EnableScheduling class MonilabExporterExApplication fun main(args: Array) { diff --git a/exporter/src/main/kotlin/lab/monilabexporterex/config/SchedulerConfig.kt b/exporter/src/main/kotlin/lab/monilabexporterex/config/SchedulerConfig.kt new file mode 100644 index 0000000..a296b3c --- /dev/null +++ b/exporter/src/main/kotlin/lab/monilabexporterex/config/SchedulerConfig.kt @@ -0,0 +1,18 @@ +package lab.monilabexporterex.config + +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler + +@Configuration +class SchedulerConfig { + + @Bean + fun taskScheduler(): ThreadPoolTaskScheduler { + val scheduler = ThreadPoolTaskScheduler() + scheduler.poolSize = 5 + scheduler.setThreadNamePrefix("metric-scheduler-") + return scheduler + } + +} diff --git a/exporter/src/main/kotlin/lab/monilabexporterex/model/ApplicationEntity.kt b/exporter/src/main/kotlin/lab/monilabexporterex/model/ApplicationEntity.kt index ee3e45c..94bfdc9 100644 --- a/exporter/src/main/kotlin/lab/monilabexporterex/model/ApplicationEntity.kt +++ b/exporter/src/main/kotlin/lab/monilabexporterex/model/ApplicationEntity.kt @@ -1,6 +1,7 @@ package lab.monilabexporterex.model import jakarta.persistence.* +import java.time.LocalDateTime @Entity @Table(name = "tb_metric_application") @@ -32,4 +33,7 @@ class ApplicationEntity( @Lob @Column(name = "custom_metrics", columnDefinition = "TEXT", nullable = false) val customMetrics: String, + + @Column(name = "registered_date_time", nullable = false, updatable = false) + var registeredDateTime: LocalDateTime = LocalDateTime.now() ) diff --git a/exporter/src/main/kotlin/lab/monilabexporterex/model/ClassLoadingInfoEntity.kt b/exporter/src/main/kotlin/lab/monilabexporterex/model/ClassLoadingInfoEntity.kt index c31d840..fa365e6 100644 --- a/exporter/src/main/kotlin/lab/monilabexporterex/model/ClassLoadingInfoEntity.kt +++ b/exporter/src/main/kotlin/lab/monilabexporterex/model/ClassLoadingInfoEntity.kt @@ -1,6 +1,7 @@ package lab.monilabexporterex.model import jakarta.persistence.* +import java.time.LocalDateTime @Entity @Table(name = "tb_metric_class_loading") @@ -31,4 +32,7 @@ class ClassLoadingInfoEntity( @Column(name = "reserved_code_cache_size", nullable = false) val reservedCodeCacheSize: Long, + + @Column(name = "registered_date_time", nullable = false, updatable = false) + var registeredDateTime: LocalDateTime = LocalDateTime.now() ) diff --git a/exporter/src/main/kotlin/lab/monilabexporterex/model/CpuEntity.kt b/exporter/src/main/kotlin/lab/monilabexporterex/model/CpuEntity.kt index 08b6a84..84394b2 100644 --- a/exporter/src/main/kotlin/lab/monilabexporterex/model/CpuEntity.kt +++ b/exporter/src/main/kotlin/lab/monilabexporterex/model/CpuEntity.kt @@ -1,6 +1,7 @@ package lab.monilabexporterex.model import jakarta.persistence.* +import java.time.LocalDateTime @Entity @Table(name = "tb_metric_cpu") @@ -31,4 +32,7 @@ class CpuEntity( @Column(name = "open_fds", nullable = false) val openFds: Long, + + @Column(name = "registered_date_time", nullable = false, updatable = false) + var registeredDateTime: LocalDateTime = LocalDateTime.now() ) diff --git a/exporter/src/main/kotlin/lab/monilabexporterex/model/GcEntity.kt b/exporter/src/main/kotlin/lab/monilabexporterex/model/GcEntity.kt index 8603b21..5fe19f0 100644 --- a/exporter/src/main/kotlin/lab/monilabexporterex/model/GcEntity.kt +++ b/exporter/src/main/kotlin/lab/monilabexporterex/model/GcEntity.kt @@ -1,6 +1,7 @@ package lab.monilabexporterex.model import jakarta.persistence.* +import java.time.LocalDateTime @Entity @Table(name = "tb_metric_gc") @@ -31,4 +32,7 @@ class GcEntity( @Column(name = "gc_strategy", nullable = false) val gcStrategy: String, + + @Column(name = "registered_date_time", nullable = false, updatable = false) + var registeredDateTime: LocalDateTime = LocalDateTime.now() ) diff --git a/exporter/src/main/kotlin/lab/monilabexporterex/model/MemoryEntity.kt b/exporter/src/main/kotlin/lab/monilabexporterex/model/MemoryEntity.kt index 924c13f..b69d6c5 100644 --- a/exporter/src/main/kotlin/lab/monilabexporterex/model/MemoryEntity.kt +++ b/exporter/src/main/kotlin/lab/monilabexporterex/model/MemoryEntity.kt @@ -1,6 +1,7 @@ package lab.monilabexporterex.model import jakarta.persistence.* +import java.time.LocalDateTime @Entity @Table(name = "tb_metric_memory") @@ -37,4 +38,7 @@ class MemoryEntity( @Column(name = "max_direct_memory_size", nullable = false) val maxDirectMemorySize: Long, + + @Column(name = "registered_date_time", nullable = false, updatable = false) + var registeredDateTime: LocalDateTime = LocalDateTime.now() ) diff --git a/exporter/src/main/kotlin/lab/monilabexporterex/model/NetworkEntity.kt b/exporter/src/main/kotlin/lab/monilabexporterex/model/NetworkEntity.kt index 31d4c12..865dbe4 100644 --- a/exporter/src/main/kotlin/lab/monilabexporterex/model/NetworkEntity.kt +++ b/exporter/src/main/kotlin/lab/monilabexporterex/model/NetworkEntity.kt @@ -1,6 +1,7 @@ package lab.monilabexporterex.model import jakarta.persistence.* +import java.time.LocalDateTime @Entity @Table(name = "tb_metric_network") @@ -31,4 +32,7 @@ class NetworkEntity( @Column(name = "prefer_ipv4", nullable = false) val preferIPv4: Boolean, + + @Column(name = "registered_date_time", nullable = false, updatable = false) + var registeredDateTime: LocalDateTime = LocalDateTime.now() ) diff --git a/exporter/src/main/kotlin/lab/monilabexporterex/model/ThreadsEntity.kt b/exporter/src/main/kotlin/lab/monilabexporterex/model/ThreadsEntity.kt index ca8ec50..e7b03e1 100644 --- a/exporter/src/main/kotlin/lab/monilabexporterex/model/ThreadsEntity.kt +++ b/exporter/src/main/kotlin/lab/monilabexporterex/model/ThreadsEntity.kt @@ -1,6 +1,7 @@ package lab.monilabexporterex.model import jakarta.persistence.* +import java.time.LocalDateTime @Entity @Table(name = "tb_metric_threads") @@ -32,4 +33,7 @@ class ThreadsEntity( @Lob @Column(name = "states", columnDefinition = "TEXT", nullable = false) val states: String, + + @Column(name = "registered_date_time", nullable = false, updatable = false) + var registeredDateTime: LocalDateTime = LocalDateTime.now() ) diff --git a/exporter/src/main/kotlin/lab/monilabexporterex/model/mapper/MetricMapper.kt b/exporter/src/main/kotlin/lab/monilabexporterex/model/mapper/MetricMapper.kt index 2d70b05..a0a731d 100644 --- a/exporter/src/main/kotlin/lab/monilabexporterex/model/mapper/MetricMapper.kt +++ b/exporter/src/main/kotlin/lab/monilabexporterex/model/mapper/MetricMapper.kt @@ -2,6 +2,7 @@ package lab.monilabexporterex.mapper import lab.monilabexporterex.exporter.data.JvmMonitoringData import lab.monilabexporterex.model.* +import java.time.LocalDateTime object JvmMonitoringMapper { @@ -15,7 +16,8 @@ object JvmMonitoringMapper { survivor = data.survivor, old = data.old, bufferPoolUsed = data.bufferPoolUsed, - maxDirectMemorySize = data.maxDirectMemorySize + maxDirectMemorySize = data.maxDirectMemorySize, + registeredDateTime = LocalDateTime.now() ) fun toData(entity: MemoryEntity): JvmMonitoringData.Memory = @@ -38,7 +40,8 @@ object JvmMonitoringMapper { pause = data.pause, allocationRate = data.allocationRate, liveDataSize = data.liveDataSize, - gcStrategy = data.gcStrategy + gcStrategy = data.gcStrategy, + registeredDateTime = LocalDateTime.now() ) fun toData(entity: GcEntity): JvmMonitoringData.Gc = @@ -59,7 +62,8 @@ object JvmMonitoringMapper { peakCount = data.peakCount, deadlockedCount = data.deadlockedCount, cpuTime = data.cpuTime, - states = data.states.toString() + states = data.states.toString(), + registeredDateTime = LocalDateTime.now() ) fun toData(entity: ThreadsEntity): JvmMonitoringData.Threads = @@ -90,7 +94,8 @@ object JvmMonitoringMapper { uptime = data.uptime, startTime = data.startTime, loadAverage = data.loadAverage, - openFds = data.openFds + openFds = data.openFds, + registeredDateTime = LocalDateTime.now() ) fun toData(entity: CpuEntity): JvmMonitoringData.Cpu = @@ -111,7 +116,8 @@ object JvmMonitoringMapper { tcpConnections = data.tcpConnections, tcpEstablished = data.tcpEstablished, openSockets = data.openSockets, - preferIPv4 = data.preferIPv4 + preferIPv4 = data.preferIPv4, + registeredDateTime = LocalDateTime.now() ) fun toData(entity: NetworkEntity): JvmMonitoringData.Network = @@ -132,7 +138,8 @@ object JvmMonitoringMapper { codeCacheUsed = data.codeCacheUsed, codeCacheMax = data.codeCacheMax, compilationTime = data.compilationTime, - reservedCodeCacheSize = data.reservedCodeCacheSize + reservedCodeCacheSize = data.reservedCodeCacheSize, + registeredDateTime = LocalDateTime.now() ) fun toData(entity: ClassLoadingInfoEntity): JvmMonitoringData.ClassLoadingInfo = @@ -153,7 +160,8 @@ object JvmMonitoringMapper { dbConnectionsActive = data.dbConnectionsActive, dbConnectionsMax = data.dbConnectionsMax, queueTasksPending = data.queueTasksPending, - customMetrics = data.customMetrics.toString() + customMetrics = data.customMetrics.toString(), + registeredDateTime = LocalDateTime.now() ) fun toData(entity: ApplicationEntity): JvmMonitoringData.Application = diff --git a/exporter/src/main/kotlin/lab/monilabexporterex/scheduler/MetricScheduler.kt b/exporter/src/main/kotlin/lab/monilabexporterex/scheduler/MetricScheduler.kt new file mode 100644 index 0000000..865826b --- /dev/null +++ b/exporter/src/main/kotlin/lab/monilabexporterex/scheduler/MetricScheduler.kt @@ -0,0 +1,46 @@ +package lab.monilabexporterex.scheduler + +import lab.monilabexporterex.exporter.JvmExporter +import lab.monilabexporterex.mapper.JvmMonitoringMapper +import lab.monilabexporterex.model.Label.* +import lab.monilabexporterex.repository.* +import org.springframework.scheduling.annotation.Scheduled +import org.springframework.stereotype.Component + +@Component +class JvmMonitoringScheduler( + private val jvmExporter: JvmExporter, + private val memoryRepository: MemoryRepository, + private val gcRepository: GcRepository, + private val threadsRepository: ThreadsRepository, + private val cpuRepository: CpuRepository, + private val networkRepository: NetworkRepository, + private val classLoadingRepository: ClassLoadingInfoRepository, + private val applicationRepository: ApplicationRepository +) { + + @Scheduled(fixedRate = 1000) + fun collectAndSaveJvmMetrics() { + val memoryEntity = JvmMonitoringMapper.toEntity(jvmExporter.getMemoryInfo(), TEST) + memoryRepository.save(memoryEntity) + + val gcEntity = JvmMonitoringMapper.toEntity(jvmExporter.getGcInfo(), TEST) + gcRepository.save(gcEntity) + + val threadsEntity = JvmMonitoringMapper.toEntity(jvmExporter.getThreadInfo(), TEST) + threadsRepository.save(threadsEntity) + + val cpuEntity = JvmMonitoringMapper.toEntity(jvmExporter.getCpuInfo(), TEST) + cpuRepository.save(cpuEntity) + + val networkEntity = JvmMonitoringMapper.toEntity(jvmExporter.getNetworkInfo(), TEST) + networkRepository.save(networkEntity) + + val classLoadingEntity = JvmMonitoringMapper.toEntity(jvmExporter.getClassLoadingInfo(), TEST) + classLoadingRepository.save(classLoadingEntity) + + val applicationEntity = JvmMonitoringMapper.toEntity(jvmExporter.getApplicationInfo(), TEST) + applicationRepository.save(applicationEntity) + } + +} diff --git a/exporter/src/main/resources/application.yml b/exporter/src/main/resources/application.yml index bf2a893..47f4559 100644 --- a/exporter/src/main/resources/application.yml +++ b/exporter/src/main/resources/application.yml @@ -7,7 +7,7 @@ spring: database-platform: org.hibernate.dialect.MySQL5InnoDBDialect show-sql: true hibernate: - ddl-auto: update + ddl-auto: create properties: hibernate: default_batch_fetch_size: 1000