Skip to content
Merged
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
8 changes: 8 additions & 0 deletions apm-lambda-extension/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"os"
"os/signal"
"path/filepath"
"sync"
"syscall"
"time"

Expand Down Expand Up @@ -74,6 +75,10 @@ func main() {
// Make channel for collecting logs and create a HTTP server to listen for them
logsChannel := make(chan logsapi.LogEvent)

// Use a wait group to ensure the background go routine sending to the APM server
// completes before signaling that the extension is ready for the next invocation.
var backgroundDataSendWg sync.WaitGroup

// Subscribe to the Logs API
err = logsapi.Subscribe(
extensionClient.ExtensionID,
Expand Down Expand Up @@ -137,10 +142,12 @@ func main() {
log.Println("funcDone signal received, not processing any more agent data")
return
case agentData := <-agentDataChannel:
backgroundDataSendWg.Add(1)
err := extension.PostToApmServer(client, agentData, config)
if err != nil {
log.Printf("Error sending to APM server, skipping: %v", err)
}
backgroundDataSendWg.Done()
}
}
}()
Expand Down Expand Up @@ -187,6 +194,7 @@ func main() {
log.Println("Time expired waiting for agent signal or runtimeDone event")
}

backgroundDataSendWg.Wait()
if config.SendStrategy == extension.SyncFlush {
// Flush APM data now that the function invocation has completed
extension.FlushAPMData(client, agentDataChannel, config)
Expand Down