diff --git a/apps/opsproof/overlay/services/jeannie.go b/apps/opsproof/overlay/services/jeannie.go index 568bad7..3691e1f 100644 --- a/apps/opsproof/overlay/services/jeannie.go +++ b/apps/opsproof/overlay/services/jeannie.go @@ -312,13 +312,8 @@ func (client *JeannieClient) request(ctx context.Context, method string, path st } func decodeStatusOutput(output string) (StatusSnapshot, error) { - start := strings.Index(output, "{") - if start < 0 { - return StatusSnapshot{}, errors.New("Jeannie status output did not contain JSON") - } - var snapshot StatusSnapshot - if err := json.Unmarshal([]byte(output[start:]), &snapshot); err != nil { + if err := decodeFinalJSONObject(output, &snapshot); err != nil { return StatusSnapshot{}, fmt.Errorf("decode Jeannie status output: %w", err) } if snapshot.Rows == nil { @@ -340,11 +335,7 @@ func decodeDoctorOutput(output string) (IncidentSnapshot, error) { Title string `json:"title"` Rows []doctorRow `json:"rows"` } - start := strings.Index(output, "{") - if start < 0 { - return IncidentSnapshot{}, errors.New("Jeannie Doctor output did not contain JSON") - } - if err := json.Unmarshal([]byte(output[start:]), &report); err != nil { + if err := decodeFinalJSONObject(output, &report); err != nil { return IncidentSnapshot{}, fmt.Errorf("decode Jeannie Doctor output: %w", err) } if report.Rows == nil { @@ -372,6 +363,29 @@ func decodeDoctorOutput(output string) (IncidentSnapshot, error) { return snapshot, nil } +func decodeFinalJSONObject(output string, target any) error { + searchEnd := len(output) + for searchEnd > 0 { + start := strings.LastIndex(output[:searchEnd], "{") + if start < 0 { + break + } + candidate := strings.TrimSpace(output[start:]) + var raw json.RawMessage + decoder := json.NewDecoder(strings.NewReader(candidate)) + if err := decoder.Decode(&raw); err == nil { + var trailing any + if err := decoder.Decode(&trailing); errors.Is(err, io.EOF) { + if err := json.Unmarshal(raw, target); err == nil { + return nil + } + } + } + searchEnd = start + } + return errors.New("output did not contain a complete JSON object") +} + func cleanExcerpt(value string, limit int) string { value = strings.Map(func(character rune) rune { if character == '\n' || character == '\t' || character >= 32 { diff --git a/apps/opsproof/overlay/services/jeannie_test.go b/apps/opsproof/overlay/services/jeannie_test.go index 68e0dc8..a0371fe 100644 --- a/apps/opsproof/overlay/services/jeannie_test.go +++ b/apps/opsproof/overlay/services/jeannie_test.go @@ -66,6 +66,18 @@ func TestDecodeStatusOutputRejectsNonJSON(t *testing.T) { } } +func TestDecodeStatusOutputUsesFinalCompleteJSONObject(t *testing.T) { + output := "probe emitted {\"message\":\"broken\nline\"}\n" + + `{"title":"Jeannie Status: all","mode":"fast","cluster":{"profile":"k3s","runtime":"k3s"},"rows":[{"status":"ok","area":"Kubernetes","check":"API readiness","summary":"ok"}]}` + snapshot, err := decodeStatusOutput(output) + if err != nil { + t.Fatal(err) + } + if snapshot.Title != "Jeannie Status: all" || len(snapshot.Rows) != 1 || snapshot.Rows[0].Check != "API readiness" { + t.Fatalf("unexpected snapshot: %#v", snapshot) + } +} + func TestDoctorNormalizesEvidenceAndRejectsArbitraryTargets(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) { switch request.URL.Path {