Golang日志在CentOS性能监控中的价值
一、核心作用与边界
二、典型落地场景
三、与系统监控的协同
四、在 CentOS 的落地要点
五、最小实践示例
import (
"go.uber.org/zap"
"net/http"
"time"
)
var sugared *zap.SugaredLogger
func init() {
logger, _ := zap.NewProduction()
sugared = logger.Sugar()
}
func timedHandler(w http.ResponseWriter, r *http.Request) {
start := time.Now()
defer func() {
sugared.Infow("http_request_completed",
"method", r.Method,
"path", r.URL.Path,
"status", http.StatusOK,
"latency_ms", time.Since(start).Milliseconds(),
"trace_id", r.Header.Get("X-Trace-ID"),
)
}()
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}
import (
"net/http"
_ "net/http/pprof"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func main() {
go func() { http.ListenAndServe("localhost:6060", nil) }() // pprof
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/", timedHandler)
http.ListenAndServe(":8080", nil)
}
# CPU/内存/负载
top
free -h
uptime
# 磁盘与 I/O
iostat -x 1 5
# 综合资源与网络
vmstat 1 5
sar -n DEV 1
/var/log/myapp/*.log {
daily
missingok
rotate 7
compress
notifempty
create 0640 root root
}
上述组合可在CentOS上形成“日志解释 + 指标趋势 + 深度剖析 + 系统观测”的可观测性闭环,既满足日常性能监控,也支撑故障定位与容量规划。