From 7b6c1100ec3e8fdd2f7be284b6614a01a1a2ae30 Mon Sep 17 00:00:00 2001 From: gearnode Date: Mon, 24 Mar 2025 10:23:42 +0100 Subject: [PATCH] Add GraphQL tracing Signed-off-by: gearnode --- pkg/server/api/console/v1/resolver.go | 3 + .../api/console/v1/tracing_extensions.go | 88 +++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 pkg/server/api/console/v1/tracing_extensions.go diff --git a/pkg/server/api/console/v1/resolver.go b/pkg/server/api/console/v1/resolver.go index 80a9e9678..e5fac899d 100644 --- a/pkg/server/api/console/v1/resolver.go +++ b/pkg/server/api/console/v1/resolver.go @@ -108,6 +108,9 @@ func graphqlHandler(proboSvc *probo.Service, usrmgrSvc *usrmgr.Service, authCfg }, ) srv.Use(extension.Introspection{}) + + srv.Use(tracingExtension{}) + srv.SetRecoverFunc( func(ctx context.Context, err any) error { panic(fmt.Errorf("graphql resolver panic: %v", err)) diff --git a/pkg/server/api/console/v1/tracing_extensions.go b/pkg/server/api/console/v1/tracing_extensions.go new file mode 100644 index 000000000..5d4194159 --- /dev/null +++ b/pkg/server/api/console/v1/tracing_extensions.go @@ -0,0 +1,88 @@ +// Copyright (c) 2025 Probo Inc . +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH +// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR +// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +// PERFORMANCE OF THIS SOFTWARE. + +package console_v1 + +import ( + "context" + + "github.com/99designs/gqlgen/graphql" + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/trace" +) + +type tracingExtension struct{} + +func (t tracingExtension) ExtensionName() string { + return "Tracing" +} + +func (t tracingExtension) Validate(schema graphql.ExecutableSchema) error { + return nil +} + +func (t tracingExtension) InterceptField(ctx context.Context, next graphql.Resolver) (interface{}, error) { + rootSpan := trace.SpanFromContext(ctx) + + if rootSpan.IsRecording() { + tracer := otel.Tracer("graphql-field") + fieldContext := graphql.GetFieldContext(ctx) + + ctx, span := tracer.Start(ctx, "GraphQL Field: "+fieldContext.Field.Name) + defer span.End() + + span.SetAttributes( + attribute.String("graphql.field.name", fieldContext.Field.Name), + attribute.String("graphql.field.path", fieldContext.Path().String()), + attribute.String("graphql.field.object", fieldContext.Object), + ) + + result, err := next(ctx) + + if err != nil { + span.RecordError(err) + } + + return result, err + } + + return next(ctx) +} + +func (t tracingExtension) InterceptOperation(ctx context.Context, next graphql.OperationHandler) graphql.ResponseHandler { + rootSpan := trace.SpanFromContext(ctx) + + if rootSpan.IsRecording() { + tracer := otel.Tracer("graphql-operation") + requestContext := graphql.GetOperationContext(ctx) + operationName := "GraphQL Operation" + if requestContext.OperationName != "" { + operationName = "GraphQL " + requestContext.OperationName + } + + ctx, span := tracer.Start(ctx, operationName) + defer span.End() + + span.SetAttributes( + attribute.String("graphql.operation_name", requestContext.OperationName), + attribute.String("graphql.operation_type", string(requestContext.Operation.Operation)), + attribute.String("graphql.query", requestContext.RawQuery), + ) + + return next(ctx) + } + + return next(ctx) +}