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 2 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
38 changes: 23 additions & 15 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,19 +472,6 @@ func (r *RequestResolver) Resolve(ctx context.Context, gqlReq *schema.Request) *
}

startTime := time.Now()
resp := &schema.Response{
Extensions: &schema.Extensions{
Tracing: &schema.Trace{
Version: 1,
StartTime: startTime.Format(time.RFC3339Nano),
},
},
}
defer func() {
endTime := time.Now()
resp.Extensions.Tracing.EndTime = endTime.Format(time.RFC3339Nano)
resp.Extensions.Tracing.Duration = endTime.Sub(startTime).Nanoseconds()
}()
ctx = context.WithValue(ctx, resolveStartTime, startTime)

// Pass in GraphQL @auth information
Expand All @@ -499,6 +486,20 @@ func (r *RequestResolver) Resolve(ctx context.Context, gqlReq *schema.Request) *
return schema.ErrorResponse(err)
}

resp = &schema.Response{
Extensions: &schema.Extensions{
Tracing: &schema.Trace{
Version: 1,
StartTime: startTime.Format(time.RFC3339Nano),
},
},
}
defer func() {
endTime := time.Now()
resp.Extensions.Tracing.EndTime = endTime.Format(time.RFC3339Nano)
resp.Extensions.Tracing.Duration = endTime.Sub(startTime).Nanoseconds()
}()

if glog.V(3) {
// don't log the introspection queries they are sent too frequently
// by GraphQL dev tools
Expand Down Expand Up @@ -533,7 +534,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 Expand Up @@ -562,6 +563,13 @@ func (r *RequestResolver) Resolve(ctx context.Context, gqlReq *schema.Request) *
}
resolveQueries()
case op.IsMutation():
// 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
defer api.PanicHandler(
func(err error) {
resp.Errors = schema.AsGQLErrors(schema.AppendGQLErrs(resp.Errors, err))
}, gqlReq.Query)
// A mutation operation can contain any number of mutation fields. Those should be executed
// serially.
// (spec https://graphql.github.io/graphql-spec/June2018/#sec-Normal-and-Serial-Execution)
Expand Down