10484: Track non-s3 errors by Go type.
[arvados.git] / services / datamanager / loggerutil / loggerutil.go
1 /* Datamanager-specific logging methods. */
2
3 package loggerutil
4
5 import (
6         "git.curoverse.com/arvados.git/sdk/go/logger"
7         "log"
8         "os"
9         "runtime"
10         "time"
11 )
12
13 // Useful to call at the beginning of execution to log info about the
14 // current run.
15 func LogRunInfo(arvLogger *logger.Logger) {
16         if arvLogger != nil {
17                 now := time.Now()
18                 arvLogger.Update(func(p map[string]interface{}, e map[string]interface{}) {
19                         runInfo := logger.GetOrCreateMap(p, "run_info")
20                         runInfo["started_at"] = now
21                         runInfo["args"] = os.Args
22                         hostname, err := os.Hostname()
23                         if err != nil {
24                                 runInfo["hostname_error"] = err.Error()
25                         } else {
26                                 runInfo["hostname"] = hostname
27                         }
28                         runInfo["pid"] = os.Getpid()
29                 })
30         }
31 }
32
33 // A LogMutator that records the current memory usage. This is most useful as a logger write hook.
34 func LogMemoryAlloc(p map[string]interface{}, e map[string]interface{}) {
35         runInfo := logger.GetOrCreateMap(p, "run_info")
36         var memStats runtime.MemStats
37         runtime.ReadMemStats(&memStats)
38         runInfo["memory_bytes_in_use"] = memStats.Alloc
39         runInfo["memory_bytes_reserved"] = memStats.Sys
40 }
41
42 func FatalWithMessage(arvLogger *logger.Logger, message string) {
43         if arvLogger != nil {
44                 arvLogger.FinalUpdate(func(p map[string]interface{}, e map[string]interface{}) {
45                         p["FATAL"] = message
46                         runInfo := logger.GetOrCreateMap(p, "run_info")
47                         runInfo["finished_at"] = time.Now()
48                 })
49         }
50
51         log.Fatalf(message)
52 }