Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dvovk/enable_dignostic #10083

Merged
merged 12 commits into from
Apr 29, 2024
4 changes: 1 addition & 3 deletions cmd/devnet/devnet/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,7 @@ func (n *devnetNode) run(ctx *cli.Context) error {

n.ethNode, err = enode.New(ctx.Context, n.nodeCfg, n.ethCfg, logger)

if metricsMux != nil {
diagnostics.Setup(ctx, metricsMux, n.ethNode)
}
diagnostics.Setup(ctx, n.ethNode, metricsMux)

n.Lock()
if n.startErr != nil {
Expand Down
4 changes: 1 addition & 3 deletions cmd/erigon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ func runErigon(cliCtx *cli.Context) error {
return err
}

if metricsMux != nil {
diagnostics.Setup(cliCtx, metricsMux, ethNode)
}
diagnostics.Setup(cliCtx, ethNode, metricsMux)

err = ethNode.Serve()
if err != nil {
Expand Down
17 changes: 16 additions & 1 deletion cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -995,9 +995,24 @@ var (
Usage: "set the cors' allow origins",
Value: cli.NewStringSlice(),
}
DiagDisabledFlag = cli.BoolFlag{
Name: "diagnostics.disabled",
Usage: "Disable diagnostics",
Value: false,
}
DiagEndpointAddrFlag = cli.StringFlag{
Name: "diagnostics.endpoint.addr",
Usage: "Diagnostics HTTP server listening interface",
Value: "0.0.0.0",
}
DiagEndpointPortFlag = cli.UintFlag{
Name: "diagnostics.endpoint.port",
Usage: "Diagnostics HTTP server listening port",
Value: 6060,
}
)

var MetricFlags = []cli.Flag{&MetricsEnabledFlag, &MetricsHTTPFlag, &MetricsPortFlag}
var MetricFlags = []cli.Flag{&MetricsEnabledFlag, &MetricsHTTPFlag, &MetricsPortFlag, &DiagDisabledFlag, &DiagEndpointAddrFlag, &DiagEndpointPortFlag}

var DiagnosticsFlags = []cli.Flag{&DiagnosticsURLFlag, &DiagnosticsURLFlag, &DiagnosticsSessionsFlag}

Expand Down
4 changes: 4 additions & 0 deletions diagnostics/block_body_download_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
)

func SetupBlockBodyDownload(metricsMux *http.ServeMux) {
if metricsMux == nil {
return
}

metricsMux.HandleFunc("/block_body_download", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
writeBlockBodyDownload(w, r)
Expand Down
4 changes: 4 additions & 0 deletions diagnostics/bodies_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
)

func SetupBodiesAccess(metricsMux *http.ServeMux, diag *diaglib.DiagnosticClient) {
if metricsMux == nil {
return
}

metricsMux.HandleFunc("/bodies", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
Expand Down
4 changes: 4 additions & 0 deletions diagnostics/bootnodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
)

func SetupBootnodesAccess(metricsMux *http.ServeMux, node *node.ErigonNode) {
if metricsMux == nil {
return
}

metricsMux.HandleFunc("/bootnodes", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
Expand Down
4 changes: 4 additions & 0 deletions diagnostics/cmd_line.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
)

func SetupCmdLineAccess(metricsMux *http.ServeMux) {
if metricsMux == nil {
return
}

metricsMux.HandleFunc("/cmdline", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
Expand Down
4 changes: 4 additions & 0 deletions diagnostics/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import (
)

func SetupDbAccess(ctx *cli.Context, metricsMux *http.ServeMux) {
if metricsMux == nil {
return
}

var dataDir string
if ctx.IsSet("datadir") {
dataDir = ctx.String("datadir")
Expand Down
4 changes: 4 additions & 0 deletions diagnostics/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
)

func SetupFlagsAccess(ctx *cli.Context, metricsMux *http.ServeMux) {
if metricsMux == nil {
return
}

metricsMux.HandleFunc("/flags", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
Expand Down
4 changes: 4 additions & 0 deletions diagnostics/header_downloader_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
)

func SetupHeaderDownloadStats(metricsMux *http.ServeMux) {
if metricsMux == nil {
return
}

metricsMux.HandleFunc("/headers_download", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
writeHeaderDownload(w, r)
Expand Down
4 changes: 4 additions & 0 deletions diagnostics/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
)

func SetupHeadersAccess(metricsMux *http.ServeMux, diag *diaglib.DiagnosticClient) {
if metricsMux == nil {
return
}

metricsMux.HandleFunc("/headers", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
Expand Down
4 changes: 4 additions & 0 deletions diagnostics/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import (
)

func SetupLogsAccess(ctx *cli.Context, metricsMux *http.ServeMux) {
if metricsMux == nil {
return
}

dirPath := ctx.String(logging.LogDirPathFlag.Name)
if dirPath == "" {
datadir := ctx.String("datadir")
Expand Down
4 changes: 4 additions & 0 deletions diagnostics/mem.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
)

func SetupMemAccess(metricsMux *http.ServeMux) {
if metricsMux == nil {
return
}

metricsMux.HandleFunc("/mem", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
Expand Down
4 changes: 4 additions & 0 deletions diagnostics/nodeinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
)

func SetupNodeInfoAccess(metricsMux *http.ServeMux, node *node.ErigonNode) {
if metricsMux == nil {
return
}

metricsMux.HandleFunc("/nodeinfo", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
writeNodeInfo(w, node)
Expand Down
4 changes: 4 additions & 0 deletions diagnostics/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ type PeerResponse struct {
}

func SetupPeersAccess(ctxclient *cli.Context, metricsMux *http.ServeMux, node *node.ErigonNode, diag *diaglib.DiagnosticClient) {
if metricsMux == nil {
return
}

metricsMux.HandleFunc("/peers", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
Expand Down
102 changes: 80 additions & 22 deletions diagnostics/setup.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,98 @@
package diagnostics

import (
"fmt"
"net/http"
"strings"

"github.com/urfave/cli/v2"

diaglib "github.com/ledgerwatch/erigon-lib/diagnostics"
"github.com/ledgerwatch/erigon/turbo/node"
"github.com/ledgerwatch/log/v3"
)

func Setup(ctx *cli.Context, metricsMux *http.ServeMux, node *node.ErigonNode) {
debugMux := http.NewServeMux()
var (
diagnosticsDisabledFlag = "diagnostics.disabled"
diagnosticsAddrFlag = "diagnostics.endpoint.addr"
diagnosticsPortFlag = "diagnostics.endpoint.port"
metricsHTTPFlag = "metrics.addr"
metricsPortFlag = "metrics.port"
)

func Setup(ctx *cli.Context, node *node.ErigonNode, metricsMux *http.ServeMux) {
if ctx.Bool(diagnosticsDisabledFlag) {
return
}

var diagMux *http.ServeMux

diagHost := ctx.String(diagnosticsAddrFlag)
diagPort := ctx.Int(diagnosticsPortFlag)
diagAddress := fmt.Sprintf("%s:%d", diagHost, diagPort)

metricsHost := ctx.String(metricsHTTPFlag)
metricsPort := ctx.Int(metricsPortFlag)
metricsAddress := fmt.Sprintf("%s:%d", metricsHost, metricsPort)

diagnostic := diaglib.NewDiagnosticClient(debugMux, node.Backend().DataDir())
if diagAddress == metricsAddress {
diagMux = SetupDiagnosticsEndpoint(metricsMux, diagAddress)
} else {
diagMux = SetupDiagnosticsEndpoint(nil, diagAddress)
}

diagnostic := diaglib.NewDiagnosticClient(diagMux, node.Backend().DataDir())
diagnostic.Setup()

metricsMux.HandleFunc("/debug/", func(w http.ResponseWriter, r *http.Request) {
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/debug")
r.URL.RawPath = strings.TrimPrefix(r.URL.RawPath, "/debug")
debugMux.ServeHTTP(w, r)
})
SetupEndpoints(ctx, node, diagMux, diagnostic)
}

func SetupDiagnosticsEndpoint(metricsMux *http.ServeMux, addres string) *http.ServeMux {
diagMux := http.NewServeMux()

SetupLogsAccess(ctx, debugMux)
SetupDbAccess(ctx, debugMux)
SetupCmdLineAccess(debugMux)
SetupFlagsAccess(ctx, debugMux)
SetupVersionAccess(debugMux)
SetupBlockBodyDownload(debugMux)
SetupHeaderDownloadStats(debugMux)
SetupNodeInfoAccess(debugMux, node)
SetupPeersAccess(ctx, debugMux, node, diagnostic)
SetupBootnodesAccess(debugMux, node)
SetupStagesAccess(debugMux, diagnostic)
SetupMemAccess(debugMux)
SetupHeadersAccess(debugMux, diagnostic)
SetupBodiesAccess(debugMux, diagnostic)
if metricsMux != nil {
SetupMiddleMuxHandler(diagMux, metricsMux, "/debug/diag")
} else {
middleMux := http.NewServeMux()
SetupMiddleMuxHandler(diagMux, middleMux, "/debug/diag")

diagServer := &http.Server{
Addr: addres,
Handler: middleMux,
}

go func() {
if err := diagServer.ListenAndServe(); err != nil {
log.Error("[Diagnostics] Failure in running diagnostics server", "err", err)
}
}()

}

return diagMux
}

func SetupMiddleMuxHandler(mux *http.ServeMux, middleMux *http.ServeMux, path string) {
middleMux.HandleFunc(path+"/", func(w http.ResponseWriter, r *http.Request) {
r.URL.Path = strings.TrimPrefix(r.URL.Path, path)
r.URL.RawPath = strings.TrimPrefix(r.URL.RawPath, path)
mux.ServeHTTP(w, r)
})
}

func SetupEndpoints(ctx *cli.Context, node *node.ErigonNode, diagMux *http.ServeMux, diagnostic *diaglib.DiagnosticClient) {
SetupLogsAccess(ctx, diagMux)
SetupDbAccess(ctx, diagMux)
SetupCmdLineAccess(diagMux)
SetupFlagsAccess(ctx, diagMux)
SetupVersionAccess(diagMux)
SetupBlockBodyDownload(diagMux)
SetupHeaderDownloadStats(diagMux)
SetupNodeInfoAccess(diagMux, node)
SetupPeersAccess(ctx, diagMux, node, diagnostic)
SetupBootnodesAccess(diagMux, node)
SetupStagesAccess(diagMux, diagnostic)
SetupMemAccess(diagMux)
SetupHeadersAccess(diagMux, diagnostic)
SetupBodiesAccess(diagMux, diagnostic)
}
4 changes: 4 additions & 0 deletions diagnostics/snapshot_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
)

func SetupStagesAccess(metricsMux *http.ServeMux, diag *diaglib.DiagnosticClient) {
if metricsMux == nil {
return
}

metricsMux.HandleFunc("/snapshot-sync", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
Expand Down
4 changes: 4 additions & 0 deletions diagnostics/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
const Version = 3

func SetupVersionAccess(metricsMux *http.ServeMux) {
if metricsMux == nil {
return
}

metricsMux.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
Expand Down
2 changes: 1 addition & 1 deletion erigon-lib/common/metrics/metrics_enabled.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ type Config struct { //nolint:maligned
var DefaultConfig = Config{
Enabled: false,
EnabledExpensive: false,
HTTP: "127.0.0.1",
HTTP: "0.0.0.0",
Port: 6060,
}
2 changes: 1 addition & 1 deletion erigon-lib/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -688,4 +688,4 @@ modernc.org/sqlite v1.26.0/go.mod h1:FL3pVXie73rg3Rii6V/u5BoHlSoyeZeIgKZEgHARyCU
rsc.io/tmplfunc v0.0.3 h1:53XFQh69AfOa8Tw0Jm7t+GV7KZhOi6jzsCzTtKbMvzU=
rsc.io/tmplfunc v0.0.3/go.mod h1:AG3sTPzElb1Io3Yg4voV9AGZJuleGAwaVRxL9M49PhA=
zombiezen.com/go/sqlite v0.13.1 h1:qDzxyWWmMtSSEH5qxamqBFmqA2BLSSbtODi3ojaE02o=
zombiezen.com/go/sqlite v0.13.1/go.mod h1:Ht/5Rg3Ae2hoyh1I7gbWtWAl89CNocfqeb/aAMTkJr4=
zombiezen.com/go/sqlite v0.13.1/go.mod h1:Ht/5Rg3Ae2hoyh1I7gbWtWAl89CNocfqeb/aAMTkJr4=
4 changes: 1 addition & 3 deletions turbo/app/snapshots_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -708,9 +708,7 @@ func doUploaderCommand(cliCtx *cli.Context) error {
return err
}

if metricsMux != nil {
diagnostics.Setup(cliCtx, metricsMux, ethNode)
}
diagnostics.Setup(cliCtx, ethNode, metricsMux)

err = ethNode.Serve()
if err != nil {
Expand Down
Loading
Loading