Skip to content

Commit

Permalink
Fix(GraphQL): Fix Execution Trace for Add and Update Mutations (#7656) (
Browse files Browse the repository at this point in the history
#7658)

* Remove unused function

* Add tracing for existence query execution

* Fix tests

(cherry picked from commit ba8f5da)
  • Loading branch information
vmrajas authored Mar 26, 2021
1 parent 0f0eab8 commit a79d379
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
8 changes: 4 additions & 4 deletions graphql/resolve/extensions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ func TestMutationsPropagateExtensions(t *testing.T) {
require.True(t, resp.Extensions.Tracing.Execution.Resolvers[0].StartOffset > 0)
require.True(t, resp.Extensions.Tracing.Execution.Resolvers[0].Duration > 0)

require.Len(t, resp.Extensions.Tracing.Execution.Resolvers[0].Dgraph, 2)
labels := []string{"mutation", "query"}
require.Len(t, resp.Extensions.Tracing.Execution.Resolvers[0].Dgraph, 3)
labels := []string{"preMutationQuery", "mutation", "query"}
for i, dgraphTrace := range resp.Extensions.Tracing.Execution.Resolvers[0].Dgraph {
require.Equal(t, dgraphTrace.Label, labels[i])
require.True(t, dgraphTrace.StartOffset > 0)
Expand Down Expand Up @@ -221,8 +221,8 @@ func TestMultipleMutationsPropagateExtensionsCorrectly(t *testing.T) {
require.True(t, resolver.StartOffset > 0)
require.True(t, resolver.Duration > 0)

require.Len(t, resolver.Dgraph, 2)
labels := []string{"mutation", "query"}
require.Len(t, resolver.Dgraph, 3)
labels := []string{"preMutationQuery", "mutation", "query"}
for j, dgraphTrace := range resolver.Dgraph {
require.Equal(t, dgraphTrace.Label, labels[j])
require.True(t, dgraphTrace.StartOffset > 0)
Expand Down
23 changes: 15 additions & 8 deletions graphql/resolve/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,16 +229,18 @@ func (mr *dgraphResolver) rewriteAndExecute(
}
}()

dgraphPreMutationQueryDuration := &schema.LabeledOffsetDuration{Label: "preMutationQuery"}
dgraphMutationDuration := &schema.LabeledOffsetDuration{Label: "mutation"}
dgraphQueryDuration := &schema.LabeledOffsetDuration{Label: "query"}
dgraphPostMutationQueryDuration := &schema.LabeledOffsetDuration{Label: "query"}
ext := &schema.Extensions{
Tracing: &schema.Trace{
Execution: &schema.ExecutionTrace{
Resolvers: []*schema.ResolverTrace{
{
Dgraph: []*schema.LabeledOffsetDuration{
dgraphPreMutationQueryDuration,
dgraphMutationDuration,
dgraphQueryDuration,
dgraphPostMutationQueryDuration,
},
},
},
Expand Down Expand Up @@ -275,12 +277,17 @@ func (mr *dgraphResolver) rewriteAndExecute(
// Don't execute the query in those cases.
// The query will also be empty in case this is not an Add or an Update Mutation.
if req.Query != "" {
// Executing and processing existence queries
queryTimer := newtimer(ctx, &dgraphPreMutationQueryDuration.OffsetDuration)
queryTimer.Start()
mutResp, err = mr.executor.Execute(ctx, req)
}
if err != nil {
gqlErr := schema.GQLWrapLocationf(
err, mutation.Location(), "mutation %s failed", mutation.Name())
return emptyResult(gqlErr), resolverFailed
queryTimer.Stop()
if err != nil {
gqlErr := schema.GQLWrapLocationf(
err, mutation.Location(), "mutation %s failed", mutation.Name())
return emptyResult(gqlErr), resolverFailed
}
ext.TouchedUids += mutResp.GetMetrics().GetNumUids()[touchedUidsKey]
}

// Parse the result of query.
Expand Down Expand Up @@ -405,7 +412,7 @@ func (mr *dgraphResolver) rewriteAndExecute(
}
commit = true

queryTimer := newtimer(ctx, &dgraphQueryDuration.OffsetDuration)
queryTimer := newtimer(ctx, &dgraphPostMutationQueryDuration.OffsetDuration)
queryTimer.Start()
qryResp, err := mr.executor.Execute(ctx, &dgoapi.Request{Query: dgraph.AsString(dgQuery),
ReadOnly: true})
Expand Down

0 comments on commit a79d379

Please sign in to comment.