Skip to content

Commit 2bb4fbb

Browse files
authored
feat: tracer for client-server debugging (#117)
Allow tracing of request/response to debug the interaction between client & server.
1 parent e59e680 commit 2bb4fbb

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

handler.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,12 @@ type handler struct {
141141
aliasedMethods map[string]string
142142

143143
paramDecoders map[reflect.Type]ParamDecoder
144+
145+
tracer Tracer
144146
}
145147

148+
type Tracer func(method string, params []reflect.Value, results []reflect.Value, err error)
149+
146150
func makeHandler(sc ServerConfig) *handler {
147151
return &handler{
148152
methods: make(map[string]methodHandler),
@@ -445,12 +449,18 @@ func (s *handler) handle(ctx context.Context, req request, w func(func(io.Writer
445449
if err != nil {
446450
rpcError(w, &req, 0, xerrors.Errorf("fatal error calling '%s': %w", req.Method, err))
447451
stats.Record(ctx, metrics.RPCRequestError.M(1))
452+
if s.tracer != nil {
453+
s.tracer(req.Method, callParams, nil, err)
454+
}
448455
return
449456
}
450457
if req.ID == nil {
451458
return // notification
452459
}
453460

461+
if s.tracer != nil {
462+
s.tracer(req.Method, callParams, callResult, nil)
463+
}
454464
// /////////////////
455465

456466
resp := response{

options_server.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type ServerConfig struct {
2121
errors *Errors
2222

2323
reverseClientBuilder func(context.Context, *wsConn) (context.Context, error)
24+
tracer Tracer
2425
}
2526

2627
type ServerOption func(c *ServerConfig)
@@ -58,6 +59,14 @@ func WithServerPingInterval(d time.Duration) ServerOption {
5859
}
5960
}
6061

62+
// WithTracer allows the instantiator to trace the method calls and results.
63+
// This is useful for debugging a client-server interaction.
64+
func WithTracer(l Tracer) ServerOption {
65+
return func(c *ServerConfig) {
66+
c.tracer = l
67+
}
68+
}
69+
6170
// WithReverseClient will allow extracting reverse client on **WEBSOCKET** calls.
6271
// RP is a proxy-struct type, much like the one passed to NewClient.
6372
func WithReverseClient[RP any](namespace string) ServerOption {

0 commit comments

Comments
 (0)