Skip to content

Commit

Permalink
Merge pull request #8 from sas1024/master
Browse files Browse the repository at this point in the history
Check zenrpc request from context exists
  • Loading branch information
sas1024 authored Oct 24, 2022
2 parents 26e4cb0 + e233561 commit 18e98dc
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions sql_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,20 @@ 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 {
return func(h zenrpc.InvokeFunc) zenrpc.InvokeFunc {
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)
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down

0 comments on commit 18e98dc

Please sign in to comment.