Skip to content

Commit

Permalink
Add SQL execution timing and tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
dosco committed Apr 1, 2019
1 parent 8ac8088 commit d66fb19
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@

.vscode
main
.DS_Store
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ Configuration files can either be in YAML or JSON their names are derived from t
host_port: 0.0.0.0:8080
web_ui: true
debug_level: 1
enable_tracing: true

# When to throw a 401 on auth failure
# valid values: always, per_query, never
Expand Down
1 change: 1 addition & 0 deletions dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ title: Super Graph Development
host_port: 0.0.0.0:8080
web_ui: true
debug_level: 1
enable_tracing: true

# Throw a 401 on auth failure for queries that need auth
# valid values: always, per_query, never
Expand Down
1 change: 1 addition & 0 deletions prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ title: Super Graph Production
host_port: 0.0.0.0:8080
web_ui: false
debug_level: 0
enable_tracing: false
auth_fail_block: always

# Postgres related environment Variables
Expand Down
68 changes: 65 additions & 3 deletions serv/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"io/ioutil"
"net/http"
"strings"
"time"

"github.com/dosco/super-graph/qcode"
"github.com/go-pg/pg"
"github.com/gorilla/websocket"
"github.com/valyala/fasttemplate"
Expand All @@ -33,8 +35,34 @@ type gqlReq struct {
}

type gqlResp struct {
Error string `json:"error,omitempty"`
Data json.RawMessage `json:"data,omitempty"`
Error string `json:"error,omitempty"`
Data json.RawMessage `json:"data,omitempty"`
Extensions extensions `json:"extensions"`
}

type extensions struct {
Tracing *trace `json:"tracing"`
}

type trace struct {
Version int `json:"version"`
StartTime time.Time `json:"startTime"`
EndTime time.Time `json:"endTime"`
Duration time.Duration `json:"duration"`
Execution execution `json:"execution"`
}

type execution struct {
Resolvers []resolver `json:"resolvers"`
}

type resolver struct {
Path []string `json:"path"`
ParentType string `json:"parentType"`
FieldName string `json:"fieldName"`
ReturnType string `json:"returnType"`
StartOffset int `json:"startOffset"`
Duration time.Duration `json:"duration"`
}

func apiv1Http(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -99,6 +127,7 @@ func apiv1Http(w http.ResponseWriter, r *http.Request) {
if debug > 0 {
fmt.Println(finalSQL)
}
st := time.Now()

var root json.RawMessage
_, err = db.Query(pg.Scan(&root), finalSQL)
Expand All @@ -108,7 +137,15 @@ func apiv1Http(w http.ResponseWriter, r *http.Request) {
return
}

json.NewEncoder(w).Encode(gqlResp{Data: json.RawMessage(root)})
et := time.Now()
resp := gqlResp{}

if tracing {
resp.Extensions = extensions{newTrace(st, et, qc)}
}

resp.Data = json.RawMessage(root)
json.NewEncoder(w).Encode(resp)
}

/*
Expand Down Expand Up @@ -174,3 +211,28 @@ func varValues(ctx context.Context) map[string]interface{} {
"user_id_provider": uidpFn,
}
}

func newTrace(st, et time.Time, qc *qcode.QCode) *trace {
du := et.Sub(et)

t := &trace{
Version: 1,
StartTime: st,
EndTime: et,
Duration: du,
Execution: execution{
[]resolver{
resolver{
Path: []string{qc.Query.Select.Table},
ParentType: "Query",
FieldName: qc.Query.Select.Table,
ReturnType: "object",
StartOffset: 1,
Duration: du,
},
},
},
}

return t
}
4 changes: 4 additions & 0 deletions serv/serv.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var (
pcompile *psql.Compiler
qcompile *qcode.Compiler
authFailBlock int
tracing bool
)

func initLog() {
Expand Down Expand Up @@ -74,6 +75,7 @@ func initConf() {
conf.SetDefault("host_port", "0.0.0.0:8080")
conf.SetDefault("web_ui", false)
conf.SetDefault("debug_level", 0)
conf.SetDefault("enable_tracing", false)

conf.SetDefault("database.type", "postgres")
conf.SetDefault("database.host", "localhost")
Expand All @@ -84,6 +86,8 @@ func initConf() {
conf.SetDefault("env", "development")
conf.BindEnv("env", "GO_ENV")

tracing = conf.GetBool("enable_tracing")

switch conf.GetString("auth_fail_block") {
case "always":
authFailBlock = authFailBlockAlways
Expand Down
Binary file modified web/public/super-graph-web-ui.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d66fb19

Please sign in to comment.