X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/e0889a8f6997327fd9b4d826237c8166cf909741..c3e2a93527a137fbeb3e2bb13c5ab344a95ab47f:/services/datamanager/loggerutil/loggerutil.go diff --git a/services/datamanager/loggerutil/loggerutil.go b/services/datamanager/loggerutil/loggerutil.go index e4a53c2d05..8111425d7a 100644 --- a/services/datamanager/loggerutil/loggerutil.go +++ b/services/datamanager/loggerutil/loggerutil.go @@ -5,20 +5,48 @@ package loggerutil import ( "git.curoverse.com/arvados.git/sdk/go/logger" "log" + "os" + "runtime" "time" ) -// Assumes you haven't already called arvLogger.Edit()! -// If you have called arvLogger.Edit() this method will hang waiting -// for the lock you're already holding. +// Useful to call at the beginning of execution to log info about the +// current run. +func LogRunInfo(arvLogger *logger.Logger) { + if arvLogger != nil { + now := time.Now() + arvLogger.Update(func(p map[string]interface{}, e map[string]interface{}) { + runInfo := logger.GetOrCreateMap(p, "run_info") + runInfo["started_at"] = now + runInfo["args"] = os.Args + hostname, err := os.Hostname() + if err != nil { + runInfo["hostname_error"] = err.Error() + } else { + runInfo["hostname"] = hostname + } + runInfo["pid"] = os.Getpid() + }) + } +} + +// A LogMutator that records the current memory usage. This is most useful as a logger write hook. +func LogMemoryAlloc(p map[string]interface{}, e map[string]interface{}) { + runInfo := logger.GetOrCreateMap(p, "run_info") + var memStats runtime.MemStats + runtime.ReadMemStats(&memStats) + runInfo["memory_bytes_in_use"] = memStats.Alloc + runInfo["memory_bytes_reserved"] = memStats.Sys +} + func FatalWithMessage(arvLogger *logger.Logger, message string) { if arvLogger != nil { - properties,_ := arvLogger.Edit() - properties["FATAL"] = message - properties["run_info"].(map[string]interface{})["end_time"] = time.Now() - arvLogger.ForceRecord() + arvLogger.FinalUpdate(func(p map[string]interface{}, e map[string]interface{}) { + p["FATAL"] = message + runInfo := logger.GetOrCreateMap(p, "run_info") + runInfo["finished_at"] = time.Now() + }) } log.Fatalf(message) } -