Skip to content

Commit 5fbd26b

Browse files
Report runtime init errors to localstack (#17)
1 parent 0636459 commit 5fbd26b

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

cmd/localstack/codearchive.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func DownloadCodeArchive(url string, targetPath string) error {
4141
// create tmp directory
4242
// empty string will make use of the default tmp directory
4343
tmpDir, err := os.MkdirTemp("", "localstack-code-archive")
44+
defer os.RemoveAll(tmpDir)
4445
if err != nil {
4546
return err
4647
}

cmd/localstack/custom_interop.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ const (
3636
Error LocalStackStatus = "error"
3737
)
3838

39-
func (l *LocalStackAdapter) SendStatus(status LocalStackStatus) error {
39+
func (l *LocalStackAdapter) SendStatus(status LocalStackStatus, payload []byte) error {
4040
status_url := fmt.Sprintf("%s/status/%s/%s", l.UpstreamEndpoint, l.RuntimeId, status)
41-
_, err := http.Post(status_url, "text", bytes.NewReader([]byte{}))
41+
_, err := http.Post(status_url, "application/json", bytes.NewReader(payload))
4242
if err != nil {
4343
return err
4444
}
@@ -195,7 +195,7 @@ func NewCustomInteropServer(lsOpts *LsOpts, delegate rapidcore.InteropServer, lo
195195

196196
func (c *CustomInteropServer) StartAcceptingDirectInvokes() error {
197197
log.Traceln("Function called")
198-
err := c.localStackAdapter.SendStatus(Ready)
198+
err := c.localStackAdapter.SendStatus(Ready, []byte{})
199199
if err != nil {
200200
return err
201201
}
@@ -209,6 +209,10 @@ func (c *CustomInteropServer) SendResponse(invokeID string, contentType string,
209209

210210
func (c *CustomInteropServer) SendErrorResponse(invokeID string, response *interop.ErrorResponse) error {
211211
log.Traceln("Function called")
212+
err := c.localStackAdapter.SendStatus(Error, response.Payload)
213+
if err != nil {
214+
return err
215+
}
212216
return c.delegate.SendErrorResponse(invokeID, response)
213217
}
214218

cmd/localstack/main.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"context"
77
log "github.com/sirupsen/logrus"
88
"go.amzn.com/lambda/rapidcore"
9-
"net/http"
10-
_ "net/http/pprof"
119
"os"
1210
"runtime/debug"
1311
"strconv"
@@ -27,7 +25,7 @@ type LsOpts struct {
2725
InitLogLevel string
2826
EdgePort string
2927
EnableXRayTelemetry string
30-
PostInvokeWaitMS string
28+
PostInvokeWaitMS string
3129
}
3230

3331
func GetEnvOrDie(env string) string {
@@ -55,7 +53,7 @@ func InitLsOpts() *LsOpts {
5553
EnableDnsServer: os.Getenv("LOCALSTACK_ENABLE_DNS_SERVER"),
5654
EnableXRayTelemetry: os.Getenv("LOCALSTACK_ENABLE_XRAY_TELEMETRY"),
5755
LocalstackIP: os.Getenv("LOCALSTACK_HOSTNAME"),
58-
PostInvokeWaitMS: os.Getenv("LOCALSTACK_POST_INVOKE_WAIT_MS"),
56+
PostInvokeWaitMS: os.Getenv("LOCALSTACK_POST_INVOKE_WAIT_MS"),
5957
}
6058
}
6159

@@ -113,7 +111,7 @@ func main() {
113111

114112
// download code archive if env variable is set
115113
if err := DownloadCodeArchives(lsOpts.CodeArchives); err != nil {
116-
log.Fatal("Failed to download code archives")
114+
log.Fatal("Failed to download code archives: " + err.Error())
117115
}
118116

119117
// parse CLI args
@@ -168,6 +166,10 @@ func main() {
168166
if len(handler) > 0 {
169167
sandbox.SetHandler(handler)
170168
}
169+
exitChan := make(chan struct{})
170+
sandbox.AddShutdownFunc(func() {
171+
exitChan <- struct{}{}
172+
})
171173

172174
// initialize all flows and start runtime API
173175
go sandbox.Create()
@@ -183,10 +185,5 @@ func main() {
183185
// start runtime init
184186
go InitHandler(sandbox, GetEnvOrDie("AWS_LAMBDA_FUNCTION_VERSION"), int64(invokeTimeoutSeconds)) // TODO: replace this with a custom init
185187

186-
// TODO: make the tracing server optional
187-
// start blocking with the tracing server
188-
err = http.ListenAndServe("0.0.0.0:"+lsOpts.InitTracingPort, http.DefaultServeMux)
189-
if err != nil {
190-
log.Fatal("Failed to start debug server")
191-
}
188+
<-exitChan
192189
}

0 commit comments

Comments
 (0)