Merge pull request #26826 from hashicorp/jbardin/plugin-crash-filter
hide provider crashes from panicwrap when logging
This commit is contained in:
commit
1d6fdff999
|
@ -172,11 +172,22 @@ func (l *logPanicWrapper) Debug(msg string, args ...interface{}) {
|
||||||
// output if this is the start of the traceback. An occasional false
|
// 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
|
// positive shouldn't be a big deal, since this is only retrieved after an
|
||||||
// error of some sort.
|
// 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 {
|
if l.inPanic && l.panicRecorder != nil {
|
||||||
l.panicRecorder(msg)
|
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...)
|
l.Logger.Debug(msg, args...)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
package logging
|
package logging
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hashicorp/go-hclog"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestPanicRecorder(t *testing.T) {
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue