From af959958716b9be96f3dd79dbf76847182647536 Mon Sep 17 00:00:00 2001 From: Brandon Duffany Date: Wed, 29 Apr 2026 15:52:42 -0400 Subject: [PATCH] Add more RequestMetadata attributes to root trace spans --- server/rpc/interceptors/interceptors.go | 47 +++++++++++++++++++------ 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/server/rpc/interceptors/interceptors.go b/server/rpc/interceptors/interceptors.go index 7923b913010..425eee6a44c 100644 --- a/server/rpc/interceptors/interceptors.go +++ b/server/rpc/interceptors/interceptors.go @@ -29,10 +29,9 @@ import ( "google.golang.org/grpc/metadata" "google.golang.org/grpc/peer" + bbspb "github.com/buildbuddy-io/buildbuddy/proto/buildbuddy_service" requestcontext "github.com/buildbuddy-io/buildbuddy/server/util/request_context" grpc_prometheus "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus" - - bbspb "github.com/buildbuddy-io/buildbuddy/proto/buildbuddy_service" ) const ( @@ -469,33 +468,59 @@ func PropagateMetadataStreamInterceptor(keys ...string) grpc.StreamServerInterce return contextReplacingStreamServerInterceptor(propagateMetadataFromIncomingToOutgoing(keys...)) } -func propagateBazelRequestMetadataIDsToSpan(ctx context.Context) { +func requestMetadataAttributes(ctx context.Context) []attribute.KeyValue { metadata := bazel_request.GetRequestMetadata(ctx) - attributes := make([]attribute.KeyValue, 0, 2) + attributes := make([]attribute.KeyValue, 0, 8) if id := metadata.GetToolInvocationId(); id != "" { attributes = append(attributes, attribute.String("invocation_id", id)) } if id := metadata.GetActionId(); id != "" { attributes = append(attributes, attribute.String("action_id", id)) } + if id := metadata.GetCorrelatedInvocationsId(); id != "" { + attributes = append(attributes, attribute.String("correlated_invocations_id", id)) + } + if mnemonic := metadata.GetActionMnemonic(); mnemonic != "" { + attributes = append(attributes, attribute.String("action_mnemonic", mnemonic)) + } + if id := metadata.GetTargetId(); id != "" { + attributes = append(attributes, attribute.String("target_id", id)) + } + if id := metadata.GetConfigurationId(); id != "" { + attributes = append(attributes, attribute.String("configuration_id", id)) + } + if toolName := metadata.GetToolDetails().GetToolName(); toolName != "" { + attributes = append(attributes, attribute.String("tool_name", toolName)) + } + if toolVersion := metadata.GetToolDetails().GetToolVersion(); toolVersion != "" { + attributes = append(attributes, attribute.String("tool_version", toolVersion)) + } + return attributes +} + +func propagateBazelRequestMetadataToSpan(ctx context.Context) { + span := trace.SpanFromContext(ctx) + if !span.IsRecording() { + return + } + attributes := requestMetadataAttributes(ctx) if len(attributes) == 0 { return } - span := trace.SpanFromContext(ctx) span.SetAttributes(attributes...) } -func propagateRequestMetadataIDsToSpanUnaryServerInterceptor() grpc.UnaryServerInterceptor { +func propagateRequestMetadataToSpanUnaryServerInterceptor() grpc.UnaryServerInterceptor { return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) { - propagateBazelRequestMetadataIDsToSpan(ctx) + propagateBazelRequestMetadataToSpan(ctx) return handler(ctx, req) } } -func propagateRequestMetadataIDsToSpanStreamServerInterceptor() grpc.StreamServerInterceptor { +func propagateRequestMetadataToSpanStreamServerInterceptor() grpc.StreamServerInterceptor { return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error { ctx := stream.Context() - propagateBazelRequestMetadataIDsToSpan(ctx) + propagateBazelRequestMetadataToSpan(ctx) return handler(srv, stream) } } @@ -531,7 +556,7 @@ func GetUnaryInterceptor(env environment.Env, extraInterceptors ...grpc.UnarySer interceptors := []grpc.UnaryServerInterceptor{ unaryRecoveryInterceptor(), copyHeadersUnaryServerInterceptor(), - propagateRequestMetadataIDsToSpanUnaryServerInterceptor(), + propagateRequestMetadataToSpanUnaryServerInterceptor(), ClientIPUnaryServerInterceptor(), subdomainUnaryServerInterceptor(), requestIDUnaryServerInterceptor(), @@ -557,7 +582,7 @@ func GetStreamInterceptor(env environment.Env, extraInterceptors ...grpc.StreamS interceptors := []grpc.StreamServerInterceptor{ streamRecoveryInterceptor(), copyHeadersStreamServerInterceptor(), - propagateRequestMetadataIDsToSpanStreamServerInterceptor(), + propagateRequestMetadataToSpanStreamServerInterceptor(), clientIPStreamServerInterceptor(), subdomainStreamServerInterceptor(), requestIDStreamServerInterceptor(),