/
logonpanic.go
49 lines (39 loc) · 1.32 KB
/
logonpanic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package watchdog
import (
"fmt"
"runtime"
"github.com/DataDog/datadog-agent/pkg/trace/log"
"github.com/DataDog/datadog-go/v5/statsd"
)
const shortErrMsgLen = 17 // 20 char max with tailing "..."
// shortMsg shortens the length of error message to avoid having high
// cardinality on "err:" tags
func shortErrMsg(msg string) string {
if len(msg) <= shortErrMsgLen {
return msg
}
return msg[:shortErrMsgLen] + "..."
}
// LogOnPanic catches panics and logs them on the fly. It also flushes
// the log file, ensuring the message appears. Then it propagates the panic
// so that the program flow remains unchanged.
func LogOnPanic(statsd statsd.ClientInterface) {
if err := recover(); err != nil {
// Full print of the trace in the logs
buf := make([]byte, 4096)
length := runtime.Stack(buf, false)
stacktrace := string(buf[:length])
errMsg := fmt.Sprintf("%v", err)
logMsg := "Unexpected panic: " + errMsg + "\n" + stacktrace
_ = statsd.Gauge("datadog.trace_agent.panic", 1, []string{
"err:" + shortErrMsg(errMsg),
}, 1)
log.Error(logMsg)
log.Flush()
panic(err)
}
}