How to implement Open Tracing in gglgen #2092
Unanswered
Virinchi2595
asked this question in
Q&A
Replies: 2 comments 2 replies
-
Did you see this package? https://github.com/99designs/gqlgen-contrib/tree/master/gqlopentracing |
Beta Was this translation helpful? Give feedback.
2 replies
-
I am using opencensus and wrote my own simple solution. I wrote it in a short manner but it seems to do a basic job: package graph
import (
"context"
"fmt"
"github.com/99designs/gqlgen/graphql"
"go.opencensus.io/trace"
)
// OpenTracing is an opencensus tracer that instruments GraphQL requests with opencensus spans
type OpenTracing struct{}
var _ interface {
graphql.ResponseInterceptor
graphql.OperationInterceptor
graphql.FieldInterceptor
graphql.HandlerExtension
} = OpenTracing{}
func (t OpenTracing) ExtensionName() string {
return "open tracing"
}
func (t OpenTracing) Validate(_ graphql.ExecutableSchema) error {
return nil
}
func (t OpenTracing) InterceptOperation(ctx context.Context, next graphql.OperationHandler) graphql.ResponseHandler {
op := graphql.GetOperationContext(ctx)
if op.OperationName == "" {
op.OperationName = "default"
}
ctx, span := trace.StartSpan(ctx, op.OperationName)
defer span.End()
return next(ctx)
}
func (t OpenTracing) InterceptField(ctx context.Context, next graphql.Resolver) (res interface{}, err error) {
field := graphql.GetFieldContext(ctx)
if field.IsResolver {
name := fmt.Sprintf("%s.%s", field.Object, field.Field.Name)
cx, span := trace.StartSpan(ctx, name)
defer span.End()
ctx = cx
}
return next(ctx)
}
func (t OpenTracing) InterceptResponse(ctx context.Context, next graphql.ResponseHandler) *graphql.Response {
ctx, span := trace.StartSpan(ctx, "response")
defer span.End()
return next(ctx)
} Then you can just use it: server.Use(OpenTracing{}) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The feature list shows that gqlgen supports open tracing but could not find any documentation on implementation details.
Any suggestions on this area will be helpful thanks.
Beta Was this translation helpful? Give feedback.
All reactions