@@ -24,6 +24,8 @@ type LsOpts struct {
2424 HotReloadingPaths []string
2525 EnableDnsServer string
2626 LocalstackIP string
27+ InitLogLevel string
28+ EdgePort string
2729}
2830
2931func GetEnvOrDie (env string ) string {
@@ -36,12 +38,15 @@ func GetEnvOrDie(env string) string {
3638
3739func InitLsOpts () * LsOpts {
3840 return & LsOpts {
41+ // required
3942 RuntimeEndpoint : GetEnvOrDie ("LOCALSTACK_RUNTIME_ENDPOINT" ),
4043 RuntimeId : GetEnvOrDie ("LOCALSTACK_RUNTIME_ID" ),
4144 // optional with default
4245 InteropPort : GetenvWithDefault ("LOCALSTACK_INTEROP_PORT" , "9563" ),
4346 InitTracingPort : GetenvWithDefault ("LOCALSTACK_RUNTIME_TRACING_PORT" , "9564" ),
4447 User : GetenvWithDefault ("LOCALSTACK_USER" , "sbx_user1051" ),
48+ InitLogLevel : GetenvWithDefault ("LOCALSTACK_INIT_LOG_LEVEL" , "debug" ),
49+ EdgePort : GetenvWithDefault ("EDGE_PORT" , "4566" ),
4550 // optional or empty
4651 CodeArchives : os .Getenv ("LOCALSTACK_CODE_ARCHIVES" ),
4752 HotReloadingPaths : strings .Split (GetenvWithDefault ("LOCALSTACK_HOT_RELOADING_PATHS" , "" ), "," ),
@@ -62,6 +67,7 @@ func UnsetLsEnvs() {
6267 "LOCALSTACK_CODE_ARCHIVES" ,
6368 "LOCALSTACK_HOT_RELOADING_PATHS" ,
6469 "LOCALSTACK_ENABLE_DNS_SERVER" ,
70+ "LOCALSTACK_INIT_LOG_LEVEL" ,
6571 // Docker container ID
6672 "HOSTNAME" ,
6773 // User
@@ -78,22 +84,33 @@ func main() {
7884 // we're setting this to the same value as in the official RIE
7985 debug .SetGCPercent (33 )
8086
87+ // configuration parsing
8188 lsOpts := InitLsOpts ()
8289 UnsetLsEnvs ()
8390
84- // set up logging (logrus)
85- //log.SetFormatter(&log.JSONFormatter{})
86- //log.SetLevel(log.TraceLevel)
87- log .SetLevel (log .DebugLevel )
91+ // set up logging
8892 log .SetReportCaller (true )
93+ switch lsOpts .InitLogLevel {
94+ case "debug" :
95+ log .SetLevel (log .DebugLevel )
96+ case "trace" :
97+ log .SetFormatter (& log.JSONFormatter {})
98+ log .SetLevel (log .TraceLevel )
99+ default :
100+ log .Fatal ("Invalid value for LOCALSTACK_INIT_LOG_LEVEL" )
101+ }
102+
103+ // enable dns server
104+ dnsServerContext , stopDnsServer := context .WithCancel (context .Background ())
105+ go RunDNSRewriter (lsOpts , dnsServerContext )
89106
90107 // download code archive if env variable is set
91108 if err := DownloadCodeArchives (lsOpts .CodeArchives ); err != nil {
92109 log .Fatal ("Failed to download code archives" )
93110 }
94- // enable dns server
95- dnsServerContext , stopDnsServer := context . WithCancel ( context . Background ())
96- go RunDNSRewriter ( lsOpts , dnsServerContext )
111+
112+ // parse CLI args
113+ bootstrap , handler := getBootstrap ( os . Args )
97114
98115 // Switch to non-root user and drop root privileges
99116 if IsRootUser () && lsOpts .User != "" {
@@ -108,23 +125,36 @@ func main() {
108125 UserLogger ().Debugln ("Process running as non-root user." )
109126 }
110127
111- // parse CLI args
112- opts , args := getCLIArgs ()
113- bootstrap , handler := getBootstrap (args , opts )
114128 logCollector := NewLogCollector ()
129+
130+ // file watcher for hot-reloading
115131 fileWatcherContext , cancelFileWatcher := context .WithCancel (context .Background ())
132+
133+ // build sandbox
116134 sandbox := rapidcore .
117135 NewSandboxBuilder (bootstrap ).
136+ //SetTracer(tracer).
118137 AddShutdownFunc (func () {
119- log .Debugln ("Closing contexts " )
138+ log .Debugln ("Stopping file watcher " )
120139 cancelFileWatcher ()
140+ log .Debugln ("Stopping DNS server" )
121141 stopDnsServer ()
122142 }).
123- AddShutdownFunc (func () { os .Exit (0 ) }).
124143 SetExtensionsFlag (true ).
125144 SetInitCachingFlag (true ).
126145 SetTailLogOutput (logCollector )
127146
147+ // xray daemon
148+ xrayConfig := initConfig ("http://" + lsOpts .LocalstackIP + ":" + lsOpts .EdgePort )
149+ d := initDaemon (xrayConfig )
150+ sandbox .AddShutdownFunc (func () {
151+ log .Debugln ("Shutting down xray daemon" )
152+ d .stop ()
153+ log .Debugln ("Flushing segments in xray daemon" )
154+ d .close ()
155+ })
156+ runDaemon (d ) // async
157+
128158 defaultInterop := sandbox .InteropServer ()
129159 interopServer := NewCustomInteropServer (lsOpts , defaultInterop , logCollector )
130160 sandbox .SetInteropServer (interopServer )
@@ -136,7 +166,7 @@ func main() {
136166 go sandbox .Create ()
137167
138168 // get timeout
139- invokeTimeoutEnv := GetEnvOrDie ("AWS_LAMBDA_FUNCTION_TIMEOUT" )
169+ invokeTimeoutEnv := GetEnvOrDie ("AWS_LAMBDA_FUNCTION_TIMEOUT" ) // TODO: collect all AWS_* env parsing
140170 invokeTimeoutSeconds , err := strconv .Atoi (invokeTimeoutEnv )
141171 if err != nil {
142172 log .Fatalln (err )
0 commit comments