mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-09 13:28:01 -05:00
Add /heads page (#3410)
This commit is contained in:
@@ -4,6 +4,7 @@ go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"chain_info.go",
|
||||
"info.go",
|
||||
"log.go",
|
||||
"metrics.go",
|
||||
"receive_attestation.go",
|
||||
|
||||
57
beacon-chain/blockchain/info.go
Normal file
57
beacon-chain/blockchain/info.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package blockchain
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sort"
|
||||
|
||||
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const latestSlotCount = 10
|
||||
|
||||
// HeadsHandler is a handler to serve /heads page in metrics.
|
||||
func (c *ChainService) HeadsHandler(w http.ResponseWriter, _ *http.Request) {
|
||||
buf := new(bytes.Buffer)
|
||||
|
||||
if _, err := fmt.Fprintf(w, "\n %s\t%s\t", "Head slot", "Head root"); err != nil {
|
||||
logrus.WithError(err).Error("Failed to render chain heads page")
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := fmt.Fprintf(w, "\n %s\t%s\t", "---------", "---------"); err != nil {
|
||||
logrus.WithError(err).Error("Failed to render chain heads page")
|
||||
return
|
||||
}
|
||||
|
||||
slots := c.latestHeadSlots()
|
||||
for _, s := range slots {
|
||||
r := hex.EncodeToString(bytesutil.Trunc(c.canonicalRoots[uint64(s)]))
|
||||
if _, err := fmt.Fprintf(w, "\n %d\t\t%s\t", s, r); err != nil {
|
||||
logrus.WithError(err).Error("Failed to render chain heads page")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
if _, err := w.Write(buf.Bytes()); err != nil {
|
||||
log.WithError(err).Error("Failed to render chain heads page")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// This returns the latest head slots in a slice and up to latestSlotCount
|
||||
func (c *ChainService) latestHeadSlots() []int {
|
||||
s := make([]int, 0, len(c.canonicalRoots))
|
||||
for k := range c.canonicalRoots {
|
||||
s = append(s, int(k))
|
||||
}
|
||||
sort.Ints(s)
|
||||
if (len(s)) > latestSlotCount {
|
||||
return s[len(s)-latestSlotCount:]
|
||||
}
|
||||
return s
|
||||
}
|
||||
@@ -414,9 +414,14 @@ func (b *BeaconNode) registerPrometheusService(ctx *cli.Context) error {
|
||||
if err := b.services.FetchService(&p); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
additionalHandlers = append(additionalHandlers, prometheus.Handler{Path: "/p2p", Handler: p.InfoHandler})
|
||||
|
||||
var c *blockchain.ChainService
|
||||
if err := b.services.FetchService(&c); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
additionalHandlers = append(additionalHandlers, prometheus.Handler{Path: "/heads", Handler: c.HeadsHandler})
|
||||
|
||||
service := prometheus.NewPrometheusService(
|
||||
fmt.Sprintf(":%d", ctx.GlobalInt64(cmd.MonitoringPortFlag.Name)),
|
||||
b.services,
|
||||
|
||||
Reference in New Issue
Block a user