[Diagnostics] expose command line args via metrics (#7271)

Co-authored-by: Alexey Sharp <alexeysharp@Alexeys-iMac.local>
This commit is contained in:
ledgerwatch 2023-04-06 16:34:06 +01:00 committed by GitHub
parent 9b5d7357dd
commit 3d904d509e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 7 deletions

22
diagnostics/cmd_line.go Normal file
View File

@ -0,0 +1,22 @@
package diagnostics
import (
"fmt"
"io"
"net/http"
"os"
)
func SetupCmdLineAccess() {
http.HandleFunc("/debug/metrics/cmdline", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
writeCmdLine(w)
})
}
func writeCmdLine(w io.Writer) {
fmt.Fprintf(w, "SUCCESS\n")
for _, arg := range os.Args {
fmt.Fprintf(w, "%s\n", arg)
}
}

View File

@ -76,9 +76,6 @@ func connectDiagnostics(cliCtx *cli.Context) error {
cancel() cancel()
}() }()
pollInterval := 500 * time.Millisecond
pollEvery := time.NewTicker(pollInterval)
defer pollEvery.Stop()
metricsURLs := cliCtx.StringSlice(metricsURLsFlag.Name) metricsURLs := cliCtx.StringSlice(metricsURLsFlag.Name)
metricsURL := metricsURLs[0] // TODO: Generalise metricsURL := metricsURLs[0] // TODO: Generalise
@ -110,14 +107,14 @@ func connectDiagnostics(cliCtx *cli.Context) error {
if err := tunnel(ctx, tlsConfig, diagnosticsUrl, metricsURL); err != nil { if err := tunnel(ctx, tlsConfig, diagnosticsUrl, metricsURL); err != nil {
return err return err
} }
log.Info("Reconnecting in 1 second...")
timer := time.NewTimer(1 * time.Second)
select { select {
case <-timer.C:
case <-ctx.Done(): case <-ctx.Done():
// Quit immediately if the context was cancelled (by Ctrl-C or TERM signal) // Quit immediately if the context was cancelled (by Ctrl-C or TERM signal)
return nil return nil
default:
} }
log.Info("Reconnecting in 10 seconds...")
time.Sleep(10 * time.Second)
} }
} }
@ -144,7 +141,7 @@ func tunnel(ctx context.Context, tlsConfig *tls.Config, diagnosticsUrl string, m
return err return err
} }
// Create a connection // Create a connection
ctx1, cancel1 := context.WithCancel(req.Context()) ctx1, cancel1 := context.WithCancel(ctx)
defer cancel1() defer cancel1()
defer resp.Body.Close() defer resp.Body.Close()
defer writer.Close() defer writer.Close()
@ -160,6 +157,7 @@ func tunnel(ctx context.Context, tlsConfig *tls.Config, diagnosticsUrl string, m
if string(firstLine) != "SUCCESS\n" { if string(firstLine) != "SUCCESS\n" {
return fmt.Errorf("connecting to diagnostics system: %s", firstLine) return fmt.Errorf("connecting to diagnostics system: %s", firstLine)
} }
log.Info("Connected")
outerLoop: outerLoop:
for { for {

View File

@ -191,6 +191,7 @@ func Setup(ctx *cli.Context) error {
exp.Setup(address) exp.Setup(address)
diagnostics.SetupLogsAccess(ctx) diagnostics.SetupLogsAccess(ctx)
diagnostics.SetupDbAccess(ctx) diagnostics.SetupDbAccess(ctx)
diagnostics.SetupCmdLineAccess()
} }
// pprof server // pprof server