Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: only flush logs if logs collection is enabled #510

Merged
merged 4 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions app/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ func (app *App) Run(ctx context.Context) error {
backgroundDataSendWg.Wait()
if event.EventType == extension.Shutdown {
app.logger.Infof("Exiting due to shutdown event with reason %s", event.ShutdownReason)
// Flush buffered logs if any
app.logsClient.FlushData(ctx, event.RequestID, event.InvokedFunctionArn, app.apmClient.LambdaDataChannel, true)
if app.logsClient != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, check lc for nil in FlushData and return early (slightly reduces number of lines changed).

// Flush buffered logs if any
app.logsClient.FlushData(ctx, event.RequestID, event.InvokedFunctionArn, app.apmClient.LambdaDataChannel, true)
}
// Since we have waited for the processEvent loop to finish we
// already have received all the data we can from the agent. So, we
// flush all the data to make sure that shutdown can correctly deduce
Expand Down Expand Up @@ -127,8 +129,10 @@ func (app *App) Run(ctx context.Context) error {
// that the underlying transport is reset for next invocation without
// waiting for grace period if it got to unhealthy state.
flushCtx, cancel := context.WithCancel(ctx)
// Flush buffered logs if any
app.logsClient.FlushData(ctx, event.RequestID, event.InvokedFunctionArn, app.apmClient.LambdaDataChannel, false)
if app.logsClient != nil {
// Flush buffered logs if any
app.logsClient.FlushData(ctx, event.RequestID, event.InvokedFunctionArn, app.apmClient.LambdaDataChannel, false)
}
// Flush APM data now that the function invocation has completed
app.apmClient.FlushAPMData(flushCtx)
cancel()
Expand Down
35 changes: 33 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,29 @@ func TestStandardEventsChain(t *testing.T) {
}
}

// TestStandardEventsChainWithoutLogs checks a nominal sequence of events (fast APM server, only one standard event)
// with logs collection disabled
func TestStandardEventsChainWithoutLogs(t *testing.T) {
l, err := logger.New(logger.WithLevel(zapcore.DebugLevel))
require.NoError(t, err)

eventsChannel := newTestStructs(t)
apmServerInternals, _ := newMockApmServer(t, l)
logsapiAddr := randomAddr()
newMockLambdaServer(t, logsapiAddr, eventsChannel, l)

eventsChain := []MockEvent{
{Type: InvokeStandard, APMServerBehavior: TimelyResponse, ExecutionDuration: 1, Timeout: 5},
}
eventQueueGenerator(eventsChain, eventsChannel)
select {
case <-runAppFull(t, logsapiAddr, true):
assert.Contains(t, apmServerInternals.Data, TimelyResponse)
case <-time.After(timeout):
t.Fatalf("timed out waiting for app to finish")
}
}

// TestFlush checks if the flushed param does not cause a panic or an unexpected behavior
func TestFlush(t *testing.T) {
l, err := logger.New(logger.WithLevel(zapcore.DebugLevel))
Expand Down Expand Up @@ -821,13 +844,21 @@ func TestMetrics(t *testing.T) {
}

func runApp(t *testing.T, logsapiAddr string) <-chan struct{} {
return runAppFull(t, logsapiAddr, false)
}

func runAppFull(t *testing.T, logsapiAddr string, disableLogsAPI bool) <-chan struct{} {
ctx, cancel := context.WithCancel(context.Background())
app, err := app.New(ctx,
opts := []app.ConfigOption{
app.WithExtensionName("apm-lambda-extension"),
app.WithLambdaRuntimeAPI(os.Getenv("AWS_LAMBDA_RUNTIME_API")),
app.WithLogLevel("debug"),
app.WithLogsapiAddress(logsapiAddr),
)
}
if disableLogsAPI {
opts = append(opts, app.WithoutLogsAPI())
}
app, err := app.New(ctx, opts...)
require.NoError(t, err)

go func() {
Expand Down
Loading