Skip to content

Fix(GraphQL): Log query along with the panic #7638

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion graphql/admin/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func recoveryHandler(next http.Handler) http.Handler {
func(err error) {
rr := schema.ErrorResponse(err)
write(w, rr, strings.Contains(r.Header.Get("Accept-Encoding"), "gzip"))
})
}, "")

next.ServeHTTP(w, r)
})
Expand Down
5 changes: 3 additions & 2 deletions graphql/api/panics.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ import (
//
// If PanicHandler recovers from a panic, it logs a stack trace, creates an error
// and applies fn to the error.
func PanicHandler(fn func(error)) {
func PanicHandler(fn func(error), query string) {
if err := recover(); err != nil {
glog.Errorf("panic: %s.\n trace: %s", err, string(debug.Stack()))
// Log the panic along with query which caused it.
glog.Errorf("panic: %s.\n query: %s\n trace: %s", err, query, string(debug.Stack()))

fn(errors.Errorf("Internal Server Error - a panic was trapped. " +
"This indicates a bug in the GraphQL server. A stack trace was logged. " +
Expand Down
20 changes: 15 additions & 5 deletions graphql/resolve/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ func New(s schema.Schema, resolverFactory ResolverFactory) *RequestResolver {
// r.GqlReq should be set with a request before Resolve is called
// and a schema and backend Dgraph should have been added.
// Resolve records any errors in the response's error field.
func (r *RequestResolver) Resolve(ctx context.Context, gqlReq *schema.Request) *schema.Response {
func (r *RequestResolver) Resolve(ctx context.Context, gqlReq *schema.Request) (resp *schema.Response) {
span := otrace.FromContext(ctx)
stop := x.SpanTimer(span, methodResolve)
defer stop()
Expand All @@ -472,7 +472,7 @@ func (r *RequestResolver) Resolve(ctx context.Context, gqlReq *schema.Request) *
}

startTime := time.Now()
resp := &schema.Response{
resp = &schema.Response{
Extensions: &schema.Extensions{
Tracing: &schema.Trace{
Version: 1,
Expand All @@ -481,6 +481,14 @@ func (r *RequestResolver) Resolve(ctx context.Context, gqlReq *schema.Request) *
},
}
defer func() {
// Panic Handler for mutation. This ensures that the mutation which causes panic
// gets logged in Alpha logs. This panic handler overrides the default Panic Handler
// used in recoveryHandler in admin/http.go
api.PanicHandler(
func(err error) {
resp.Errors = schema.AsGQLErrors(schema.AppendGQLErrs(resp.Errors, err))
}, gqlReq.Query)

endTime := time.Now()
resp.Extensions.Tracing.EndTime = endTime.Format(time.RFC3339Nano)
resp.Extensions.Tracing.Duration = endTime.Sub(startTime).Nanoseconds()
Expand All @@ -490,13 +498,15 @@ func (r *RequestResolver) Resolve(ctx context.Context, gqlReq *schema.Request) *
// Pass in GraphQL @auth information
ctx, err := r.schema.Meta().AuthMeta().AttachAuthorizationJwt(ctx, gqlReq.Header)
if err != nil {
return schema.ErrorResponse(err)
resp.Errors = schema.AsGQLErrors(err)
return
}

ctx = x.AttachJWTNamespace(ctx)
op, err := r.schema.Operation(gqlReq)
if err != nil {
return schema.ErrorResponse(err)
resp.Errors = schema.AsGQLErrors(err)
return
}

if glog.V(3) {
Expand Down Expand Up @@ -533,7 +543,7 @@ func (r *RequestResolver) Resolve(ctx context.Context, gqlReq *schema.Request) *
Field: q,
Err: err,
}
})
}, gqlReq.Query)
allResolved[storeAt] = r.resolvers.queryResolverFor(q).Resolve(ctx, q)
}(q, i)
}
Expand Down