Skip to content

Commit

Permalink
feat: tracer for client-server debugging (#117)
Browse files Browse the repository at this point in the history
Allow tracing of request/response to debug the interaction between client & server.
  • Loading branch information
snadrus authored Jul 30, 2024
1 parent e59e680 commit 2bb4fbb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
10 changes: 10 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,12 @@ type handler struct {
aliasedMethods map[string]string

paramDecoders map[reflect.Type]ParamDecoder

tracer Tracer
}

type Tracer func(method string, params []reflect.Value, results []reflect.Value, err error)

func makeHandler(sc ServerConfig) *handler {
return &handler{
methods: make(map[string]methodHandler),
Expand Down Expand Up @@ -445,12 +449,18 @@ func (s *handler) handle(ctx context.Context, req request, w func(func(io.Writer
if err != nil {
rpcError(w, &req, 0, xerrors.Errorf("fatal error calling '%s': %w", req.Method, err))
stats.Record(ctx, metrics.RPCRequestError.M(1))
if s.tracer != nil {
s.tracer(req.Method, callParams, nil, err)
}
return
}
if req.ID == nil {
return // notification
}

if s.tracer != nil {
s.tracer(req.Method, callParams, callResult, nil)
}
// /////////////////

resp := response{
Expand Down
9 changes: 9 additions & 0 deletions options_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type ServerConfig struct {
errors *Errors

reverseClientBuilder func(context.Context, *wsConn) (context.Context, error)
tracer Tracer
}

type ServerOption func(c *ServerConfig)
Expand Down Expand Up @@ -58,6 +59,14 @@ func WithServerPingInterval(d time.Duration) ServerOption {
}
}

// WithTracer allows the instantiator to trace the method calls and results.
// This is useful for debugging a client-server interaction.
func WithTracer(l Tracer) ServerOption {
return func(c *ServerConfig) {
c.tracer = l
}
}

// WithReverseClient will allow extracting reverse client on **WEBSOCKET** calls.
// RP is a proxy-struct type, much like the one passed to NewClient.
func WithReverseClient[RP any](namespace string) ServerOption {
Expand Down

0 comments on commit 2bb4fbb

Please sign in to comment.