Skip to content

Commit

Permalink
chore(p2p): extend server tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
Wondertan committed Sep 18, 2024
1 parent 8f53979 commit e8b2052
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions p2p/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ func (serv *ExchangeServer[H]) Stop(context.Context) error {

// requestHandler handles inbound HeaderRequests.
func (serv *ExchangeServer[H]) requestHandler(stream network.Stream) {
ctx, cancel := context.WithTimeout(serv.ctx, serv.Params.RequestTimeout)
defer cancel()

ctx, span := tracerServ.Start(ctx, "request", trace.WithAttributes(
attribute.String("peerID", stream.Conn().RemotePeer().String()),
))
defer span.End()

err := stream.SetReadDeadline(time.Now().Add(serv.Params.ReadDeadline))
if err != nil {
log.Debugf("error setting deadline: %s", err)
Expand All @@ -99,15 +107,14 @@ func (serv *ExchangeServer[H]) requestHandler(stream network.Stream) {
if err != nil {
log.Warnw("server: reading header request from stream", "err", err)
stream.Reset() //nolint:errcheck
span.SetStatus(codes.Error, err.Error())
return
}
if err = stream.CloseRead(); err != nil {
log.Error(err)
span.SetStatus(codes.Error, err.Error())
}

ctx, cancel := context.WithTimeout(serv.ctx, serv.Params.RequestTimeout)
defer cancel()

var headers []H
// retrieve and write Headers
switch pbreq.Data.(type) {
Expand All @@ -126,7 +133,9 @@ func (serv *ExchangeServer[H]) requestHandler(stream network.Stream) {
code = p2p_pb.StatusCode_OK
case header.ErrNotFound:
code = p2p_pb.StatusCode_NOT_FOUND
span.SetStatus(codes.Ok, err.Error())
default:
span.SetStatus(codes.Error, err.Error())
stream.Reset() //nolint:errcheck
return
}
Expand All @@ -150,13 +159,15 @@ func (serv *ExchangeServer[H]) requestHandler(stream network.Stream) {
if err != nil {
log.Warnw("server: marshaling header to proto", "height", h.Height, "err", err)
stream.Reset() //nolint:errcheck
span.SetStatus(codes.Error, err.Error())
return
}
}
_, err = serde.Write(stream, &p2p_pb.HeaderResponse{Body: bin, StatusCode: code})
if err != nil {
log.Warnw("server: writing header to stream", "err", err)
stream.Reset() //nolint:errcheck
span.SetStatus(codes.Error, err.Error())
return
}
}
Expand Down

0 comments on commit e8b2052

Please sign in to comment.