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: add normalizedQuery to query plan and request info to trace #1045

Merged
Merged
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions v2/pkg/engine/resolve/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ type PhaseStats struct {
DurationSinceStartPretty string `json:"duration_since_start_pretty"`
}

type requestContextKey struct{}

func SetTraceStart(ctx context.Context, predictableDebugTimings bool) context.Context {
info := &TraceInfo{}
if predictableDebugTimings {
Expand Down Expand Up @@ -267,3 +269,16 @@ func SetPlannerStats(ctx context.Context, stats PhaseStats) {
}
info.PlannerStats = SetDebugStats(info, stats, 4)
}

func GetRequest(ctx context.Context) *RequestData {
// The context might not have trace info, in that case we return nil
req, ok := ctx.Value(requestContextKey{}).(*RequestData)
if !ok {
return nil
}
return req
}

func SetRequest(ctx context.Context, r *RequestData) context.Context {
return context.WithValue(ctx, requestContextKey{}, r)
}
21 changes: 12 additions & 9 deletions v2/pkg/engine/resolve/fetchtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
type FetchTreeNode struct {
Kind FetchTreeNodeKind `json:"kind"`
// Only set for subscription
Trigger *FetchTreeNode `json:"trigger"`
Item *FetchItem `json:"item"`
ChildNodes []*FetchTreeNode `json:"child_nodes"`
Trigger *FetchTreeNode `json:"trigger"`
Item *FetchItem `json:"item"`
ChildNodes []*FetchTreeNode `json:"child_nodes"`
NormalizedQuery string `json:"normalized_query"`
}

type FetchTreeNodeKind string
Expand Down Expand Up @@ -147,11 +148,12 @@ func (n *FetchTreeNode) Trace() *FetchTreeTraceNode {
}

type FetchTreeQueryPlanNode struct {
Version string `json:"version,omitempty"`
Kind FetchTreeNodeKind `json:"kind"`
Trigger *FetchTreeQueryPlan `json:"trigger,omitempty"`
Children []*FetchTreeQueryPlanNode `json:"children,omitempty"`
Fetch *FetchTreeQueryPlan `json:"fetch,omitempty"`
Version string `json:"version,omitempty"`
Kind FetchTreeNodeKind `json:"kind"`
Trigger *FetchTreeQueryPlan `json:"trigger,omitempty"`
Children []*FetchTreeQueryPlanNode `json:"children,omitempty"`
Fetch *FetchTreeQueryPlan `json:"fetch,omitempty"`
NormalizedQuery string `json:"normalizedQuery,omitempty"`
}

type FetchTreeQueryPlan struct {
Expand Down Expand Up @@ -194,7 +196,8 @@ func (n *FetchTreeNode) queryPlan() *FetchTreeQueryPlanNode {
return nil
}
queryPlan := &FetchTreeQueryPlanNode{
Kind: n.Kind,
Kind: n.Kind,
NormalizedQuery: n.NormalizedQuery,
}
switch n.Kind {
case FetchTreeNodeKindSingle:
Expand Down
24 changes: 23 additions & 1 deletion v2/pkg/engine/resolve/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package resolve

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

type TraceOptions struct {
Expand Down Expand Up @@ -59,16 +61,36 @@ func (r *TraceOptions) DisableAll() {
r.IncludeTraceOutputInResponseExtensions = false
}

type BodyData struct {
Query string `json:"query,omitempty"`
OperationName string `json:"operationName,omitempty"`
Variables json.RawMessage `json:"variables,omitempty"`
}

type RequestData struct {
Method string `json:"method"`
URL string `json:"url"`
Headers http.Header `json:"headers"`
Body BodyData `json:"body,omitempty"`
}

type TraceData struct {
Version string `json:"version"`
Info *TraceInfo `json:"info"`
Fetches *FetchTreeTraceNode `json:"fetches"`
Request *RequestData `json:"request,omitempty"`
}

func GetTrace(ctx context.Context, fetchTree *FetchTreeNode) TraceData {
return TraceData{
trace := TraceData{
Version: "1",
Info: GetTraceInfo(ctx),
Fetches: fetchTree.Trace(),
}

if req := GetRequest(ctx); req != nil {
trace.Request = req
}

return trace
}
Loading