Skip to content

Commit

Permalink
Enable tracing of GraphQL requests (guacsec#940)
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin Conner <[email protected]>
Co-authored-by: Kevin Conner <[email protected]>
  • Loading branch information
knrc and knrc authored Jun 13, 2023
1 parent 4afe4f6 commit 41d4ee8
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 1 deletion.
4 changes: 3 additions & 1 deletion cmd/guacgql/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var flags = struct {
backend string
port int
debug bool
tracegql bool
testData bool

// Needed only if using neo4j backend
Expand All @@ -47,6 +48,7 @@ var rootCmd = &cobra.Command{
flags.backend = viper.GetString("gql-backend")
flags.port = viper.GetInt("gql-listen-port")
flags.debug = viper.GetBool("gql-debug")
flags.tracegql = viper.GetBool("gql-trace")
flags.testData = viper.GetBool("gql-test-data")

flags.nUser = viper.GetString("neo4j-user")
Expand All @@ -63,7 +65,7 @@ func init() {

set, err := cli.BuildFlags([]string{
"neo4j-addr", "neo4j-user", "neo4j-pass", "neo4j-realm", "gql-test-data",
"gql-listen-port", "gql-debug", "gql-backend"})
"gql-listen-port", "gql-debug", "gql-backend", "gql-trace"})
if err != nil {
fmt.Fprintf(os.Stderr, "failed to setup flag: %v", err)
os.Exit(1)
Expand Down
7 changes: 7 additions & 0 deletions cmd/guacgql/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"time"

"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/handler/debug"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -55,6 +56,12 @@ func startServer(cmd *cobra.Command) {
logger.Errorf("unable to initialize graphql server: %v", err)
os.Exit(1)
}

if flags.tracegql {
tracer := &debug.Tracer{}
srv.Use(tracer)
}

http.Handle("/query", srv)
if flags.debug {
http.Handle("/", playground.Handler("GraphQL playground", "/query"))
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ require (
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/klauspost/compress v1.16.5 // indirect
github.com/letsencrypt/boulder v0.0.0-20221109233200-85aa52084eaf // indirect
github.com/logrusorgru/aurora/v3 v3.0.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1516,6 +1516,8 @@ github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/linode/linodego v1.4.0/go.mod h1:PVsRxSlOiJyvG4/scTszpmZDTdgS+to3X6eS8pRrWI8=
github.com/linode/linodego v1.12.0/go.mod h1:NJlzvlNtdMRRkXb0oN6UWzUkj6t+IBsyveHgZ5Ppjyk=
github.com/linuxkit/virtsock v0.0.0-20201010232012-f8cee7dfc7a3/go.mod h1:3r6x7q95whyfWQpmGZTu3gk3v2YkMi05HEzl7Tf7YEo=
github.com/logrusorgru/aurora/v3 v3.0.0 h1:R6zcoZZbvVcGMvDCKo45A9U/lzYyzl5NfYIvznmDfE4=
github.com/logrusorgru/aurora/v3 v3.0.0/go.mod h1:vsR12bk5grlLvLXAYrBsb5Oc/N+LxAlxggSjiwMnCUc=
github.com/lyft/protoc-gen-star v0.6.0/go.mod h1:TGAoBVkt8w7MPG72TrKIu85MIdXwDuzJYeZuUPFPNwA=
github.com/lyft/protoc-gen-star v0.6.1/go.mod h1:TGAoBVkt8w7MPG72TrKIu85MIdXwDuzJYeZuUPFPNwA=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
Expand Down
1 change: 1 addition & 0 deletions pkg/cli/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func init() {
set.String("gql-backend", "inmem", "backend used for graphql api server: [neo4j | inmem]")
set.Int("gql-listen-port", 8080, "port used for graphql api server")
set.Bool("gql-debug", false, "debug flag which enables the graphQL playground")
set.Bool("gql-trace", false, "flag which enables tracing of graphQL requests and responses on the console")
set.Bool("gql-test-data", false, "Populate backend with test data")

set.String("neo4j-addr", "neo4j://localhost:7687", "address to neo4j db")
Expand Down

0 comments on commit 41d4ee8

Please sign in to comment.