Skip to content

Commit

Permalink
Add error counter for SSE endpoint (#14681)
Browse files Browse the repository at this point in the history
* add error counter for SSE endpoint

* add changelog entry
  • Loading branch information
dB2510 authored Nov 29, 2024
1 parent 1139c90 commit 9fa49e7
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ The format is based on Keep a Changelog, and this project adheres to Semantic Ve
- Better attestation packing for Electra. [PR](https://github.com/prysmaticlabs/prysm/pull/14534)
- P2P: Add logs when a peer is (dis)connected. Add the reason of the disconnection when we initiate it.
- Added a Prometheus error counter metric for HTTP requests to track beacon node requests.
- Added a Prometheus error counter metric for SSE requests.

### Changed

Expand Down
7 changes: 7 additions & 0 deletions beacon-chain/rpc/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ func (e *endpoint) handlerWithMiddleware() http.HandlerFunc {
)

return func(w http.ResponseWriter, r *http.Request) {
// SSE errors are handled separately to avoid interference with the streaming
// mechanism and ensure accurate error tracking.
if e.template == "/eth/v1/events" {
handler.ServeHTTP(w, r)
return
}

rw := &responseWriter{ResponseWriter: w, statusCode: http.StatusOK}
handler.ServeHTTP(rw, r)

Expand Down
2 changes: 2 additions & 0 deletions beacon-chain/rpc/eth/events/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ go_library(
"//time/slots:go_default_library",
"@com_github_ethereum_go_ethereum//common/hexutil:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_prometheus_client_golang//prometheus:go_default_library",
"@com_github_prometheus_client_golang//prometheus/promauto:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
],
)
Expand Down
22 changes: 20 additions & 2 deletions beacon-chain/rpc/eth/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prysmaticlabs/prysm/v5/api"
"github.com/prysmaticlabs/prysm/v5/api/server/structs"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/feed"
Expand Down Expand Up @@ -76,6 +78,14 @@ var (
errWriterUnusable = errors.New("http response writer is unusable")
)

var httpSSEErrorCount = promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "http_sse_error_count",
Help: "Total HTTP errors for server sent events endpoint",
},
[]string{"endpoint", "error"},
)

// The eventStreamer uses lazyReaders to defer serialization until the moment the value is ready to be written to the client.
type lazyReader func() io.Reader

Expand Down Expand Up @@ -145,6 +155,13 @@ func newTopicRequest(topics []string) (*topicRequest, error) {
// Servers may send SSE comments beginning with ':' for any purpose,
// including to keep the event stream connection alive in the presence of proxy servers.
func (s *Server) StreamEvents(w http.ResponseWriter, r *http.Request) {
var err error
defer func() {
if err != nil {
httpSSEErrorCount.WithLabelValues(r.URL.Path, err.Error()).Inc()
}
}()

log.Debug("Starting StreamEvents handler")
ctx, span := trace.StartSpan(r.Context(), "events.StreamEvents")
defer span.End()
Expand Down Expand Up @@ -174,7 +191,7 @@ func (s *Server) StreamEvents(w http.ResponseWriter, r *http.Request) {
defer cancel()
es := newEventStreamer(buffSize, ka)

go es.outboxWriteLoop(ctx, cancel, sw)
go es.outboxWriteLoop(ctx, cancel, sw, r.URL.Path)
if err := es.recvEventLoop(ctx, cancel, topics, s); err != nil {
log.WithError(err).Debug("Shutting down StreamEvents handler.")
}
Expand Down Expand Up @@ -264,11 +281,12 @@ func newlineReader() io.Reader {

// outboxWriteLoop runs in a separate goroutine. Its job is to write the values in the outbox to
// the client as fast as the client can read them.
func (es *eventStreamer) outboxWriteLoop(ctx context.Context, cancel context.CancelFunc, w *streamingResponseWriterController) {
func (es *eventStreamer) outboxWriteLoop(ctx context.Context, cancel context.CancelFunc, w *streamingResponseWriterController, endpoint string) {
var err error
defer func() {
if err != nil {
log.WithError(err).Debug("Event streamer shutting down due to error.")
httpSSEErrorCount.WithLabelValues(endpoint, err.Error()).Inc()
}
es.exit()
}()
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/rpc/eth/events/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
statefeed "github.com/prysmaticlabs/prysm/v5/beacon-chain/core/feed/state"
)

// Server defines a server implementation of the gRPC events service,
// Server defines a server implementation of the http events service,
// providing RPC endpoints to subscribe to events from the beacon node.
type Server struct {
StateNotifier statefeed.Notifier
Expand Down

0 comments on commit 9fa49e7

Please sign in to comment.