Skip to content

Commit

Permalink
feat: Parse log lines before sending to Axiom
Browse files Browse the repository at this point in the history
  • Loading branch information
dasfmi committed Oct 17, 2023
1 parent 1ff8f14 commit a3bdce4
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"io"
"net/http"
"regexp"
"strings"

"os"
"strconv"
Expand Down Expand Up @@ -36,6 +38,8 @@ var (
axiomMetaInfo = map[string]string{}
)

var logLineRgx, _ = regexp.Compile(`^([0-9.:TZ-]{20,})\s+([0-9a-f-]{36})\s+(ERROR|INFO|WARN|DEBUG)\s+(.*)`)

func init() {
logger, _ = zap.NewProduction()

Expand Down Expand Up @@ -86,6 +90,16 @@ func httpHandler(ax *flusher.Axiom, runtimeDone chan struct{}) http.HandlerFunc
// replace the time field with axiom's _time
e["_time"], e["time"] = e["time"], nil

if e["type"] == "function" {
// parse the record
matches := logLineRgx.FindStringSubmatch(e["record"].(string))
if len(matches) == 5 {
e["message"] = matches[4]
e["record"] = map[string]any{"requestId": matches[2], "message": e["record"], "timestamp": matches[1]}
e["level"] = strings.ToLower(matches[3])
}
}

// decide if the handler should notify the extension that the runtime is done
if e["type"] == "platform.runtimeDone" && !firstInvocationDone {
notifyRuntimeDone = true
Expand Down

0 comments on commit a3bdce4

Please sign in to comment.