-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved performance while keeping flexibility.
- Loading branch information
Showing
3 changed files
with
99 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package trace | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/neelance/graphql-go/errors" | ||
opentracing "github.com/opentracing/opentracing-go" | ||
"github.com/opentracing/opentracing-go/ext" | ||
) | ||
|
||
type TraceQueryFinishFunc func([]*errors.QueryError) | ||
type TraceFieldFinishFunc func(*errors.QueryError) | ||
|
||
type Tracer interface { | ||
TraceQuery(ctx context.Context, queryString string, operationName string, variables map[string]interface{}) (context.Context, TraceQueryFinishFunc) | ||
TraceField(ctx context.Context, label, typeName, fieldName string, trivial bool, args map[string]interface{}) (context.Context, TraceFieldFinishFunc) | ||
} | ||
|
||
type OpenTracingTracer struct{} | ||
|
||
func (OpenTracingTracer) TraceQuery(ctx context.Context, queryString string, operationName string, variables map[string]interface{}) (context.Context, TraceQueryFinishFunc) { | ||
span, spanCtx := opentracing.StartSpanFromContext(ctx, "GraphQL request") | ||
span.SetTag("graphql.query", queryString) | ||
|
||
if operationName != "" { | ||
span.SetTag("graphql.operationName", operationName) | ||
} | ||
|
||
if len(variables) != 0 { | ||
span.SetTag("graphql.variables", variables) | ||
} | ||
|
||
return spanCtx, func(errs []*errors.QueryError) { | ||
if len(errs) > 0 { | ||
msg := errs[0].Error() | ||
if len(errs) > 1 { | ||
msg += fmt.Sprintf(" (and %d more errors)", len(errs)-1) | ||
} | ||
ext.Error.Set(span, true) | ||
span.SetTag("graphql.error", msg) | ||
} | ||
span.Finish() | ||
} | ||
} | ||
|
||
func (OpenTracingTracer) TraceField(ctx context.Context, label, typeName, fieldName string, trivial bool, args map[string]interface{}) (context.Context, TraceFieldFinishFunc) { | ||
if trivial { | ||
return ctx, noop | ||
} | ||
|
||
span, spanCtx := opentracing.StartSpanFromContext(ctx, label) | ||
span.SetTag("graphql.type", typeName) | ||
span.SetTag("graphql.field", fieldName) | ||
for name, value := range args { | ||
span.SetTag("graphql.args."+name, value) | ||
} | ||
|
||
return spanCtx, func(err *errors.QueryError) { | ||
if err != nil { | ||
ext.Error.Set(span, true) | ||
span.SetTag("graphql.error", err.Error()) | ||
} | ||
span.Finish() | ||
} | ||
} | ||
|
||
func noop(*errors.QueryError) {} |