From 851c8254eeb563e7d48ca95ea3f62b70a7f8736c Mon Sep 17 00:00:00 2001 From: Jean-Louis Voiseux <48380853+jlvoiseux@users.noreply.github.com> Date: Thu, 10 Mar 2022 11:31:20 +0100 Subject: [PATCH 1/5] Use Init and Exit error codes (incl. demo code) --- apm-lambda-extension/extension/client.go | 4 +- apm-lambda-extension/main.go | 55 +++++++++++++++++++++++- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/apm-lambda-extension/extension/client.go b/apm-lambda-extension/extension/client.go index a20f2976..2af4e75b 100644 --- a/apm-lambda-extension/extension/client.go +++ b/apm-lambda-extension/extension/client.go @@ -161,7 +161,7 @@ func (e *Client) InitError(ctx context.Context, errorType string) (*StatusRespon } defer httpRes.Body.Close() - if httpRes.StatusCode != 200 { + if httpRes.StatusCode != 202 { return nil, fmt.Errorf("initialization error request failed with status %s", httpRes.Status) } res := StatusResponse{} @@ -188,7 +188,7 @@ func (e *Client) ExitError(ctx context.Context, errorType string) (*StatusRespon } defer httpRes.Body.Close() - if httpRes.StatusCode != 200 { + if httpRes.StatusCode != 202 { return nil, fmt.Errorf("exit error request failed with status %s", httpRes.Status) } res := StatusResponse{} diff --git a/apm-lambda-extension/main.go b/apm-lambda-extension/main.go index 6def582f..1efeb130 100644 --- a/apm-lambda-extension/main.go +++ b/apm-lambda-extension/main.go @@ -19,6 +19,7 @@ package main import ( "context" + "fmt" "log" "net/http" "os" @@ -40,6 +41,7 @@ var ( /* --- elastic vars --- */ func main() { + ctx, cancel := context.WithCancel(context.Background()) sigs := make(chan os.Signal, 1) @@ -54,10 +56,30 @@ func main() { // register extension with AWS Extension API res, err := extensionClient.Register(ctx, extensionName) if err != nil { - panic(err) + status, errRuntime := extensionClient.InitError(ctx, err.Error()) + if errRuntime != nil { + panic(errRuntime) + } + log.Printf("Error: %v\n", err) + log.Printf("Init error signal sent to runtime : %v\n", status) + log.Println("Exiting") + return } log.Printf("Register response: %v\n", extension.PrettyPrint(res)) + /////////////// USED FOR DEMONSTRATION PURPOSES - TO BE REMOVED ///////////////////// + if os.Getenv("ELASTIC_APM_LAMBDA_CRASH_SIM") == "init" { + status, errRuntime := extensionClient.InitError(ctx, "Extension.InitError") + if errRuntime != nil { + panic(errRuntime) + } + log.Printf("Error: %v\n", fmt.Errorf("extension init crash simulation")) + log.Printf("Init error signal sent to runtime : %v\n", status) + log.Println("Exiting") + return + } + /////////////// USED FOR DEMONSTRATION PURPOSES - TO BE REMOVED ///////////////////// + // pulls ELASTIC_ env variable into globals for easy access config := extension.ProcessEnv() @@ -99,7 +121,12 @@ func main() { log.Println("Waiting for next event...") event, err := extensionClient.NextEvent(ctx) if err != nil { + status, err := extensionClient.ExitError(ctx, err.Error()) + if err != nil { + panic(err) + } log.Printf("Error: %v\n", err) + log.Printf("Exit signal sent to runtime : %v\n", status) log.Println("Exiting") return } @@ -124,6 +151,19 @@ func main() { return } + /////////////// USED FOR DEMONSTRATION PURPOSES - TO BE REMOVED ///////////////////// + if os.Getenv("ELASTIC_APM_LAMBDA_CRASH_SIM") == "event" { + status, err := extensionClient.ExitError(ctx, "Extension.UnknownError") + if err != nil { + panic(err) + } + log.Printf("Error: %v\n", fmt.Errorf("extension event crash simulation")) + log.Printf("Exit error signal sent to runtime : %v\n", status) + log.Println("Exiting") + return + } + /////////////// USED FOR DEMONSTRATION PURPOSES - TO BE REMOVED ///////////////////// + // Receive agent data as it comes in and post it to the APM server. // Stop checking for, and sending agent data when the function invocation // has completed, signaled via a channel. @@ -136,6 +176,19 @@ func main() { log.Println("funcDone signal received, not processing any more agent data") return case agentData := <-agentDataChannel: + + /////////////// USED FOR DEMONSTRATION PURPOSES - TO BE REMOVED ///////////////////// + if os.Getenv("ELASTIC_APM_LAMBDA_CRASH_SIM") == "apm" { + status, err := extensionClient.ExitError(ctx, "Extension.UnknownError") + if err != nil { + panic(err) + } + log.Printf("Error: %v\n", fmt.Errorf("extension apm crash simulation")) + log.Printf("Exit error signal sent to runtime : %v\n", status) + return + } + /////////////// USED FOR DEMONSTRATION PURPOSES - TO BE REMOVED ///////////////////// + err := extension.PostToApmServer(client, agentData, config) if err != nil { log.Printf("Error sending to APM server, skipping: %v", err) From fefa4c4618ffb15e0cb56c44ea1b0e435685fdb9 Mon Sep 17 00:00:00 2001 From: Jean-Louis Voiseux <48380853+jlvoiseux@users.noreply.github.com> Date: Thu, 10 Mar 2022 12:01:22 +0100 Subject: [PATCH 2/5] Remove APM example --- apm-lambda-extension/main.go | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/apm-lambda-extension/main.go b/apm-lambda-extension/main.go index 1efeb130..58622aed 100644 --- a/apm-lambda-extension/main.go +++ b/apm-lambda-extension/main.go @@ -60,8 +60,8 @@ func main() { if errRuntime != nil { panic(errRuntime) } - log.Printf("Error: %v\n", err) - log.Printf("Init error signal sent to runtime : %v\n", status) + log.Printf("Error: %s", err) + log.Printf("Init error signal sent to runtime : %s", status) log.Println("Exiting") return } @@ -73,8 +73,8 @@ func main() { if errRuntime != nil { panic(errRuntime) } - log.Printf("Error: %v\n", fmt.Errorf("extension init crash simulation")) - log.Printf("Init error signal sent to runtime : %v\n", status) + log.Printf("Error: %s", fmt.Errorf("extension init crash simulation")) + log.Printf("Init error signal sent to runtime : %s", status) log.Println("Exiting") return } @@ -125,8 +125,8 @@ func main() { if err != nil { panic(err) } - log.Printf("Error: %v\n", err) - log.Printf("Exit signal sent to runtime : %v\n", status) + log.Printf("Error: %s", err) + log.Printf("Exit signal sent to runtime : %s", status) log.Println("Exiting") return } @@ -157,8 +157,8 @@ func main() { if err != nil { panic(err) } - log.Printf("Error: %v\n", fmt.Errorf("extension event crash simulation")) - log.Printf("Exit error signal sent to runtime : %v\n", status) + log.Printf("Error: %s", fmt.Errorf("extension event crash simulation")) + log.Printf("Exit error signal sent to runtime : %s", status) log.Println("Exiting") return } @@ -176,19 +176,6 @@ func main() { log.Println("funcDone signal received, not processing any more agent data") return case agentData := <-agentDataChannel: - - /////////////// USED FOR DEMONSTRATION PURPOSES - TO BE REMOVED ///////////////////// - if os.Getenv("ELASTIC_APM_LAMBDA_CRASH_SIM") == "apm" { - status, err := extensionClient.ExitError(ctx, "Extension.UnknownError") - if err != nil { - panic(err) - } - log.Printf("Error: %v\n", fmt.Errorf("extension apm crash simulation")) - log.Printf("Exit error signal sent to runtime : %v\n", status) - return - } - /////////////// USED FOR DEMONSTRATION PURPOSES - TO BE REMOVED ///////////////////// - err := extension.PostToApmServer(client, agentData, config) if err != nil { log.Printf("Error sending to APM server, skipping: %v", err) From 79f7b7b588791085705e9560cda3bf2828f9ccef Mon Sep 17 00:00:00 2001 From: Jean-Louis Voiseux <48380853+jlvoiseux@users.noreply.github.com> Date: Thu, 10 Mar 2022 15:26:18 +0100 Subject: [PATCH 3/5] Increase the range of valid StatusCodes Co-authored-by: Felix Barnsteiner --- apm-lambda-extension/extension/client.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apm-lambda-extension/extension/client.go b/apm-lambda-extension/extension/client.go index 2af4e75b..6eede380 100644 --- a/apm-lambda-extension/extension/client.go +++ b/apm-lambda-extension/extension/client.go @@ -161,7 +161,7 @@ func (e *Client) InitError(ctx context.Context, errorType string) (*StatusRespon } defer httpRes.Body.Close() - if httpRes.StatusCode != 202 { + if httpRes.StatusCode > 299 { return nil, fmt.Errorf("initialization error request failed with status %s", httpRes.Status) } res := StatusResponse{} @@ -188,7 +188,7 @@ func (e *Client) ExitError(ctx context.Context, errorType string) (*StatusRespon } defer httpRes.Body.Close() - if httpRes.StatusCode != 202 { + if httpRes.StatusCode > 299 { return nil, fmt.Errorf("exit error request failed with status %s", httpRes.Status) } res := StatusResponse{} From 77b60227bc3d14921fa65d06f62255b3a27283b8 Mon Sep 17 00:00:00 2001 From: Jean-Louis Voiseux <48380853+jlvoiseux@users.noreply.github.com> Date: Thu, 10 Mar 2022 17:43:06 +0100 Subject: [PATCH 4/5] Remove demonstration code --- apm-lambda-extension/main.go | 27 --------------------------- 1 file changed, 27 deletions(-) diff --git a/apm-lambda-extension/main.go b/apm-lambda-extension/main.go index 58622aed..bb9eeebc 100644 --- a/apm-lambda-extension/main.go +++ b/apm-lambda-extension/main.go @@ -19,7 +19,6 @@ package main import ( "context" - "fmt" "log" "net/http" "os" @@ -67,19 +66,6 @@ func main() { } log.Printf("Register response: %v\n", extension.PrettyPrint(res)) - /////////////// USED FOR DEMONSTRATION PURPOSES - TO BE REMOVED ///////////////////// - if os.Getenv("ELASTIC_APM_LAMBDA_CRASH_SIM") == "init" { - status, errRuntime := extensionClient.InitError(ctx, "Extension.InitError") - if errRuntime != nil { - panic(errRuntime) - } - log.Printf("Error: %s", fmt.Errorf("extension init crash simulation")) - log.Printf("Init error signal sent to runtime : %s", status) - log.Println("Exiting") - return - } - /////////////// USED FOR DEMONSTRATION PURPOSES - TO BE REMOVED ///////////////////// - // pulls ELASTIC_ env variable into globals for easy access config := extension.ProcessEnv() @@ -151,19 +137,6 @@ func main() { return } - /////////////// USED FOR DEMONSTRATION PURPOSES - TO BE REMOVED ///////////////////// - if os.Getenv("ELASTIC_APM_LAMBDA_CRASH_SIM") == "event" { - status, err := extensionClient.ExitError(ctx, "Extension.UnknownError") - if err != nil { - panic(err) - } - log.Printf("Error: %s", fmt.Errorf("extension event crash simulation")) - log.Printf("Exit error signal sent to runtime : %s", status) - log.Println("Exiting") - return - } - /////////////// USED FOR DEMONSTRATION PURPOSES - TO BE REMOVED ///////////////////// - // Receive agent data as it comes in and post it to the APM server. // Stop checking for, and sending agent data when the function invocation // has completed, signaled via a channel. From ccdc8c1144e8cd0d17527de25b528e7f6018e79f Mon Sep 17 00:00:00 2001 From: Jean-Louis Voiseux <48380853+jlvoiseux@users.noreply.github.com> Date: Thu, 31 Mar 2022 12:45:34 +0200 Subject: [PATCH 5/5] Quick fix : make changes compatible with logger --- apm-lambda-extension/main.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/apm-lambda-extension/main.go b/apm-lambda-extension/main.go index 895d9a6a..c1aa95d5 100644 --- a/apm-lambda-extension/main.go +++ b/apm-lambda-extension/main.go @@ -19,7 +19,6 @@ package main import ( "context" - "log" "net/http" "os" "os/signal" @@ -65,9 +64,9 @@ func main() { if errRuntime != nil { panic(errRuntime) } - log.Printf("Error: %s", err) - log.Printf("Init error signal sent to runtime : %s", status) - log.Println("Exiting") + extension.Log.Errorf("Error: %s", err) + extension.Log.Infof("Init error signal sent to runtime : %s", status) + extension.Log.Infof("Exiting") return } extension.Log.Debugf("Register response: %v", extension.PrettyPrint(res)) @@ -117,9 +116,9 @@ func main() { if err != nil { panic(err) } - extension.Log.Printf("Error: %s", err) - extension.Log.Printf("Exit signal sent to runtime : %s", status) - extension.Log.Println("Exiting") + extension.Log.Errorf("Error: %s", err) + extension.Log.Infof("Exit signal sent to runtime : %s", status) + extension.Log.Infof("Exiting") return } extension.Log.Debug("Received event.")