Skip to content
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

feat(GraphQL): Adds Apollo tracing to GraphQL extensions. (#5692) #5855

Merged
merged 1 commit into from
Jul 7, 2020
Merged
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
feat(GraphQL): Adds Apollo tracing to GraphQL extensions. (#5692)
* feat(GraphQL): Adds Apollo tracing to graphql extensions. (#5541)
* feat(GraphQl): Adds "--graphql_extensions" flag to alpha for turning extensions on/off.  (#5677)

(cherry picked from commit ad0914a)
JatinDev543 committed Jul 7, 2020
commit 81b7620f9f9e45dcb0f31b7ade3038c72ed39d38
3 changes: 2 additions & 1 deletion dgraph/cmd/alpha/run.go
Original file line number Diff line number Diff line change
@@ -46,7 +46,6 @@ import (
"github.com/dgraph-io/dgraph/tok"
"github.com/dgraph-io/dgraph/worker"
"github.com/dgraph-io/dgraph/x"

"github.com/golang/glog"
"github.com/pkg/errors"
"github.com/spf13/cast"
@@ -196,6 +195,7 @@ they form a Raft group and provide synchronous replication.

flag.Bool("graphql_introspection", true, "Set to false for no GraphQL schema introspection")
flag.Bool("ludicrous_mode", false, "Run alpha in ludicrous mode")
flag.Bool("graphql_extensions", true, "Set to false if extensions not required in GraphQL response body")
flag.Duration("graphql_poll_interval", time.Second, "polling interval for graphql subscription.")
}

@@ -636,6 +636,7 @@ func run() {
x.Config.QueryEdgeLimit = cast.ToUint64(Alpha.Conf.GetString("query_edge_limit"))
x.Config.NormalizeNodeLimit = cast.ToInt(Alpha.Conf.GetString("normalize_node_limit"))
x.Config.PollInterval = Alpha.Conf.GetDuration("graphql_poll_interval")
x.Config.GraphqlExtension = Alpha.Conf.GetBool("graphql_extensions")

x.PrintVersion()
glog.Infof("x.Config: %+v", x.Config)
228 changes: 228 additions & 0 deletions graphql/resolve/extensions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
/*
* Copyright 2020 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package resolve

import (
"testing"
"time"

"github.com/dgraph-io/dgraph/graphql/test"
"github.com/stretchr/testify/require"
)

func TestQueriesPropagateExtensions(t *testing.T) {
gqlSchema := test.LoadSchemaFromString(t, testGQLSchema)
query := `
query {
getAuthor(id: "0x1") {
name
}
}`

resp := resolveWithClient(gqlSchema, query, nil,
&executor{
queryTouched: 2,
mutationTouched: 5,
})

require.NotNil(t, resp)
require.Nil(t, resp.Errors)
require.NotNil(t, resp.Extensions)

require.Equal(t, uint64(2), resp.Extensions.TouchedUids)
require.NotNil(t, resp.Extensions.Tracing)

require.Equal(t, resp.Extensions.Tracing.Version, 1)
_, err := time.Parse(time.RFC3339Nano, resp.Extensions.Tracing.StartTime)
require.NoError(t, err)
_, err = time.Parse(time.RFC3339Nano, resp.Extensions.Tracing.EndTime)
require.NoError(t, err)
require.True(t, resp.Extensions.Tracing.Duration > 0)
require.NotNil(t, resp.Extensions.Tracing.Execution)

require.Len(t, resp.Extensions.Tracing.Execution.Resolvers, 1)
require.Equal(t, resp.Extensions.Tracing.Execution.Resolvers[0].Path, []interface{}{"getAuthor"})
require.Equal(t, resp.Extensions.Tracing.Execution.Resolvers[0].ParentType, "Query")
require.Equal(t, resp.Extensions.Tracing.Execution.Resolvers[0].FieldName, "getAuthor")
require.Equal(t, resp.Extensions.Tracing.Execution.Resolvers[0].ReturnType, "Author")
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, 1)
require.Equal(t, resp.Extensions.Tracing.Execution.Resolvers[0].Dgraph[0].Label, "query")
require.True(t, resp.Extensions.Tracing.Execution.Resolvers[0].Dgraph[0].StartOffset > 0)
require.True(t, resp.Extensions.Tracing.Execution.Resolvers[0].Dgraph[0].Duration > 0)

}

func TestMultipleQueriesPropagateExtensionsCorrectly(t *testing.T) {
gqlSchema := test.LoadSchemaFromString(t, testGQLSchema)
query := `
query {
a: getAuthor(id: "0x1") {
name
}
b: getAuthor(id: "0x2") {
name
}
c: getAuthor(id: "0x3") {
name
}
}`

resp := resolveWithClient(gqlSchema, query, nil,
&executor{
queryTouched: 2,
mutationTouched: 5,
})

require.NotNil(t, resp)
require.Nil(t, resp.Errors)
require.NotNil(t, resp.Extensions)

require.Equal(t, uint64(6), resp.Extensions.TouchedUids)
require.NotNil(t, resp.Extensions.Tracing)

require.Equal(t, resp.Extensions.Tracing.Version, 1)
_, err := time.Parse(time.RFC3339Nano, resp.Extensions.Tracing.StartTime)
require.NoError(t, err)
_, err = time.Parse(time.RFC3339Nano, resp.Extensions.Tracing.EndTime)
require.NoError(t, err)
require.True(t, resp.Extensions.Tracing.Duration > 0)
require.NotNil(t, resp.Extensions.Tracing.Execution)

require.Len(t, resp.Extensions.Tracing.Execution.Resolvers, 3)
aliases := []string{"a", "b", "c"}
for i, resolver := range resp.Extensions.Tracing.Execution.Resolvers {
require.Equal(t, resolver.Path, []interface{}{aliases[i]})
require.Equal(t, resolver.ParentType, "Query")
require.Equal(t, resolver.FieldName, aliases[i])
require.Equal(t, resolver.ReturnType, "Author")
require.True(t, resolver.StartOffset > 0)
require.True(t, resolver.Duration > 0)
require.Len(t, resolver.Dgraph, 1)
require.Equal(t, resolver.Dgraph[0].Label, "query")
require.True(t, resolver.Dgraph[0].StartOffset > 0)
require.True(t, resolver.Dgraph[0].Duration > 0)
}
}

func TestMutationsPropagateExtensions(t *testing.T) {
gqlSchema := test.LoadSchemaFromString(t, testGQLSchema)
mutation := `mutation {
addPost(input: [{title: "A Post", author: {id: "0x1"}}]) {
post {
title
}
}
}`

resp := resolveWithClient(gqlSchema, mutation, nil,
&executor{
queryTouched: 2,
mutationTouched: 5,
})

require.NotNil(t, resp)
require.Nil(t, resp.Errors)
require.NotNil(t, resp.Extensions)

// as both .Mutate() and .Query() should get called, so we should get their merged result
require.Equal(t, uint64(7), resp.Extensions.TouchedUids)
require.NotNil(t, resp.Extensions.Tracing)

require.Equal(t, resp.Extensions.Tracing.Version, 1)
_, err := time.Parse(time.RFC3339Nano, resp.Extensions.Tracing.StartTime)
require.NoError(t, err)
_, err = time.Parse(time.RFC3339Nano, resp.Extensions.Tracing.EndTime)
require.NoError(t, err)
require.True(t, resp.Extensions.Tracing.Duration > 0)
require.NotNil(t, resp.Extensions.Tracing.Execution)

require.Len(t, resp.Extensions.Tracing.Execution.Resolvers, 1)
require.Equal(t, resp.Extensions.Tracing.Execution.Resolvers[0].Path, []interface{}{"addPost"})
require.Equal(t, resp.Extensions.Tracing.Execution.Resolvers[0].ParentType, "Mutation")
require.Equal(t, resp.Extensions.Tracing.Execution.Resolvers[0].FieldName, "addPost")
require.Equal(t, resp.Extensions.Tracing.Execution.Resolvers[0].ReturnType, "AddPostPayload")
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"}
for i, dgraphTrace := range resp.Extensions.Tracing.Execution.Resolvers[0].Dgraph {
require.Equal(t, dgraphTrace.Label, labels[i])
require.True(t, dgraphTrace.StartOffset > 0)
require.True(t, dgraphTrace.Duration > 0)
}
}

func TestMultipleMutationsPropagateExtensionsCorrectly(t *testing.T) {
gqlSchema := test.LoadSchemaFromString(t, testGQLSchema)
mutation := `mutation {
a: addPost(input: [{title: "A Post", author: {id: "0x1"}}]) {
post {
title
}
}
b: addPost(input: [{title: "A Post", author: {id: "0x2"}}]) {
post {
title
}
}
}`

resp := resolveWithClient(gqlSchema, mutation, nil,
&executor{
queryTouched: 2,
mutationTouched: 5,
})

require.NotNil(t, resp)
require.Nil(t, resp.Errors)
require.NotNil(t, resp.Extensions)

// as both .Mutate() and .Query() should get called, so we should get their merged result
require.Equal(t, uint64(14), resp.Extensions.TouchedUids)
require.NotNil(t, resp.Extensions.Tracing)

require.Equal(t, resp.Extensions.Tracing.Version, 1)
_, err := time.Parse(time.RFC3339Nano, resp.Extensions.Tracing.StartTime)
require.NoError(t, err)
_, err = time.Parse(time.RFC3339Nano, resp.Extensions.Tracing.EndTime)
require.NoError(t, err)
require.True(t, resp.Extensions.Tracing.Duration > 0)
require.NotNil(t, resp.Extensions.Tracing.Execution)

require.Len(t, resp.Extensions.Tracing.Execution.Resolvers, 2)
aliases := []string{"a", "b"}
for i, resolver := range resp.Extensions.Tracing.Execution.Resolvers {
require.Equal(t, resolver.Path, []interface{}{aliases[i]})
require.Equal(t, resolver.ParentType, "Mutation")
require.Equal(t, resolver.FieldName, aliases[i])
require.Equal(t, resolver.ReturnType, "AddPostPayload")
require.True(t, resolver.StartOffset > 0)
require.True(t, resolver.Duration > 0)

require.Len(t, resolver.Dgraph, 2)
labels := []string{"mutation", "query"}
for j, dgraphTrace := range resolver.Dgraph {
require.Equal(t, dgraphTrace.Label, labels[j])
require.True(t, dgraphTrace.StartOffset > 0)
require.True(t, dgraphTrace.Duration > 0)
}
}
}
Loading