Skip to content

Commit

Permalink
Report runtime init errors to localstack (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikschubert authored Mar 29, 2023
1 parent 0636459 commit 5fbd26b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
1 change: 1 addition & 0 deletions cmd/localstack/codearchive.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func DownloadCodeArchive(url string, targetPath string) error {
// create tmp directory
// empty string will make use of the default tmp directory
tmpDir, err := os.MkdirTemp("", "localstack-code-archive")
defer os.RemoveAll(tmpDir)
if err != nil {
return err
}
Expand Down
10 changes: 7 additions & 3 deletions cmd/localstack/custom_interop.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ const (
Error LocalStackStatus = "error"
)

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

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

func (c *CustomInteropServer) SendErrorResponse(invokeID string, response *interop.ErrorResponse) error {
log.Traceln("Function called")
err := c.localStackAdapter.SendStatus(Error, response.Payload)
if err != nil {
return err
}
return c.delegate.SendErrorResponse(invokeID, response)
}

Expand Down
19 changes: 8 additions & 11 deletions cmd/localstack/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"context"
log "github.com/sirupsen/logrus"
"go.amzn.com/lambda/rapidcore"
"net/http"
_ "net/http/pprof"
"os"
"runtime/debug"
"strconv"
Expand All @@ -27,7 +25,7 @@ type LsOpts struct {
InitLogLevel string
EdgePort string
EnableXRayTelemetry string
PostInvokeWaitMS string
PostInvokeWaitMS string
}

func GetEnvOrDie(env string) string {
Expand Down Expand Up @@ -55,7 +53,7 @@ func InitLsOpts() *LsOpts {
EnableDnsServer: os.Getenv("LOCALSTACK_ENABLE_DNS_SERVER"),
EnableXRayTelemetry: os.Getenv("LOCALSTACK_ENABLE_XRAY_TELEMETRY"),
LocalstackIP: os.Getenv("LOCALSTACK_HOSTNAME"),
PostInvokeWaitMS: os.Getenv("LOCALSTACK_POST_INVOKE_WAIT_MS"),
PostInvokeWaitMS: os.Getenv("LOCALSTACK_POST_INVOKE_WAIT_MS"),
}
}

Expand Down Expand Up @@ -113,7 +111,7 @@ func main() {

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

// parse CLI args
Expand Down Expand Up @@ -168,6 +166,10 @@ func main() {
if len(handler) > 0 {
sandbox.SetHandler(handler)
}
exitChan := make(chan struct{})
sandbox.AddShutdownFunc(func() {
exitChan <- struct{}{}
})

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

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

0 comments on commit 5fbd26b

Please sign in to comment.