From d475bab15f01030d18d6545596424a84ae82c841 Mon Sep 17 00:00:00 2001 From: naviechan Date: Mon, 1 May 2023 22:38:00 +0800 Subject: [PATCH] Add diagnostics endpoint for flags (#7417) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To expose context flags through diagnostic endpoint `/debug/metrics/flags`. The current `/debug/metrics/cmdline` only provides the command run by user. In the case when user runs Erigon using config file, `cmdline`’s info does not truly reflect the ‘launch setting' and flags set that Erigon is running on. ## Example ### Command ``` erigon --datadir /tmp/data --config test.yaml ``` ### Pseudo config file (in yaml) ``` datadir : '/tmp/data' chain : "sepolia" http : true metrics: true private.api.addr : "localhost:9090" http.api : ["eth","debug","net"] ``` ### Output ``` SUCCESS chain=sepolia config=test.yaml datadir=/tmp/data http=true http.api=eth,debug,net metrics=true private.api.addr=localhost:9090 ``` --- cmd/utils/flags/flags.go | 4 ++++ diagnostics/CHANGELOG.md | 23 +++++++++++++++++++++++ diagnostics/flags.go | 23 +++++++++++++++++++++++ diagnostics/version.go | 2 +- turbo/debug/flags.go | 1 + 5 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 diagnostics/CHANGELOG.md create mode 100644 diagnostics/flags.go diff --git a/cmd/utils/flags/flags.go b/cmd/utils/flags/flags.go index c4e583424f9..58887f9f8e3 100644 --- a/cmd/utils/flags/flags.go +++ b/cmd/utils/flags/flags.go @@ -46,6 +46,10 @@ func (s *DirectoryString) Set(value string) error { return nil } +func (s *DirectoryString) Get() any { + return s.String() +} + var ( _ cli.Flag = (*DirectoryFlag)(nil) _ cli.RequiredFlag = (*DirectoryFlag)(nil) diff --git a/diagnostics/CHANGELOG.md b/diagnostics/CHANGELOG.md new file mode 100644 index 00000000000..b0e88879c9a --- /dev/null +++ b/diagnostics/CHANGELOG.md @@ -0,0 +1,23 @@ +# Changelog + +All notable changes to `diagnostics` will be documented in this file. + +## Version 2 + +### Added + +- Introduce `flags` endpoint (#7417) + +### Changed + +- Increment diagnostic version to 2 in `version.go` (#7417) + +## Version 1 + +### Added + +- Introduce diagnostics versioning via `version` endpoint (#7300) +- Introduce `logs/list` and `logs/read` endpoint (#6930) +- Introduce `db/list`, `db/tables` and `db/read` endpoint (#6930) +- Introduce `cmdline` (#7271) +- Introduce `block_body_download` (#7373) diff --git a/diagnostics/flags.go b/diagnostics/flags.go new file mode 100644 index 00000000000..df6c9eaaf24 --- /dev/null +++ b/diagnostics/flags.go @@ -0,0 +1,23 @@ +package diagnostics + +import ( + "fmt" + "io" + "net/http" + + "github.com/urfave/cli/v2" +) + +func SetupFlagsAccess(ctx *cli.Context) { + http.HandleFunc("/debug/metrics/flags", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Access-Control-Allow-Origin", "*") + writeFlags(w, ctx) + }) +} + +func writeFlags(w io.Writer, ctx *cli.Context) { + fmt.Fprintf(w, "SUCCESS\n") + for _, flagName := range ctx.FlagNames() { + fmt.Fprintf(w, "%s=%v\n", flagName, ctx.Value(flagName)) + } +} diff --git a/diagnostics/version.go b/diagnostics/version.go index a8b97e73d2e..4fe3b5f68f3 100644 --- a/diagnostics/version.go +++ b/diagnostics/version.go @@ -8,7 +8,7 @@ import ( "github.com/ledgerwatch/erigon/params" ) -const Version = 1 +const Version = 2 func SetupVersionAccess() { http.HandleFunc("/debug/metrics/version", func(w http.ResponseWriter, r *http.Request) { diff --git a/turbo/debug/flags.go b/turbo/debug/flags.go index 54a8273e976..78277781cef 100644 --- a/turbo/debug/flags.go +++ b/turbo/debug/flags.go @@ -192,6 +192,7 @@ func Setup(ctx *cli.Context) error { diagnostics.SetupLogsAccess(ctx) diagnostics.SetupDbAccess(ctx) diagnostics.SetupCmdLineAccess() + diagnostics.SetupFlagsAccess(ctx) diagnostics.SetupVersionAccess() diagnostics.SetupBlockBodyDownload() }