From 23b59b67f602b6de9618ba1aae3349bee269baf6 Mon Sep 17 00:00:00 2001 From: Will Browne Date: Thu, 15 Aug 2024 14:37:36 +0100 Subject: [PATCH] tidy --- backend/log/context_grpc.go | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/backend/log/context_grpc.go b/backend/log/context_grpc.go index bb9fa958c..7a113a464 100644 --- a/backend/log/context_grpc.go +++ b/backend/log/context_grpc.go @@ -8,10 +8,12 @@ import ( "google.golang.org/grpc/metadata" ) -var loggerParamsCtxMetadataKey = "loggerParamsCtxMetadata" +const ( + loggerParamsCtxMetadataKey = "loggerParamsCtxMetadata" + logParamSeparator = ":" +) -// WithContextualAttributesForOutgoingContext returns a new context with the given key/value log parameters appended to the existing ones. -// It's possible to get a logger with those contextual parameters by using [FromContext]. +// WithContextualAttributesForOutgoingContext will append the given key/value log parameters to the outgoing context. func WithContextualAttributesForOutgoingContext(ctx context.Context, logParams []any) context.Context { if len(logParams) == 0 || len(logParams)%2 != 0 { return ctx @@ -20,24 +22,23 @@ func WithContextualAttributesForOutgoingContext(ctx context.Context, logParams [ for i := 0; i < len(logParams); i += 2 { k := logParams[i].(string) v := logParams[i+1].(string) - ctx = metadata.AppendToOutgoingContext(ctx, loggerParamsCtxMetadataKey, fmt.Sprintf("%s:%s", k, v)) + ctx = metadata.AppendToOutgoingContext(ctx, loggerParamsCtxMetadataKey, fmt.Sprintf("%s%s%s", k, logParamSeparator, v)) } return ctx } -// ContextualAttributesFromIncomingContext returns the contextual key/value log parameters from the given context. -// If no contextual log parameters are set, it returns nil. +// ContextualAttributesFromIncomingContext returns the contextual key/value log parameters from the given incoming context. func ContextualAttributesFromIncomingContext(ctx context.Context) []any { logParams := metadata.ValueFromIncomingContext(ctx, loggerParamsCtxMetadataKey) if len(logParams) == 0 { return nil } - var res []any + var attrs []any for _, param := range logParams { - kv := strings.Split(param, ":") - res = append(res, kv[0], kv[1]) + kv := strings.Split(param, logParamSeparator) + attrs = append(attrs, kv[0], kv[1]) } - return res + return attrs }