Skip to content

Commit

Permalink
Always create REPORT log message when logs enabled.
Browse files Browse the repository at this point in the history
  • Loading branch information
purple4reina committed Jan 4, 2024
1 parent 2064047 commit 6b693a6
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 56 deletions.
5 changes: 4 additions & 1 deletion pkg/serverless/logs/logs_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ func (lc *LambdaLogsCollector) processMessage(
lc.executionContext.UpdateStartTime(lc.invocationStartTime)
}

if message.logType == logTypePlatformReport {
message.stringRecord = createStringRecordForReportLog(lc.invocationStartTime, lc.invocationEndTime, message)
}

if lc.enhancedMetricsEnabled {
proactiveInit := false
coldStart := false
Expand Down Expand Up @@ -292,7 +296,6 @@ func (lc *LambdaLogsCollector) processMessage(
outOfMemoryRequestId = message.objectRecord.requestID
}
serverlessMetrics.GenerateEnhancedMetricsFromReportLog(args)
message.stringRecord = createStringRecordForReportLog(lc.invocationStartTime, lc.invocationEndTime, message)
}
if message.logType == logTypePlatformRuntimeDone {
serverlessMetrics.GenerateEnhancedMetricsFromRuntimeDoneLog(
Expand Down
115 changes: 60 additions & 55 deletions pkg/serverless/logs/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -673,65 +673,70 @@ func TestProcessLogMessageLogsNotEnabled(t *testing.T) {
}

func TestProcessLogMessagesTimeoutLogFromReportLog(t *testing.T) {
logChannel := make(chan *config.ChannelMessage)
log := fxutil.Test[log.Component](t, logimpl.MockModule())
demux := aggregator.InitTestAgentDemultiplexerWithFlushInterval(log, time.Hour)
defer demux.Stop(false)

mockExecutionContext := &executioncontext.ExecutionContext{}
lc := &LambdaLogsCollector{
arn: "my-arn",
lastRequestID: "myRequestID",
logsEnabled: true,
enhancedMetricsEnabled: true,
out: logChannel,
extraTags: &Tags{
Tags: []string{"tag0:value0,tag1:value1"},
},
executionContext: mockExecutionContext,
demux: demux,
invocationStartTime: time.Now(),
invocationEndTime: time.Now().Add(10 * time.Millisecond),
}
for _, enhancedMetricsEnabled := range []bool{true, false} {
t.Run(fmt.Sprint(enhancedMetricsEnabled), func(t *testing.T) {

logChannel := make(chan *config.ChannelMessage)
log := fxutil.Test[log.Component](t, logimpl.MockModule())
demux := aggregator.InitTestAgentDemultiplexerWithFlushInterval(log, time.Hour)
defer demux.Stop(false)

mockExecutionContext := &executioncontext.ExecutionContext{}
lc := &LambdaLogsCollector{
arn: "my-arn",
lastRequestID: "myRequestID",
logsEnabled: true,
enhancedMetricsEnabled: enhancedMetricsEnabled,
out: logChannel,
extraTags: &Tags{
Tags: []string{"tag0:value0,tag1:value1"},
},
executionContext: mockExecutionContext,
demux: demux,
invocationStartTime: time.Now(),
invocationEndTime: time.Now().Add(10 * time.Millisecond),
}

reportLogMessage := LambdaLogAPIMessage{
objectRecord: platformObjectRecord{
requestID: "myRequestID",
reportLogItem: reportLogMetrics{
durationMs: 100.00,
billedDurationMs: 100,
memorySizeMB: 128,
maxMemoryUsedMB: 128,
initDurationMs: 50.00,
},
status: timeoutStatus,
},
logType: logTypePlatformReport,
stringRecord: "contents to be overwritten",
}
reportLogMessage := LambdaLogAPIMessage{
objectRecord: platformObjectRecord{
requestID: "myRequestID",
reportLogItem: reportLogMetrics{
durationMs: 100.00,
billedDurationMs: 100,
memorySizeMB: 128,
maxMemoryUsedMB: 128,
initDurationMs: 50.00,
},
status: timeoutStatus,
},
logType: logTypePlatformReport,
stringRecord: "contents to be overwritten",
}

logMessages := []LambdaLogAPIMessage{
reportLogMessage,
}
logMessages := []LambdaLogAPIMessage{
reportLogMessage,
}

go lc.processLogMessages(logMessages)
go lc.processLogMessages(logMessages)

expectedStringRecord := []string{
createStringRecordForReportLog(lc.invocationStartTime, lc.invocationEndTime, &reportLogMessage),
createStringRecordForTimeoutLog(&reportLogMessage),
}
expectedErrors := []bool{false, true}
for i := 0; i < len(expectedStringRecord); i++ {
select {
case received := <-logChannel:
assert.NotNil(t, received)
assert.Equal(t, "my-arn", received.Lambda.ARN)
assert.Equal(t, "myRequestID", received.Lambda.RequestID)
assert.Equal(t, expectedStringRecord[i], string(received.Content))
assert.Equal(t, expectedErrors[i], received.IsError)
case <-time.After(100 * time.Millisecond):
assert.Fail(t, "We should have received logs")
}
expectedStringRecord := []string{
createStringRecordForReportLog(lc.invocationStartTime, lc.invocationEndTime, &reportLogMessage),
createStringRecordForTimeoutLog(&reportLogMessage),
}
expectedErrors := []bool{false, true}
for i := 0; i < len(expectedStringRecord); i++ {
select {
case received := <-logChannel:
assert.NotNil(t, received)
assert.Equal(t, "my-arn", received.Lambda.ARN)
assert.Equal(t, "myRequestID", received.Lambda.RequestID)
assert.Equal(t, expectedStringRecord[i], string(received.Content))
assert.Equal(t, expectedErrors[i], received.IsError)
case <-time.After(100 * time.Millisecond):
assert.Fail(t, "We should have received logs")
}
}
})
}
}

Expand Down

0 comments on commit 6b693a6

Please sign in to comment.