From 0947750ca088f36d80a3c5306190a04cf8c77ddd Mon Sep 17 00:00:00 2001
From: ale <ale@incal.net>
Date: Sun, 14 Apr 2024 14:46:28 +0100
Subject: [PATCH] Recurse JSON decoding into multiple levels of 'message'
 attributes

Ignore case, to cover 'message.MESSAGE', which we encounter with the
logging-operator host-tailer.
---
 schema/tuples.go | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/schema/tuples.go b/schema/tuples.go
index 12979af..11b56c3 100644
--- a/schema/tuples.go
+++ b/schema/tuples.go
@@ -83,13 +83,22 @@ func unmarshalRecordRecursively(record []byte) (map[string]any, error) {
 		return nil, err
 	}
 
-	// Try to unmarshal 'message' if it looks like JSON (with optional @cee prefix).
+	// Try to unmarshal 'message' (or 'MESSAGE') if it looks like
+	// JSON (possibly with an optional '@cee:' prefix).
+	var message, fieldName string
 	if s, ok := m["message"].(string); ok {
-		s = strings.TrimPrefix(s, "@cee:")
-		if strings.HasPrefix(s, "{") {
-			var m2 map[string]any
-			if err := json.NewDecoder(strings.NewReader(s)).Decode(&m2); err == nil {
-				m["message"] = m2
+		fieldName = "message"
+		message = s
+	} else if s, ok := m["MESSAGE"].(string); ok {
+		fieldName = "MESSAGE"
+		message = s
+	}
+
+	if message != "" {
+		message = strings.TrimPrefix(message, "@cee:")
+		if strings.HasPrefix(message, "{") {
+			if m2, err := unmarshalRecordRecursively([]byte(message)); err == nil {
+				m[fieldName] = m2
 			}
 		}
 	}
-- 
GitLab