diff --git a/lambda/logserver/logserver.go b/lambda/logserver/logserver.go index ca47310..f7fe2a1 100644 --- a/lambda/logserver/logserver.go +++ b/lambda/logserver/logserver.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "net" "net/http" + "regexp" "strconv" "sync" "time" @@ -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 } @@ -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) @@ -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{}) @@ -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{}, } diff --git a/lambda/logserver/logserver_test.go b/lambda/logserver/logserver_test.go index b7003d5..2061ba7 100644 --- a/lambda/logserver/logserver_test.go +++ b/lambda/logserver/logserver_test.go @@ -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", }, } @@ -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", }, } @@ -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()) } diff --git a/util/extension.go b/util/extension.go index 0847f21..c957a52 100644 --- a/util/extension.go +++ b/util/extension.go @@ -2,6 +2,6 @@ package util const ( Name = "newrelic-lambda-extension" - Version = "2.0.2" + Version = "2.0.3" Id = Name + ":" + Version )