Skip to content

Commit b307ea4

Browse files
authored
fix: only flush logs if logs collection is enabled (#510)
* fix: only flush logs if logs collection is enabled * test: add standard events chain test with logs collection disabled There is no coverage/test for the app when logs collection is disabled Add a testcase to ensure standard events chain work even if logs collection is disabled.
1 parent f2f7de6 commit b307ea4

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

app/run.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,10 @@ func (app *App) Run(ctx context.Context) error {
9696
backgroundDataSendWg.Wait()
9797
if event.EventType == extension.Shutdown {
9898
app.logger.Infof("Exiting due to shutdown event with reason %s", event.ShutdownReason)
99-
// Flush buffered logs if any
100-
app.logsClient.FlushData(ctx, event.RequestID, event.InvokedFunctionArn, app.apmClient.LambdaDataChannel, true)
99+
if app.logsClient != nil {
100+
// Flush buffered logs if any
101+
app.logsClient.FlushData(ctx, event.RequestID, event.InvokedFunctionArn, app.apmClient.LambdaDataChannel, true)
102+
}
101103
// Since we have waited for the processEvent loop to finish we
102104
// already have received all the data we can from the agent. So, we
103105
// flush all the data to make sure that shutdown can correctly deduce
@@ -127,8 +129,10 @@ func (app *App) Run(ctx context.Context) error {
127129
// that the underlying transport is reset for next invocation without
128130
// waiting for grace period if it got to unhealthy state.
129131
flushCtx, cancel := context.WithCancel(ctx)
130-
// Flush buffered logs if any
131-
app.logsClient.FlushData(ctx, event.RequestID, event.InvokedFunctionArn, app.apmClient.LambdaDataChannel, false)
132+
if app.logsClient != nil {
133+
// Flush buffered logs if any
134+
app.logsClient.FlushData(ctx, event.RequestID, event.InvokedFunctionArn, app.apmClient.LambdaDataChannel, false)
135+
}
132136
// Flush APM data now that the function invocation has completed
133137
app.apmClient.FlushAPMData(flushCtx)
134138
cancel()

main_test.go

+33-2
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,29 @@ func TestStandardEventsChain(t *testing.T) {
485485
}
486486
}
487487

488+
// TestStandardEventsChainWithoutLogs checks a nominal sequence of events (fast APM server, only one standard event)
489+
// with logs collection disabled
490+
func TestStandardEventsChainWithoutLogs(t *testing.T) {
491+
l, err := logger.New(logger.WithLevel(zapcore.DebugLevel))
492+
require.NoError(t, err)
493+
494+
eventsChannel := newTestStructs(t)
495+
apmServerInternals, _ := newMockApmServer(t, l)
496+
logsapiAddr := randomAddr()
497+
newMockLambdaServer(t, logsapiAddr, eventsChannel, l)
498+
499+
eventsChain := []MockEvent{
500+
{Type: InvokeStandard, APMServerBehavior: TimelyResponse, ExecutionDuration: 1, Timeout: 5},
501+
}
502+
eventQueueGenerator(eventsChain, eventsChannel)
503+
select {
504+
case <-runAppFull(t, logsapiAddr, true):
505+
assert.Contains(t, apmServerInternals.Data, TimelyResponse)
506+
case <-time.After(timeout):
507+
t.Fatalf("timed out waiting for app to finish")
508+
}
509+
}
510+
488511
// TestFlush checks if the flushed param does not cause a panic or an unexpected behavior
489512
func TestFlush(t *testing.T) {
490513
l, err := logger.New(logger.WithLevel(zapcore.DebugLevel))
@@ -821,13 +844,21 @@ func TestMetrics(t *testing.T) {
821844
}
822845

823846
func runApp(t *testing.T, logsapiAddr string) <-chan struct{} {
847+
return runAppFull(t, logsapiAddr, false)
848+
}
849+
850+
func runAppFull(t *testing.T, logsapiAddr string, disableLogsAPI bool) <-chan struct{} {
824851
ctx, cancel := context.WithCancel(context.Background())
825-
app, err := app.New(ctx,
852+
opts := []app.ConfigOption{
826853
app.WithExtensionName("apm-lambda-extension"),
827854
app.WithLambdaRuntimeAPI(os.Getenv("AWS_LAMBDA_RUNTIME_API")),
828855
app.WithLogLevel("debug"),
829856
app.WithLogsapiAddress(logsapiAddr),
830-
)
857+
}
858+
if disableLogsAPI {
859+
opts = append(opts, app.WithoutLogsAPI())
860+
}
861+
app, err := app.New(ctx, opts...)
831862
require.NoError(t, err)
832863

833864
go func() {

0 commit comments

Comments
 (0)