From: Brian Brazil Date: Wed, 20 Sep 2017 12:47:22 +0000 (+0100) Subject: Allow logs to be viewed more than once X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=30560769ab1dbaa42ce0dec3233fc2f38722c02f;p=blackbox_exporter.git Allow logs to be viewed more than once --- 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) }