Moved some logging code from datamananager to loggerutil.
authormishaz <misha@curoverse.com>
Tue, 10 Feb 2015 02:11:34 +0000 (02:11 +0000)
committerTom Clegg <tom@curoverse.com>
Fri, 13 Feb 2015 21:25:31 +0000 (16:25 -0500)
services/datamanager/datamanager.go
services/datamanager/loggerutil/loggerutil.go

index f63f4628a0cf9aec06078507ab495651a113a160..bd68db112ad26f4eedfe61c6cf5657d48b33cb24 100644 (file)
@@ -9,9 +9,8 @@ import (
        "git.curoverse.com/arvados.git/sdk/go/util"
        "git.curoverse.com/arvados.git/services/datamanager/collection"
        "git.curoverse.com/arvados.git/services/datamanager/keep"
+       "git.curoverse.com/arvados.git/services/datamanager/loggerutil"
        "log"
-       "os"
-       "runtime"
        "time"
 )
 
@@ -52,23 +51,9 @@ func main() {
                        WriteInterval: time.Second * time.Duration(logFrequencySeconds)})
        }
 
+       loggerutil.LogRunInfo(arvLogger)
        if arvLogger != nil {
-               now := time.Now()
-               arvLogger.Update(func(p map[string]interface{}, e map[string]interface{}) {
-                       runInfo := make(map[string]interface{})
-                       runInfo["time_started"] = 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()
-                       p["run_info"] = runInfo
-               })
-
-               arvLogger.AddWriteHook(LogMemoryAlloc)
+               arvLogger.AddWriteHook(loggerutil.LogMemoryAlloc)
        }
 
        collectionChannel := make(chan collection.ReadCollections)
@@ -96,11 +81,3 @@ func main() {
                })
        }
 }
-
-// TODO(misha): Consider moving this to loggerutil
-func LogMemoryAlloc(properties map[string]interface{}, entry map[string]interface{}) {
-       runInfo := properties["run_info"].(map[string]interface{})
-       var memStats runtime.MemStats
-       runtime.ReadMemStats(&memStats)
-       runInfo["alloc_bytes_in_use"] = memStats.Alloc
-}
index c19a7abba2708a2e0afd15f08e57abccfec2f650..1514922396dbeb5c3693c1040cd306f04c53d556 100644 (file)
@@ -5,9 +5,42 @@ package loggerutil
 import (
        "git.curoverse.com/arvados.git/sdk/go/logger"
        "log"
+       "os"
+       "runtime"
        "time"
 )
 
+// Useful to call at the begining 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 := make(map[string]interface{})
+                       runInfo["time_started"] = 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()
+                       p["run_info"] = runInfo
+               })
+       }
+}
+
+// A LogMutator that records the current memory usage. This is most useful as a logger write hook.
+//
+// Assumes we already have a map named "run_info" in properties. LogRunInfo() can create such a map for you if you call it.
+func LogMemoryAlloc(p map[string]interface{}, e map[string]interface{}) {
+       runInfo := p["run_info"].(map[string]interface{})
+       var memStats runtime.MemStats
+       runtime.ReadMemStats(&memStats)
+       runInfo["alloc_bytes_in_use"] = memStats.Alloc
+}
+
 func FatalWithMessage(arvLogger *logger.Logger, message string) {
        if arvLogger != nil {
                arvLogger.FinalUpdate(func(p map[string]interface{}, e map[string]interface{}) {