Skip to content

Commit

Permalink
custom getPayload metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
liamaharon committed Aug 23, 2024
1 parent 0788bf1 commit a884bf8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
5 changes: 5 additions & 0 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,11 @@ func (b *Builder) GetPayload(request PayloadRequestV1) (*builderSpec.VersionedSu
}

func (b *Builder) handleGetPayload(w http.ResponseWriter, req *http.Request) {
start := time.Now()
vars := mux.Vars(req)
slot, err := strconv.Atoi(vars["slot"])
if err != nil {
updateServeTimeHistogram("getPayload", false, time.Since(start))
respondError(w, http.StatusBadRequest, "incorrect slot")
return
}
Expand All @@ -238,16 +240,19 @@ func (b *Builder) handleGetPayload(w http.ResponseWriter, req *http.Request) {
})
if err != nil {
handleError(w, err)
updateServeTimeHistogram("getPayload", false, time.Since(start))
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(bestSubmission); err != nil {
updateServeTimeHistogram("getPayload", false, time.Since(start))
log.Error("could not encode response", "err", err)
respondError(w, http.StatusInternalServerError, "could not encode response")
return
}
updateServeTimeHistogram("getPayload", true, time.Since(start))
}

func (b *Builder) saveBlockSubmission(opts SubmitBlockOpts) error {
Expand Down
39 changes: 39 additions & 0 deletions builder/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2020 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package builder

import (
"fmt"
"time"

"github.com/ethereum/go-ethereum/metrics"
)

var serveTimeHistName = "builder/duration"

// updateServeTimeHistogram tracks the serving time of serving a call.
func updateServeTimeHistogram(method string, success bool, elapsed time.Duration) {
note := "success"
if !success {
note = "failure"
}
h := fmt.Sprintf("%s/%s/%s", serveTimeHistName, method, note)
sampler := func() metrics.Sample {
return metrics.NewExpDecaySample(1028, 0.015)
}
metrics.GetOrRegisterHistogramLazy(h, nil, sampler).Update(elapsed.Nanoseconds())
}
4 changes: 1 addition & 3 deletions rpc/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ func updateServeTimeHistogram(method string, success bool, elapsed time.Duration
}
h := fmt.Sprintf("%s/%s/%s", serveTimeHistName, method, note)
sampler := func() metrics.Sample {
return metrics.ResettingSample(
metrics.NewExpDecaySample(1028, 0.015),
)
return metrics.NewExpDecaySample(1028, 0.015)
}
metrics.GetOrRegisterHistogramLazy(h, nil, sampler).Update(elapsed.Nanoseconds())
}

0 comments on commit a884bf8

Please sign in to comment.