Skip to content

Commit

Permalink
Merge pull request #82 from newrelic/mrickard/lambda-1321/record-in-l…
Browse files Browse the repository at this point in the history
…og-might-be-string

fix: Handle log event.Record that's a string instead of a map
  • Loading branch information
mrickard authored Sep 28, 2021
2 parents 2667683 + 016e456 commit 81c21a7
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 16 deletions.
29 changes: 19 additions & 10 deletions lambda/logserver/logserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"net"
"net/http"
"regexp"
"strconv"
"sync"
"time"
Expand All @@ -26,11 +27,11 @@ type LogLine struct {
}

type LogServer struct {
listenString string
server *http.Server
platformLogChan chan LogLine
functionLogChan chan []LogLine
lastRequestId string
listenString string
server *http.Server
platformLogChan chan LogLine
functionLogChan chan []LogLine
lastRequestId string
lastRequestIdLock *sync.Mutex
}

Expand Down Expand Up @@ -98,6 +99,8 @@ func formatReport(metrics map[string]interface{}) string {
return ret
}

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

func (ls *LogServer) handler(res http.ResponseWriter, req *http.Request) {
defer util.Close(req.Body)

Expand All @@ -118,7 +121,13 @@ func (ls *LogServer) handler(res http.ResponseWriter, req *http.Request) {
switch event.Type {
case "platform.start":
ls.lastRequestIdLock.Lock()
ls.lastRequestId = event.Record.(map[string]interface{})["requestId"].(string)
switch event.Record.(type) {
case map[string]interface{}:
ls.lastRequestId = event.Record.(map[string]interface{})["requestId"].(string)
case string:
recordString := event.Record.(string)
ls.lastRequestId = requestIdRegExp.FindStringSubmatch(recordString)[1]
}
ls.lastRequestIdLock.Unlock()
case "platform.report":
record := event.Record.(map[string]interface{})
Expand Down Expand Up @@ -171,10 +180,10 @@ func startInternal(host string) (*LogServer, error) {
server := &http.Server{}

logServer := &LogServer{
listenString: listener.Addr().String(),
server: server,
platformLogChan: make(chan LogLine, platformLogBufferSize),
functionLogChan: make(chan []LogLine),
listenString: listener.Addr().String(),
server: server,
platformLogChan: make(chan LogLine, platformLogBufferSize),
functionLogChan: make(chan []LogLine),
lastRequestIdLock: &sync.Mutex{},
}

Expand Down
42 changes: 37 additions & 5 deletions lambda/logserver/logserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ func TestFunctionLogs(t *testing.T) {

testEvents := []api.LogEvent{
{
Time: time.Now().Add(-100*time.Millisecond),
Time: time.Now().Add(-100 * time.Millisecond),
Type: "platform.start",
Record: map[string]interface{}{
"requestId": "testRequestId",
},
},
{
Time: time.Now().Add(-50*time.Millisecond),
Type: "function",
Time: time.Now().Add(-50 * time.Millisecond),
Type: "function",
Record: "log line 1",
},
}
Expand Down Expand Up @@ -99,8 +99,8 @@ func TestFunctionLogs(t *testing.T) {

testEvents2 := []api.LogEvent{
{
Time: time.Now().Add(500*time.Millisecond),
Type: "function",
Time: time.Now().Add(500 * time.Millisecond),
Type: "function",
Record: "log line 2",
},
}
Expand All @@ -124,6 +124,38 @@ func TestFunctionLogs(t *testing.T) {
assert.Equal(t, "log line 2", string(logLines2[0].Content))
assert.Equal(t, "testRequestId", logLines2[0].RequestID)

testEvents3 := []api.LogEvent{
{
Time: time.Now().Add(600 * time.Millisecond),
Type: "platform.start",
Record: "RequestId: abcdef01-a2b3-4321-cd89-0123456789ab",
},
{
Time: time.Now().Add(700 * time.Millisecond),
Type: "function",
Record: "log line 3, for testing start line record as string",
},
}

testEventBytes, err = json.Marshal(testEvents3)
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)
}()

logLines3, _ := logs.AwaitFunctionLogs()

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.Nil(t, logs.Close())
}

Expand Down
2 changes: 1 addition & 1 deletion util/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package util

const (
Name = "newrelic-lambda-extension"
Version = "2.0.2"
Version = "2.0.3"
Id = Name + ":" + Version
)

0 comments on commit 81c21a7

Please sign in to comment.