Skip to content

Commit

Permalink
Remove maxPreservedFailedResults, merge all results into common view …
Browse files Browse the repository at this point in the history
…in List()

Signed-off-by: Jake Utley <[email protected]>
  • Loading branch information
jutley committed Sep 5, 2019
1 parent eae7c47 commit 2dc250f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 62 deletions.
23 changes: 7 additions & 16 deletions history.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ type result struct {
}

type resultHistory struct {
mu sync.Mutex
nextId int64
results []*result
maxResults uint
preservedFailedResults []*result
maxPreservedFailedResults uint
mu sync.Mutex
nextId int64
results []*result
preservedFailedResults []*result
maxResults uint
}

// Add a result to the history.
Expand All @@ -52,7 +51,7 @@ func (rh *resultHistory) Add(moduleName, target, debugOutput string, success boo
if uint(len(rh.results)) > rh.maxResults {
if !rh.results[0].success {
rh.preservedFailedResults = append(rh.preservedFailedResults, rh.results[0])
if uint(len(rh.preservedFailedResults)) > rh.maxPreservedFailedResults {
if uint(len(rh.preservedFailedResults)) > rh.maxResults {
preservedFailedResults := make([]*result, len(rh.preservedFailedResults)-1)
copy(preservedFailedResults, rh.preservedFailedResults[1:])
rh.preservedFailedResults = preservedFailedResults
Expand All @@ -69,15 +68,7 @@ func (rh *resultHistory) List() []*result {
rh.mu.Lock()
defer rh.mu.Unlock()

return rh.results[:]
}

// ListPreservedFailures returns a list of all preserved failed results.
func (rh *resultHistory) ListPreservedFailures() []*result {
rh.mu.Lock()
defer rh.mu.Unlock()

return rh.preservedFailedResults[:]
return append(rh.preservedFailedResults[:], rh.results...)
}

// Get returns a given result.
Expand Down
28 changes: 5 additions & 23 deletions history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

func TestHistoryKeepsLatestResults(t *testing.T) {
history := &resultHistory{maxResults: 3, maxPreservedFailedResults: 3}
history := &resultHistory{maxResults: 3}
for i := 0; i < 4; i++ {
history.Add("module", "target", fmt.Sprintf("result %d", i), true)
}
Expand All @@ -39,22 +39,18 @@ func FillHistoryWithMaxSuccesses(h *resultHistory) {
}

func FillHistoryWithMaxPreservedFailures(h *resultHistory) {
for i := uint(0); i < h.maxPreservedFailedResults; i++ {
for i := uint(0); i < h.maxResults; i++ {
h.Add("module", "target", fmt.Sprintf("result %d", h.nextId), false)
}
}

func TestHistoryPreservesExpiredFailedResults(t *testing.T) {
history := &resultHistory{maxResults: 3, maxPreservedFailedResults: 3}
history := &resultHistory{maxResults: 3}

// Success are expired, no failues are expired
FillHistoryWithMaxSuccesses(history)
FillHistoryWithMaxPreservedFailures(history)
savedResults := history.List()
savedFailedResults := history.ListPreservedFailures()
if len(savedFailedResults) > 0 {
t.Errorf("Preserved failures contains failures unnecessarily.")
}
for i := uint(0); i < uint(len(savedResults)); i++ {
expectedDebugOutput := fmt.Sprintf("result %d", i+history.maxResults)
if savedResults[i].debugOutput != expectedDebugOutput {
Expand All @@ -65,15 +61,8 @@ func TestHistoryPreservesExpiredFailedResults(t *testing.T) {
// Failures are expired, should all be preserved
FillHistoryWithMaxPreservedFailures(history)
savedResults = history.List()
savedFailedResults = history.ListPreservedFailures()
for i := uint(0); i < uint(len(savedFailedResults)); i++ {
expectedDebugOutput := fmt.Sprintf("result %d", i+history.maxResults)
if savedFailedResults[i].debugOutput != expectedDebugOutput {
t.Errorf("History contained the wrong result at index %d. Expected: %s, Actual: %s", i, expectedDebugOutput, savedResults[i].debugOutput)
}
}
for i := uint(0); i < uint(len(savedResults)); i++ {
expectedDebugOutput := fmt.Sprintf("result %d", i+history.maxResults+history.maxPreservedFailedResults)
expectedDebugOutput := fmt.Sprintf("result %d", i+history.maxResults)
if savedResults[i].debugOutput != expectedDebugOutput {
t.Errorf("History contained the wrong result at index %d. Expected: %s, Actual: %s", i, expectedDebugOutput, savedResults[i].debugOutput)
}
Expand All @@ -83,15 +72,8 @@ func TestHistoryPreservesExpiredFailedResults(t *testing.T) {
FillHistoryWithMaxPreservedFailures(history)
FillHistoryWithMaxSuccesses(history)
savedResults = history.List()
savedFailedResults = history.ListPreservedFailures()
for i := uint(0); i < uint(len(savedFailedResults)); i++ {
expectedDebugOutput := fmt.Sprintf("result %d", i+history.maxResults+history.maxPreservedFailedResults*2)
if savedFailedResults[i].debugOutput != expectedDebugOutput {
t.Errorf("History contained the wrong result at index %d. Expected: %s, Actual: %s", i, expectedDebugOutput, savedResults[i].debugOutput)
}
}
for i := uint(0); i < uint(len(savedResults)); i++ {
expectedDebugOutput := fmt.Sprintf("result %d", i+history.maxResults+history.maxPreservedFailedResults*3)
expectedDebugOutput := fmt.Sprintf("result %d", i+history.maxResults*3)
if savedResults[i].debugOutput != expectedDebugOutput {
t.Errorf("History contained the wrong result at index %d. Expected: %s, Actual: %s", i, expectedDebugOutput, savedResults[i].debugOutput)
}
Expand Down
29 changes: 6 additions & 23 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,11 @@ var (
C: &config.Config{},
}

configFile = kingpin.Flag("config.file", "Blackbox exporter configuration file.").Default("blackbox.yml").String()
listenAddress = kingpin.Flag("web.listen-address", "The address to listen on for HTTP requests.").Default(":9115").String()
timeoutOffset = kingpin.Flag("timeout-offset", "Offset to subtract from timeout in seconds.").Default("0.5").Float64()
configCheck = kingpin.Flag("config.check", "If true validate the config file and then exit.").Default().Bool()
historyLimit = kingpin.Flag("history.limit", "The maximum amount of items to keep in the history.").Default("100").Uint()
historyPreservedFailedLimit = kingpin.Flag("history.preserved-failed-limit", "The maximum amount of failed items to preserve after expiration.").Default("5").Uint()
configFile = kingpin.Flag("config.file", "Blackbox exporter configuration file.").Default("blackbox.yml").String()
listenAddress = kingpin.Flag("web.listen-address", "The address to listen on for HTTP requests.").Default(":9115").String()
timeoutOffset = kingpin.Flag("timeout-offset", "Offset to subtract from timeout in seconds.").Default("0.5").Float64()
configCheck = kingpin.Flag("config.check", "If true validate the config file and then exit.").Default().Bool()
historyLimit = kingpin.Flag("history.limit", "The maximum amount of items to keep in the history.").Default("100").Uint()

Probers = map[string]prober.ProbeFn{
"http": prober.ProbeHTTP,
Expand Down Expand Up @@ -201,7 +200,7 @@ func run() int {
kingpin.HelpFlag.Short('h')
kingpin.Parse()
logger := promlog.New(promlogConfig)
rh := &resultHistory{maxResults: *historyLimit, maxPreservedFailedResults: *historyPreservedFailedLimit}
rh := &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 @@ -288,22 +287,6 @@ func run() int {
html.EscapeString(r.moduleName), html.EscapeString(r.target), success, r.id)
}

w.Write([]byte(`</table>
<h2>Preserved Failed Probes</h2>
<table border='1'><tr><th>Module</th><th>Target</th><th>Result</th><th>Debug</th>`))

preservedFailedResults := rh.ListPreservedFailures()

for i := len(preservedFailedResults) - 1; i >= 0; i-- {
r := preservedFailedResults[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)
}

w.Write([]byte(`</table>
</body>
</html>`))
Expand Down

0 comments on commit 2dc250f

Please sign in to comment.