Merge pull request #949 from prometheus/fabxc/pql-rt

Handle parser runtime panics gracefully
This commit is contained in:
Fabian Reinartz 2015-08-02 17:23:35 +02:00
commit de848f7e1a
2 changed files with 38 additions and 2 deletions

View file

@ -20,7 +20,10 @@ import (
"strings"
"time"
"github.com/prometheus/log"
clientmodel "github.com/prometheus/client_golang/model"
"github.com/prometheus/prometheus/storage/metric"
"github.com/prometheus/prometheus/util/strutil"
)
@ -323,10 +326,16 @@ func (p *parser) recover(errp *error) {
e := recover()
if e != nil {
if _, ok := e.(runtime.Error); ok {
panic(e)
}
// Print the stack trace but do not inhibit the running application.
buf := make([]byte, 64<<10)
buf = buf[:runtime.Stack(buf, false)]
log.Errorf("parser panic: %v\n%s", e, buf)
*errp = fmt.Errorf("unexpected error")
} else {
*errp = e.(error)
}
}
return
}

View file

@ -1354,3 +1354,30 @@ func TestParseSeries(t *testing.T) {
}
}
}
func TestRecoverRuntime(t *testing.T) {
var p *parser
var err error
defer p.recover(&err)
// Cause a runtime panic.
var a []int
a[123] = 1
if err.Error() != "unexpected error" {
t.Fatalf("wrong error message: %q, expected %q", err, "unexpected error")
}
}
func TestRecoverError(t *testing.T) {
var p *parser
var err error
defer p.recover(&err)
e := fmt.Errorf("custom error")
panic(e)
if err.Error() != e.Error() {
t.Fatalf("wrong error message: %q, expected %q", err, e)
}
}