-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
59 lines (52 loc) · 1.75 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"firetail-lambda-extension/extensionsapi"
"firetail-lambda-extension/logsapi"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"strconv"
"time"
)
func main() {
// Configure logging
extensionName := path.Base(os.Args[0])
log.SetPrefix(fmt.Sprintf("[%s] ", extensionName))
if isDebug, err := strconv.ParseBool(os.Getenv("FIRETAIL_EXTENSION_DEBUG")); err != nil || !isDebug {
// If we're not in debug mode, we'll just send the logs to the void
log.SetOutput(ioutil.Discard)
} else {
log.Println("Firetail extension starting in debug mode.")
}
// This context will be cancelled whenever a SIGTERM or SIGINT signal is received
// We'll use it for our requests to the extensions API & to shutdown the log server
ctx := getContext()
// Create a Lambda Extensions API client & register our extension
extensionClient := extensionsapi.NewClient()
_, err := extensionClient.Register(ctx, extensionName)
if err != nil {
panic(err)
}
log.Println("Registered extension, ID:", extensionClient.ExtensionID)
// Create a logsApiClient, start it & remember to shut it down when we're done
logsApiClient, err := logsapi.NewClient(logsapi.Options{
ExtensionID: extensionClient.ExtensionID,
LogServerAddress: "sandbox:1234",
})
if err != nil {
panic(err)
}
go logsApiClient.Start(ctx)
defer logsApiClient.Shutdown(ctx)
// awaitShutdown will block until a shutdown event is received, or the context is cancelled
reason, err := awaitShutdown(extensionClient, ctx)
if err != nil {
panic(err)
}
log.Println("Shutting down, reason:", reason)
// Sleep for 500ms to allow any final logs to be sent to the extension by the Lambda Logs API
log.Printf("Sleeping for 500ms to allow final logs to be processed...")
time.Sleep(500 * time.Millisecond)
}