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

Add fetch flags #10

Merged
merged 5 commits into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,14 @@ for the content fetched by the `fetchContent` javascript function and inserted i

![cmd line](/images/cmd_line.png)

## Flags
Operator can look at the flags that are set in cli context by the user to launch Erigon node. The corresponding code in Erigon is in the file `diagnostics/flags.go`. This is particularly useful when user launches Erigon using a config file with `--config` and [Command line arguments](#command-line-arguments) cannot fully capture the true state of the 'launch setting'. The returned flags are the result after parsing command line argument and config file by Erigon.

The code on the side of the diagnostics system is spread across files `cmd/ui_handler.go` (invocation of `processFlags` function), `cmd/flags.go`, `assests/template/session.html` (html template the part where the button `Fetch Flags` is defined with the javascript handler), `assests/script/session.js` (function `fetchContent`), `assets/template/flags.html` (html template for the content fetched by the `fetchContent` javascript function and inserted into the HTML div element).

![flags](/images/flags.png)


## Logs

Since version 2.43.0, Erigon nodes write logs by default with `INFO` level into `<datadir>/logs` directory, there is log rotation. Using diagnostics system,
Expand Down
12 changes: 12 additions & 0 deletions assets/template/flags.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<table>
<tr>
<th>Flag</th>
<th>Value</th>
</tr>
{{range $key, $value := .FlagPayload}}
<tr>
<td><code>{{$key}}</code></td>
<td><code>{{$value}}</code></td>
</tr>
{{end}}
</table>
4 changes: 4 additions & 0 deletions assets/template/session.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ <h2>SESSION: {{.SessionName}} PIN: {{.SessionPin}}</h2>
<button type="button" onclick="fetchContent('{{.SessionName}}', 'command line args', '/ui/cmd_line', 'cmdlineargs')">Fetch Command Line</button>
<div id="cmdlineargs"></div>
</div>
<div>
<button type="button" onclick="fetchContent('{{.SessionName}}', 'context flags', '/ui/flags', 'flags')">Fetch Flags</button>
<div id="flags"></div>
</div>
<div>
<button type="button" onclick="fetchContent('{{.SessionName}}', 'log list', '/ui/log_list', 'loglist')">Fetch Log List</button>
<div id="loglist"></div>
Expand Down
56 changes: 56 additions & 0 deletions cmd/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cmd

import (
"fmt"
"html/template"
"net/http"
"strings"
)

type Flags struct {
Success bool
Error string
FlagPayload map[string]string
}

func processFlags(w http.ResponseWriter, templ *template.Template, success bool, result string, versions Versions) {

if !versions.Success {
fmt.Fprintf(w, "Unable to process flag due to inability to get node version: %s", versions.Error)
return
}

if versions.NodeVersion < 2 {
fmt.Fprintf(w, "Flags only support version >= 2. Node version: %d", versions.NodeVersion)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's probably good enough for now, we shall test it

return
}

var flags Flags
flags.FlagPayload = make(map[string]string)
if success {
lines := strings.Split(result, "\n")
if len(lines) > 0 && strings.HasPrefix(lines[0], successLine) {
flags.Success = true

for _, line := range lines[1:] {
if len(line) > 0 {
flagName, flagValue, found := strings.Cut(line, "=")
if !found {
flags.Error = fmt.Sprintf("fail to parse line %s", line)
flags.Success = false
} else {
flags.FlagPayload[flagName] = flagValue
}
}
}
} else {
flags.Error = fmt.Sprintf("incorrect response (first line needs to be SUCCESS): %v", lines)
}
} else {
flags.Error = result
}
if err := templ.ExecuteTemplate(w, "flags.html", flags); err != nil {
fmt.Fprintf(w, "Executing flags template: %v", err)
return
}
}
8 changes: 7 additions & 1 deletion cmd/ui_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
const sessionIdCookieName = "sessionId"
const sessionIdCookieDuration = 30 * 24 * 3600 // 30 days

var uiRegex = regexp.MustCompile("^/ui/(cmd_line|log_list|log_head|log_tail|log_download|versions|reorgs|bodies_download|)$")
var uiRegex = regexp.MustCompile("^/ui/(cmd_line|flags|log_list|log_head|log_tail|log_download|versions|reorgs|bodies_download|)$")

func (uih *UiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
m := uiRegex.FindStringSubmatch(r.URL.Path)
Expand Down Expand Up @@ -71,6 +71,12 @@ func (uih *UiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
success, result := uih.fetch("/cmdline\n", requestChannel)
processCmdLineArgs(w, uih.uiTemplate, success, result)
return
case "flags":
versionCallSuccess, versionCallResult := uih.fetch("/version\n", requestChannel)
versions := processVersions(w, uih.uiTemplate, versionCallSuccess, versionCallResult, true)
success, result := uih.fetch("/flags\n", requestChannel)
processFlags(w, uih.uiTemplate, success, result, versions)
return
case "log_list":
success, result := uih.fetch("/logs/list\n", requestChannel)
processLogList(w, uih.uiTemplate, success, uiSession.SessionName, result)
Expand Down
19 changes: 17 additions & 2 deletions cmd/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,19 @@ type Versions struct {
GitCommit string
}

func processVersions(w http.ResponseWriter, templ *template.Template, success bool, result string) {
func processVersions(w http.ResponseWriter, templ *template.Template, success bool, result string, skipUpdateHTML ...bool) Versions {
versions := processVersionsResponse(w, templ, success, result)

if len(skipUpdateHTML) == 0 || !skipUpdateHTML[0] {
updateHTML(w, templ, versions)
}

return versions
}

func processVersionsResponse(w http.ResponseWriter, templ *template.Template, success bool, result string) Versions {
var versions Versions

if success {
lines := strings.Split(result, "\n")
if len(lines) > 0 && strings.HasPrefix(lines[0], successLine) {
Expand Down Expand Up @@ -49,8 +60,12 @@ func processVersions(w http.ResponseWriter, templ *template.Template, success bo
} else {
versions.Error = result
}

return versions
}

func updateHTML(w http.ResponseWriter, templ *template.Template, versions Versions) {
if err := templ.ExecuteTemplate(w, "versions.html", versions); err != nil {
fmt.Fprintf(w, "Executing versions template: %v", err)
return
}
}
Binary file added images/flags.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.