From ec88bc5584ae5f8dfa29b1580528a2b3f526c0b6 Mon Sep 17 00:00:00 2001 From: Alexander Korelskiy Date: Mon, 24 Oct 2022 13:05:28 +0300 Subject: [PATCH 1/2] Check zenrpc request from context exists --- sql_logger.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/sql_logger.go b/sql_logger.go index 6c636b7..a055c40 100644 --- a/sql_logger.go +++ b/sql_logger.go @@ -66,7 +66,10 @@ 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) @@ -112,7 +115,10 @@ 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) From e233561f85e37e969471f6a9c55f1b208969acb1 Mon Sep 17 00:00:00 2001 From: Alexander Korelskiy Date: Mon, 24 Oct 2022 13:27:02 +0300 Subject: [PATCH 2/2] Add documentation --- sql_logger.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/sql_logger.go b/sql_logger.go index a055c40..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 { @@ -70,6 +70,7 @@ func WithTiming(isDevel bool, allowDebugFunc AllowDebugFunc) zenrpc.MiddlewareFu if !ok || req == nil { return h(ctx, method, params) } + reqClone := req.Clone(ctx) if reqClone == nil || !allowDebugFunc(reqClone) { return h(ctx, method, params) @@ -102,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() @@ -119,6 +120,7 @@ func WithSQLLogger(db *pg.DB, isDevel bool, allowDebugFunc, allowSqlDebugFunc Al if !ok || req == nil { return h(ctx, method, params) } + reqClone := req.Clone(ctx) if reqClone == nil || !allowDebugFunc(reqClone) { return h(ctx, method, params)