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

adding probe error log support #383

Closed
wants to merge 2 commits into from
Closed
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
26 changes: 19 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ var (
}
)

func probeHandler(w http.ResponseWriter, r *http.Request, c *config.Config, logger log.Logger, rh *resultHistory) {
func probeHandler(w http.ResponseWriter, r *http.Request, c *config.Config, logger log.Logger, rh *resultHistory, eh *resultHistory) {
moduleName := r.URL.Query().Get("module")
if moduleName == "" {
moduleName = "http_2xx"
Expand Down Expand Up @@ -132,7 +132,11 @@ func probeHandler(w http.ResponseWriter, r *http.Request, c *config.Config, logg
}

debugOutput := DebugOutput(&module, &sl.buffer, registry)
rh.Add(moduleName, target, debugOutput, success)
if !success {
eh.Add(moduleName, target, debugOutput, success)
} else{
rh.Add(moduleName, target, debugOutput, success)
}

if r.URL.Query().Get("debug") == "true" {
w.Header().Set("Content-Type", "text/plain")
Expand Down Expand Up @@ -211,6 +215,7 @@ func main() {
kingpin.Parse()
logger := promlog.New(allowedLevel)
rh := &resultHistory{maxResults: *historyLimit}
eh := &resultHistory{maxResults: *historyLimit}

level.Info(logger).Log("msg", "Starting blackbox_exporter", "version", version.Info())
level.Info(logger).Log("msg", "Build context", version.BuildContext())
Expand Down Expand Up @@ -270,7 +275,7 @@ func main() {
sc.Lock()
conf := sc.C
sc.Unlock()
probeHandler(w, r, conf, logger, rh)
probeHandler(w, r, conf, logger, rh, eh)
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
Expand All @@ -285,14 +290,18 @@ func main() {
<h2>Recent Probes</h2>
<table border='1'><tr><th>Module</th><th>Target</th><th>Result</th><th>Debug</th>`))

results := rh.List()
results := eh.List()
Copy link
Contributor

Choose a reason for hiding this comment

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

You're not merging the lists, it should be ordered by time.

Copy link
Author

Choose a reason for hiding this comment

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

result struct has no datetime field, so it will be hard to merge and sort by time.

Copy link
Contributor

Choose a reason for hiding this comment

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

You could use the ids

Copy link
Author

Choose a reason for hiding this comment

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

The ids in two list might conflicts since both set and increase independently .

Copy link
Contributor

Choose a reason for hiding this comment

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

A common counter will handle that

for i := len(results) - 1; i >= 0; i-- {
r := results[i]
success := "<strong>Failure</strong>"
fmt.Fprintf(w, "<tr><td>%s</td><td>%s</td><td>%s</td><td><a href='logs?id=%d&error=true'>Logs</a></td></td>",
html.EscapeString(r.moduleName), html.EscapeString(r.target), success, r.id)
}

results = rh.List()
for i := len(results) - 1; i >= 0; i-- {
r := results[i]
success := "Success"
if !r.success {
success = "<strong>Failure</strong>"
}
fmt.Fprintf(w, "<tr><td>%s</td><td>%s</td><td>%s</td><td><a href='logs?id=%d'>Logs</a></td></td>",
html.EscapeString(r.moduleName), html.EscapeString(r.target), success, r.id)
}
Expand All @@ -308,6 +317,9 @@ func main() {
return
}
result := rh.Get(id)
if r.URL.Query().Get("error") == "true" {
Copy link
Contributor

Choose a reason for hiding this comment

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

This should work transparently

result = eh.Get(id)
}
if result == nil {
http.Error(w, "Probe id not found", 404)
return
Expand Down
4 changes: 2 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestPrometheusTimeoutHTTP(t *testing.T) {

rr := httptest.NewRecorder()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
probeHandler(w, r, c, log.NewNopLogger(), &resultHistory{})
probeHandler(w, r, c, log.NewNopLogger(), &resultHistory{}, &resultHistory{})
})

handler.ServeHTTP(rr, req)
Expand All @@ -65,7 +65,7 @@ func TestPrometheusConfigSecretsHidden(t *testing.T) {
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
probeHandler(w, r, c, log.NewNopLogger(), &resultHistory{})
probeHandler(w, r, c, log.NewNopLogger(), &resultHistory{}, &resultHistory{})
})
handler.ServeHTTP(rr, req)

Expand Down