Merge pull request #26826 from hashicorp/jbardin/plugin-crash-filter

hide provider crashes from panicwrap when logging
This commit is contained in:
James Bardin 2020-11-05 11:04:24 -05:00 committed by GitHub
commit 1d6fdff999
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 1 deletions

View File

@ -172,11 +172,22 @@ func (l *logPanicWrapper) Debug(msg string, args ...interface{}) {
// output if this is the start of the traceback. An occasional false
// positive shouldn't be a big deal, since this is only retrieved after an
// error of some sort.
l.inPanic = l.inPanic || strings.HasPrefix(msg, "panic: ") || strings.HasPrefix(msg, "fatal error: ")
panicPrefix := strings.HasPrefix(msg, "panic: ") || strings.HasPrefix(msg, "fatal error: ")
l.inPanic = l.inPanic || panicPrefix
if l.inPanic && l.panicRecorder != nil {
l.panicRecorder(msg)
}
// If we have logging turned on, we need to prevent panicwrap from seeing
// this as a core panic. This can be done by obfuscating the panic error
// line.
if panicPrefix {
colon := strings.Index(msg, ":")
msg = strings.ToUpper(msg[:colon]) + msg[colon:]
}
l.Logger.Debug(msg, args...)
}

View File

@ -1,9 +1,12 @@
package logging
import (
"bytes"
"fmt"
"strings"
"testing"
"github.com/hashicorp/go-hclog"
)
func TestPanicRecorder(t *testing.T) {
@ -49,3 +52,31 @@ func TestPanicLimit(t *testing.T) {
}
}
}
func TestLogPanicWrapper(t *testing.T) {
var buf bytes.Buffer
logger := hclog.NewInterceptLogger(&hclog.LoggerOptions{
Name: "test",
Level: hclog.Debug,
Output: &buf,
DisableTime: true,
})
wrapped := (&logPanicWrapper{
Logger: logger,
}).Named("test")
wrapped.Debug("panic: invalid foo of bar")
wrapped.Debug("\tstack trace")
expected := `[DEBUG] test.test: PANIC: invalid foo of bar
[DEBUG] test.test: stack trace
`
got := buf.String()
if expected != got {
t.Fatalf("Expected:\n%q\nGot:\n%q", expected, got)
}
}