Skip to content

Commit f2e4a52

Browse files
authored
Merge pull request #27 from axiomhq/parse-json-logs
parse json logs, duplicate record to message
2 parents ae88e5a + 19398fa commit f2e4a52

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

server/server.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,27 @@ func httpHandler(ax *flusher.Axiom, runtimeDone chan struct{}) http.HandlerFunc
9191
e["_time"], e["time"] = e["time"], nil
9292

9393
if e["type"] == "function" {
94-
// parse the record
95-
matches := logLineRgx.FindStringSubmatch(e["record"].(string))
96-
if len(matches) == 5 {
97-
e["message"] = matches[4]
98-
e["record"] = map[string]any{"requestId": matches[2], "message": e["record"], "timestamp": matches[1]}
99-
e["level"] = strings.ToLower(matches[3])
94+
e["message"] = e["record"]
95+
if recordStr, ok := e["record"].(string); ok && len(recordStr) > 0 {
96+
recordStr = strings.Trim(recordStr, "\n")
97+
// parse the record
98+
// first check if the record is a json object, if not parse it as a text log line
99+
if recordStr[0] == '{' && recordStr[len(recordStr)-1] == '}' {
100+
var record map[string]any
101+
err = json.Unmarshal([]byte(recordStr), &record)
102+
if err != nil {
103+
logger.Error("Error unmarshalling record:", zap.Error(err))
104+
// do not return, we want to continue processing the event
105+
} else {
106+
e["record"] = record
107+
}
108+
} else {
109+
matches := logLineRgx.FindStringSubmatch(recordStr)
110+
if len(matches) == 5 {
111+
e["record"] = map[string]any{"requestId": matches[2], "message": matches[4], "timestamp": matches[1], "level": e["level"]}
112+
e["level"] = strings.ToLower(matches[3])
113+
}
114+
}
100115
}
101116
}
102117

version/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package version
22

33
// manually set constant version
4-
const version string = "v9"
4+
const version string = "v10"
55

66
// Get returns the Go module version of the axiom-go module.
77
func Get() string {

0 commit comments

Comments
 (0)