Skip to content

Commit 9fa49e7

Browse files
authored
Add error counter for SSE endpoint (#14681)
* add error counter for SSE endpoint * add changelog entry
1 parent 1139c90 commit 9fa49e7

File tree

5 files changed

+31
-3
lines changed

5 files changed

+31
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ The format is based on Keep a Changelog, and this project adheres to Semantic Ve
3030
- Better attestation packing for Electra. [PR](https://github.com/prysmaticlabs/prysm/pull/14534)
3131
- P2P: Add logs when a peer is (dis)connected. Add the reason of the disconnection when we initiate it.
3232
- Added a Prometheus error counter metric for HTTP requests to track beacon node requests.
33+
- Added a Prometheus error counter metric for SSE requests.
3334

3435
### Changed
3536

beacon-chain/rpc/endpoints.go

+7
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ func (e *endpoint) handlerWithMiddleware() http.HandlerFunc {
6262
)
6363

6464
return func(w http.ResponseWriter, r *http.Request) {
65+
// SSE errors are handled separately to avoid interference with the streaming
66+
// mechanism and ensure accurate error tracking.
67+
if e.template == "/eth/v1/events" {
68+
handler.ServeHTTP(w, r)
69+
return
70+
}
71+
6572
rw := &responseWriter{ResponseWriter: w, statusCode: http.StatusOK}
6673
handler.ServeHTTP(rw, r)
6774

beacon-chain/rpc/eth/events/BUILD.bazel

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ go_library(
3232
"//time/slots:go_default_library",
3333
"@com_github_ethereum_go_ethereum//common/hexutil:go_default_library",
3434
"@com_github_pkg_errors//:go_default_library",
35+
"@com_github_prometheus_client_golang//prometheus:go_default_library",
36+
"@com_github_prometheus_client_golang//prometheus/promauto:go_default_library",
3537
"@com_github_sirupsen_logrus//:go_default_library",
3638
],
3739
)

beacon-chain/rpc/eth/events/events.go

+20-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212

1313
"github.com/ethereum/go-ethereum/common/hexutil"
1414
"github.com/pkg/errors"
15+
"github.com/prometheus/client_golang/prometheus"
16+
"github.com/prometheus/client_golang/prometheus/promauto"
1517
"github.com/prysmaticlabs/prysm/v5/api"
1618
"github.com/prysmaticlabs/prysm/v5/api/server/structs"
1719
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/feed"
@@ -76,6 +78,14 @@ var (
7678
errWriterUnusable = errors.New("http response writer is unusable")
7779
)
7880

81+
var httpSSEErrorCount = promauto.NewCounterVec(
82+
prometheus.CounterOpts{
83+
Name: "http_sse_error_count",
84+
Help: "Total HTTP errors for server sent events endpoint",
85+
},
86+
[]string{"endpoint", "error"},
87+
)
88+
7989
// The eventStreamer uses lazyReaders to defer serialization until the moment the value is ready to be written to the client.
8090
type lazyReader func() io.Reader
8191

@@ -145,6 +155,13 @@ func newTopicRequest(topics []string) (*topicRequest, error) {
145155
// Servers may send SSE comments beginning with ':' for any purpose,
146156
// including to keep the event stream connection alive in the presence of proxy servers.
147157
func (s *Server) StreamEvents(w http.ResponseWriter, r *http.Request) {
158+
var err error
159+
defer func() {
160+
if err != nil {
161+
httpSSEErrorCount.WithLabelValues(r.URL.Path, err.Error()).Inc()
162+
}
163+
}()
164+
148165
log.Debug("Starting StreamEvents handler")
149166
ctx, span := trace.StartSpan(r.Context(), "events.StreamEvents")
150167
defer span.End()
@@ -174,7 +191,7 @@ func (s *Server) StreamEvents(w http.ResponseWriter, r *http.Request) {
174191
defer cancel()
175192
es := newEventStreamer(buffSize, ka)
176193

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

265282
// outboxWriteLoop runs in a separate goroutine. Its job is to write the values in the outbox to
266283
// the client as fast as the client can read them.
267-
func (es *eventStreamer) outboxWriteLoop(ctx context.Context, cancel context.CancelFunc, w *streamingResponseWriterController) {
284+
func (es *eventStreamer) outboxWriteLoop(ctx context.Context, cancel context.CancelFunc, w *streamingResponseWriterController, endpoint string) {
268285
var err error
269286
defer func() {
270287
if err != nil {
271288
log.WithError(err).Debug("Event streamer shutting down due to error.")
289+
httpSSEErrorCount.WithLabelValues(endpoint, err.Error()).Inc()
272290
}
273291
es.exit()
274292
}()

beacon-chain/rpc/eth/events/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
statefeed "github.com/prysmaticlabs/prysm/v5/beacon-chain/core/feed/state"
1313
)
1414

15-
// Server defines a server implementation of the gRPC events service,
15+
// Server defines a server implementation of the http events service,
1616
// providing RPC endpoints to subscribe to events from the beacon node.
1717
type Server struct {
1818
StateNotifier statefeed.Notifier

0 commit comments

Comments
 (0)