Skip to content

Commit

Permalink
opentracing middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Scarr committed Apr 5, 2018
1 parent 57adb24 commit 5b90850
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 5 deletions.
59 changes: 57 additions & 2 deletions example/dataloader/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,70 @@ package main
import (
"log"
"net/http"
"net/url"
"time"

"github.com/go-chi/chi"
"github.com/opentracing-contrib/go-stdlib/nethttp"
"github.com/opentracing/opentracing-go"
"github.com/vektah/gqlgen/example/dataloader"
"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() {

http.Handle("/", handler.Playground("Dataloader", "/query"))

http.Handle("/query", dataloader.LoaderMiddleware(handler.GraphQL(dataloader.MakeExecutableSchema(&dataloader.Resolver{}))))
tracer := startAppdashServer()

router := chi.NewRouter()
router.Use(Opentracing(tracer))
router.Use(dataloader.LoaderMiddleware)

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

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

func Opentracing(tracer opentracing.Tracer) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return nethttp.Middleware(tracer, next)
}
}

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.Fatal(http.ListenAndServe(":8082", nil))
log.Println("Appdash web UI running on HTTP :8700")
return tracer
}
4 changes: 1 addition & 3 deletions handler/graphql.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package handler

import (
"context"
"encoding/json"
"fmt"
"net/http"

"strings"

"context"

"github.com/gorilla/websocket"
"github.com/vektah/gqlgen/graphql"
"github.com/vektah/gqlgen/neelance/errors"
Expand Down
36 changes: 36 additions & 0 deletions opentracing/opentracing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package opentracing

import (
"context"
"fmt"

"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/opentracing/opentracing-go/log"
"github.com/vektah/gqlgen/graphql"
)

func Middleware() graphql.ResolverMiddleware {
return func(ctx context.Context, next graphql.Resolver) (interface{}, error) {
rctx := graphql.GetResolverContext(ctx)

fmt.Println("SPAN", rctx.Object+"_"+rctx.Field.Name)
span, ctx := opentracing.StartSpanFromContext(ctx, rctx.Object+"_"+rctx.Field.Name)
defer span.Finish()

res, err := next(ctx)

span.LogFields(
log.String("object", rctx.Object),
log.String("name", rctx.Field.Name),
log.String("alias", rctx.Field.Alias),
)

if err != nil {
ext.Error.Set(span, true)
span.LogFields(log.String("error", err.Error()))
}

return res, err
}
}
3 changes: 3 additions & 0 deletions test/resolvers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ func TestErrorConverter(t *testing.T) {
func mkctx(doc *query.Document, errFn func(e error) string) context.Context {
return graphql.WithRequestContext(context.Background(), &graphql.RequestContext{
Doc: doc,
Middleware: func(ctx context.Context, next graphql.Resolver) (res interface{}, err error) {
return next(ctx)
},
Builder: gqlerrors.Builder{
ErrorMessageFn: errFn,
},
Expand Down

0 comments on commit 5b90850

Please sign in to comment.