Skip to content

Commit

Permalink
Put failure results on one log line
Browse files Browse the repository at this point in the history
Signed-off-by: Tobias Guggenmos <[email protected]>
  • Loading branch information
slrtbtfs committed Dec 12, 2024
1 parent 0a50e3d commit 73630e8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
7 changes: 2 additions & 5 deletions prober/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -920,18 +920,15 @@ func TestFailIfNotSSLLogMsg(t *testing.T) {
},
} {
t.Run(title, func(t *testing.T) {
recorder := logRecorder{next: promslog.NewNopLogger()}
registry := prometheus.NewRegistry()
testCTX, cancel := context.WithTimeout(context.Background(), Timeout)
defer cancel()

logger := slog.New(&recorder)
result := ProbeHTTP(testCTX, tc.URL, tc.Config, registry, logger)
result.log(logger, 1)
result := ProbeHTTP(testCTX, tc.URL, tc.Config, registry, promslog.NewNopLogger())
if result.success != tc.Success {
t.Fatalf("Expected success=%v, got=%v", tc.Success, result)
}
if seen := recorder.msgs[Msg]; seen != tc.MessageExpected {
if seen := result.failureReason == Msg; seen != tc.MessageExpected {
t.Fatalf("SSL message expected=%v, seen=%v", tc.MessageExpected, seen)
}
})
Expand Down
13 changes: 8 additions & 5 deletions prober/prober.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (r *ProbeResult) failureInfoGauge() *prometheus.GaugeVec {
} else if r.failureReason == "" {
// Should not happen, but there theoretically might be an
// inconsistent state of the struct.
r.failureReason = "unknown"
r.failureReason = "Unkown"
}

labels := []string{"reason"}
Expand All @@ -85,15 +85,18 @@ func (r *ProbeResult) log(logger *slog.Logger, duration float64) {
if r.failureReason == "" {
// Should not happen, but there theoretically might be an
// inconsistent state of the struct.
r.failureReason = "unknown"
r.failureReason = "Probe failed for unkown reason"
}
// converting the []string slice to an []any slice is a bit finicky
logDetails := make([]any, 0, len(r.failureDetails)+2)
logDetails := make([]any, 0, len(r.failureDetails)+4)
logDetails = append(logDetails, "reason")
logDetails = append(logDetails, r.failureReason)
for _, d := range r.failureDetails {
logDetails = append(logDetails, d)
}
logger.Error(r.failureReason, logDetails...)
logger.Error("Probe failed", "duration_seconds", duration)
logDetails = append(logDetails, "duration")
logDetails = append(logDetails, duration)
logger.Error("Probe failed", logDetails...)
}
}

Expand Down

0 comments on commit 73630e8

Please sign in to comment.