-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add basic support for OpenCensus tracing (resolvers only) (#70)
* feat: Add basic support for OpenCensus tracing (resolvers only) * chore: add dep to go.mod
- Loading branch information
1 parent
7d7c6df
commit a2dabe9
Showing
3 changed files
with
71 additions
and
9 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,52 @@ | ||
package graphql | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/99designs/gqlgen/graphql" | ||
"go.opencensus.io/trace" | ||
) | ||
|
||
var _ graphql.HandlerExtension = &OpenCensusTracingExtension{} | ||
var _ graphql.ResponseInterceptor = &OpenCensusTracingExtension{} | ||
var _ graphql.FieldInterceptor = &OpenCensusTracingExtension{} | ||
var _ graphql.OperationInterceptor = &OpenCensusTracingExtension{} | ||
|
||
type OpenCensusTracingExtension struct{} | ||
|
||
func (o *OpenCensusTracingExtension) ExtensionName() string { | ||
return "OpenCensusTracing" | ||
} | ||
|
||
func (o *OpenCensusTracingExtension) InterceptField(ctx context.Context, next graphql.Resolver) (res interface{}, err error) { | ||
fieldContext := graphql.GetFieldContext(ctx) | ||
|
||
// for now, we only trace resolvers | ||
if !fieldContext.IsResolver { | ||
return next(ctx) | ||
} | ||
|
||
spanName := fmt.Sprintf("graphql/resolver/%s", fieldContext.Field.Name) | ||
if fieldContext.Object != "Query" && fieldContext.Object != "Mutation" { | ||
// enrich information if the resolver is registered on a graphql subtype, e.g. resolver that resolves `sortedList` on type `Users_Result` | ||
spanName = fmt.Sprintf("graphql/resolver/%s/%s", fieldContext.Object, fieldContext.Field.Name) | ||
} | ||
|
||
ctx, span := trace.StartSpan(ctx, spanName) | ||
defer span.End() | ||
|
||
return next(ctx) | ||
} | ||
|
||
func (o *OpenCensusTracingExtension) InterceptOperation(ctx context.Context, next graphql.OperationHandler) graphql.ResponseHandler { | ||
return next(ctx) | ||
} | ||
|
||
func (o *OpenCensusTracingExtension) InterceptResponse(ctx context.Context, next graphql.ResponseHandler) *graphql.Response { | ||
return next(ctx) | ||
} | ||
|
||
func (o *OpenCensusTracingExtension) Validate(_ graphql.ExecutableSchema) error { | ||
return nil | ||
} |