@@ -24,6 +24,7 @@ import (
2424 "os"
2525 "os/signal"
2626 "path/filepath"
27+ "sync"
2728 "syscall"
2829 "time"
2930
@@ -74,6 +75,10 @@ func main() {
7475 // Make channel for collecting logs and create a HTTP server to listen for them
7576 logsChannel := make (chan logsapi.LogEvent )
7677
78+ // Use a wait group to ensure the background go routine sending to the APM server
79+ // completes before signaling that the extension is ready for the next invocation.
80+ var backgroundDataSendWg sync.WaitGroup
81+
7782 // Subscribe to the Logs API
7883 err = logsapi .Subscribe (
7984 extensionClient .ExtensionID ,
@@ -137,10 +142,12 @@ func main() {
137142 log .Println ("funcDone signal received, not processing any more agent data" )
138143 return
139144 case agentData := <- agentDataChannel :
145+ backgroundDataSendWg .Add (1 )
140146 err := extension .PostToApmServer (client , agentData , config )
141147 if err != nil {
142148 log .Printf ("Error sending to APM server, skipping: %v" , err )
143149 }
150+ backgroundDataSendWg .Done ()
144151 }
145152 }
146153 }()
@@ -187,6 +194,7 @@ func main() {
187194 log .Println ("Time expired waiting for agent signal or runtimeDone event" )
188195 }
189196
197+ backgroundDataSendWg .Wait ()
190198 if config .SendStrategy == extension .SyncFlush {
191199 // Flush APM data now that the function invocation has completed
192200 extension .FlushAPMData (client , agentDataChannel , config )
0 commit comments