Skip to content

Commit

Permalink
fix: Initial attempt at handling platform logs as string (#88)
Browse files Browse the repository at this point in the history
* fix: Initial attempt at handling platform logs as string

Signed-off-by: mrickard <[email protected]>

* fix: Adding test for platform-report-as-string

Signed-off-by: mrickard <[email protected]>
  • Loading branch information
mrickard authored Nov 1, 2021
1 parent d0577fb commit 5d2c26b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 8 deletions.
24 changes: 18 additions & 6 deletions lambda/logserver/logserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func formatReport(metrics map[string]interface{}) string {
return ret
}

var requestIdRegExp, _ = regexp.Compile("RequestId: ([a-zA-Z0-9-]+)")
var reportStringRegExp, _ = regexp.Compile("RequestId: ([a-zA-Z0-9-]+)(.*)")

func (ls *LogServer) handler(res http.ResponseWriter, req *http.Request) {
defer util.Close(req.Body)
Expand All @@ -126,17 +126,29 @@ func (ls *LogServer) handler(res http.ResponseWriter, req *http.Request) {
ls.lastRequestId = event.Record.(map[string]interface{})["requestId"].(string)
case string:
recordString := event.Record.(string)
ls.lastRequestId = requestIdRegExp.FindStringSubmatch(recordString)[1]
ls.lastRequestId = reportStringRegExp.FindStringSubmatch(recordString)[1]
}
ls.lastRequestIdLock.Unlock()
case "platform.report":
record := event.Record.(map[string]interface{})
metrics := record["metrics"].(map[string]interface{})
requestId := record["requestId"].(string)
metricString := ""
requestId := ""
switch event.Record.(type) {
case map[string]interface{}:
record := event.Record.(map[string]interface{})
metrics := record["metrics"].(map[string]interface{})
metricString = formatReport(metrics)
requestId = record["requestId"].(string)
case string:
recordString := event.Record.(string)
results := reportStringRegExp.FindStringSubmatch(recordString)
requestId = results[1]
metricString = results[2]
}

reportStr := fmt.Sprintf(
"REPORT RequestId: %v%s",
requestId,
formatReport(metrics),
metricString,
)
reportLine := LogLine{
Time: event.Time,
Expand Down
40 changes: 38 additions & 2 deletions lambda/logserver/logserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,13 @@ func TestFunctionLogs(t *testing.T) {
assert.Equal(t, "log line 2", string(logLines2[0].Content))
assert.Equal(t, "testRequestId", logLines2[0].RequestID)

testRequestId := "abcdef01-a2b3-4321-cd89-0123456789ab"

testEvents3 := []api.LogEvent{
{
Time: time.Now().Add(600 * time.Millisecond),
Type: "platform.start",
Record: "RequestId: abcdef01-a2b3-4321-cd89-0123456789ab",
Record: "RequestId: " + testRequestId,
},
{
Time: time.Now().Add(700 * time.Millisecond),
Expand All @@ -154,7 +156,41 @@ func TestFunctionLogs(t *testing.T) {

assert.Equal(t, 1, len(logLines3))
assert.Equal(t, "log line 3, for testing start line record as string", string(logLines3[0].Content))
assert.Equal(t, "abcdef01-a2b3-4321-cd89-0123456789ab", logLines3[0].RequestID)
assert.Equal(t, testRequestId, logLines3[0].RequestID)

platformMetricString := "REPORT RequestId: " + testRequestId + "\tDuration: 25.30 ms\tBilled Duration: 100 ms\tMemory Size: 128 MB\tMax Memory Used: 74 MB\tInit Duration: 202.00 ms"

testEvents4 := []api.LogEvent{
{
Time: time.Now().Add(800 * time.Millisecond),
Type: "platform.report",
Record: platformMetricString,
},
{
Time: time.Now().Add(900 * time.Millisecond),
Type: "function",
Record: "log line 4, testing platform metrics as string",
},
}

testEventBytes, err = json.Marshal(testEvents4)
assert.NoError(t, err)

req, err = http.NewRequest("POST", realEndpoint, bytes.NewBuffer(testEventBytes))
assert.NoError(t, err)

go func() {
res, err := client.Do(req)
assert.NoError(t, err)
assert.Equal(t, 200, res.StatusCode)
assert.Equal(t, http.NoBody, res.Body)
}()

logLines4, _ := logs.AwaitFunctionLogs()

assert.Equal(t, 1, len(logLines4))
assert.Equal(t, "log line 4, testing platform metrics as string", string(logLines4[0].Content))
assert.Equal(t, testRequestId, logLines4[0].RequestID)

assert.Nil(t, logs.Close())
}
Expand Down

0 comments on commit 5d2c26b

Please sign in to comment.