From 30560769ab1dbaa42ce0dec3233fc2f38722c02f Mon Sep 17 00:00:00 2001 From: Brian Brazil Date: Wed, 20 Sep 2017 13:47:22 +0100 Subject: [PATCH] Allow logs to be viewed more than once --- history.go | 5 ++--- main.go | 10 +++++----- main_test.go | 3 +-- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/history.go b/history.go index 5560690..c39de0b 100644 --- a/history.go +++ b/history.go @@ -14,7 +14,6 @@ package main import ( - "bytes" "sync" ) @@ -22,7 +21,7 @@ type result struct { id int64 moduleName string target string - debugOutput *bytes.Buffer + debugOutput string success bool } @@ -33,7 +32,7 @@ type resultHistory struct { } // Add a result to the history. -func (rh *resultHistory) Add(moduleName, target string, debugOutput *bytes.Buffer, success bool) { +func (rh *resultHistory) Add(moduleName, target, debugOutput string, success bool) { rh.mu.Lock() defer rh.mu.Unlock() diff --git a/main.go b/main.go index f98c7c1..80fbc11 100644 --- a/main.go +++ b/main.go @@ -133,7 +133,7 @@ func probeHandler(w http.ResponseWriter, r *http.Request, c *config.Config, logg if r.URL.Query().Get("debug") == "true" { w.Header().Set("Content-Type", "text/plain") - debugOutput.WriteTo(w) + w.Write([]byte(debugOutput)) return } @@ -174,7 +174,7 @@ func (sl scrapeLogger) Log(keyvals ...interface{}) error { } // Returns plaintext debug output for a probe. -func DebugOutput(module *config.Module, logBuffer *bytes.Buffer, registry *prometheus.Registry) *bytes.Buffer { +func DebugOutput(module *config.Module, logBuffer *bytes.Buffer, registry *prometheus.Registry) string { buf := &bytes.Buffer{} fmt.Fprintf(buf, "Logs for the probe:\n") logBuffer.WriteTo(buf) @@ -193,7 +193,7 @@ func DebugOutput(module *config.Module, logBuffer *bytes.Buffer, registry *prome } buf.Write(c) - return buf + return buf.String() } func init() { @@ -278,7 +278,7 @@ func main() { results := rh.List() - for i := len(results) - 1; i > 0; i-- { + for i := len(results) - 1; i >= 0; i-- { r := results[i] success := "Success" if !r.success { @@ -304,7 +304,7 @@ func main() { return } w.Header().Set("Content-Type", "text/plain") - result.debugOutput.WriteTo(w) + w.Write([]byte(result.debugOutput)) }) http.HandleFunc("/config", func(w http.ResponseWriter, r *http.Request) { diff --git a/main_test.go b/main_test.go index 7bd66e6..05ac44d 100644 --- a/main_test.go +++ b/main_test.go @@ -80,9 +80,8 @@ func TestPrometheusConfigSecretsHidden(t *testing.T) { func TestDebugOutputSecretsHidden(t *testing.T) { module := c.Modules["http_2xx"] - buf := DebugOutput(&module, &bytes.Buffer{}, prometheus.NewRegistry()) + out := DebugOutput(&module, &bytes.Buffer{}, prometheus.NewRegistry()) - out := buf.String() if strings.Contains(out, "mysecret") { t.Errorf("Secret exposed in debug output: %v", out) } -- 2.25.1