cuelang.org/go@v0.10.1/internal/httplog/context.go (about) 1 package httplog 2 3 import "context" 4 5 type ( 6 allowedURLQueryParamsKey struct{} 7 redactResponseBodyKey struct{} 8 redactRequestBodyKey struct{} 9 ) 10 11 // ContextWithAllowedURLQueryParams returns a context that will allow only the URL 12 // query parameters for which the given allow function returns true. All others will 13 // be redacted from the logs. 14 func ContextWithAllowedURLQueryParams(ctx context.Context, allow func(key string) bool) context.Context { 15 return context.WithValue(ctx, allowedURLQueryParamsKey{}, allow) 16 } 17 18 func queryParamChecker(ctx context.Context) func(string) bool { 19 f, ok := ctx.Value(allowedURLQueryParamsKey{}).(func(string) bool) 20 if ok { 21 return f 22 } 23 return func(string) bool { 24 return false 25 } 26 } 27 28 // RedactResponseBody returns a context that will cause 29 // [Transport] to redact the response body when logging HTTP responses. 30 // If reason is empty, the body will not be redacted. 31 func RedactResponseBody(ctx context.Context, reason string) context.Context { 32 return context.WithValue(ctx, redactResponseBodyKey{}, reason) 33 } 34 35 func shouldRedactBody(ctx context.Context, isRequest bool) string { 36 key := any(redactResponseBodyKey{}) 37 if isRequest { 38 key = redactRequestBodyKey{} 39 } 40 s, _ := ctx.Value(key).(string) 41 return s 42 } 43 44 // RedactRequestBody returns a context that will cause 45 // the logger to redact the request body when logging HTTP requests. 46 func RedactRequestBody(ctx context.Context, reason string) context.Context { 47 return context.WithValue(ctx, redactRequestBodyKey{}, reason) 48 }