Skip to content

Commit

Permalink
Fix result output bug 🪲
Browse files Browse the repository at this point in the history
  • Loading branch information
benc-uk committed Apr 5, 2023
1 parent 76db782 commit 09bdf32
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 33 deletions.
21 changes: 10 additions & 11 deletions services/runner/monitor/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"time"
)

func (m *Monitor) runHTTP() (*types.Result, map[string]any) {
func (m *Monitor) runHTTP() *types.Result {
r := types.NewResult(m.Name, m.Target, m.ID)

var err error
Expand All @@ -30,21 +30,21 @@ func (m *Monitor) runHTTP() (*types.Result, map[string]any) {
if timeoutProp != "" {
timeout, err = time.ParseDuration(timeoutProp)
if err != nil {
return types.NewFailedResult(m.Name, m.Target, m.ID, err), nil
return types.NewFailedResult(m.Name, m.Target, m.ID, err)
}
}

validateTLSProp := m.Properties["validateTLS"]
if validateTLSProp != "" {
validateTLS, err = strconv.ParseBool(validateTLSProp)
if err != nil {
return types.NewFailedResult(m.Name, m.Target, m.ID, err), nil
return types.NewFailedResult(m.Name, m.Target, m.ID, err)
}
}

req, err := http.NewRequest(method, m.Target, nil)
if err != nil {
return types.NewFailedResult(m.Name, m.Target, m.ID, err), nil
return types.NewFailedResult(m.Name, m.Target, m.ID, err)
}

if m.Properties["body"] != "" {
Expand All @@ -56,7 +56,7 @@ func (m *Monitor) runHTTP() (*types.Result, map[string]any) {

err = json.Unmarshal([]byte(m.Properties["headers"]), &headers)
if err != nil {
return types.NewFailedResult(m.Name, m.Target, m.ID, err), nil
return types.NewFailedResult(m.Name, m.Target, m.ID, err)
}

for k, v := range headers {
Expand All @@ -74,7 +74,7 @@ func (m *Monitor) runHTTP() (*types.Result, map[string]any) {

resp, err := client.Do(req)
if err != nil {
return types.NewFailedResult(m.Name, m.Target, m.ID, err), nil
return types.NewFailedResult(m.Name, m.Target, m.ID, err)
}
defer resp.Body.Close()

Expand All @@ -83,7 +83,7 @@ func (m *Monitor) runHTTP() (*types.Result, map[string]any) {
// Read response body
body, err := io.ReadAll(resp.Body)
if err != nil {
return types.NewFailedResult(m.Name, m.Target, m.ID, err), nil
return types.NewFailedResult(m.Name, m.Target, m.ID, err)
}

bodyStr := string(body)
Expand All @@ -92,7 +92,7 @@ func (m *Monitor) runHTTP() (*types.Result, map[string]any) {
if m.Properties["bodyRegex"] != "" {
re, err := regexp.Compile(m.Properties["bodyRegex"])
if err != nil {
return types.NewFailedResult(m.Name, m.Target, m.ID, err), nil
return types.NewFailedResult(m.Name, m.Target, m.ID, err)
}

match := re.FindStringSubmatch(bodyStr)
Expand Down Expand Up @@ -126,9 +126,8 @@ func (m *Monitor) runHTTP() (*types.Result, map[string]any) {
}
}

// Save all outputs to the result but omit the body
// Save all outputs
r.Outputs = outputs
r.Outputs["body"] = "<OMITTED>"

return r, outputs
return r
}
20 changes: 12 additions & 8 deletions services/runner/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,30 +91,28 @@ func (m *Monitor) run() (bool, *types.Result) {

var result *types.Result

var outputs map[string]interface{}

log.Printf("### Running monitor '%s' at '%s'", m.Name, m.Target)

switch m.Type {
case typeHTTP:
result, outputs = m.runHTTP()
result = m.runHTTP()

case typePing:
result, outputs = m.runPing()
result = m.runPing()

case typeTCP:
result, outputs = m.runTCP()
result = m.runTCP()

default:
log.Printf("### Unknown monitor type '%s', will be skipped", m.Type)
return false, nil
}

if os.Getenv("DEBUG") == "true" {
log.Printf("### DEBUG '%s' outputs: %+v", m.Name, outputs)
log.Printf("### DEBUG '%s' outputs: %+v", m.Name, result.Outputs)
}

if m.Rule != "" && outputs != nil {
if m.Rule != "" && result.Outputs != nil {
//log.Printf("### Running rule '%s' for monitor '%s'", m.Rule, m.Name)
ruleExp, err := govaluate.NewEvaluableExpression(m.Rule)
if err != nil {
Expand All @@ -124,7 +122,7 @@ func (m *Monitor) run() (bool, *types.Result) {
return false, result
}

res, err := ruleExp.Evaluate(outputs)
res, err := ruleExp.Evaluate(result.Outputs)
if err != nil {
result = types.NewFailedResult(m.Name, m.Target, m.ID, fmt.Errorf("rule eval error: "+err.Error()))
_ = storeResult(m, *result)
Expand All @@ -146,6 +144,12 @@ func (m *Monitor) run() (bool, *types.Result) {
}
}

// Remove the body from the outputs after rules are checked
// TODO: Horrible leakiness from HTTP monitor here, should be fixed
if result.Outputs["body"] != nil {
result.Outputs["body"] = "*** Removed ***"
}

err := storeResult(m, *result)
if err != nil {
log.Printf("### Error storing result: %s", err.Error())
Expand Down
4 changes: 1 addition & 3 deletions services/runner/monitor/monitor_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package monitor

import (
"io"
"log"
"testing"
"time"
)

func init() {
// Comment out this line to see debug output
log.SetOutput(io.Discard)
//log.SetOutput(io.Discard)
}

func TestMonitorDisabledStart(t *testing.T) {
Expand Down
14 changes: 7 additions & 7 deletions services/runner/monitor/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
ping "github.com/prometheus-community/pro-bing"
)

func (m *Monitor) runPing() (*types.Result, map[string]any) {
func (m *Monitor) runPing() *types.Result {
r := types.NewResult(m.Name, m.Target, m.ID)

var err error
Expand All @@ -21,29 +21,29 @@ func (m *Monitor) runPing() (*types.Result, map[string]any) {
if countProp != "" {
count, err = strconv.Atoi(countProp)
if err != nil {
return types.NewFailedResult(m.Name, m.Target, m.ID, err), nil
return types.NewFailedResult(m.Name, m.Target, m.ID, err)
}
}

intervalProp := m.Properties["interval"]
if intervalProp != "" {
interval, err = time.ParseDuration(intervalProp)
if err != nil {
return types.NewFailedResult(m.Name, m.Target, m.ID, err), nil
return types.NewFailedResult(m.Name, m.Target, m.ID, err)
}
}

timeoutProp := m.Properties["timeout"]
if timeoutProp != "" {
timeout, err = time.ParseDuration(timeoutProp)
if err != nil {
return types.NewFailedResult(m.Name, m.Target, m.ID, err), nil
return types.NewFailedResult(m.Name, m.Target, m.ID, err)
}
}

pinger, err := ping.NewPinger(m.Target)
if err != nil {
return types.NewFailedResult(m.Name, m.Target, m.ID, err), nil
return types.NewFailedResult(m.Name, m.Target, m.ID, err)
}

pinger.SetPrivileged(true)
Expand All @@ -53,7 +53,7 @@ func (m *Monitor) runPing() (*types.Result, map[string]any) {

err = pinger.Run() // NOTE: Blocks
if err != nil {
return types.NewFailedResult(m.Name, m.Target, m.ID, err), nil
return types.NewFailedResult(m.Name, m.Target, m.ID, err)
}

stats := pinger.Statistics()
Expand All @@ -69,5 +69,5 @@ func (m *Monitor) runPing() (*types.Result, map[string]any) {
r.Value = int(stats.AvgRtt.Milliseconds())
r.Outputs = outputs

return r, outputs
return r
}
8 changes: 4 additions & 4 deletions services/runner/monitor/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"
)

func (m *Monitor) runTCP() (*types.Result, map[string]any) {
func (m *Monitor) runTCP() *types.Result {
r := types.NewResult(m.Name, m.Target, m.ID)

var err error
Expand All @@ -18,7 +18,7 @@ func (m *Monitor) runTCP() (*types.Result, map[string]any) {
if timeoutProp != "" {
timeout, err = time.ParseDuration(timeoutProp)
if err != nil {
return types.NewFailedResult(m.Name, m.Target, m.ID, err), nil
return types.NewFailedResult(m.Name, m.Target, m.ID, err)
}
}

Expand All @@ -27,7 +27,7 @@ func (m *Monitor) runTCP() (*types.Result, map[string]any) {

conn, err := dialer.Dial("tcp", m.Target)
if err != nil {
return types.NewFailedResult(m.Name, m.Target, m.ID, err), nil
return types.NewFailedResult(m.Name, m.Target, m.ID, err)
}

r.Value = int(time.Since(start).Milliseconds())
Expand All @@ -41,5 +41,5 @@ func (m *Monitor) runTCP() (*types.Result, map[string]any) {

r.Outputs = outputs

return r, outputs
return r
}

0 comments on commit 09bdf32

Please sign in to comment.