Skip to content

Commit

Permalink
add spans to stages
Browse files Browse the repository at this point in the history
  • Loading branch information
kofoworola committed Jul 20, 2023
1 parent 6d0b924 commit 10c8a38
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 22 deletions.
2 changes: 1 addition & 1 deletion gateway/api_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ type APISpec struct {
Engine *graphql.ExecutionEngine
CancelV2 context.CancelFunc
EngineV2 *graphql.ExecutionEngineV2
CustomExecutor *graphql.CustomExecutionEngineV2Executor
CustomExecutor graphql.ExecutionEngineV2Executor
HooksV2 struct {
BeforeFetchHook resolve.BeforeFetchHook
AfterFetchHook resolve.AfterFetchHook
Expand Down
97 changes: 79 additions & 18 deletions gateway/custom_otel_graphql_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,63 +17,124 @@ type OtelGraphqlEngineV2 struct {
mutex sync.Mutex
logger *logrus.Entry
spec *APISpec
spanContext context.Context
traceContext context.Context
tracerProvider otel.TracerProvider

engine graphql.CustomExecutionEngineV2
engine *graphql.ExecutionEngineV2
rootExecutor graphql.ExecutionEngineV2Executor
}

func (o *OtelGraphqlEngineV2) setContext(ctx context.Context) {
o.mutex.Lock()
defer o.mutex.Unlock()
o.spanContext = ctx
o.traceContext = ctx
}

func (o *OtelGraphqlEngineV2) setRootExecutor(executor graphql.ExecutionEngineV2Executor) {
o.rootExecutor = executor
}

func (o *OtelGraphqlEngineV2) Normalize(operation *graphql.Request) error {
var operationName = "NormalizeRequest"
if tyktrace.IsEnabled() {
span, ctx := tyktrace.Span(o.spanContext, operationName)
span, ctx := tyktrace.Span(o.traceContext, operationName)
defer span.Finish()
o.setContext(ctx)
} else {
ctx, span := o.tracerProvider.Tracer().Start(o.spanContext, operationName)
ctx, span := o.tracerProvider.Tracer().Start(o.traceContext, operationName)
defer span.End()
o.setContext(ctx)
}
return o.engine.Normalize(operation)
}

func (o *OtelGraphqlEngineV2) ValidateForSchema(operation *graphql.Request) error {
//TODO implement me
panic("implement me")
var operationName = "ValidateRequest"
if tyktrace.IsEnabled() {
span, ctx := tyktrace.Span(o.traceContext, operationName)
defer span.Finish()
o.setContext(ctx)
} else {
ctx, span := o.tracerProvider.Tracer().Start(o.traceContext, operationName)
defer span.End()
o.setContext(ctx)
}
return o.engine.ValidateForSchema(operation)
}

func (o *OtelGraphqlEngineV2) Setup(ctx context.Context, postProcessor *postprocess.Processor, resolveContext *resolve.Context, operation *graphql.Request, options ...graphql.ExecutionOptionsV2) {
//TODO implement me
panic("implement me")
var operationName = "SetupResolver"
if tyktrace.IsEnabled() {
span, ctx := tyktrace.Span(o.traceContext, operationName)
defer span.Finish()
o.setContext(ctx)
} else {
ctx, span := o.tracerProvider.Tracer().Start(o.traceContext, operationName)
defer span.End()
o.setContext(ctx)
}
o.engine.Setup(ctx, postProcessor, resolveContext, operation, options...)
}

func (o *OtelGraphqlEngineV2) Plan(postProcessor *postprocess.Processor, operation *graphql.Request, report *operationreport.Report) (plan.Plan, error) {
//TODO implement me
panic("implement me")
var operationName = "GeneratePlan"
if tyktrace.IsEnabled() {
span, ctx := tyktrace.Span(o.traceContext, operationName)
defer span.Finish()
o.setContext(ctx)
} else {
ctx, span := o.tracerProvider.Tracer().Start(o.traceContext, operationName)
defer span.End()
o.setContext(ctx)
}
return o.engine.Plan(postProcessor, operation, report)
}

func (o *OtelGraphqlEngineV2) Resolve(resolveContext *resolve.Context, planResult plan.Plan, writer resolve.FlushWriter) error {
//TODO implement me
panic("implement me")
var operationName = "ResolvePlan"
if tyktrace.IsEnabled() {
span, ctx := tyktrace.Span(o.traceContext, operationName)
defer span.Finish()
o.setContext(ctx)
} else {
ctx, span := o.tracerProvider.Tracer().Start(o.traceContext, operationName)
defer span.End()
o.setContext(ctx)
}
return o.engine.Resolve(resolveContext, planResult, writer)
}

func (o *OtelGraphqlEngineV2) Teardown() {
//TODO implement me
panic("implement me")
}

func (o *OtelGraphqlEngineV2) InputValidation(operation *graphql.Request) error {
//TODO implement me
panic("implement me")
var operationName = "InputValidation"
if tyktrace.IsEnabled() {
span, ctx := tyktrace.Span(o.traceContext, operationName)
defer span.Finish()
o.setContext(ctx)
} else {
ctx, span := o.tracerProvider.Tracer().Start(o.traceContext, operationName)
defer span.End()
o.setContext(ctx)
}
return o.engine.InputValidation(operation)
}

func (o *OtelGraphqlEngineV2) Execute(inCtx context.Context, operation *graphql.Request, writer resolve.FlushWriter, options ...graphql.ExecutionOptionsV2) error {
if tyktrace.IsEnabled() {
span, ctx := tyktrace.Span(inCtx, "GraphqlEngine")
defer span.Finish()
o.setContext(ctx)
} else {
ctx, span := o.tracerProvider.Tracer().Start(inCtx, "GraphqlEngine")
defer span.End()
o.setContext(ctx)
}
return o.rootExecutor.Execute(inCtx, operation, writer, options...)
}

func NewOtelGraphqlEngineV2(tracerProvider otel.TracerProvider, engine graphql.CustomExecutionEngineV2) *OtelGraphqlEngineV2 {
func NewOtelGraphqlEngineV2(tracerProvider otel.TracerProvider, engine *graphql.ExecutionEngineV2) *OtelGraphqlEngineV2 {
return &OtelGraphqlEngineV2{
tracerProvider: tracerProvider,
engine: engine,
Expand Down
6 changes: 4 additions & 2 deletions gateway/mw_graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,12 @@ func (m *GraphQLMiddleware) initGraphQLEngineV2(logger *abstractlogger.LogrusLog
m.Spec.GraphQLExecutor.EngineV2 = engine
conf := m.Gw.GetConfig()
if conf.OpenTelemetry.Enabled {
executor, err := gql.NewCustomExecutionEngineV2Executor(NewOtelGraphqlEngineV2(m.Gw.TracerProvider, engine))
executor := NewOtelGraphqlEngineV2(m.Gw.TracerProvider, engine)
rootExecutor, err := gql.NewCustomExecutionEngineV2Executor(executor)
if err != nil {
m.Logger().WithError(err).Error("could not create execution engine executor v2")
m.Logger().WithError(err).Error("error creating custom execution engine v2")
}
executor.setRootExecutor(rootExecutor)
m.Spec.GraphQLExecutor.CustomExecutor = executor
} else {

Expand Down
13 changes: 12 additions & 1 deletion gateway/reverse_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -1137,8 +1137,19 @@ func (p *ReverseProxy) handoverRequestToGraphQLExecutionEngine(roundTripper *Tyk

isProxyOnly := isGraphQLProxyOnly(p.TykAPISpec)
reqCtx := context.Background()
// TODO remove this as a span and simply pass the last span
// add the tracer context to the request context
if trace.IsEnabled() {
span, c := trace.Span(outreq.Context(), "ReverseProxy")
defer span.Finish()
reqCtx = c
} else {
c, span := p.Gw.TracerProvider.Tracer().Start(outreq.Context(), "ReverseProxy")
defer span.End()
reqCtx = c
}
if isProxyOnly {
reqCtx = NewGraphQLProxyOnlyContext(context.Background(), outreq)
reqCtx = NewGraphQLProxyOnlyContext(reqCtx, outreq)
}

resultWriter := graphql.NewEngineResultWriter()
Expand Down

0 comments on commit 10c8a38

Please sign in to comment.