Skip to content

Commit

Permalink
Diag session routing (erigontech#8232)
Browse files Browse the repository at this point in the history
Code to support react based UI for diagnostics:

* pprof, prometheus and diagnistics rationalized to use a single router
(i.e. they can all run in the same port)
* support_cmd updated to support node routing (was only first node)
* Multi content support in router tunnel (application/octet-stream &
appliaction/json)
* Routing requests changed from using http forms to rest + query params
* REST query requests can now be made against erigon base port and
diagnostics with the same url format/params

---------

Co-authored-by: dvovk <[email protected]>
Co-authored-by: Mark Holt <[email protected]>
  • Loading branch information
3 people authored Sep 25, 2023
1 parent 5641b52 commit f794438
Show file tree
Hide file tree
Showing 32 changed files with 945 additions and 403 deletions.
3 changes: 2 additions & 1 deletion cmd/abigen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ package main
import (
"encoding/json"
"fmt"
"github.com/ledgerwatch/erigon-lib/common"
"io"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/ledgerwatch/erigon-lib/common"

"github.com/ledgerwatch/log/v3"
"github.com/urfave/cli/v2"

Expand Down
2 changes: 1 addition & 1 deletion cmd/caplin-phase1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func runCaplinNode(cliCtx *cli.Context) error {
if err != nil {
log.Error("[Phase1] Could not initialize caplin", "err", err)
}
if _, err := debug.Setup(cliCtx, true /* root logger */); err != nil {
if _, _, err := debug.Setup(cliCtx, true /* root logger */); err != nil {
return err
}
log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(cfg.LogLvl), log.StderrHandler))
Expand Down
9 changes: 8 additions & 1 deletion cmd/devnet/devnet/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
context "context"
"fmt"
"math/big"
"net/http"
"sync"

"github.com/c2h5oh/datasize"
"github.com/ledgerwatch/erigon/cmd/devnet/accounts"
"github.com/ledgerwatch/erigon/cmd/devnet/args"
"github.com/ledgerwatch/erigon/cmd/devnet/requests"
"github.com/ledgerwatch/erigon/diagnostics"
"github.com/ledgerwatch/erigon/eth/ethconfig"
"github.com/ledgerwatch/erigon/node/nodecfg"
"github.com/ledgerwatch/erigon/params"
Expand Down Expand Up @@ -129,6 +131,7 @@ func (n *node) ChainID() *big.Int {
func (n *node) run(ctx *cli.Context) error {
var logger log.Logger
var err error
var metricsMux *http.ServeMux

defer n.done()
defer func() {
Expand All @@ -141,7 +144,7 @@ func (n *node) run(ctx *cli.Context) error {
n.Unlock()
}()

if logger, err = debug.Setup(ctx, false /* rootLogger */); err != nil {
if logger, metricsMux, err = debug.Setup(ctx, false /* rootLogger */); err != nil {
return err
}

Expand All @@ -166,6 +169,10 @@ func (n *node) run(ctx *cli.Context) error {

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

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

n.Lock()
if n.startErr != nil {
n.startErr <- err
Expand Down
4 changes: 2 additions & 2 deletions cmd/devnet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ var (
}

metricsURLsFlag = cli.StringSliceFlag{
Name: "metrics.urls",
Name: "debug.urls",
Usage: "internal flag",
}

Expand Down Expand Up @@ -199,7 +199,7 @@ func action(ctx *cli.Context) error {
if metrics {
// TODO should get this from the network as once we have multiple nodes we'll need to iterate the
// nodes and create a series of urls - for the moment only one is supported
ctx.Set("metrics.urls", fmt.Sprintf("http://localhost:%d/debug/metrics/", ctx.Int("metrics.port")))
ctx.Set("metrics.urls", fmt.Sprintf("http://localhost:%d/debug/", ctx.Int("metrics.port")))
}

// start the network with each node in a go routine
Expand Down
11 changes: 10 additions & 1 deletion cmd/erigon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package main
import (
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
"reflect"
"strings"

"github.com/ledgerwatch/erigon-lib/common/dbg"
"github.com/ledgerwatch/erigon/diagnostics"
"github.com/ledgerwatch/erigon/metrics"
"github.com/ledgerwatch/log/v3"
"github.com/pelletier/go-toml"
Expand Down Expand Up @@ -55,7 +57,9 @@ func runErigon(cliCtx *cli.Context) error {

var logger log.Logger
var err error
if logger, err = debug.Setup(cliCtx, true /* root logger */); err != nil {
var metricsMux *http.ServeMux

if logger, metricsMux, err = debug.Setup(cliCtx, true /* root logger */); err != nil {
return err
}

Expand All @@ -73,6 +77,11 @@ func runErigon(cliCtx *cli.Context) error {
log.Error("Erigon startup", "err", err)
return err
}

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

err = ethNode.Serve()
if err != nil {
log.Error("error while serving an Erigon node", "err", err)
Expand Down
17 changes: 17 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -813,10 +813,27 @@ var (
Usage: "Max allowed page size for search methods",
Value: 25,
}

DiagnosticsURLFlag = cli.StringFlag{
Name: "diagnostics.url",
Usage: "URL of the diagnostics system provided by the support team",
}

DiagnosticsInsecureFlag = cli.BoolFlag{
Name: "diagnostics.insecure",
Usage: "Allows communication with diagnostics system using self-signed TLS certificates",
}

DiagnosticsSessionsFlag = cli.StringSliceFlag{
Name: "diagnostics.ids",
Usage: "Comma separated list of support session ids to connect to",
}
)

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

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

// setNodeKey loads a node key from command line flags if provided,
// otherwise it tries to load it from datadir,
// otherwise it generates a new key in datadir.
Expand Down
4 changes: 4 additions & 0 deletions cmd/utils/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,10 @@ func (b *bigValue) Set(s string) error {
return nil
}

func (b *bigValue) Get() any {
return b.String()
}

// GlobalBig returns the value of a BigFlag from the global flag set.
func GlobalBig(ctx *cli.Context, name string) *big.Int {
val := ctx.Generic(name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func SetupBlockBodyDownload(metricsMux *http.ServeMux) {
metricsMux.HandleFunc("/debug/metrics/block_body_download", func(w http.ResponseWriter, r *http.Request) {
metricsMux.HandleFunc("/block_body_download", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
writeBlockBodyDownload(w, r)
})
Expand Down
29 changes: 18 additions & 11 deletions diagnostics/cmd_line.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
package diagnostics

import (
"fmt"
"io"
"net/http"
"os"
"strconv"
"strings"
)

func SetupCmdLineAccess(metricsMux *http.ServeMux) {
metricsMux.HandleFunc("/debug/metrics/cmdline", func(w http.ResponseWriter, r *http.Request) {
metricsMux.HandleFunc("/cmdline", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
writeCmdLine(w)
})
}
w.Header().Set("Content-Type", "application/json")

var space []byte

func writeCmdLine(w io.Writer) {
fmt.Fprintf(w, "SUCCESS\n")
for _, arg := range os.Args {
fmt.Fprintf(w, "%s\n", arg)
}
w.Write([]byte{'"'})
for _, arg := range os.Args {
if len(space) > 0 {
w.Write(space)
} else {
space = []byte(" ")
}

w.Write([]byte(strings.Trim(strconv.Quote(arg), `"`)))
}
w.Write([]byte{'"'})
})
}
Loading

0 comments on commit f794438

Please sign in to comment.