Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(p2p): extend server tracing #218

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
Comment on lines 166 to 173
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set status Ok in the end?

Expand Down
Loading