Skip to content

Commit

Permalink
Merge pull request #70 from blocknative/fix/metric-error
Browse files Browse the repository at this point in the history
unwrap error for metrics
  • Loading branch information
aratz-lasa authored Jan 11, 2023
2 parents a607a8b + b51bb2e commit c1cd8fc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
27 changes: 23 additions & 4 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/lthibault/log"
"github.com/prometheus/client_golang/prometheus"

"github.com/blocknative/dreamboat/pkg/relay"
"github.com/blocknative/dreamboat/pkg/structs"
)

Expand Down Expand Up @@ -146,7 +147,7 @@ func (a *API) registerValidator(w http.ResponseWriter, r *http.Request) (status

m := structs.NewMetricGroup(4)
if err = a.reg.RegisterValidator(r.Context(), m, payload); err != nil {
m.ObserveWithError(a.m.RelayTiming, err)
m.ObserveWithError(a.m.RelayTiming, unwrapError(err, "register validator unknown"))
a.m.ApiReqCounter.WithLabelValues("registerValidator", "400", "register validator").Inc()
a.l.With(log.F{
"code": 400,
Expand All @@ -170,7 +171,7 @@ func (a *API) getHeader(w http.ResponseWriter, r *http.Request) (int, error) {
m := structs.NewMetricGroup(4)
response, err := a.r.GetHeader(r.Context(), m, req)
if err != nil {
m.ObserveWithError(a.m.RelayTiming, err)
m.ObserveWithError(a.m.RelayTiming, unwrapError(err, "get header unknown"))
a.m.ApiReqCounter.WithLabelValues("getHeader", "400", "get header").Inc()
slot, _ := req.Slot()
proposer, _ := req.Pubkey()
Expand Down Expand Up @@ -209,7 +210,7 @@ func (a *API) getPayload(w http.ResponseWriter, r *http.Request) (int, error) {
m := structs.NewMetricGroup(4)
payload, err := a.r.GetPayload(r.Context(), m, &req)
if err != nil {
m.ObserveWithError(a.m.RelayTiming, err)
m.ObserveWithError(a.m.RelayTiming, unwrapError(err, "get payload unknown"))
a.m.ApiReqCounter.WithLabelValues("getPayload", "400", "get payload").Inc()
a.l.With(log.F{
"code": 400,
Expand Down Expand Up @@ -250,7 +251,7 @@ func (a *API) submitBlock(w http.ResponseWriter, r *http.Request) (int, error) {

m := structs.NewMetricGroup(4)
if err := a.r.SubmitBlock(r.Context(), m, &req); err != nil {
m.ObserveWithError(a.m.RelayTiming, err)
m.ObserveWithError(a.m.RelayTiming, unwrapError(err, "submit block unknown"))
if errors.Is(err, structs.ErrPayloadAlreadyDelivered) {
a.m.ApiReqCounter.WithLabelValues("submitBlock", "400", "payload already delivered").Inc()
} else {
Expand Down Expand Up @@ -521,3 +522,21 @@ type jsonError struct {
Code int `json:"code"`
Message string `json:"message"`
}

func unwrapError(err error, defaultMsg string) error {
if errors.Is(err, relay.ErrNoPayloadFound) {
return relay.ErrNoPayloadFound
} else if errors.Is(err, relay.ErrNoBuilderBid) {
return relay.ErrNoBuilderBid
} else if errors.Is(err, relay.ErrBadHeader) {
return relay.ErrBadHeader
} else if errors.Is(err, relay.ErrMissingRequest) {
return relay.ErrMissingRequest
} else if errors.Is(err, relay.ErrMissingSecretKey) {
return relay.ErrMissingSecretKey
} else if errors.Is(err, relay.ErrOldSlot) {
return relay.ErrOldSlot
}

return errors.New(defaultMsg)
}
15 changes: 7 additions & 8 deletions pkg/relay/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@ type Verifier interface {
}

var (
ErrNoPayloadFound = errors.New("no payload found")
ErrMissingRequest = errors.New("req is nil")
ErrMissingSecretKey = errors.New("secret key is nil")
UnregisteredValidatorMsg = "unregistered validator"
ErrNoBuilderBid = errors.New("no builder bid")
ErrOldSlot = errors.New("requested slot is old")
ErrBadHeader = "invalid block header from datastore"
ErrNoPayloadFound = errors.New("no payload found")
ErrMissingRequest = errors.New("req is nil")
ErrMissingSecretKey = errors.New("secret key is nil")
ErrNoBuilderBid = errors.New("no builder bid")
ErrOldSlot = errors.New("requested slot is old")
ErrBadHeader = errors.New("invalid block header from datastore")
)

type Datastore interface {
Expand Down Expand Up @@ -155,7 +154,7 @@ func (rs *Relay) GetHeader(ctx context.Context, m *structs.MetricGroup, request
header := maxProfitBlock.Header

if header.Header == nil || (header.Header.ParentHash != parentHash) {
logger.Debug(ErrBadHeader)
logger.Debug(ErrBadHeader.Error())
rs.m.MissHeaderCount.WithLabelValues("badHeader").Add(1)
return nil, ErrNoBuilderBid
}
Expand Down

0 comments on commit c1cd8fc

Please sign in to comment.