Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 36 additions & 11 deletions server/rpc/interceptors/interceptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -531,7 +556,7 @@ func GetUnaryInterceptor(env environment.Env, extraInterceptors ...grpc.UnarySer
interceptors := []grpc.UnaryServerInterceptor{
unaryRecoveryInterceptor(),
copyHeadersUnaryServerInterceptor(),
propagateRequestMetadataIDsToSpanUnaryServerInterceptor(),
propagateRequestMetadataToSpanUnaryServerInterceptor(),
ClientIPUnaryServerInterceptor(),
subdomainUnaryServerInterceptor(),
requestIDUnaryServerInterceptor(),
Expand All @@ -557,7 +582,7 @@ func GetStreamInterceptor(env environment.Env, extraInterceptors ...grpc.StreamS
interceptors := []grpc.StreamServerInterceptor{
streamRecoveryInterceptor(),
copyHeadersStreamServerInterceptor(),
propagateRequestMetadataIDsToSpanStreamServerInterceptor(),
propagateRequestMetadataToSpanStreamServerInterceptor(),
clientIPStreamServerInterceptor(),
subdomainStreamServerInterceptor(),
requestIDStreamServerInterceptor(),
Expand Down
Loading