157 lines
4.5 KiB
Go
157 lines
4.5 KiB
Go
// Package task provides metrics logging for asynchronous task system
|
|
package task
|
|
|
|
import (
|
|
"context"
|
|
"runtime"
|
|
"time"
|
|
|
|
"modelRT/logger"
|
|
)
|
|
|
|
// MetricsLogger logs task system metrics using the existing logging system
|
|
type MetricsLogger struct {
|
|
ctx context.Context
|
|
}
|
|
|
|
// NewMetricsLogger creates a new MetricsLogger
|
|
func NewMetricsLogger(ctx context.Context) *MetricsLogger {
|
|
return &MetricsLogger{ctx: ctx}
|
|
}
|
|
|
|
// LogTaskMetrics records task processing metrics
|
|
func (m *MetricsLogger) LogTaskMetrics(taskType TaskType, status string, processingTime time.Duration, retryCount int) {
|
|
logger.Info(m.ctx, "Task metrics",
|
|
"task_type", taskType,
|
|
"status", status,
|
|
"processing_time_ms", processingTime.Milliseconds(),
|
|
"retry_count", retryCount,
|
|
"metric_type", "task_processing",
|
|
"timestamp", time.Now().Unix(),
|
|
)
|
|
}
|
|
|
|
// LogQueueMetrics records queue metrics
|
|
func (m *MetricsLogger) LogQueueMetrics(queueDepth int, queueLatency time.Duration) {
|
|
logger.Info(m.ctx, "Queue metrics",
|
|
"queue_depth", queueDepth,
|
|
"queue_latency_ms", queueLatency.Milliseconds(),
|
|
"metric_type", "queue",
|
|
"timestamp", time.Now().Unix(),
|
|
)
|
|
}
|
|
|
|
// LogWorkerMetrics records worker metrics
|
|
func (m *MetricsLogger) LogWorkerMetrics(activeWorkers, idleWorkers, totalWorkers int, memoryUsage uint64, cpuLoad float64) {
|
|
logger.Info(m.ctx, "Worker metrics",
|
|
"active_workers", activeWorkers,
|
|
"idle_workers", idleWorkers,
|
|
"total_workers", totalWorkers,
|
|
"memory_usage_mb", memoryUsage/(1024*1024),
|
|
"cpu_load_percent", cpuLoad,
|
|
"metric_type", "worker",
|
|
"timestamp", time.Now().Unix(),
|
|
)
|
|
}
|
|
|
|
// LogRetryMetrics records retry metrics
|
|
func (m *MetricsLogger) LogRetryMetrics(taskType TaskType, retryCount int, success bool, delay time.Duration) {
|
|
logger.Info(m.ctx, "Retry metrics",
|
|
"task_type", taskType,
|
|
"retry_count", retryCount,
|
|
"retry_success", success,
|
|
"retry_delay_ms", delay.Milliseconds(),
|
|
"metric_type", "retry",
|
|
"timestamp", time.Now().Unix(),
|
|
)
|
|
}
|
|
|
|
// LogSystemMetrics records system-level metrics (memory, CPU, goroutines)
|
|
func (m *MetricsLogger) LogSystemMetrics() {
|
|
var memStats runtime.MemStats
|
|
runtime.ReadMemStats(&memStats)
|
|
|
|
logger.Info(m.ctx, "System metrics",
|
|
"metric_type", "system",
|
|
"timestamp", time.Now().Unix(),
|
|
"goroutines", runtime.NumGoroutine(),
|
|
"memory_alloc_mb", memStats.Alloc/(1024*1024),
|
|
"memory_total_alloc_mb", memStats.TotalAlloc/(1024*1024),
|
|
"memory_sys_mb", memStats.Sys/(1024*1024),
|
|
"memory_heap_alloc_mb", memStats.HeapAlloc/(1024*1024),
|
|
"memory_heap_sys_mb", memStats.HeapSys/(1024*1024),
|
|
"memory_heap_inuse_mb", memStats.HeapInuse/(1024*1024),
|
|
"gc_pause_total_ns", memStats.PauseTotalNs,
|
|
"num_gc", memStats.NumGC,
|
|
)
|
|
}
|
|
|
|
// LogTaskCompletionMetrics records detailed task completion metrics
|
|
func (m *MetricsLogger) LogTaskCompletionMetrics(taskID, taskType, status string, startTime, endTime time.Time, retryCount int, errorMsg string) {
|
|
duration := endTime.Sub(startTime)
|
|
|
|
logger.Info(m.ctx, "Task completion metrics",
|
|
"metric_type", "task_completion",
|
|
"timestamp", time.Now().Unix(),
|
|
"task_id", taskID,
|
|
"task_type", taskType,
|
|
"status", status,
|
|
"duration_ms", duration.Milliseconds(),
|
|
"start_time", startTime.Unix(),
|
|
"end_time", endTime.Unix(),
|
|
"retry_count", retryCount,
|
|
"has_error", errorMsg != "",
|
|
"error_msg", errorMsg,
|
|
)
|
|
}
|
|
|
|
// LogHealthCheckMetrics records health check metrics
|
|
func (m *MetricsLogger) LogHealthCheckMetrics(healthy bool, checkDuration time.Duration, components map[string]bool) {
|
|
logger.Info(m.ctx, "Health check metrics",
|
|
"metric_type", "health_check",
|
|
"timestamp", time.Now().Unix(),
|
|
"healthy", healthy,
|
|
"check_duration_ms", checkDuration.Milliseconds(),
|
|
"components", components,
|
|
)
|
|
}
|
|
|
|
// PeriodicMetricsLogger periodically logs system and worker metrics
|
|
type PeriodicMetricsLogger struct {
|
|
ctx context.Context
|
|
interval time.Duration
|
|
stopChan chan struct{}
|
|
metricsLog *MetricsLogger
|
|
}
|
|
|
|
// NewPeriodicMetricsLogger creates a new PeriodicMetricsLogger
|
|
func NewPeriodicMetricsLogger(ctx context.Context, interval time.Duration) *PeriodicMetricsLogger {
|
|
return &PeriodicMetricsLogger{
|
|
ctx: ctx,
|
|
interval: interval,
|
|
stopChan: make(chan struct{}),
|
|
metricsLog: NewMetricsLogger(ctx),
|
|
}
|
|
}
|
|
|
|
// Start begins periodic metrics logging
|
|
func (p *PeriodicMetricsLogger) Start() {
|
|
go func() {
|
|
ticker := time.NewTicker(p.interval)
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
select {
|
|
case <-p.stopChan:
|
|
return
|
|
case <-ticker.C:
|
|
p.metricsLog.LogSystemMetrics()
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
|
|
// Stop stops periodic metrics logging
|
|
func (p *PeriodicMetricsLogger) Stop() {
|
|
close(p.stopChan)
|
|
} |