Merge pull request #1012 from laushinka/master

Log runtime errors during query evaluation instead of panicking.
This commit is contained in:
Fabian Reinartz 2015-08-19 17:21:11 +02:00
commit 6a713eedca
3 changed files with 40 additions and 6 deletions

View File

@ -22,6 +22,7 @@ import (
"strconv" "strconv"
"time" "time"
"github.com/prometheus/log"
"golang.org/x/net/context" "golang.org/x/net/context"
clientmodel "github.com/prometheus/client_golang/model" clientmodel "github.com/prometheus/client_golang/model"
@ -260,7 +261,7 @@ func contextDone(ctx context.Context, env string) error {
} }
} }
// Engine handles the liftetime of queries from beginning to end. // Engine handles the lifetime of queries from beginning to end.
// It is connected to a storage. // It is connected to a storage.
type Engine struct { type Engine struct {
// The storage on which the engine operates. // The storage on which the engine operates.
@ -554,13 +555,18 @@ func (ev *evaluator) error(err error) {
func (ev *evaluator) recover(errp *error) { func (ev *evaluator) recover(errp *error) {
e := recover() e := recover()
if e != nil { if e != nil {
// Do not recover from runtime errors.
if _, ok := e.(runtime.Error); ok { 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) *errp = e.(error)
} }
} }
}
// evalScalar attempts to evaluate e to a scalar value and errors otherwise. // evalScalar attempts to evaluate e to a scalar value and errors otherwise.
func (ev *evaluator) evalScalar(e Expr) *Scalar { func (ev *evaluator) evalScalar(e Expr) *Scalar {

View File

@ -1,6 +1,7 @@
package promql package promql
import ( import (
"fmt"
"testing" "testing"
"time" "time"
@ -179,3 +180,30 @@ func TestEngineShutdown(t *testing.T) {
t.Fatalf("expected cancelation error, got %q", res2.Err) t.Fatalf("expected cancelation error, got %q", res2.Err)
} }
} }
func TestRecoverEvaluatorRuntime(t *testing.T) {
var ev *evaluator
var err error
defer ev.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 TestRecoverEvaluatorError(t *testing.T) {
var ev *evaluator
var err error
defer ev.recover(&err)
e := fmt.Errorf("custom error")
panic(e)
if err.Error() != e.Error() {
t.Fatalf("wrong error message: %q, expected %q", err, e)
}
}

View File

@ -1428,7 +1428,7 @@ func TestParseSeries(t *testing.T) {
} }
} }
func TestRecoverRuntime(t *testing.T) { func TestRecoverParserRuntime(t *testing.T) {
var p *parser var p *parser
var err error var err error
defer p.recover(&err) defer p.recover(&err)
@ -1442,7 +1442,7 @@ func TestRecoverRuntime(t *testing.T) {
} }
} }
func TestRecoverError(t *testing.T) { func TestRecoverParserError(t *testing.T) {
var p *parser var p *parser
var err error var err error
defer p.recover(&err) defer p.recover(&err)