Skip to content

Commit

Permalink
Merge pull request #89 from vektah/opentracing-parent-span
Browse files Browse the repository at this point in the history
 Add parent opentracing span around root query/mutation/resolvers
  • Loading branch information
vektah authored Apr 23, 2018
2 parents 877f75a + d157ac3 commit ab3803a
Show file tree
Hide file tree
Showing 23 changed files with 324 additions and 173 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/internal/tests/testdata/graphql-js
/vendor
/docs/public
/example/chat/node_modules
/example/chat/package-lock.json
2 changes: 1 addition & 1 deletion .gometalinter.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sort": ["path"],
"Deadline": "2m",
"Deadline": "5m",
"Linters": {
"errcheck": {
"Command": "errcheck -abspath -ignore '[rR]ead|[wW]rite|Close'",
Expand Down
4 changes: 2 additions & 2 deletions codegen/templates/data.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions codegen/templates/field.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
return graphql.Defer(func() (ret graphql.Marshaler) {
defer func() {
if r := recover(); r != nil {
userErr := ec.Recover(r)
userErr := ec.Recover(ctx, r)
ec.Error(userErr)
ret = graphql.Null
}
Expand All @@ -53,7 +53,7 @@
Args: {{if $field.Args }}args{{else}}nil{{end}},
Field: field,
})
resTmp, err := ec.Middleware(rctx, func(rctx context.Context) (interface{}, error) {
resTmp, err := ec.ResolverMiddleware(rctx, func(rctx context.Context) (interface{}, error) {
return ec.resolvers.{{ $object.GQLType }}_{{ $field.GQLName }}({{ $field.CallArgs }})
})
if err != nil {
Expand Down
44 changes: 26 additions & 18 deletions codegen/templates/generated.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ func (e *executableSchema) Query(ctx context.Context, op *query.Operation) *grap
{{- if .QueryRoot }}
ec := executionContext{graphql.GetRequestContext(ctx), e.resolvers}

data := ec._{{.QueryRoot.GQLType}}(ctx, op.Selections)
var buf bytes.Buffer
data.MarshalGQL(&buf)
buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {
data := ec._{{.QueryRoot.GQLType}}(ctx, op.Selections)
var buf bytes.Buffer
data.MarshalGQL(&buf)
return buf.Bytes()
})

return &graphql.Response{
Data: buf.Bytes(),
Data: buf,
Errors: ec.Errors,
}
{{- else }}
Expand All @@ -49,12 +52,15 @@ func (e *executableSchema) Mutation(ctx context.Context, op *query.Operation) *g
{{- if .MutationRoot }}
ec := executionContext{graphql.GetRequestContext(ctx), e.resolvers}

data := ec._{{.MutationRoot.GQLType}}(ctx, op.Selections)
var buf bytes.Buffer
data.MarshalGQL(&buf)
buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {
data := ec._{{.MutationRoot.GQLType}}(ctx, op.Selections)
var buf bytes.Buffer
data.MarshalGQL(&buf)
return buf.Bytes()
})

return &graphql.Response{
Data: buf.Bytes(),
Data: buf,
Errors: ec.Errors,
}
{{- else }}
Expand All @@ -73,18 +79,20 @@ func (e *executableSchema) Subscription(ctx context.Context, op *query.Operation

var buf bytes.Buffer
return func() *graphql.Response {
buf.Reset()
data := next()
if data == nil {
return nil
}
data.MarshalGQL(&buf)
buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {
buf.Reset()
data := next()

if data == nil {
return nil
}
data.MarshalGQL(&buf)
return buf.Bytes()
})

errs := ec.Errors
ec.Errors = nil
return &graphql.Response{
Data: buf.Bytes(),
Errors: errs,
Data: buf,
Errors: ec.Errors,
}
}
{{- else }}
Expand Down
50 changes: 29 additions & 21 deletions example/chat/generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,31 @@ func (e *executableSchema) Schema() *schema.Schema {
func (e *executableSchema) Query(ctx context.Context, op *query.Operation) *graphql.Response {
ec := executionContext{graphql.GetRequestContext(ctx), e.resolvers}

data := ec._Query(ctx, op.Selections)
var buf bytes.Buffer
data.MarshalGQL(&buf)
buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {
data := ec._Query(ctx, op.Selections)
var buf bytes.Buffer
data.MarshalGQL(&buf)
return buf.Bytes()
})

return &graphql.Response{
Data: buf.Bytes(),
Data: buf,
Errors: ec.Errors,
}
}

func (e *executableSchema) Mutation(ctx context.Context, op *query.Operation) *graphql.Response {
ec := executionContext{graphql.GetRequestContext(ctx), e.resolvers}

data := ec._Mutation(ctx, op.Selections)
var buf bytes.Buffer
data.MarshalGQL(&buf)
buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {
data := ec._Mutation(ctx, op.Selections)
var buf bytes.Buffer
data.MarshalGQL(&buf)
return buf.Bytes()
})

return &graphql.Response{
Data: buf.Bytes(),
Data: buf,
Errors: ec.Errors,
}
}
Expand All @@ -68,18 +74,20 @@ func (e *executableSchema) Subscription(ctx context.Context, op *query.Operation

var buf bytes.Buffer
return func() *graphql.Response {
buf.Reset()
data := next()
if data == nil {
return nil
}
data.MarshalGQL(&buf)
buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {
buf.Reset()
data := next()

if data == nil {
return nil
}
data.MarshalGQL(&buf)
return buf.Bytes()
})

errs := ec.Errors
ec.Errors = nil
return &graphql.Response{
Data: buf.Bytes(),
Errors: errs,
Data: buf,
Errors: ec.Errors,
}
}
}
Expand Down Expand Up @@ -235,7 +243,7 @@ func (ec *executionContext) _Mutation_post(ctx context.Context, field graphql.Co
Args: args,
Field: field,
})
resTmp, err := ec.Middleware(rctx, func(rctx context.Context) (interface{}, error) {
resTmp, err := ec.ResolverMiddleware(rctx, func(rctx context.Context) (interface{}, error) {
return ec.resolvers.Mutation_post(rctx, args["text"].(string), args["username"].(string), args["roomName"].(string))
})
if err != nil {
Expand Down Expand Up @@ -290,7 +298,7 @@ func (ec *executionContext) _Query_room(ctx context.Context, field graphql.Colle
return graphql.Defer(func() (ret graphql.Marshaler) {
defer func() {
if r := recover(); r != nil {
userErr := ec.Recover(r)
userErr := ec.Recover(ctx, r)
ec.Error(userErr)
ret = graphql.Null
}
Expand All @@ -300,7 +308,7 @@ func (ec *executionContext) _Query_room(ctx context.Context, field graphql.Colle
Args: args,
Field: field,
})
resTmp, err := ec.Middleware(rctx, func(rctx context.Context) (interface{}, error) {
resTmp, err := ec.ResolverMiddleware(rctx, func(rctx context.Context) (interface{}, error) {
return ec.resolvers.Query_room(rctx, args["name"].(string))
})
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions example/chat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"dependencies": {
"apollo-cache-inmemory": "^1.1.9",
"apollo-client": "^2.2.5",
"graphql-tag": "^2.9.1",
"graphql": "^0.13.2",
"react": "^16.2.0",
"react-apollo": "^2.1.0-beta.2",
"react-dom": "^16.2.0",
Expand Down
43 changes: 43 additions & 0 deletions example/chat/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,26 @@ package main
import (
"log"
"net/http"
"net/url"
"time"

"github.com/gorilla/websocket"
"github.com/opentracing/opentracing-go"
"github.com/vektah/gqlgen/example/chat"
"github.com/vektah/gqlgen/handler"
gqlopentracing "github.com/vektah/gqlgen/opentracing"
"sourcegraph.com/sourcegraph/appdash"
appdashtracer "sourcegraph.com/sourcegraph/appdash/opentracing"
"sourcegraph.com/sourcegraph/appdash/traceapp"
)

func main() {
startAppdashServer()

http.Handle("/", handler.Playground("Todo", "/query"))
http.Handle("/query", handler.GraphQL(chat.MakeExecutableSchema(chat.New()),
handler.ResolverMiddleware(gqlopentracing.ResolverMiddleware()),
handler.RequestMiddleware(gqlopentracing.RequestMiddleware()),
handler.WebsocketUpgrader(websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
Expand All @@ -20,3 +31,35 @@ func main() {
)
log.Fatal(http.ListenAndServe(":8085", nil))
}

func startAppdashServer() opentracing.Tracer {
memStore := appdash.NewMemoryStore()
store := &appdash.RecentStore{
MinEvictAge: 5 * time.Minute,
DeleteStore: memStore,
}

url, err := url.Parse("http://localhost:8700")
if err != nil {
log.Fatal(err)
}
tapp, err := traceapp.New(nil, url)
if err != nil {
log.Fatal(err)
}
tapp.Store = store
tapp.Queryer = memStore

go func() {
log.Fatal(http.ListenAndServe(":8700", tapp))
}()
tapp.Store = store
tapp.Queryer = memStore

collector := appdash.NewLocalCollector(store)
tracer := appdashtracer.NewTracer(collector)
opentracing.InitGlobalTracer(tracer)

log.Println("Appdash web UI running on HTTP :8700")
return tracer
}
31 changes: 17 additions & 14 deletions example/dataloader/generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ func (e *executableSchema) Schema() *schema.Schema {
func (e *executableSchema) Query(ctx context.Context, op *query.Operation) *graphql.Response {
ec := executionContext{graphql.GetRequestContext(ctx), e.resolvers}

data := ec._Query(ctx, op.Selections)
var buf bytes.Buffer
data.MarshalGQL(&buf)
buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {
data := ec._Query(ctx, op.Selections)
var buf bytes.Buffer
data.MarshalGQL(&buf)
return buf.Bytes()
})

return &graphql.Response{
Data: buf.Bytes(),
Data: buf,
Errors: ec.Errors,
}
}
Expand Down Expand Up @@ -145,7 +148,7 @@ func (ec *executionContext) _Customer_address(ctx context.Context, field graphql
return graphql.Defer(func() (ret graphql.Marshaler) {
defer func() {
if r := recover(); r != nil {
userErr := ec.Recover(r)
userErr := ec.Recover(ctx, r)
ec.Error(userErr)
ret = graphql.Null
}
Expand All @@ -155,7 +158,7 @@ func (ec *executionContext) _Customer_address(ctx context.Context, field graphql
Args: nil,
Field: field,
})
resTmp, err := ec.Middleware(rctx, func(rctx context.Context) (interface{}, error) {
resTmp, err := ec.ResolverMiddleware(rctx, func(rctx context.Context) (interface{}, error) {
return ec.resolvers.Customer_address(rctx, obj)
})
if err != nil {
Expand All @@ -177,7 +180,7 @@ func (ec *executionContext) _Customer_orders(ctx context.Context, field graphql.
return graphql.Defer(func() (ret graphql.Marshaler) {
defer func() {
if r := recover(); r != nil {
userErr := ec.Recover(r)
userErr := ec.Recover(ctx, r)
ec.Error(userErr)
ret = graphql.Null
}
Expand All @@ -187,7 +190,7 @@ func (ec *executionContext) _Customer_orders(ctx context.Context, field graphql.
Args: nil,
Field: field,
})
resTmp, err := ec.Middleware(rctx, func(rctx context.Context) (interface{}, error) {
resTmp, err := ec.ResolverMiddleware(rctx, func(rctx context.Context) (interface{}, error) {
return ec.resolvers.Customer_orders(rctx, obj)
})
if err != nil {
Expand Down Expand Up @@ -280,7 +283,7 @@ func (ec *executionContext) _Order_items(ctx context.Context, field graphql.Coll
return graphql.Defer(func() (ret graphql.Marshaler) {
defer func() {
if r := recover(); r != nil {
userErr := ec.Recover(r)
userErr := ec.Recover(ctx, r)
ec.Error(userErr)
ret = graphql.Null
}
Expand All @@ -290,7 +293,7 @@ func (ec *executionContext) _Order_items(ctx context.Context, field graphql.Coll
Args: nil,
Field: field,
})
resTmp, err := ec.Middleware(rctx, func(rctx context.Context) (interface{}, error) {
resTmp, err := ec.ResolverMiddleware(rctx, func(rctx context.Context) (interface{}, error) {
return ec.resolvers.Order_items(rctx, obj)
})
if err != nil {
Expand Down Expand Up @@ -341,7 +344,7 @@ func (ec *executionContext) _Query_customers(ctx context.Context, field graphql.
return graphql.Defer(func() (ret graphql.Marshaler) {
defer func() {
if r := recover(); r != nil {
userErr := ec.Recover(r)
userErr := ec.Recover(ctx, r)
ec.Error(userErr)
ret = graphql.Null
}
Expand All @@ -351,7 +354,7 @@ func (ec *executionContext) _Query_customers(ctx context.Context, field graphql.
Args: nil,
Field: field,
})
resTmp, err := ec.Middleware(rctx, func(rctx context.Context) (interface{}, error) {
resTmp, err := ec.ResolverMiddleware(rctx, func(rctx context.Context) (interface{}, error) {
return ec.resolvers.Query_customers(rctx)
})
if err != nil {
Expand Down Expand Up @@ -393,7 +396,7 @@ func (ec *executionContext) _Query_torture(ctx context.Context, field graphql.Co
return graphql.Defer(func() (ret graphql.Marshaler) {
defer func() {
if r := recover(); r != nil {
userErr := ec.Recover(r)
userErr := ec.Recover(ctx, r)
ec.Error(userErr)
ret = graphql.Null
}
Expand All @@ -403,7 +406,7 @@ func (ec *executionContext) _Query_torture(ctx context.Context, field graphql.Co
Args: args,
Field: field,
})
resTmp, err := ec.Middleware(rctx, func(rctx context.Context) (interface{}, error) {
resTmp, err := ec.ResolverMiddleware(rctx, func(rctx context.Context) (interface{}, error) {
return ec.resolvers.Query_torture(rctx, args["customerIds"].([][]int))
})
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion example/dataloader/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ func main() {
router.Use(dataloader.LoaderMiddleware)

router.Handle("/", handler.Playground("Dataloader", "/query"))
router.Handle("/query", handler.GraphQL(dataloader.MakeExecutableSchema(&dataloader.Resolver{}), handler.Use(gqlopentracing.Middleware())))
router.Handle("/query", handler.GraphQL(
dataloader.MakeExecutableSchema(&dataloader.Resolver{}),
handler.ResolverMiddleware(gqlopentracing.ResolverMiddleware()),
handler.RequestMiddleware(gqlopentracing.RequestMiddleware()),
))

log.Println("connect to http://localhost:8082/ for graphql playground")
log.Fatal(http.ListenAndServe(":8082", router))
Expand Down
Loading

0 comments on commit ab3803a

Please sign in to comment.