Skip to content

Commit f794438

Browse files
mh0ltdvovkMark Holt
authored
Diag session routing (erigontech#8232)
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]>
1 parent 5641b52 commit f794438

32 files changed

+945
-403
lines changed

cmd/abigen/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ package main
1919
import (
2020
"encoding/json"
2121
"fmt"
22-
"github.com/ledgerwatch/erigon-lib/common"
2322
"io"
2423
"os"
2524
"path/filepath"
2625
"regexp"
2726
"strings"
2827

28+
"github.com/ledgerwatch/erigon-lib/common"
29+
2930
"github.com/ledgerwatch/log/v3"
3031
"github.com/urfave/cli/v2"
3132

cmd/caplin-phase1/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func runCaplinNode(cliCtx *cli.Context) error {
6060
if err != nil {
6161
log.Error("[Phase1] Could not initialize caplin", "err", err)
6262
}
63-
if _, err := debug.Setup(cliCtx, true /* root logger */); err != nil {
63+
if _, _, err := debug.Setup(cliCtx, true /* root logger */); err != nil {
6464
return err
6565
}
6666
log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(cfg.LogLvl), log.StderrHandler))

cmd/devnet/devnet/node.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import (
44
context "context"
55
"fmt"
66
"math/big"
7+
"net/http"
78
"sync"
89

910
"github.com/c2h5oh/datasize"
1011
"github.com/ledgerwatch/erigon/cmd/devnet/accounts"
1112
"github.com/ledgerwatch/erigon/cmd/devnet/args"
1213
"github.com/ledgerwatch/erigon/cmd/devnet/requests"
14+
"github.com/ledgerwatch/erigon/diagnostics"
1315
"github.com/ledgerwatch/erigon/eth/ethconfig"
1416
"github.com/ledgerwatch/erigon/node/nodecfg"
1517
"github.com/ledgerwatch/erigon/params"
@@ -129,6 +131,7 @@ func (n *node) ChainID() *big.Int {
129131
func (n *node) run(ctx *cli.Context) error {
130132
var logger log.Logger
131133
var err error
134+
var metricsMux *http.ServeMux
132135

133136
defer n.done()
134137
defer func() {
@@ -141,7 +144,7 @@ func (n *node) run(ctx *cli.Context) error {
141144
n.Unlock()
142145
}()
143146

144-
if logger, err = debug.Setup(ctx, false /* rootLogger */); err != nil {
147+
if logger, metricsMux, err = debug.Setup(ctx, false /* rootLogger */); err != nil {
145148
return err
146149
}
147150

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

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

172+
if metricsMux != nil {
173+
diagnostics.Setup(ctx, metricsMux, n.ethNode)
174+
}
175+
169176
n.Lock()
170177
if n.startErr != nil {
171178
n.startErr <- err

cmd/devnet/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ var (
107107
}
108108

109109
metricsURLsFlag = cli.StringSliceFlag{
110-
Name: "metrics.urls",
110+
Name: "debug.urls",
111111
Usage: "internal flag",
112112
}
113113

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

205205
// start the network with each node in a go routine

cmd/erigon/main.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package main
33
import (
44
"errors"
55
"fmt"
6+
"net/http"
67
"os"
78
"path/filepath"
89
"reflect"
910
"strings"
1011

1112
"github.com/ledgerwatch/erigon-lib/common/dbg"
13+
"github.com/ledgerwatch/erigon/diagnostics"
1214
"github.com/ledgerwatch/erigon/metrics"
1315
"github.com/ledgerwatch/log/v3"
1416
"github.com/pelletier/go-toml"
@@ -55,7 +57,9 @@ func runErigon(cliCtx *cli.Context) error {
5557

5658
var logger log.Logger
5759
var err error
58-
if logger, err = debug.Setup(cliCtx, true /* root logger */); err != nil {
60+
var metricsMux *http.ServeMux
61+
62+
if logger, metricsMux, err = debug.Setup(cliCtx, true /* root logger */); err != nil {
5963
return err
6064
}
6165

@@ -73,6 +77,11 @@ func runErigon(cliCtx *cli.Context) error {
7377
log.Error("Erigon startup", "err", err)
7478
return err
7579
}
80+
81+
if metricsMux != nil {
82+
diagnostics.Setup(cliCtx, metricsMux, ethNode)
83+
}
84+
7685
err = ethNode.Serve()
7786
if err != nil {
7887
log.Error("error while serving an Erigon node", "err", err)

cmd/utils/flags.go

+17
Original file line numberDiff line numberDiff line change
@@ -813,10 +813,27 @@ var (
813813
Usage: "Max allowed page size for search methods",
814814
Value: 25,
815815
}
816+
817+
DiagnosticsURLFlag = cli.StringFlag{
818+
Name: "diagnostics.url",
819+
Usage: "URL of the diagnostics system provided by the support team",
820+
}
821+
822+
DiagnosticsInsecureFlag = cli.BoolFlag{
823+
Name: "diagnostics.insecure",
824+
Usage: "Allows communication with diagnostics system using self-signed TLS certificates",
825+
}
826+
827+
DiagnosticsSessionsFlag = cli.StringSliceFlag{
828+
Name: "diagnostics.ids",
829+
Usage: "Comma separated list of support session ids to connect to",
830+
}
816831
)
817832

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

835+
var DiagnosticsFlags = []cli.Flag{&DiagnosticsURLFlag, &DiagnosticsURLFlag, &DiagnosticsSessionsFlag}
836+
820837
// setNodeKey loads a node key from command line flags if provided,
821838
// otherwise it tries to load it from datadir,
822839
// otherwise it generates a new key in datadir.

cmd/utils/flags/flags.go

+4
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,10 @@ func (b *bigValue) Set(s string) error {
305305
return nil
306306
}
307307

308+
func (b *bigValue) Get() any {
309+
return b.String()
310+
}
311+
308312
// GlobalBig returns the value of a BigFlag from the global flag set.
309313
func GlobalBig(ctx *cli.Context, name string) *big.Int {
310314
val := ctx.Generic(name)

diagnostics/block_body_download.go renamed to diagnostics/block_body_download_stats.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
func SetupBlockBodyDownload(metricsMux *http.ServeMux) {
13-
metricsMux.HandleFunc("/debug/metrics/block_body_download", func(w http.ResponseWriter, r *http.Request) {
13+
metricsMux.HandleFunc("/block_body_download", func(w http.ResponseWriter, r *http.Request) {
1414
w.Header().Set("Access-Control-Allow-Origin", "*")
1515
writeBlockBodyDownload(w, r)
1616
})

diagnostics/cmd_line.go

+18-11
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
package diagnostics
22

33
import (
4-
"fmt"
5-
"io"
64
"net/http"
75
"os"
6+
"strconv"
7+
"strings"
88
)
99

1010
func SetupCmdLineAccess(metricsMux *http.ServeMux) {
11-
metricsMux.HandleFunc("/debug/metrics/cmdline", func(w http.ResponseWriter, r *http.Request) {
11+
metricsMux.HandleFunc("/cmdline", func(w http.ResponseWriter, r *http.Request) {
1212
w.Header().Set("Access-Control-Allow-Origin", "*")
13-
writeCmdLine(w)
14-
})
15-
}
13+
w.Header().Set("Content-Type", "application/json")
14+
15+
var space []byte
1616

17-
func writeCmdLine(w io.Writer) {
18-
fmt.Fprintf(w, "SUCCESS\n")
19-
for _, arg := range os.Args {
20-
fmt.Fprintf(w, "%s\n", arg)
21-
}
17+
w.Write([]byte{'"'})
18+
for _, arg := range os.Args {
19+
if len(space) > 0 {
20+
w.Write(space)
21+
} else {
22+
space = []byte(" ")
23+
}
24+
25+
w.Write([]byte(strings.Trim(strconv.Quote(arg), `"`)))
26+
}
27+
w.Write([]byte{'"'})
28+
})
2229
}

0 commit comments

Comments
 (0)