Skip to content

Report runtime init errors to localstack #17

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

Merged
merged 2 commits into from
Mar 29, 2023
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
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
}