diff --git a/sql_logger.go b/sql_logger.go index 6c636b7..ee7d8ef 100644 --- a/sql_logger.go +++ b/sql_logger.go @@ -57,8 +57,8 @@ func SqlGroupFromContext(ctx context.Context) string { return r } -// WithTiming adds timings in JSON-RPC 2.0 Response via `extensions` field (not in spec). Middleware is active -// when `isDevel=true` or AllowDebugFunc returns `true`. +// WithTiming adds timings in JSON-RPC 2.0 Response via `extensions` field (not in spec). +// Middleware is active when `isDevel=true` or AllowDebugFunc returns `true` and http request is set. // `DurationLocal` – total method execution time in ms. // If `DurationRemote` or `DurationDiff` are set then `DurationLocal` excludes these values. func WithTiming(isDevel bool, allowDebugFunc AllowDebugFunc) zenrpc.MiddlewareFunc { @@ -66,7 +66,11 @@ func WithTiming(isDevel bool, allowDebugFunc AllowDebugFunc) zenrpc.MiddlewareFu return func(ctx context.Context, method string, params json.RawMessage) (resp zenrpc.Response) { // check for debug id if !isDevel { - req, _ := zenrpc.RequestFromContext(ctx) + req, ok := zenrpc.RequestFromContext(ctx) + if !ok || req == nil { + return h(ctx, method, params) + } + reqClone := req.Clone(ctx) if reqClone == nil || !allowDebugFunc(reqClone) { return h(ctx, method, params) @@ -99,8 +103,8 @@ func WithTiming(isDevel bool, allowDebugFunc AllowDebugFunc) zenrpc.MiddlewareFu } // WithSQLLogger adds `SQL` or `DurationSQL` fields in JSON-RPC 2.0 Response `extensions` field (not in spec). -// `DurationSQL` field is set then `isDevel=true` or AllowDebugFunc(allowDebugFunc) returns `true`. -// `SQL` field is set then `isDevel=true` or AllowDebugFunc(allowDebugFunc, allowSqlDebugFunc) returns `true`. +// `DurationSQL` field is set then `isDevel=true` or AllowDebugFunc(allowDebugFunc) returns `true` and http request is set. +// `SQL` field is set then `isDevel=true` or AllowDebugFunc(allowDebugFunc, allowSqlDebugFunc) returns `true` and http request is set. func WithSQLLogger(db *pg.DB, isDevel bool, allowDebugFunc, allowSqlDebugFunc AllowDebugFunc) zenrpc.MiddlewareFunc { // init sql logger ql := NewSqlQueryLogger() @@ -112,7 +116,11 @@ func WithSQLLogger(db *pg.DB, isDevel bool, allowDebugFunc, allowSqlDebugFunc Al // check for debug id if !isDevel { - req, _ := zenrpc.RequestFromContext(ctx) + req, ok := zenrpc.RequestFromContext(ctx) + if !ok || req == nil { + return h(ctx, method, params) + } + reqClone := req.Clone(ctx) if reqClone == nil || !allowDebugFunc(reqClone) { return h(ctx, method, params)