From 97e2b0a2eff9f9b8d966812bafff03fb49392d2f Mon Sep 17 00:00:00 2001 From: wangqing Date: Thu, 6 Nov 2025 17:03:18 +0800 Subject: [PATCH] fmt code Signed-off-by: wangqing --- collector/errors.go | 12 ++++++------ collector/host_libvirt_backend.go | 12 ++++++------ collector/logging.go | 2 +- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/collector/errors.go b/collector/errors.go index 7aadb03..30ac7eb 100644 --- a/collector/errors.go +++ b/collector/errors.go @@ -8,11 +8,11 @@ import ( // CollectionError represents an error that occurred during metric collection type CollectionError struct { - Domain string - Metric string - Op string - Err error - Context map[string]any + Domain string + Metric string + Op string + Err error + Context map[string]any } // Error implements the error interface @@ -95,4 +95,4 @@ func SafeGetDomainUUID(domain *libvirt.Domain) string { return "unknown" } return uuid -} \ No newline at end of file +} diff --git a/collector/host_libvirt_backend.go b/collector/host_libvirt_backend.go index 9db52c7..0c04a54 100644 --- a/collector/host_libvirt_backend.go +++ b/collector/host_libvirt_backend.go @@ -783,13 +783,13 @@ func (mc *LibvirtMetricsCollector) collectHostCPUUsage(conn *libvirt.Connect) fl // If we can't get CPU stats with -1, try getting stats for CPU 0 and calculate manually return mc.calculateCPUUsageFromSingleCore(conn) } - + // If utilization is directly available, use it if cpuStats.UtilizationSet { // Utilization is in centi-percent (0-10000), convert to percentage return float64(cpuStats.Utilization) / 100.0 } - + // Calculate CPU usage manually from individual stats return mc.calculateCPUUsageFromStats(cpuStats) } @@ -800,7 +800,7 @@ func (mc *LibvirtMetricsCollector) calculateCPUUsageFromSingleCore(conn *libvirt if err != nil { return 0.0 } - + return mc.calculateCPUUsageFromStats(cpuStats) } @@ -808,7 +808,7 @@ func (mc *LibvirtMetricsCollector) calculateCPUUsageFromSingleCore(conn *libvirt func (mc *LibvirtMetricsCollector) calculateCPUUsageFromStats(cpuStats *libvirt.NodeCPUStats) float64 { total := uint64(0) idle := uint64(0) - + // Sum up all the time components if cpuStats.KernelSet { total += cpuStats.Kernel @@ -827,12 +827,12 @@ func (mc *LibvirtMetricsCollector) calculateCPUUsageFromStats(cpuStats *libvirt. if cpuStats.IntrSet { total += cpuStats.Intr } - + // Calculate usage percentage: (total - idle) / total * 100 if total > 0 { return float64(total-idle) / float64(total) * 100.0 } - + return 0.0 } diff --git a/collector/logging.go b/collector/logging.go index 01dfc53..2da3bbb 100644 --- a/collector/logging.go +++ b/collector/logging.go @@ -91,4 +91,4 @@ func domainStateToString(state libvirt.DomainState) string { default: return "unknown" } -} \ No newline at end of file +} -- Gitee